Skip to content

Commit

Permalink
Merge pull request #71 from DougSchmidt-AI/feature/Issue-70-LocationD…
Browse files Browse the repository at this point in the history
…eleterShouldSkipLockedData

Feature/issue 70 location deleter should skip locked data
  • Loading branch information
Doug Schmidt authored Sep 17, 2018
2 parents ecd664c + faaeaf0 commit 33badcd
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 17 deletions.
78 changes: 67 additions & 11 deletions TimeSeries/PublicApis/SdkExamples/LocationDeleter/Deleter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down Expand Up @@ -91,17 +93,26 @@ private void DeleteSpecifiedFieldVisits()

Log.Info($"Inspecting {locationQuantity} for field visits {string.Join(" and ", timeRange)} ...");

LockedVisitCount = 0;

var deletedVisitCount = 0;

foreach (var locationInfo in ResolvedLocations)
{
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()
Expand All @@ -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())
{
Expand Down Expand Up @@ -153,6 +181,8 @@ private int DeleteVisitsAtLocation(LocationInfo locationInfo)
return visits.Count;
}

private int LockedTimeSeriesCount { get; set; }

private void DeleteSpecifiedTimeSeries()
{
if (!Context.TimeSeriesToDelete.Any())
Expand All @@ -161,17 +191,26 @@ 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)
{
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)
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@
<Reference Include="AQService.Common">
<HintPath>InternalLibraries\AQService.Common.dll</HintPath>
</Reference>
<Reference Include="Aquarius.Client, Version=18.1.1.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Aquarius.SDK.18.1.1\lib\net45\Aquarius.Client.dll</HintPath>
<Reference Include="Aquarius.Client, Version=18.6.4.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Aquarius.SDK.18.6.4\lib\net45\Aquarius.Client.dll</HintPath>
</Reference>
<Reference Include="Aquarius.Client.Legacy, Version=18.1.1.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Aquarius.SDK.Legacy.18.1.1\lib\net45\Aquarius.Client.Legacy.dll</HintPath>
<Reference Include="Aquarius.Client.Legacy, Version=18.6.4.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Aquarius.SDK.Legacy.18.6.4\lib\net45\Aquarius.Client.Legacy.dll</HintPath>
</Reference>
<Reference Include="CommunicationShared">
<HintPath>InternalLibraries\CommunicationShared.dll</HintPath>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Aquarius.SDK" version="18.1.1" targetFramework="net47" />
<package id="Aquarius.SDK.Legacy" version="18.1.1" targetFramework="net47" />
<package id="Aquarius.SDK" version="18.6.4" targetFramework="net47" />
<package id="Aquarius.SDK.Legacy" version="18.6.4" targetFramework="net47" />
<package id="Costura.Fody" version="1.6.2" targetFramework="net47" developmentDependency="true" />
<package id="Fody" version="2.0.0" targetFramework="net47" developmentDependency="true" />
<package id="Humanizer.Core" version="2.2.0" targetFramework="net47" />
Expand Down

0 comments on commit 33badcd

Please sign in to comment.