예제 #1
0
        private void AppendRecalculatedPoints(Instant firstPointTime)
        {
            var request = new TimeSeriesDataCorrectedServiceRequest
            {
                TimeSeriesUniqueId = _sourceTimeSeriesUniqueId,
                QueryFrom          = firstPointTime.ToDateTimeOffset(),
                GetParts           = "PointsOnly"
            };
            var sourcePoints = _client.Publish.Get(request).Points;

            var points = RecalculatePoints(sourcePoints).ToList();

            Console.WriteLine($"Appending {points.Count} recalculated points to timeSeriesUniqueId={_reflectedTimeSeriesUniqueId:N} starting at {points.First().Time}");

            var stopwatch = Stopwatch.StartNew();

            var result = _client.Acquisition.RequestAndPollUntilComplete(
                client => client.Post(new PostReflectedTimeSeries
            {
                UniqueId  = _reflectedTimeSeriesUniqueId,
                Points    = points,
                TimeRange = new Interval(points.First().Time.GetValueOrDefault(), Instant.MaxValue)
            }),
                (client, response) => client.Get(new GetTimeSeriesAppendStatus {
                AppendRequestIdentifier = response.AppendRequestIdentifier
            }),
                polledStatus => polledStatus.AppendStatus != AppendStatusCode.Pending);

            if (result.AppendStatus != AppendStatusCode.Completed)
            {
                throw new Exception($"Unexpected append status={result.AppendStatus}");
            }

            Console.WriteLine($"Appended {result.NumberOfPointsAppended} points (deleting {result.NumberOfPointsDeleted} points) in {stopwatch.ElapsedMilliseconds / 1000.0:F1} seconds.");
        }
예제 #2
0
        private TimeSeriesDataServiceResponse FetchMinimumTimeSeries(
            TimeSeriesChangeEvent detectedChange,
            TimeSeriesDescription timeSeriesDescription,
            SensorInfo existingSensor,
            TimeSeriesDataCorrectedServiceRequest dataRequest,
            ref bool deleteExistingSensor,
            ref ComputationPeriod period)
        {
            TimeSeriesDataServiceResponse timeSeries;

            if (!deleteExistingSensor && GetLastSensorTime(existingSensor) < dataRequest.QueryFrom)
            {
                // All the changed points have occurred after the last sensor point which exists in the SOS server.
                // This is the preferred code path, since we only need to export the new points.
                timeSeries = Aquarius.Publish.Get(dataRequest);

                if (period == ComputationPeriod.Unknown)
                {
                    // We may have just fetched enough recent points to determine the time-series frequency
                    period = ComputationPeriodEstimator.InferPeriodFromRecentPoints(timeSeries);
                }

                TrimEarlyPoints(timeSeriesDescription, timeSeries, period);

                return(timeSeries);
            }

            if (GetLastSensorTime(existingSensor) >= detectedChange.FirstPointChanged)
            {
                // A point has changed before the last known observation, so we'll need to throw out the entire sensor
                deleteExistingSensor = true;

                // We'll also need to fetch more data again
                dataRequest.QueryFrom = null;
            }

            timeSeries = FetchRecentSignal(timeSeriesDescription, dataRequest, ref period);

            if (GetLastSensorTime(existingSensor) >= detectedChange.FirstPointChanged)
            {
                // A point has changed before the last known observation, so we'll need to throw out the entire sensor
                deleteExistingSensor = true;

                // We'll also need to fetch more data again
                dataRequest.QueryFrom = null;
                timeSeries            = FetchRecentSignal(timeSeriesDescription, dataRequest, ref period);
            }

            TrimEarlyPoints(timeSeriesDescription, timeSeries, period);

            return(timeSeries);
        }
예제 #3
0
        private bool HaveExistingSosPointsChanged(
            TimeSeriesDataCorrectedServiceRequest dataRequest,
            DateTimeOffset?lastSensorTime,
            TimeSeriesChangeEvent detectedChange,
            TimeSeriesDescription timeSeriesDescription)
        {
            if (detectedChange.HasAttributeChange ?? false)
            {
                return(true);
            }

            if (!detectedChange.FirstPointChanged.HasValue || !lastSensorTime.HasValue)
            {
                return(false);
            }

            if (lastSensorTime < detectedChange.FirstPointChanged)
            {
                return(false);
            }

            var timeSeriesIdentifier = timeSeriesDescription.Identifier;

            var sosPoints  = new Queue <TimeSeriesPoint>(Sos.GetObservations(timeSeriesDescription, AddMilliseconds(detectedChange.FirstPointChanged.Value, -1), AddMilliseconds(lastSensorTime.Value, 1)));
            var aqtsPoints = new Queue <TimeSeriesPoint>(Aquarius.Publish.Get(dataRequest).Points);

            var sosCount  = sosPoints.Count;
            var aqtsCount = aqtsPoints.Count;

            Log.Info($"Fetched {sosCount} SOS points and {aqtsCount} AQUARIUS points for '{timeSeriesIdentifier}' from {dataRequest.QueryFrom:O} ...");

            while (sosPoints.Any() || aqtsPoints.Any())
            {
                var sosPoint  = sosPoints.FirstOrDefault();
                var aqtsPoint = aqtsPoints.FirstOrDefault();

                if (aqtsPoint == null)
                {
                    Log.Warn($"'{timeSeriesIdentifier}': AQUARIUS now has fewer points than SOS@{sosPoint?.Timestamp.DateTimeOffset:O}");
                    return(true);
                }

                if (sosPoint == null)
                {
                    break;
                }

                var aqtsValue = (dataRequest.ApplyRounding ?? false)
                    ? double.Parse(aqtsPoint.Value.Display)
                    : aqtsPoint.Value.Numeric;

                var sosValue = sosPoint.Value.Numeric;

                if (sosPoint.Timestamp.DateTimeOffset != aqtsPoint.Timestamp.DateTimeOffset)
                {
                    Log.Warn($"'{timeSeriesIdentifier}': Different timestamps: AQUARIUS={aqtsValue}@{aqtsPoint.Timestamp.DateTimeOffset:O} vs SOS={sosValue}@{sosPoint.Timestamp.DateTimeOffset:O}");
                    return(true);
                }

                if (!DoubleHelper.AreSame(aqtsValue, sosValue))
                {
                    Log.Warn($"'{timeSeriesIdentifier}': Different values @ {aqtsPoint.Timestamp.DateTimeOffset:O}: AQUARIUS={aqtsValue} vs SOS={sosValue}");
                    return(true);
                }

                sosPoints.Dequeue();
                aqtsPoints.Dequeue();
            }

            Log.Info($"'{timeSeriesDescription.Identifier}': All {sosCount} SOS points match between SOS and AQUARIUS.");
            dataRequest.QueryFrom = lastSensorTime.Value.AddTicks(1);

            return(false);
        }
예제 #4
0
        private void ExportTimeSeries(bool clearExportedData,
                                      TimeSeriesChangeEvent detectedChange,
                                      TimeSeriesDescription timeSeriesDescription)
        {
            var locationInfo = GetLocationInfo(timeSeriesDescription.LocationIdentifier);

            var(exportDuration, exportLabel) = GetExportDuration(timeSeriesDescription);

            var dataRequest = new TimeSeriesDataCorrectedServiceRequest
            {
                TimeSeriesUniqueId = timeSeriesDescription.UniqueId,
                QueryFrom          = GetInitialQueryFrom(detectedChange),
                ApplyRounding      = Context.ApplyRounding,
            };

            var existingSensor       = Sos.FindExistingSensor(timeSeriesDescription);
            var deleteExistingSensor = clearExportedData && existingSensor != null;
            var assignedOffering     = existingSensor?.Identifier;

            var lastSensorTime = GetLastSensorTime(existingSensor);

            if (HaveExistingSosPointsChanged(dataRequest, lastSensorTime, detectedChange, timeSeriesDescription))
            {
                Log.Warn($"FirstPointChanged={detectedChange.FirstPointChanged:O} AttributeChange={detectedChange.HasAttributeChange} of '{timeSeriesDescription.Identifier}' precedes LastSensorTime={lastSensorTime:O} of '{existingSensor?.Identifier}'. Forcing delete of existing sensor.");

                // A point has changed before the last known observation, so we'll need to throw out the entire sensor
                deleteExistingSensor = true;

                // We'll also need to fetch more data again
                dataRequest.QueryFrom = null;
            }

            if (dataRequest.QueryFrom == null)
            {
                // Get the full extraction
                var endPoint     = dataRequest.QueryTo ?? DateTimeOffset.Now;
                var startOfToday = new DateTimeOffset(endPoint.Year, endPoint.Month, endPoint.Day, 0, 0, 0,
                                                      timeSeriesDescription.UtcOffsetIsoDuration.ToTimeSpan());

                dataRequest.QueryFrom = startOfToday - exportDuration;
            }

            Log.Info($"Fetching changes from '{timeSeriesDescription.Identifier}' FirstPointChanged={detectedChange.FirstPointChanged:O} HasAttributeChanged={detectedChange.HasAttributeChange} QueryFrom={dataRequest.QueryFrom:O} ...");

            var timeSeries = Aquarius.Publish.Get(dataRequest);

            TrimExcludedPoints(timeSeriesDescription, timeSeries);

            var createSensor = existingSensor == null || deleteExistingSensor;

            TimeSeriesPointFilter.FilterTimeSeriesPoints(timeSeries);

            var exportSummary = $"{timeSeries.NumPoints} points [{timeSeries.Points.FirstOrDefault()?.Timestamp.DateTimeOffset:O} to {timeSeries.Points.LastOrDefault()?.Timestamp.DateTimeOffset:O}] from '{timeSeriesDescription.Identifier}' with ExportDuration={exportLabel}";

            ExportedTimeSeriesCount += 1;
            ExportedPointCount      += timeSeries.NumPoints ?? 0;

            if (Context.DryRun)
            {
                if (deleteExistingSensor)
                {
                    LogDryRun($"Would delete existing sensor '{existingSensor?.Identifier}'");
                }

                if (createSensor)
                {
                    LogDryRun($"Would create new sensor for '{timeSeriesDescription.Identifier}'");
                }

                LogDryRun($"Would export {exportSummary}.");
                return;
            }

            Log.Info($"Exporting {exportSummary} ...");

            if (deleteExistingSensor)
            {
                Sos.DeleteSensor(timeSeries);
                Sos.DeleteDeletedObservations();
            }

            if (createSensor)
            {
                var sensor = Sos.InsertSensor(timeSeries);

                assignedOffering = sensor.AssignedOffering;
            }

            Sos.InsertObservation(assignedOffering, locationInfo.LocationData, locationInfo.LocationDescription, timeSeries);
        }
예제 #5
0
        private void ExportTimeSeries(
            bool clearExportedData,
            TimeSeriesChangeEvent detectedChange,
            TimeSeriesDescription timeSeriesDescription)
        {
            Log.Info($"Fetching changes from '{timeSeriesDescription.Identifier}' FirstPointChanged={detectedChange.FirstPointChanged:O} HasAttributeChanged={detectedChange.HasAttributeChange} ...");

            var locationInfo = GetLocationInfo(timeSeriesDescription.LocationIdentifier);

            var period = GetTimeSeriesPeriod(timeSeriesDescription);

            var dataRequest = new TimeSeriesDataCorrectedServiceRequest
            {
                TimeSeriesUniqueId = timeSeriesDescription.UniqueId,
                QueryFrom          = GetInitialQueryFrom(detectedChange),
                ApplyRounding      = Context.ApplyRounding,
            };

            var existingSensor       = Sos.FindExistingSensor(timeSeriesDescription);
            var deleteExistingSensor = clearExportedData && existingSensor != null;
            var assignedOffering     = existingSensor?.Identifier;

            var timeSeries = FetchMinimumTimeSeries(detectedChange, timeSeriesDescription, existingSensor, dataRequest, ref deleteExistingSensor, ref period);

            var createSensor = existingSensor == null || deleteExistingSensor;

            TimeSeriesPointFilter.FilterTimeSeriesPoints(timeSeries);

            var exportSummary = $"{timeSeries.NumPoints} points [{timeSeries.Points.FirstOrDefault()?.Timestamp.DateTimeOffset:O} to {timeSeries.Points.LastOrDefault()?.Timestamp.DateTimeOffset:O}] from '{timeSeriesDescription.Identifier}' with Frequency={period}";

            ExportedTimeSeriesCount += 1;
            ExportedPointCount      += timeSeries.NumPoints ?? 0;

            if (Context.DryRun)
            {
                if (deleteExistingSensor)
                {
                    LogDryRun($"Would delete existing sensor '{existingSensor?.Identifier}'");
                }

                if (createSensor)
                {
                    LogDryRun($"Would create new sensor for '{timeSeriesDescription.Identifier}'");
                }

                LogDryRun($"Would export {exportSummary}.");
                return;
            }

            Log.Info($"Exporting {exportSummary} ...");

            if (deleteExistingSensor)
            {
                Sos.DeleteSensor(timeSeries);
                Sos.DeleteDeletedObservations();
            }

            if (createSensor)
            {
                var sensor = Sos.InsertSensor(timeSeries);

                assignedOffering = sensor.AssignedOffering;
            }

            Sos.InsertObservation(assignedOffering, locationInfo.LocationData, locationInfo.LocationDescription, timeSeries, timeSeriesDescription);
        }