private async Task VisitPayloadsAsync(MiniParseResult miniParseResult) { var payloads = miniParseResult.Model.Payloads; // Loop all payloads except CounterPayload and visit var exceptionInformation = new ConcurrentQueue <(string details, Exception exception)>(); foreach (var payload in payloads.Where(p => !(p is CounterPayload))) { try { payload.Visit(this); } catch (Exception ex) { exceptionInformation.Enqueue((payload.GetType().Name, ex)); } } var counterDataList = new List <CounterData>(); foreach (var counterPayload in payloads.OfType <CounterPayload>()) { if (TryMapCounterPayload(miniParseResult, counterPayload, out CounterData counterData)) { counterDataList.Add(counterData); } } try { await _counterService.StoreAsync(_correlationId, counterDataList); } catch (Exception ex) { exceptionInformation.Enqueue((nameof(CounterPayload), ex)); } // Only throw exception when all counters fail if (payloads.Count > 0 && exceptionInformation.Count == payloads.Count) { throw new AggregateException(exceptionInformation.Select(x => x.exception)); } // Else just log as error foreach (var info in exceptionInformation) { _logger.LogError(info.exception, $"Error storing Payload '{info.details}'"); } }
private static async Task RunAsync(ILogger logger, ICounterStoreService service, string path) { int roundAtMinutes = 5; string sn = Path.GetFileNameWithoutExtension(path).Substring(0, 13); string[] counterIds = { "0181", "0182", "0281", "0282", "2421" }; var list = new Dictionary <string, IList <CounterData> >(); foreach (var counterId in counterIds) { var result = ReadFile(logger, path + counterId + ".qbx", sn, int.Parse(counterId)); list.Add(counterId, result); } int batchValue = 12345; var values = list.Values.SelectMany(v => v).ToList(); var f = values.First(); var l = values.Last(); logger.LogInformation($"First: {f} and Last = {l}"); var grouped = from v in values group v by new { v.SerialNumber, v.CounterId, MeasureTimeRounded = v.MeasureTime.Truncate(TimeSpan.FromMinutes(roundAtMinutes)) } into g select new CounterData { SerialNumber = g.Key.SerialNumber, CounterId = g.Key.CounterId, MeasureTime = g.Key.MeasureTimeRounded, PulseValue = g.Max(s => s.PulseValue) }; var sorted = grouped.OrderBy(k => k.MeasureTime).ToList(); var sortedAndGrouped = sorted.GroupBy(s => s.MeasureTime).ToList(); Console.WriteLine($"values Count = {values.Count}"); Console.WriteLine($"sorted Count = {sorted.Count}"); Console.WriteLine($"sortedAndGrouped Count = {sortedAndGrouped.Count}"); int i = 1; var batch = new List <CounterData>(); foreach (var grp in sortedAndGrouped) { string guid = Guid.NewGuid().ToString(); foreach (var x in grp) { if (batch.Count < batchValue) { batch.Add(x); } else { Console.WriteLine($"{(100.0 * i) / sortedAndGrouped.Count:F}%"); await service.StoreAsync(guid, batch); batch.Clear(); } } i = i + 1; } }