public async Task ProcessPurchasesAsync(List <ProductPurchase> orders) { Logger.Debug("{0}: {1} orders", nameof(this.ProcessPurchasesAsync), orders?.Count); if (orders == null) { return; } //Dictionary<int, ProductStockTrend> ProductStockTrends try { Dictionary <int, int> productIds = new Dictionary <int, int>(); DateTime now = DateTime.UtcNow; Dictionary <int, ProductStockTrend> productStockTrends = await this.StateManager.GetStateAsync <Dictionary <int, ProductStockTrend> >("ProductStockTrends"); foreach (ProductPurchase order in orders) { // Add this product trend if we don't track it yet if (!productStockTrends.ContainsKey(order.ProductId)) { productStockTrends.Add( order.ProductId, new ProductStockTrend() { ProductId = order.ProductId, ProductName = order.ProductName }); } ProductStockTrend trend = productStockTrends[order.ProductId]; trend.Reset(now.AddMonths(-1), DateTime.UtcNow); trend.AddOrder(order, this.notificationAttempts); productIds[order.ProductId] = 0; } await this.StateManager.SetStateAsync <Dictionary <int, ProductStockTrend> >("ProductStockTrends", productStockTrends); await this.CalculatePredictionsAsync(productIds.Keys); } catch (Exception ex) { Logger.Error(ex, nameof(this.ProcessPurchasesAsync)); throw; } }
private async Task CalculatePredictionsAsync(IEnumerable <int> productIds) { Logger.Debug(nameof(this.CalculatePredictionsAsync)); Dictionary <int, ProductStockTrend> productTrends = await this.StateManager.GetStateAsync <Dictionary <int, ProductStockTrend> >("ProductStockTrends"); AzureMlBatchResponse response = await this.mlClient.InvokeBatchAsync(new AzureMlBatchRequest(productIds.Select(x => productTrends[x]))); foreach (AzureMlBatchResponse.ResponseLine r in response.Lines) { ProductStockTrend trend = productTrends[r.ProductId]; trend.Probability = r.Probability; trend.Reorder = r.Reorder; } await this.StateManager.SetStateAsync <Dictionary <int, ProductStockTrend> >("ProductStockTrends", productTrends); }