Пример #1
0
        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);
        }
Пример #2
0
        private async Task <ActionMappingBlobResults> GetActionsAndEtagAsync()
        {
            CloudBlobContainer container = await BlobStorageHelper.BuildBlobContainerAsync(_connectionString, _containerName);

            CloudBlockBlob blob   = container.GetBlockBlobReference(_blobName);
            bool           exists = await blob.ExistsAsync();

            var mappings = new List <ActionMapping>();

            if (exists)
            {
                await blob.FetchAttributesAsync();

                long blobLength = blob.Properties.Length;

                if (blobLength > 0)
                {
                    byte[] existingBytes = new byte[blobLength];
                    await blob.DownloadToByteArrayAsync(existingBytes, 0);

                    // get the existing mappings in object form
                    string existingJsonData = Encoding.UTF8.GetString(existingBytes);
                    mappings = JsonConvert.DeserializeObject <List <ActionMapping> >(existingJsonData);
                }

                return(new ActionMappingBlobResults(mappings, blob.Properties.ETag));
            }

            // if it doesn't exist, return the empty list and an empty string for the ETag
            return(new ActionMappingBlobResults(mappings, ""));
        }
Пример #3
0
        private async Task PersistRulesToBlobStorageAsync(List <DeviceRuleBlobEntity> blobList)
        {
            CloudBlobContainer container = await BlobStorageHelper.BuildBlobContainerAsync(_storageAccountConnectionString, _deviceRulesBlobStoreContainerName);

            string   updatedJson = JsonConvert.SerializeObject(blobList);
            DateTime saveDate    = DateTime.UtcNow.AddMinutes(blobSaveMinutesInTheFuture);
            string   dateString  = saveDate.ToString("d", _formatInfo);
            string   timeString  = saveDate.ToString("t", _formatInfo);

            CloudBlockBlob blockBlob = container.GetBlockBlobReference(string.Format(@"{0}\{1}\{2}", dateString, timeString, _blobName));
            await blockBlob.UploadTextAsync(updatedJson);
        }
Пример #4
0
        public async Task SaveMappingAsync(ActionMapping m)
        {
            ActionMappingBlobResults existingResults = await GetActionsAndEtagAsync();

            List <ActionMapping> existingMappings = existingResults.ActionMappings;

            // look for the new mapping
            var           ruleoutput = m.RuleOutput;
            ActionMapping found      = existingMappings.FirstOrDefault(a => a.RuleOutput.ToLower() == m.RuleOutput.ToLower());

            if (found == null)
            {
                // add the new mapping
                existingMappings.Add(m);
            }
            else
            {
                // update the ActionId for the found mapping
                found.ActionId = m.ActionId;
            }

            // now save back to the blob
            string newJsonData = JsonConvert.SerializeObject(existingMappings);

            byte[] newBytes = Encoding.UTF8.GetBytes(newJsonData);

            CloudBlobContainer container = await BlobStorageHelper.BuildBlobContainerAsync(_connectionString, _containerName);

            CloudBlockBlob blob = container.GetBlockBlobReference(_blobName);

            await blob.UploadFromByteArrayAsync(
                newBytes,
                0,
                newBytes.Length,
                AccessCondition.GenerateIfMatchCondition(existingResults.ETag),
                null,
                null);
        }
        /// <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);
        }
Пример #7
0
        /// <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);
        }