Ejemplo n.º 1
0
        public async Task <MetricsTimeSeriesResultDto> Handle(GetMetricTimeSeriesQuery request, CancellationToken cancellationToken)
        {
            var dbMetrics = await ValidateMetrics(request);

            if (dbMetrics.Count == 0)
            {
                return(new MetricsTimeSeriesResultDto());
            }

            var timePeriod = TimePeriodUtils.Parse(request.StartTimestamp, request.EndTimestamp, request.LiveSpan);

            MetricsTimeSeriesResultDto result = new MetricsTimeSeriesResultDto();

            for (int i = 0; i < request.Metrics.Count; i++)
            {
                var requestMetric = request.Metrics[i];
                var dbMetric      = dbMetrics.FirstOrDefault(x => x.Name.Equals(requestMetric.MetricName) && x.Namespace.Equals(requestMetric.Namespace));

                if (dbMetric == null)
                {
                    continue;
                }

                // Retrieve the metrics
                var metricValues = await metricsRetriever.QueryAsync(dbMetric, timePeriod);

                // Down sample the metrics
                var samplingResult = metricDownSampler.DownSample(metricValues, request.SamplingTime, timePeriod.StartTimestamp, timePeriod.EndTimestamp);

                var metricValue = GetMetricTimeSeriesValueForSamplingType(requestMetric, samplingResult);

                result.Values.Add(metricValue);

                if (i == 0)
                {
                    result.Timestamps    = samplingResult.MetricValues.Select(x => x.TimeStamp).ToList();
                    result.Count         = samplingResult.MetricValues.Count;
                    result.SamplingValue = samplingResult.DownsamplingValue;
                }
            }

            return(result);
        }
        public async Task <List <MetricAggregatedValueDto> > Handle(GetMetricsAggregatedValueQuery request, CancellationToken cancellationToken)
        {
            var dbMetrics = await ValidateMetrics(request);

            if (dbMetrics.Count == 0)
            {
                return(new List <MetricAggregatedValueDto>());
            }

            var timePeriod = TimePeriodUtils.Parse(request.StartTimestamp, request.EndTimestamp, request.LiveSpan);

            var result = new List <MetricAggregatedValueDto>();

            for (int i = 0; i < request.Metrics.Count; i++)
            {
                var requestMetric = request.Metrics[i];
                var dbMetric      = dbMetrics.FirstOrDefault(x => x.Name.Equals(requestMetric.MetricName) && x.Namespace.Equals(requestMetric.Namespace));

                if (dbMetric == null)
                {
                    continue;
                }

                // Retrieve the metrics
                var metricValues = await metricsRetriever.QueryAsync(dbMetric, new TimePeriod()
                {
                    StartTimestamp = timePeriod.StartTimestamp, EndTimestamp = timePeriod.EndTimestamp
                });

                var metricValue = GetMetricTimeSeriesValue(requestMetric, metricValues);

                result.Add(metricValue);
            }

            return(result);
        }