コード例 #1
0
        public async Task <IEnumerable <LoadMetric> > Aggregate(IReliableStateManager stateManager, CancellationToken cancellationToken)
        {
            Dictionary <string, double> totals = new Dictionary <string, double>();

            await stateManager.ForeachAsync(
                cancellationToken,
                async item =>
            {
                cancellationToken.ThrowIfCancellationRequested();

                IMetricReliableCollection metricCollection = item as IMetricReliableCollection;

                if (metricCollection != null)
                {
                    using (ITransaction tx = stateManager.CreateTransaction())
                    {
                        IEnumerable <DecimalLoadMetric> metrics = await metricCollection.GetLoadMetricsAsync(tx, cancellationToken);

                        foreach (DecimalLoadMetric metric in metrics)
                        {
                            if (totals.ContainsKey(metric.Name))
                            {
                                totals[metric.Name] += metric.Value;
                            }
                            else
                            {
                                totals[metric.Name] = metric.Value;
                            }
                        }
                    }
                }
            });

            return(totals.Select(x => new LoadMetric(x.Key, (int)Math.Round(x.Value))));
        }