Пример #1
0
        public async Task <MachineRunResults> CreateRunResults(MachineRunResults results)
        {
            var sql = @"
                INSERT INTO dbo.MachineRunResults
                    (MachineRuns
                    ,RunTime
                    ,CountBrokenMachines
                    ,SmashedCount
                    ,SlashedCount
                    ,TrashedCount
                    ,WidgetsDestroyed)
                VALUES
                    (@MachineRuns
                    ,@RunTime
                    ,@CountBrokenMachines
                    ,@SmashedCount
                    ,@SlashedCount
                    ,@TrashedCount
                    ,@WidgetsDestroyed)";

            await _conn.ExecuteAsync(sql, new
            {
                MachineRuns = JsonConvert.SerializeObject(new { results.MachineRuns, results.OrderStart, results.OrderEnd }),
                results.RunTime,
                results.CountBrokenMachines,
                results.SmashedCount,
                results.SlashedCount,
                results.TrashedCount,
                results.WidgetsDestroyed
            });

            return(results);
        }
        public static async Task <MachineRunResults> ProcessResults([ActivityTrigger] List <MachineRun> machines,
                                                                    [ServiceBus("orders", Connection = "ServiceBusConnection")] ICollector <string> orderOutput,
                                                                    [ServiceBus("logs", Connection = "ServiceBusConnection")] ICollector <string> logOutput,
                                                                    ILogger log)
        {
            log.LogDebug("starting run log");


            try
            {
                using (IDbConnection conn = new SqlConnection(Environment.GetEnvironmentVariable("SqlConnection")))
                {
                    var orderRepo  = new OrderRepository(conn);
                    var openOrders = orderRepo.GetOpenOrders().Result;

                    //store initial results
                    var results = new MachineRunResults(machines, openOrders.ToList());
                    results.OrderEnd = new List <Order>();

                    foreach (var order in openOrders.OrderBy(o => o.CreateDate))
                    {
                        var result = OrderProcessing.RunMachines(order, machines);

                        results.OrderEnd.Add(result.Order);
                        results.SmashedCount     += result.SmashedCount;
                        results.SlashedCount     += result.SlashedCount;
                        results.TrashedCount     += result.TrashedCount;
                        results.WidgetsDestroyed += result.WidgetsDestroyed;

                        machines = results.MachineRuns;

                        bool fixOffByOne = false;

                        if (order.PendingCount < 0)
                        {
                            fixOffByOne = true;

                            if (order.SmashedCount > -(order.PendingCount))
                            {
                                order.SmashedCount += order.PendingCount;
                                order.PendingCount  = 0;
                            }
                            else if (order.SlashedCount > -(order.PendingCount))
                            {
                                order.SlashedCount += order.PendingCount;
                                order.PendingCount  = 0;
                            }
                            else
                            {
                                order.CompletedCount += order.PendingCount;
                                order.PendingCount    = 0;
                            }
                        }

                        if (order.SmashedCount < 0)
                        {
                            fixOffByOne         = true;
                            order.PendingCount += order.SmashedCount;
                            order.SmashedCount  = 0;
                        }

                        if (order.SlashedCount < 0)
                        {
                            fixOffByOne         = true;
                            order.PendingCount += order.SlashedCount;
                            order.SlashedCount  = 0;
                        }

                        if (result.SlashedCount > 0 || results.SmashedCount > 0 || results.TrashedCount > 0 || results.WidgetsDestroyed > 0 || fixOffByOne)
                        {
                            var orderUpdate = orderRepo.UpdateOrder(result.Order).Result;


                            orderOutput.Add(JsonConvert.SerializeObject(result.Order,
                                                                        new JsonSerializerSettings
                            {
                                ContractResolver = new CamelCasePropertyNamesContractResolver()
                            }));
                            logOutput.Add($"{TimeZoneInfo.ConvertTimeFromUtc(DateTime.UtcNow, TimeZoneInfo.FindSystemTimeZoneById("US Eastern Standard Time")).ToLongTimeString()} Order updated. Id: {result.Order.Id}, (SM: {result.SmashedCount}, SL: {result.SlashedCount}, TR: {result.TrashedCount}, Failed: {result.WidgetsDestroyed})");
                        }
                    }

                    log.LogWarning($"Processing Complete {results.SmashedCount}, {results.SlashedCount}, {results.TrashedCount}, {results.WidgetsDestroyed}");

                    return(results);
                }
            }
            catch (Exception e)
            {
                log.LogError(e.Message);
            }

            return(null);
        }