DD.WellWorkover.Cloud/AsbCloudInfrastructure/Services/DetectOperations/WorkOperationDetection.cs
2024-07-24 13:19:13 +03:00

93 lines
3.0 KiB
C#

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using AsbCloudApp.Data;
using AsbCloudApp.Repositories;
using AsbCloudApp.Requests;
using AsbCloudApp.Services;
using AsbCloudInfrastructure.Background;
using Microsoft.Extensions.DependencyInjection;
namespace AsbCloudInfrastructure.Services.DetectOperations;
public class WorkOperationDetection : Work
{
private static readonly IDictionary<int, DateTime> CacheOfStartDatesByTelemetryId = new Dictionary<int, DateTime>();
public WorkOperationDetection()
: base("Operation detection")
{
Timeout = TimeSpan.FromMinutes(20);
OnErrorAsync = (id, exception, _) =>
{
var text = $"work {id}, when {CurrentState?.State}, throw error:{exception.Message}";
Trace.TraceWarning(text);
return Task.CompletedTask;
};
}
protected override async Task Action(string id,
IServiceProvider services,
Action<string, double?> onProgressCallback,
CancellationToken token)
{
var telemetryRepository = services.GetRequiredService<ICrudRepository<TelemetryDto>>();
var detectedOperationRepository = services.GetRequiredService<IDetectedOperationRepository>();
var detectedOperationService = services.GetRequiredService<IDetectedOperationService>();
var telemetryDataSaubService = services.GetRequiredService<ITelemetryDataSaubService>();
var idsTelemetry = (await telemetryRepository.GetAllAsync(token))
.Select(t => t.Id)
.ToArray();
var telemetriesDateRanges = await telemetryDataSaubService.GetDateRangesAsync(token);
var lastDetectedOperations = await detectedOperationRepository.GetLastDetectedOperationsAsync(token);
for (int i = 0; i < idsTelemetry.Length; i++)
{
var idTelemetry = idsTelemetry[i];
if (!telemetriesDateRanges.TryGetValue(idTelemetry, out var telemetryDateRange))
continue;
var dateBegin = telemetryDateRange.From.DateTime;
var dateEnd = telemetryDateRange.To.DateTime;
if (lastDetectedOperations.TryGetValue(idTelemetry, out var lastDetectedOperation))
dateBegin = lastDetectedOperation.DateEnd.UtcDateTime;
if (CacheOfStartDatesByTelemetryId.TryGetValue(idTelemetry, out var dateBeginFromCahce))
dateBegin = dateBeginFromCahce;
onProgressCallback.Invoke($"Start detecting telemetry: {idTelemetry} from {dateBegin}", i / idsTelemetry.Length);
const int pointsCount = 4 * 86_400;
while (dateBegin < dateEnd)
{
var request = new TelemetryDataRequest
{
GeDate = dateBegin,
Take = pointsCount,
Order = 0,
GeBlockPosition = 0
};
var detectedOperations =
await detectedOperationService.DetectOperationsAsync(idTelemetry, request, lastDetectedOperation, token);
await detectedOperationRepository.InsertRangeAsync(detectedOperations.Items, token);
dateBegin = detectedOperations.LastDate.UtcDateTime;
CacheOfStartDatesByTelemetryId[idTelemetry] = dateBegin;
await detectedOperationRepository.InsertRangeAsync(detectedOperations.Items, token);
}
}
}
}