public static async Task createMonthlyDeviceConsumptionItem(string device_id, DateTime period, long consuption, long waterReadfirstEventInTheMonth, long waterReadfirstEventInTheLastMonth, string user_id) { MonthlyDeviceConsumptionItem monthlyDeviceConsumptionItem = new MonthlyDeviceConsumptionItem() { id = $"{device_id}-{period.Month}-{period.Year}", consumption_sum = consuption, month = period.Month, year = period.Year, device_id = device_id, user_id = user_id, first_water_read = waterReadfirstEventInTheLastMonth, last_water_read = waterReadfirstEventInTheMonth }; // Create an item in the container representing the bill. ItemResponse <MonthlyDeviceConsumptionItem> monthlyConsumptionResponse = await Resources.monthly_consumption_container.UpsertItemAsync <MonthlyDeviceConsumptionItem>(monthlyDeviceConsumptionItem); // Note that after creating the item, we can access the body of the item with the Resource property off the ItemResponse. Console.WriteLine("Created item in database with id: {0}\n", monthlyConsumptionResponse.Resource.id); }
public static async Task <IActionResult> getUserConsumption( [HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "consumption_per_month/userId={userId}&year={year}")] HttpRequest request, [CosmosDB( databaseName: "waterly_db", collectionName: "waterly_devices", ConnectionStringSetting = "CosmosDBConnection", SqlQuery = "SELECT * FROM c WHERE c.userId = {userId}")] IEnumerable <DeviceItem> devices, string year, ILogger log) { Container BillsContainer = Resources.bill_container; Container ConsumptionContainer = Resources.monthly_consumption_container; int cur_year = Int32.Parse(year); //for all year, dict for month number and dict userConsumptionPerMonthDict List <Dictionary <string, long> > consumptions_months_list = new List <Dictionary <string, long> >(); for (int month_num = 1; month_num < 13; month_num++) { //for each month, dict for device id and consumption sum Dictionary <string, long> userConsumptionPerMonthDict = new Dictionary <string, long>(); userConsumptionPerMonthDict.Add("Month", month_num); //Add avg per month var sqlQueryText = $"SELECT TOP 1 * FROM c WHERE c.month = {month_num} AND c.year = {cur_year}"; QueryDefinition bills_query = new QueryDefinition(sqlQueryText); FeedIterator <BillItem> bill_iterator = BillsContainer.GetItemQueryIterator <BillItem>(bills_query); Microsoft.Azure.Cosmos.FeedResponse <BillItem> currentResultSet; long avg = 0; while (bill_iterator.HasMoreResults) { currentResultSet = await bill_iterator.ReadNextAsync(); if (currentResultSet.Count == 0) { break; } else { BillItem first_bill = currentResultSet.First(); avg = (long)first_bill.avg; avg /= 10; break; } } userConsumptionPerMonthDict.Add("Average", avg); foreach (DeviceItem device_item in devices) { //calculate sum for month_num for device_item string device_id = device_item.id; sqlQueryText = $"SELECT * FROM c WHERE c.month = {month_num} AND c.year = {cur_year} AND c.device_id = '{device_id}'"; QueryDefinition consumption_query = new QueryDefinition(sqlQueryText); FeedIterator <MonthlyDeviceConsumptionItem> consumption_iterator = ConsumptionContainer.GetItemQueryIterator <MonthlyDeviceConsumptionItem>(consumption_query); long consumption_per_device_month = 0; Microsoft.Azure.Cosmos.FeedResponse <MonthlyDeviceConsumptionItem> ConsumptionCurrentResultSet; while (consumption_iterator.HasMoreResults) { ConsumptionCurrentResultSet = await consumption_iterator.ReadNextAsync(); if (ConsumptionCurrentResultSet.Count == 0) { break; } else { MonthlyDeviceConsumptionItem first_sum_item = ConsumptionCurrentResultSet.First(); consumption_per_device_month = first_sum_item.consumption_sum / 1000000; break; } } //add to dict userConsumptionPerMonthDict.Add(device_item.name, consumption_per_device_month); } consumptions_months_list.Add(userConsumptionPerMonthDict); } return(new OkObjectResult(consumptions_months_list)); }