public static async Task <IActionResult> Run( [HttpTrigger(AuthorizationLevel.Function, "get")] HttpRequest req, ILogger log) { log.LogInformation("C# HTTP trigger function processed a request."); CloudTable table = StaticHelpers.GetTable("IotTableHourly"); List <ResponseItem> allItems = await StaticHelpers.GetAlltableItemAsyncResponseItem(table); int daysInt = 7; string days = req.Query["days"]; if (!String.IsNullOrWhiteSpace(days)) { daysInt = int.Parse(days); } var filteredItems = allItems .Where(i => DateTime.ParseExact(i.RowKey, "dd-MM-yyyy HH:mm:ss", CultureInfo.InvariantCulture) >= DateTime.Now.AddDays(-daysInt)) .ToList(); if (filteredItems.Count != 0) { string top = req.Query["top"]; if (!String.IsNullOrWhiteSpace(top)) { int topint = Int32.Parse(top); return(new OkObjectResult(filteredItems.Take(topint))); } else { return(new OkObjectResult(filteredItems)); } } else { return(new BadRequestResult()); } }
private static async Task CleanUpOldItemsAsync(List <MyTableEntity> allItems) { CloudTable table = StaticHelpers.GetTable("IotTable"); int rowOffset = 0; while (rowOffset < allItems.Count) { var batch = new TableBatchOperation(); // next batch var rows = allItems .Skip(rowOffset) .Take(100) .ToList(); rows.ForEach(item => batch.Add(TableOperation.Delete(item))); // submit await table.ExecuteBatchAsync(batch); rowOffset += rows.Count; } }
public static async Task RunAsync([TimerTrigger("0 50 23 * * * ")] TimerInfo myTimer, ILogger log) { log.LogInformation($"C# Timer trigger function executed at: {DateTime.Now}"); CloudTable table = StaticHelpers.GetTable("IotTableHourly"); List <ResponseItem> allItems = await StaticHelpers.GetAlltableItemResponseAsync(table, DateTime.Now.ToString("MMMM-yyyy")); allItems.ForEach(i => { var date = DateTime.ParseExact(s: i.RowKey, format: "dd-MM-yyyy HH:mm:ss", CultureInfo.InvariantCulture); i.RowKey = date.Day.ToString(); }); List <string> DateRanges = allItems.Select(i => i.RowKey).Distinct().ToList(); List <ResponseItem> result = new List <ResponseItem>(); foreach (var item in DateRanges) { List <ResponseItem> range = allItems.Where(i => i.RowKey == item).ToList(); ResponseItem entity = new ResponseItem { PartitionKey = range[0].PartitionKey, RowKey = item, humidity = (int)Math.Round(range.Average(i => i.humidity), 0), temperature = (int)Math.Round(range.Average(i => i.temperature), 0), AQI = (int)Math.Round(range.Average(i => i.AQI), 0) }; result.Add(entity); } CloudTable tableDaily = StaticHelpers.GetTable("IotTableDaily"); foreach (var item in result) { await tableDaily.ExecuteAsync(TableOperation.InsertOrReplace(item)); } }