private async Task <IEnumerable <IListBlobItem> > LoadApplicableListBlobItemsAsync() { CloudBlobContainer container = await BlobStorageHelper.BuildBlobContainerAsync( this.alertsContainerConnectionString, this.alertsStoreContainerName); IEnumerable <IListBlobItem> blobs = await BlobStorageHelper.LoadBlobItemsAsync( async (token) => { return(await container.ListBlobsSegmentedAsync( this.deviceAlertsDataPrefix, true, BlobListingDetails.None, null, token, null, null)); }); if (blobs != null) { blobs = blobs.OrderByDescending(t => BlobStorageHelper.ExtractBlobItemDate(t)); } return(blobs); }
/// <summary> /// Loads the most recent Device telemetry. /// </summary> /// <param name="deviceId"> /// The ID of the Device for which telemetry should be returned. /// </param> /// <param name="minTime"> /// The minimum time of record of the telemetry that should be returned. /// </param> /// <returns> /// Telemetry for the Device specified by deviceId, inclusively since /// minTime. /// </returns> public async Task <IEnumerable <DeviceTelemetryModel> > LoadLatestDeviceTelemetryAsync( string deviceId, DateTime minTime) { IEnumerable <DeviceTelemetryModel> result = new DeviceTelemetryModel[0]; CloudBlobContainer container = await BlobStorageHelper.BuildBlobContainerAsync(this._telemetryStoreConnectionString, _telemetryContainerName); IEnumerable <IListBlobItem> blobs = await BlobStorageHelper.LoadBlobItemsAsync( async (token) => { return(await container.ListBlobsSegmentedAsync( _telemetryDataPrefix, true, BlobListingDetails.None, null, token, null, null)); }); blobs = blobs.OrderByDescending(t => BlobStorageHelper.ExtractBlobItemDate(t)); CloudBlockBlob blockBlob; IEnumerable <DeviceTelemetryModel> blobModels; foreach (IListBlobItem blob in blobs) { if ((blockBlob = blob as CloudBlockBlob) == null) { continue; } try { blobModels = await LoadBlobTelemetryModelsAsync(blockBlob); } catch { continue; } if (blobModels == null) { break; } int preFilterCount = blobModels.Count(); blobModels = blobModels.Where( t => (t != null) && t.Timestamp.HasValue && t.Timestamp.Value >= minTime); if (preFilterCount == 0) { break; } result = result.Concat(blobModels); if (preFilterCount != blobModels.Count()) { break; } } if (!string.IsNullOrEmpty(deviceId)) { result = result.Where(t => t.DeviceId == deviceId); } return(result); }
/// <summary> /// Loads the most recent DeviceTelemetrySummaryModel for a specified Device. /// </summary> /// <param name="deviceId"> /// The ID of the Device for which a telemetry summary model should be /// returned. /// </param> /// <param name="minTime"> /// If provided the the minimum time stamp of the summary data that should /// be loaded. /// </param> /// <returns> /// The most recent DeviceTelemetrySummaryModel for the Device, /// specified by deviceId. /// </returns> public async Task <DeviceTelemetrySummaryModel> LoadLatestDeviceTelemetrySummaryAsync( string deviceId, DateTime?minTime) { DeviceTelemetrySummaryModel summaryModel = null; CloudBlobContainer container = await BlobStorageHelper.BuildBlobContainerAsync( this._telemetryStoreConnectionString, _telemetryContainerName); IEnumerable <IListBlobItem> blobs = await BlobStorageHelper.LoadBlobItemsAsync( async (token) => { return(await container.ListBlobsSegmentedAsync( _telemetrySummaryPrefix, true, BlobListingDetails.None, null, token, null, null)); }); blobs = blobs.OrderByDescending(t => BlobStorageHelper.ExtractBlobItemDate(t)); IEnumerable <DeviceTelemetrySummaryModel> blobModels; CloudBlockBlob blockBlob; foreach (IListBlobItem blob in blobs) { if ((blockBlob = blob as CloudBlockBlob) == null) { continue; } // Translate LastModified to local time zone. DateTimeOffsets // don't do this automatically. This is for equivalent behavior // with parsed DateTimes. if (minTime.HasValue && (blockBlob.Properties != null) && blockBlob.Properties.LastModified.HasValue && (blockBlob.Properties.LastModified.Value.LocalDateTime < minTime.Value)) { break; } try { blobModels = await LoadBlobTelemetrySummaryModelsAsync(blockBlob); } catch { continue; } if (blobModels == null) { break; } blobModels = blobModels.Where(t => t != null); if (!string.IsNullOrEmpty(deviceId)) { blobModels = blobModels.Where(t => t.DeviceId == deviceId); } summaryModel = blobModels.LastOrDefault(); if (summaryModel != null) { break; } } return(summaryModel); }
/// <summary> /// Loads the latest Device Alert History items. /// </summary> /// <param name="maxItems"> /// The maximum number of Device Alert History items to return. /// </param> /// <returns> /// The latest Device Alert History items. /// </returns> public async Task <IEnumerable <AlertHistoryItemModel> > LoadLatestAlertHistoryAsync( int maxItems) { IEnumerable <IListBlobItem> blobs; CloudBlockBlob blockBlob; CloudBlobContainer container; List <AlertHistoryItemModel> result; IEnumerable <AlertHistoryItemModel> segment; if (maxItems <= 0) { throw new ArgumentOutOfRangeException( "maxItems", "maxItems is not a positive integer."); } result = new List <AlertHistoryItemModel>(); container = await BlobStorageHelper.BuildBlobContainerAsync( this.alertsContainerConnectionString, this.alertsStoreContainerName); blobs = await BlobStorageHelper.LoadBlobItemsAsync( async (token) => { return(await container.ListBlobsSegmentedAsync( this.deviceAlertsDataPrefix, true, BlobListingDetails.None, null, token, null, null)); }); blobs = blobs.OrderByDescending( t => BlobStorageHelper.ExtractBlobItemDate(t)); foreach (IListBlobItem blob in blobs) { if ((blockBlob = blob as CloudBlockBlob) == null) { continue; } segment = await ProduceAlertHistoryItemsAsync(blockBlob); segment = segment.OrderByDescending(t => t.Timestamp); result.AddRange(segment); if (result.Count >= maxItems) { return(result.Take(maxItems)); } } return(result); }
/// <summary> /// Loads the most recent Device telemetry. /// </summary> /// <param name="deviceId"> /// The ID of the Device for which telemetry should be returned. /// </param> /// <param name="minTime"> /// The minimum time of record of the telemetry that should be returned. /// </param> /// <returns> /// Telemetry for the Device specified by deviceId, inclusively since /// minTime. /// </returns> public async Task <IEnumerable <DeviceTelemetryModel> > LoadLatestDeviceTelemetryAsync( string deviceId, IList <DeviceTelemetryFieldModel> telemetryFields, DateTime minTime) { IEnumerable <DeviceTelemetryModel> result = new DeviceTelemetryModel[0]; CloudBlobContainer container = await BlobStorageHelper.BuildBlobContainerAsync(this._telemetryStoreConnectionString, this._telemetryContainerName); IEnumerable <IListBlobItem> blobs = await BlobStorageHelper.LoadBlobItemsAsync( async (token) => { return(await container.ListBlobsSegmentedAsync( this._telemetryDataPrefix, true, BlobListingDetails.None, null, token, null, null)); }); blobs = blobs .OrderByDescending(t => BlobStorageHelper.ExtractBlobItemDate(t)); CloudBlockBlob blockBlob; IEnumerable <DeviceTelemetryModel> blobModels; foreach (IListBlobItem blob in blobs) { if ((blockBlob = blob as CloudBlockBlob) == null) { continue; } // Translate LastModified to local time zone. DateTimeOffsets // don't do this automatically. This is for equivalent behavior // with parsed DateTimes. if ((blockBlob.Properties != null) && blockBlob.Properties.LastModified.HasValue && (blockBlob.Properties.LastModified.Value.LocalDateTime < minTime)) { break; } try { blobModels = await LoadBlobTelemetryModelsAsync(blockBlob, telemetryFields); } catch { continue; } if (blobModels == null) { break; } int preFilterCount = blobModels.Count(); blobModels = blobModels.Where( t => (t != null) && t.Timestamp.HasValue && t.Timestamp.Value >= minTime); if (preFilterCount == 0) { break; } result = result.Concat(blobModels); } if (!string.IsNullOrEmpty(deviceId)) { result = result.Where(t => t.DeviceId == deviceId); } return(result); }
/// <summary> /// Loads the most recent DeviceTelemetrySummaryModel for a specified Device. /// </summary> /// <param name="deviceId"> /// The ID of the Device for which a telemetry summary model should be /// returned. /// </param> /// <param name="minTime"> /// If provided the the minimum time stamp of the summary data that should /// be loaded. /// </param> /// <returns> /// The most recent DeviceTelemetrySummaryModel for the Device, /// specified by deviceId. /// </returns> public async Task <DeviceTelemetrySummaryModel> LoadLatestDeviceTelemetrySummaryAsync( string deviceId, DateTime?minTime) { IEnumerable <DeviceTelemetrySummaryModel> blobModels; IEnumerable <IListBlobItem> blobs; CloudBlockBlob blockBlob; CloudBlobContainer container; DeviceTelemetrySummaryModel summaryModel; if (minTime.HasValue) { minTime = minTime.Value.ToUniversalTime(); } summaryModel = null; container = await BlobStorageHelper.BuildBlobContainerAsync( this._telemetryStoreConnectionString, _telemetryContainerName); blobs = await BlobStorageHelper.LoadBlobItemsAsync( async (token) => { return(await container.ListBlobsSegmentedAsync( _telemetrySummaryPrefix, true, BlobListingDetails.None, null, token, null, null)); }); blobs = blobs.OrderByDescending( t => BlobStorageHelper.ExtractBlobItemDate(t)); foreach (IListBlobItem blob in blobs) { if ((blockBlob = blob as CloudBlockBlob) == null) { continue; } if (minTime.HasValue && (blockBlob.Properties != null) && blockBlob.Properties.LastModified.HasValue && (blockBlob.Properties.LastModified.Value.UtcDateTime < minTime)) { break; } try { blobModels = await LoadBlobTelemetrySummaryModelsAsync(blockBlob); } catch { continue; } if (blobModels == null) { break; } blobModels = blobModels.Where(t => t != null); if (!string.IsNullOrEmpty(deviceId)) { blobModels = blobModels.Where(t => t.DeviceId == deviceId); } summaryModel = blobModels.LastOrDefault(); if (summaryModel != null) { break; } } return(summaryModel); }