diff --git a/TimeSeries/PublicApis/SdkExamples/LocationDeleter/Deleter.cs b/TimeSeries/PublicApis/SdkExamples/LocationDeleter/Deleter.cs index 13b9dd0c..edd3691c 100644 --- a/TimeSeries/PublicApis/SdkExamples/LocationDeleter/Deleter.cs +++ b/TimeSeries/PublicApis/SdkExamples/LocationDeleter/Deleter.cs @@ -59,6 +59,8 @@ private static string GetExecutingFileVersion() return $"{MethodBase.GetCurrentMethod().DeclaringType.Namespace} v{fileVersionInfo.FileVersion}"; } + private int LockedVisitCount { get; set; } + private void DeleteSpecifiedFieldVisits() { if (!IsFieldVisitDeletionEnabled()) @@ -91,6 +93,8 @@ private void DeleteSpecifiedFieldVisits() Log.Info($"Inspecting {locationQuantity} for field visits {string.Join(" and ", timeRange)} ..."); + LockedVisitCount = 0; + var deletedVisitCount = 0; foreach (var locationInfo in ResolvedLocations) @@ -98,10 +102,17 @@ private void DeleteSpecifiedFieldVisits() deletedVisitCount += DeleteVisitsAtLocation(locationInfo); } + var lockedVisitSummary = string.Empty; + + if (LockedVisitCount > 0) + { + lockedVisitSummary = $", skipping {"locked field visit".ToQuantity(LockedVisitCount)}"; + } + if (Context.DryRun) - Log.Info($"Dry run completed. {"field visit".ToQuantity(InspectedFieldVisits)} would have been deleted from {locationQuantity}."); + Log.Info($"Dry run completed. {"field visit".ToQuantity(InspectedFieldVisits)} would have been deleted from {locationQuantity}{lockedVisitSummary}."); else - Log.Info($"Deleted {"field visit".ToQuantity(deletedVisitCount)} from {locationQuantity}."); + Log.Info($"Deleted {"field visit".ToQuantity(deletedVisitCount)} from {locationQuantity}{lockedVisitSummary}."); } private bool IsFieldVisitDeletionEnabled() @@ -114,11 +125,28 @@ private int DeleteVisitsAtLocation(LocationInfo locationInfo) var siteVisitLocation = GetSiteVisitLocation(locationInfo); var visits = _siteVisit.Get(new GetLocationVisits + { + Id = siteVisitLocation.Id, + StartTime = Context.VisitsAfter?.UtcDateTime, + EndTime = Context.VisitsBefore?.UtcDateTime + }) + .OrderBy(v => v.StartDate) + .ToList(); + + var lockedVisits = visits + .Where(v => v.IsLocked) + .ToList(); + + if (lockedVisits.Any()) { - Id = siteVisitLocation.Id, - StartTime = Context.VisitsAfter?.UtcDateTime, - EndTime = Context.VisitsBefore?.UtcDateTime - }); + Log.Warn($"Skipping deletion of {"locked field visit".ToQuantity(lockedVisits.Count)} in location '{locationInfo.Identifier}'."); + + LockedVisitCount += lockedVisits.Count; + + visits = visits + .Where(v => !v.IsLocked) + .ToList(); + } if (!visits.Any()) { @@ -153,6 +181,8 @@ private int DeleteVisitsAtLocation(LocationInfo locationInfo) return visits.Count; } + private int LockedTimeSeriesCount { get; set; } + private void DeleteSpecifiedTimeSeries() { if (!Context.TimeSeriesToDelete.Any()) @@ -161,6 +191,8 @@ private void DeleteSpecifiedTimeSeries() if (Is3X()) throw new ExpectedException($"Time-series deletion is not supported for AQTS {Client.ServerVersion}"); + LockedTimeSeriesCount = 0; + var deletedTimeSeriesCount = 0; foreach (var timeSeriesIdentifier in Context.TimeSeriesToDelete) @@ -168,10 +200,17 @@ private void DeleteSpecifiedTimeSeries() deletedTimeSeriesCount += DeleteTimeSeries(timeSeriesIdentifier); } + var lockedTimeSeriesSummary = string.Empty; + + if (LockedTimeSeriesCount > 0) + { + lockedTimeSeriesSummary = $", skipping {LockedTimeSeriesCount} locked time-series"; + } + if (Context.DryRun) - Log.Info($"Dry run complete. {InspectedTimeSeries} time-series would have been deleted."); + Log.Info($"Dry run complete. {InspectedTimeSeries} time-series would have been deleted{lockedTimeSeriesSummary}."); else - Log.Info($"Deleted {deletedTimeSeriesCount} of {Context.TimeSeriesToDelete.Count} time-series."); + Log.Info($"Deleted {deletedTimeSeriesCount} of {Context.TimeSeriesToDelete.Count} time-series{lockedTimeSeriesSummary}."); } private int DeleteTimeSeries(string timeSeriesIdentifierOrGuid) @@ -201,10 +240,27 @@ private int DeleteTimeSeries(string timeSeriesIdentifierOrGuid) } Log.Info($"Deleting '{timeSeriesDescription.Identifier}' ..."); - DeleteTimeSeries(timeSeriesDescription); - Log.Info($"Deleted '{timeSeriesDescription.Identifier}' successfully."); - return 1; + try + { + DeleteTimeSeries(timeSeriesDescription); + Log.Info($"Deleted '{timeSeriesDescription.Identifier}' successfully."); + + return 1; + } + catch (WebServiceException exception) + { + if (exception.ErrorCode == "DeleteLockedTimeSeriesException") + { + Log.Warn($"Time-series '{timeSeriesDescription.Identifier}' has locked data and cannot be deleted."); + + ++LockedTimeSeriesCount; + + return 0; + } + + throw; + } } private void DeleteTimeSeries(TimeSeriesDescription timeSeriesDescription) diff --git a/TimeSeries/PublicApis/SdkExamples/LocationDeleter/LocationDeleter.csproj b/TimeSeries/PublicApis/SdkExamples/LocationDeleter/LocationDeleter.csproj index 31d90e92..faae5029 100644 --- a/TimeSeries/PublicApis/SdkExamples/LocationDeleter/LocationDeleter.csproj +++ b/TimeSeries/PublicApis/SdkExamples/LocationDeleter/LocationDeleter.csproj @@ -40,11 +40,11 @@ InternalLibraries\AQService.Common.dll - - ..\packages\Aquarius.SDK.18.1.1\lib\net45\Aquarius.Client.dll + + ..\packages\Aquarius.SDK.18.6.4\lib\net45\Aquarius.Client.dll - - ..\packages\Aquarius.SDK.Legacy.18.1.1\lib\net45\Aquarius.Client.Legacy.dll + + ..\packages\Aquarius.SDK.Legacy.18.6.4\lib\net45\Aquarius.Client.Legacy.dll InternalLibraries\CommunicationShared.dll diff --git a/TimeSeries/PublicApis/SdkExamples/LocationDeleter/PrivateApis.cs b/TimeSeries/PublicApis/SdkExamples/LocationDeleter/PrivateApis.cs index 602b312b..54f8e65e 100644 --- a/TimeSeries/PublicApis/SdkExamples/LocationDeleter/PrivateApis.cs +++ b/TimeSeries/PublicApis/SdkExamples/LocationDeleter/PrivateApis.cs @@ -58,6 +58,7 @@ public class Visit public long Id { get; set; } public DateTime StartDate { get; set; } public DateTime? EndDate { get; set; } + public bool IsLocked { get; set; } } [Route("/visits/{Id}", HttpMethods.Delete)] diff --git a/TimeSeries/PublicApis/SdkExamples/LocationDeleter/packages.config b/TimeSeries/PublicApis/SdkExamples/LocationDeleter/packages.config index d8c0e743..d3dc8d8b 100644 --- a/TimeSeries/PublicApis/SdkExamples/LocationDeleter/packages.config +++ b/TimeSeries/PublicApis/SdkExamples/LocationDeleter/packages.config @@ -1,7 +1,7 @@  - - + +