public async Task ProcessAsync(SubscriptionEntity entity) { List <UsageRecordEntity> records; UsageAggregationGetResponse usage; try { if (this.aggregation == null) { this.aggregation = await this.GetAggregationManagementClient(entity.SubscriptionId); } records = new List <UsageRecordEntity>(); // TODO - Refactor this code to utillize the continuation token if one is returned from this call. usage = await this.aggregation.UsageAggregates.GetAsync( DateTime.Now.AddDays(-ApplicationConfiguration.UsageRequestNumberOfDays), DateTime.Now, AggregationGranularity.Daily, true, string.Empty); records.AddRange(usage.UsageAggregations.Select(r => new UsageRecordEntity { Category = r.Properties.MeterCategory, CustomerId = entity.CustomerId, Id = r.Properties.MeterId, Location = InstanceData.GetInstance(r.Properties.InstanceData).Location, Name = r.Name, Quantity = Convert.ToDouble(r.Properties.Quantity), ResourceUri = InstanceData.GetInstance(r.Properties.InstanceData).ResourceUri.ToString(), Subcategory = r.Properties.MeterSubCategory, SubscriptionId = entity.SubscriptionId, Tags = JsonConvert.SerializeObject(InstanceData.GetInstance(r.Properties.InstanceData)?.Tags), Total = 0, // GetTotal(record.Resource.Id, record.Quantity), UniqueId = Guid.NewGuid(), Unit = r.Properties.Unit, UsageEndTime = r.Properties.UsageEndTime, UsageStartTime = r.Properties.UsageStartTime })); if (records.Count > 0) { await Program.Core.Storage.WriteToBlobAsync( "usage", $"{entity.SubscriptionId}-{DateTime.Now:yyyy-MM-dd-HH-mm-ss}.json", records); Parallel.ForEach(records, async record => { // Ensure the partition key for the entity is configured. record.PartitionKey = $"{record.CustomerId}_{record.SubscriptionId}"; // Ensure the row key for the entity is configured. record.RowKey = $"{record.UniqueId}"; // Write the usage record to an Azure storage table. await Program.Core.Storage.WriteToTableAsync("azureusage", record); }); } } finally { records = null; usage = null; } }
/// <summary> /// Initializes a new instance of the <see cref="UsageProcessor"/> class. /// </summary> /// <param name="client">The management client for retrieving usage records.</param> public UsageProcessor(IUsageAggregationManagementClient client) { this.aggregation = client; }