public async Task OnGetRemoveColumn(string drain, string apa) { // Create reference an existing table CloudTable table = await TableStorageCommon.CreateTableAsync("Drains"); string b = apa; string a = drain.Substring(0, 1); // Get existing data for a specific master_node and address var drainExistingRow = await TableStorageUtils.RetrieveEntityUsingPointQueryAsync(table, a, drain); switch (apa) { case "AverageActivity": drainExistingRow.AverageActivity = 0; break; case "AverageRest": drainExistingRow.AverageRest = 0; break; } await TableStorageUtils.InsertOrMergeEntityAsync(table, drainExistingRow); // return "deleted"; }
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(); } }