Ejemplo n.º 1
0
 public TimeSeriesService(ITimeSeriesReaderFactory timeSeriesReaderFactory, ITimeSeriesReader meteoReader, ISummaryWriter excelWriter, IFileService fileService)
 {
     this.timeSeriesReaderFactory = timeSeriesReaderFactory ?? throw new ArgumentNullException(nameof(timeSeriesReaderFactory));
     this.meteoReader             = meteoReader ?? throw new ArgumentNullException(nameof(meteoReader));
     this.excelWriter             = excelWriter ?? throw new ArgumentNullException(nameof(excelWriter));
     this.fileService             = fileService ?? throw new ArgumentNullException(nameof(fileService));
 }
Ejemplo n.º 2
0
        private async Task <TimeSeries> GetSingleTimeSeriesAsync(
            string requestId,
            ITimeSeriesReader reader,
            IReadOnlyDictionary <string, string> dimensionCombination,
            long startTime,
            long endTime,
            int seriesResolutionInMinutes,
            long unitsInArchive)
        {
            var datapoints = new List <KeyValuePair <long, double> >();

            if (unitsInArchive > 0)
            {
                var archivedEndTime = startTime + (unitsInArchive * seriesResolutionInMinutes * 60);
                if (endTime < archivedEndTime)
                {
                    archivedEndTime = endTime;
                }

                // Load and aggregate the archived time series
                datapoints.AddRange(await this.AggregateArchivedAsync(
                                        requestId,
                                        reader.MetricName,
                                        dimensionCombination,
                                        startTime,
                                        archivedEndTime,
                                        seriesResolutionInMinutes));

                startTime = archivedEndTime;
            }

            if (startTime < endTime)
            {
                // Load rest part from MDM directly
                var series = await this.GetTimeSeriesAsync(
                    requestId,
                    reader,
                    dimensionCombination,
                    startTime,
                    endTime,
                    seriesResolutionInMinutes);

                datapoints.AddRange(series.Datepoints);
            }

            return(new TimeSeries
            {
                StartTime = startTime,
                EndTime = endTime,
                SeriesResolutionInMinutes = seriesResolutionInMinutes,
                Datepoints = datapoints.ToDictionary(p => p.Key, p => p.Value),
                DimensionCombination = dimensionCombination
            });
        }
Ejemplo n.º 3
0
        private async Task TryArchiveSingleMetricTimeSeriesAsync(
            ITimeSeriesReader reader,
            long startTime,
            long endTime)
        {
            // List dimension values with metrics in last month
            var dimensionCombinations = await reader.GetDimensionValuesAsync(
                this.dimensionNames,
                startTime,
                endTime);

            // Select dimension values to be archived
            var dimensionCombinationsToBeArchived = dimensionCombinations
                                                    .Where(this.dimensionValueSelector)
                                                    .ToList();

            foreach (var dimensionCombination in dimensionCombinationsToBeArchived)
            {
                try
                {
                    // Renew lease for each loop
                    await this.metricStore.RenewLeaseAsync();

                    await this.ArchiveSingleTimeSeriesAsync(
                        reader,
                        dimensionCombination,
                        startTime,
                        endTime);
                }
                catch (Exception ex)
                {
                    SmsProviderEventSource.Current.ErrorException(
                        EventSourceBase.EmptyTrackingId,
                        this,
                        nameof(this.TryArchiveSingleMetricTimeSeriesAsync),
                        OperationStates.Failed,
                        $"Failed to archive metrics {reader.MetricName} of {dimensionCombination}",
                        ex);
                }
            }
        }
Ejemplo n.º 4
0
        private async Task ArchiveSingleTimeSeriesAsync(
            ITimeSeriesReader reader,
            IReadOnlyDictionary <string, string> dimensionCombination,
            long startTime,
            long endTime)
        {
            // Skip dimension value which has already has been archived
            var archiveName = ArchiveName(
                reader.MetricName,
                dimensionCombination,
                startTime);

            if (await this.metricStore.IsTimeSeriesExist(archiveName))
            {
                return;
            }

            // Get time series from MDM, then archive to storage
            var series = await this.GetTimeSeriesAsync(
                EventSourceBase.EmptyTrackingId,
                reader,
                dimensionCombination,
                startTime,
                endTime,
                ArchiveResolutionInMinutes);

            await this.metricStore.PutTimeSeries(
                archiveName,
                series);

            SmsProviderEventSource.Current.Info(
                EventSourceBase.EmptyTrackingId,
                this,
                nameof(this.ArchiveSingleTimeSeriesAsync),
                OperationStates.Succeeded,
                $"Archived metrics as {archiveName}");
        }
Ejemplo n.º 5
0
        private async Task <TimeSeries> GetTimeSeriesAsync(
            string requestId,
            ITimeSeriesReader reader,
            IReadOnlyDictionary <string, string> dimensionCombination,
            long startTime,
            long endTime,
            int seriesResolutionInMinutes)
        {
            var timeSeriesDescription = GetTimeSeriesDescription(
                reader.MetricName,
                dimensionCombination,
                startTime,
                endTime);

            SmsProviderEventSource.Current.Info(
                requestId,
                this,
                nameof(this.GetTimeSeriesAsync),
                OperationStates.Starting,
                $"Start loading MDM metric {timeSeriesDescription}");

            var output = await reader.GetTimeSeriesAsync(
                dimensionCombination,
                startTime,
                endTime,
                seriesResolutionInMinutes);

            SmsProviderEventSource.Current.Info(
                requestId,
                this,
                nameof(this.GetTimeSeriesAsync),
                OperationStates.Succeeded,
                $"Succeeded in loading MDM metric {timeSeriesDescription}. {output.Datepoints.Count} datapoints returned");

            return(output);
        }