// <InsertItem> public static async Task <DrainTableStorageEntity> InsertOrMergeEntityAsync(CloudTable table, DrainTableStorageEntity entity) { if (entity == null) { throw new ArgumentNullException("entity"); } try { // Create the InsertOrReplace table operation TableOperation insertOrMergeOperation = TableOperation.InsertOrMerge(entity); // Execute the operation. TableResult result = await table.ExecuteAsync(insertOrMergeOperation); DrainTableStorageEntity insertedCustomer = result.Result as DrainTableStorageEntity; if (result.RequestCharge.HasValue) { Console.WriteLine("Request Charge of InsertOrMerge Operation: " + result.RequestCharge); } return(insertedCustomer); } catch (StorageException e) { Console.WriteLine(e.Message); Console.ReadLine(); throw; } }
// <QueryData> public static async Task <DrainTableStorageEntity> RetrieveEntityUsingPointQueryAsync(CloudTable table, string partitionKey, string rowKey) { try { TableOperation retrieveOperation = TableOperation.Retrieve <DrainTableStorageEntity>(partitionKey, rowKey); TableResult result = await table.ExecuteAsync(retrieveOperation); DrainTableStorageEntity customer = result.Result as DrainTableStorageEntity; if (customer != null) { Console.WriteLine("\t{0}\t{1}\t{2}\t{3}", customer.PartitionKey, customer.RowKey, customer.TimeUp, customer.TimeDown); } if (result.RequestCharge.HasValue) { Console.WriteLine("Request Charge of Retrieve Operation: " + result.RequestCharge); } return(customer); } catch (StorageException e) { Console.WriteLine(e.Message); Console.ReadLine(); throw; } }
// <DeleteItem> public static async Task DeleteEntityAsync(CloudTable table, DrainTableStorageEntity deleteEntity) { try { if (deleteEntity == null) { throw new ArgumentNullException("deleteEntity"); } TableOperation deleteOperation = TableOperation.Delete(deleteEntity); TableResult result = await table.ExecuteAsync(deleteOperation); if (result.RequestCharge.HasValue) { Console.WriteLine("Request Charge of Delete Operation: " + result.RequestCharge); } } catch (StorageException e) { Console.WriteLine(e.Message); Console.ReadLine(); throw; } }
private async Task SaveToTableStorage(ActivityViewModel drainPatrolViewModel) { // Create reference to an existing table CloudTable table = await TableStorageCommon.CreateTableAsync("Drains"); DrainTableStorageEntity drainExistingRow = new DrainTableStorageEntity(); // Get existing data for a specific master_node and address drainExistingRow = await TableStorageUtils.RetrieveEntityUsingPointQueryAsync(table, drainPatrolViewModel.Master_node.ToString(), drainPatrolViewModel.Address); // Create a new/update record for Azure Table Storage DrainTableStorageEntity drain = new DrainTableStorageEntity(drainPatrolViewModel.Master_node.ToString(), drainPatrolViewModel.Address); // Check if address is actice if (drainPatrolViewModel.Active) { // Store data in Azure nosql table if Active == true drain.TimeUp = drainPatrolViewModel.Time.ToLocalTime(); drain.TimeDown = drainExistingRow.TimeDown.ToLocalTime(); drain.IsActive = drainPatrolViewModel.Active; drain.AverageActivity = drainExistingRow.AverageActivity; var diff = (drainPatrolViewModel.Time - drainExistingRow.TimeDown).TotalSeconds; if (drainExistingRow.AverageRest == 0) { drain.AverageRest = (int)diff; } else { drain.AverageRest = (int)((drainExistingRow.AverageRest + diff) / 2); } // Add hourly counter if within same hour otherwise save count to Azure SQL table AcitvityCounts if (DateExtensions.NewHour(drainPatrolViewModel.Time.ToLocalTime(), drainExistingRow.TimeUp.ToLocalTime())) { // New hour reset counter to one drain.HourlyCount = 1; if (DateExtensions.IsNewDay(drainPatrolViewModel.Time.ToLocalTime(), drainExistingRow.TimeUp.ToLocalTime())) { drain.DailyCount = 1; } else { drain.DailyCount = drain.DailyCount + 1; } var averageCount = drainExistingRow.AverageActivity; // Save counter ActivityCount ac = new ActivityCount { Address = drainExistingRow.RowKey, CountActivity = drainExistingRow.HourlyCount, Hourly = DateExtensions.RemoveMinutesAndSeconds(convertToLocalTimeZone(drainExistingRow.TimeUp)), AverageCount = averageCount }; drain.AverageActivity = (averageCount + drainExistingRow.HourlyCount) / 2; await TableStorageUtils.InsertOrMergeEntityAsync(table, drain); _context.ActivityCounts.Add(ac); await _context.SaveChangesAsync(); } else { // withing the same hour add one to existing sum. drain.HourlyCount = drainExistingRow.HourlyCount + 1; drain.DailyCount = drainExistingRow.DailyCount + 1; // Save updated to the Azure nosql table await TableStorageUtils.InsertOrMergeEntityAsync(table, drain); } } else { // Get data from Azure table and store data in one row on ActivityPerRow // drain = await TableStorageUtils.RetrieveEntityUsingPointQueryAsync(table, drainPatrolViewModel.Master_node.ToString(), drainPatrolViewModel.Address); drain.TimeUp = drainExistingRow.TimeUp.ToLocalTime(); drain.TimeDown = drainPatrolViewModel.Time.ToLocalTime(); drain.IsActive = drainPatrolViewModel.Active; drain.HourlyCount = drainExistingRow.HourlyCount; drain.AverageRest = drainExistingRow.AverageRest; var diff = (drain.TimeDown - drain.TimeUp).TotalSeconds; if (drainExistingRow.AverageActivity == 0) { drain.AverageActivity = (int)diff; } else { drain.AverageActivity = (int)((drainExistingRow.AverageActivity + diff) / 2); } await TableStorageUtils.InsertOrMergeEntityAsync(table, drain); bool isGroup = false; if (drain.RowKey.Contains("7") || drain.RowKey.Contains("8")) { isGroup = true; } var perRowData = new ActivityPerRow { Address = drain.RowKey, TimeUp = drain.TimeUp, TimeDown = drain.TimeDown, TimeDiff = (drain.TimeDown - drain.TimeUp).TotalMilliseconds, //(drain.TimeDown - drain.TimeUp.AddHours(1)).TotalMilliseconds, IsGroupAddress = isGroup }; _context.ActivityPerRows.Add(perRowData); await _context.SaveChangesAsync(); } }