/// <summary> /// Save statistics data to database. /// </summary> /// <param name="saveAll">Current time interval would not be saved if false.</param> private void SaveStatistics(bool saveAll) { var stats = new List <StatisticsRecord>(); lock (_lock) { DateTime currentTimestampBucket = GetCurrentTimestampBucket(); var newData = new Dictionary <DateTime, Dictionary <Guid?, StatisticsRecord> >(); foreach (var bucketData in _statData) { if (!saveAll && bucketData.Key == currentTimestampBucket) { newData.Add(bucketData.Key, bucketData.Value); } else { stats.AddRange(bucketData.Value.Values); } } _statData.Clear(); foreach (var stat in newData) { _statData[stat.Key] = stat.Value; } } if (stats.Count != 0) { _saveService.Save(stats); } }
/// <summary> /// Save statistics data to database. /// </summary> /// <param name="saveAll">Current time interval would not be saved if false.</param> private void SaveStatistics(bool saveAll) { var stats = new List <StatisticsRecord>(_unsavedStatistics); _unsavedStatistics.Clear(); lock (_lock) { DateTime currentTimestampBucket = GetCurrentTimestampBucket(); var newData = new Dictionary <DateTime, Dictionary <Guid?, StatisticsRecord> >(); foreach (var bucketData in _statData) { if (!saveAll && bucketData.Key == currentTimestampBucket) { newData.Add(bucketData.Key, bucketData.Value); } else { stats.AddRange(bucketData.Value.Values); } } _statData.Clear(); foreach (var stat in newData) { _statData[stat.Key] = stat.Value; } } if (stats.Count != 0) { try { _saveService.Save(stats); } catch (Exception exception) { _unsavedStatistics.AddRange(stats); _logger.LogError("Error of statistics saving, we try to next time...", exception.ToString()); } } }
private void TimerCallBack(object state) { var stats = new List <StatisticsRecord>(); try { var subscriptionsQueueEntries = GetWatchedQueues(); foreach (var q in subscriptionsQueueEntries) { var subPk = ((KeyGuid)(q.Subscription.__PrimaryKey)).Guid; _previousMessageStats.TryGetValue(subPk, out var previousMessageStats); var statisticsRecord = GetStatisticsByQueue(q, previousMessageStats); if (!IsZerroStatistics(statisticsRecord)) { stats.Add(statisticsRecord); } _previousMessageStats[subPk] = q.Queue.MessageStats; } } catch (Exception e) { _logger.LogError("Error was raised while getting statistics from Rabbit MQ.", e.ToString()); } try { _statisticsSaveService.Save(stats); } catch (Exception e) { _logger.LogError("A error was raised while writing statistics from Rabbit MQ to a Database.", e.ToString()); } }