public CountersInfoAggregator(NetEmptyCategory rootCategory)
        {
            if (rootCategory == null)
            {
                throw new ArgumentNullException("rootCategory");
            }

            _rootCategory = rootCategory;
            _aggreagetedCategoriesInfo = new List <CategoryAggregatedInfo>();
            _lastCheckChanges          = CountersInfoAggregatorChangeKind.None;
        }
        /// <summary>
        /// Собрать данные по категориям (пересобирает только изменившиеся категории)
        /// </summary>
        /// <param name="force">Пересканировать всё полностью</param>
        /// <returns>Что изменилось</returns>
        public CountersInfoAggregatorChangeKind Refresh(bool force = false)
        {
            CountersInfoAggregatorChangeKind checkChanges = CountersInfoAggregatorChangeKind.None;
            bool needFullRescan = force || _rootCategory.IsChildCategoriesChanged || _aggreagetedCategoriesInfo.Count == 0;

            if (!needFullRescan)
            {
                foreach (var categoryInfo in _aggreagetedCategoriesInfo)
                {
                    if (categoryInfo.SourceCategoryExtended.IsChildCategoriesChanged)
                    {
                        needFullRescan = true;
                        break;
                    }

                    if (categoryInfo.SourceCategoryExtended.IsChildInstancesChanged || categoryInfo.SourceCategoryExtended.IsCountersChanged)
                    {
                        if (categoryInfo.SourceCategoryExtended.IsChildInstancesChanged)
                        {
                            checkChanges = checkChanges | CountersInfoAggregatorChangeKind.Instances;
                        }
                        if (categoryInfo.SourceCategoryExtended.IsCountersChanged)
                        {
                            checkChanges = checkChanges | CountersInfoAggregatorChangeKind.Counters;
                        }
                        categoryInfo.Instances.Clear();
                        FillInstanceInfo(categoryInfo, categoryInfo.Instances);
                        FillCounterDescriptors(categoryInfo, categoryInfo.CounterDescriptors);
                    }
                }
            }

            if (needFullRescan)
            {
                _rootCategory.MarkChildCategoriesChangedAsViewed();
                _aggreagetedCategoriesInfo = RescanCategories(_rootCategory);
                checkChanges = CountersInfoAggregatorChangeKind.All;
            }

            _lastCheckChanges = checkChanges;
            return(checkChanges);
        }
Example #3
0
 /// <summary>
 /// Выполнить рассылку данных
 /// </summary>
 /// <param name="data">Агрегированные данные счётчиков</param>
 /// <param name="changes">Произошедшие изменения</param>
 /// <param name="token">Токен отмены</param>
 protected abstract void PerformDistribution(List <CategoryAggregatedInfo> data, CountersInfoAggregatorChangeKind changes, CancellationToken token);
Example #4
0
        /// <summary>
        /// Выполнить рассылку данных
        /// </summary>
        /// <param name="data">Агрегированные данные счётчиков</param>
        /// <param name="changes">Произошедшие изменения</param>
        /// <param name="token">Токен отмены</param>
        protected override void PerformDistribution(List <CategoryAggregatedInfo> data, CountersInfoAggregatorChangeKind changes, CancellationToken token)
        {
            if (data == null)
            {
                throw new ArgumentNullException("data");
            }

            if (!_netClient.HasConnection)
            {
                return;
            }

            List <GraphiteCounterData> graphiteData = new List <GraphiteCounterData>(50);

            try
            {
                foreach (var categoryInfo in data)
                {
                    if (token.IsCancellationRequested)
                    {
                        return;
                    }

                    graphiteData.Clear();

                    // Добавляем счётчики
                    DateTime startTime = DateTime.Now;
                    AddCountersFromCategory(_prefix, categoryInfo, graphiteData);
                    DateTime endTime     = DateTime.Now;
                    DateTime averageTime = startTime + TimeSpan.FromMilliseconds((endTime - startTime).TotalMilliseconds / 2);

                    // Рассылаем значения
                    if (graphiteData.Count > 0)
                    {
                        _netClient.SendCounterValues(averageTime, graphiteData);
                    }
                }
            }
            catch (CommunicationException)
            {
            }
            catch (TimeoutException)
            {
            }
        }
        /// <summary>
        /// Выполнить рассылку данных
        /// </summary>
        /// <param name="data">Агрегированные данные счётчиков</param>
        /// <param name="changes">Произошедшие изменения</param>
        /// <param name="token">Токен отмены</param>
        protected override void PerformDistribution(List <CategoryAggregatedInfo> data, CountersInfoAggregatorChangeKind changes, CancellationToken token)
        {
            if (data == null)
            {
                throw new ArgumentNullException("data");
            }

            if (!_netClient.HasConnection)
            {
                return;
            }

            try
            {
                // Проверяем, надо ли разослать обновлённое описание структуры
                if (changes.HasFlag(CountersInfoAggregatorChangeKind.Categoties) ||
                    changes.HasFlag(CountersInfoAggregatorChangeKind.Counters) ||
                    _netClient.WasReconnected)
                {
                    var description = BuildCategoriesDescription(data);
                    _netClient.UpdateDescription(_clientData, description);
                    _netClient.MarkReconnectedWasProcessed();
                }

                // Рассылаем значения
                DateTimeOffset startTime   = DateTimeOffset.Now;
                var            values      = BuildCategoriesValueData(data);
                DateTimeOffset endTime     = DateTimeOffset.Now;
                DateTimeOffset averageTime = startTime + TimeSpan.FromMilliseconds((endTime - startTime).TotalMilliseconds / 2);
                _netClient.UpdateValues(averageTime, values);
            }
            catch (CommunicationException cex)
            {
                LogError(cex, "Network error during data distribution to " + _netClient.RemoteSideName);
            }
            catch (TimeoutException tex)
            {
                LogError(tex, "Network error during data distribution to " + _netClient.RemoteSideName);
            }
        }