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"; }
public async Task OnGetResetGrids() { // Create reference to an existing table CloudTable table = await TableStorageCommon.CreateTableAsync("ResetGrid"); ResetGridTableStorageEntity resetGridExistingRow = new ResetGridTableStorageEntity(); // Get existing data for a specific master_node and address resetGridExistingRow = await TableStorageUtils.RetrieveResetGridEntityUsingPointQueryAsync(table, "one", "two"); // Create a new/update record for Azure Table Storage var now = DateTime.Now; if (now > resetGridExistingRow.ResetDate.ToLocalTime() && now < resetGridExistingRow.ResetDate.ToLocalTime().AddHours(MyConsts.resetGridWaitTime)) { now = now.AddDays(-1); } else { } ResetGridTableStorageEntity drain = new ResetGridTableStorageEntity("one", "two", now); await TableStorageUtils.InsertOrMergeResetGridEntityAsync(table, drain); }
public async Task Drain(string drain) { // Create reference an existing table CloudTable table = await TableStorageCommon.CreateTableAsync("Drains"); // Get existing data for address var drainExistingRow = await TableStorageUtils.RetrieveEntityUsingPointQueryAsync(table, drain.Substring(0, 1), drain); drainExistingRow.TimeUp = drainExistingRow.TimeUp.ToLocalTime(); drainExistingRow.TimeDown = drainExistingRow.TimeDown.ToLocalTime(); DateTime dateNow = DateTime.Now; await Clients.Caller.SendAsync("Drain", drain, drainExistingRow, dateNow); }
public async Task OnGet() { // Create reference an existing table CloudTable table = await TableStorageCommon.CreateTableAsync("Drains"); // Get reset time from ResetGrid CloudTable tableResetGrid = await TableStorageCommon.CreateTableAsync("ResetGrid"); ResetGridTableStorageEntity resetGridExistingRow = new ResetGridTableStorageEntity(); resetGridExistingRow = await TableStorageUtils.RetrieveResetGridEntityUsingPointQueryAsync(tableResetGrid, "one", "two"); // Get rows newer than that date qResult = TableStorageUtils.GetAll(table); // Filter only data from resetDate. Valid 2 hours. var now = DateTime.Now; ViewData["buttonText"] = "Återställ grid i 2h"; if (now > resetGridExistingRow.ResetDate.ToLocalTime() && now < resetGridExistingRow.ResetDate.ToLocalTime().AddHours(MyConsts.resetGridWaitTime)) { qResult = table.ExecuteQuery(new TableQuery <DrainTableStorageEntity>()).Where(a => a.TimeUp.ToLocalTime() > resetGridExistingRow.ResetDate.ToLocalTime()).ToList(); ViewData["buttonText"] = resetGridExistingRow.ResetDate.ToLocalTime().ToString(); } ViewData["qResult"] = JsonConvert.SerializeObject(qResult); var weekAgo = DateTime.Now.AddDays(-6); var data = qResult.Where(a => a.TimeUp <= weekAgo); ViewData["noActivations"] = JsonConvert.SerializeObject(data); }
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(); } }