public async Task ProcessAsync(DateTime passedDate) { foreach (var product in await _dataStorage.GetProducts()) { var dailyCounter = await _dataStorage.GetCounterOrDefault(product, PeriodKind.Daily, passedDate, CounterKind.Total); if (!dailyCounter.IsClosed) { dailyCounter.IsClosed = true; await _dataStorage.UpdateCounter(dailyCounter, 0); } var currentDate = DateTime.Now.Date; var currentDailyCounter = await _dataStorage.GetCounterOrDefault(product, PeriodKind.Daily, currentDate, CounterKind.Total); await _broadcaster.SendCounter(new CounterMessage() { PeriodKind = PeriodKind.Daily, PeriodStart = currentDate, Product = product, Kind = CounterKind.Total, Value = currentDailyCounter.Value }); } }
public async Task ProcessAsync(DateTime dateForHandle) { var products = await _dataStorage.GetProducts(); foreach (var product in products) { var calendar = CultureInfo.GetCultureInfo("ru-RU").Calendar; var dayOfWeek = calendar.GetDayOfWeek(dateForHandle); var dayOffset = DayOfWeek.Monday - dayOfWeek; // because Sunday is 7th day in a week but not 0th if (dayOffset > 0) { dayOffset -= DaysInWeek; } var periodStart = calendar.AddDays(dateForHandle, dayOffset); var weeklyCounter = await _dataStorage.GetCounterOrDefault(product, PeriodKind.Weekly, periodStart, CounterKind.Average); if (!weeklyCounter.IsClosed) { var dailyCounter = await _dataStorage.GetCounterOrDefault(product, PeriodKind.Daily, dateForHandle, CounterKind.Total); // check if dailyCounter already was accepted if (weeklyCounter.PeriodActualDate < dailyCounter.PeriodStart) { var totalDays = (-1 * dayOffset + 1); var average = (weeklyCounter.Value * -1 * dayOffset + dailyCounter.Value) / totalDays; if (totalDays == DaysInWeek) { weeklyCounter.IsClosed = true; } weeklyCounter.PeriodActualDate = dailyCounter.PeriodStart; weeklyCounter = await _dataStorage.UpdateCounter(weeklyCounter, average - weeklyCounter.Value); } } await _broadcaster.SendCounter(new CounterMessage { Product = product, PeriodKind = PeriodKind.Weekly, Value = weeklyCounter.Value, PeriodStart = periodStart, Kind = CounterKind.Average }); } }
public async Task ProcessAsync(SaleSystemNotification notification) { var periodKind = PeriodKind.Daily; var counterKind = CounterKind.Total; var counter = new Counter() { Product = notification.Product, PeriodKind = periodKind, PeriodStart = DateTimeOffset.Now.Date, Kind = counterKind }; await _logger.Write(notification); var delta = notification.Increment ? 1 : -1; var savedCounter = await _storage.UpdateCounter(counter, delta); await _broadcaster.SendCounter(new CounterMessage { Product = notification.Product, Value = savedCounter.Value, PeriodStart = counter.PeriodStart.Date, Kind = counterKind, PeriodKind = periodKind }); }