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); }
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); }