2021-07-21 15:29:19 +05:00
|
|
|
|
using AsbCloudApp.Data;
|
2023-12-25 10:53:45 +05:00
|
|
|
|
using AsbCloudApp.Requests;
|
2021-07-21 15:29:19 +05:00
|
|
|
|
using AsbCloudApp.Services;
|
2021-05-18 12:33:23 +05:00
|
|
|
|
using AsbCloudDb.Model;
|
2022-12-02 14:48:23 +05:00
|
|
|
|
using AsbCloudInfrastructure.Background;
|
2021-07-21 15:29:19 +05:00
|
|
|
|
using AsbSaubReport;
|
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
2021-05-18 12:33:23 +05:00
|
|
|
|
using System;
|
2021-05-31 12:33:17 +05:00
|
|
|
|
using System.Collections.Generic;
|
2021-07-21 15:29:19 +05:00
|
|
|
|
using System.Linq;
|
2021-08-11 16:54:42 +05:00
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
2021-05-18 12:33:23 +05:00
|
|
|
|
|
2023-12-05 14:48:56 +05:00
|
|
|
|
namespace AsbCloudInfrastructure.Services;
|
|
|
|
|
|
|
|
|
|
public class ReportService : IReportService
|
2021-05-18 12:33:23 +05:00
|
|
|
|
{
|
2023-12-05 14:48:56 +05:00
|
|
|
|
private readonly IAsbCloudDbContext db;
|
|
|
|
|
private readonly ITelemetryService telemetryService;
|
|
|
|
|
private readonly FileService fileService;
|
|
|
|
|
private readonly IWellService wellService;
|
|
|
|
|
private readonly BackgroundWorker backgroundWorkerService;
|
|
|
|
|
|
|
|
|
|
public ReportService(IAsbCloudDbContext db,
|
|
|
|
|
ITelemetryService telemetryService,
|
|
|
|
|
IWellService wellService,
|
|
|
|
|
FileService fileService,
|
|
|
|
|
BackgroundWorker backgroundWorkerService)
|
|
|
|
|
{
|
|
|
|
|
this.db = db;
|
|
|
|
|
this.wellService = wellService;
|
|
|
|
|
this.backgroundWorkerService = backgroundWorkerService;
|
|
|
|
|
this.telemetryService = telemetryService;
|
|
|
|
|
this.fileService = fileService;
|
|
|
|
|
}
|
2023-04-18 16:22:53 +05:00
|
|
|
|
|
2023-12-25 10:53:45 +05:00
|
|
|
|
public string EnqueueCreateReportWork(int idWell, int idUser, ReportParametersRequest request, Action<object, string> progressHandler)
|
2021-05-18 12:33:23 +05:00
|
|
|
|
{
|
2023-12-25 10:53:45 +05:00
|
|
|
|
var work = new WorkToCreateReport(idWell, idUser, request, progressHandler);
|
2022-12-02 14:48:23 +05:00
|
|
|
|
|
2023-12-05 14:48:56 +05:00
|
|
|
|
work.OnErrorAsync = (message, exception, token) => Task.Run(() => progressHandler.Invoke(new
|
2023-12-25 10:53:45 +05:00
|
|
|
|
{
|
|
|
|
|
Operation = "error",
|
|
|
|
|
Progress = 100f,
|
|
|
|
|
Message = string.IsNullOrEmpty(message)
|
2023-12-05 14:48:56 +05:00
|
|
|
|
? exception.Message
|
|
|
|
|
: message,
|
2023-12-25 10:53:45 +05:00
|
|
|
|
Exception = exception,
|
|
|
|
|
}, work.Id)
|
2023-12-05 14:48:56 +05:00
|
|
|
|
, token);
|
|
|
|
|
|
|
|
|
|
backgroundWorkerService.Enqueue(work);
|
|
|
|
|
|
|
|
|
|
progressHandler.Invoke(new ReportProgressDto
|
2021-05-18 12:33:23 +05:00
|
|
|
|
{
|
2023-12-05 14:48:56 +05:00
|
|
|
|
Operation = "Ожидает начала в очереди.",
|
|
|
|
|
Progress = 0f,
|
2023-12-25 10:53:45 +05:00
|
|
|
|
}, work.Id);
|
|
|
|
|
return work.Id;
|
2023-12-05 14:48:56 +05:00
|
|
|
|
}
|
2022-01-05 17:50:45 +05:00
|
|
|
|
|
2023-12-05 14:48:56 +05:00
|
|
|
|
public int GetReportPagesCount(int idWell, DateTime begin, DateTime end, int stepSeconds, int format)
|
|
|
|
|
{
|
|
|
|
|
var timezoneOffset = wellService.GetTimezone(idWell).Hours;
|
|
|
|
|
var beginRemote = begin.ToTimeZoneOffsetHours(timezoneOffset);
|
|
|
|
|
var endRemote = end.ToTimeZoneOffsetHours(timezoneOffset);
|
2021-05-18 12:33:23 +05:00
|
|
|
|
|
2023-12-05 14:48:56 +05:00
|
|
|
|
var generator = GetReportGenerator(idWell, beginRemote, endRemote, stepSeconds, format, db);
|
|
|
|
|
var pagesCount = generator.GetPagesCount();
|
|
|
|
|
return pagesCount;
|
|
|
|
|
}
|
2021-05-31 12:33:17 +05:00
|
|
|
|
|
2023-12-05 14:48:56 +05:00
|
|
|
|
public DatesRangeDto? GetDatesRangeOrDefault(int idWell)
|
|
|
|
|
{
|
|
|
|
|
var telemetry = telemetryService.GetOrDefaultTelemetryByIdWell(idWell);
|
|
|
|
|
if (telemetry is null)
|
|
|
|
|
return null;
|
|
|
|
|
var range = telemetryService.GetDatesRange(telemetry.Id);
|
|
|
|
|
return range;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<IEnumerable<ReportPropertiesDto>> GetAllReportsByWellAsync(int idWell, CancellationToken token)
|
|
|
|
|
{
|
|
|
|
|
var timezoneOffset = wellService.GetTimezone(idWell).Hours;
|
|
|
|
|
var propertiesQuery = db.ReportProperties.Include(r => r.File)
|
|
|
|
|
.Where(p => p.IdWell == idWell)
|
|
|
|
|
.OrderBy(o => o.File.UploadDate)
|
|
|
|
|
.AsNoTracking()
|
|
|
|
|
.Take(1024);
|
|
|
|
|
var entities = await propertiesQuery.ToListAsync(token);
|
|
|
|
|
var dtos = entities.Select(p => new ReportPropertiesDto
|
2022-01-05 17:50:45 +05:00
|
|
|
|
{
|
2023-12-05 14:48:56 +05:00
|
|
|
|
Id = p.Id,
|
|
|
|
|
Name = p.File.Name,
|
|
|
|
|
File = new FileInfoDto
|
2022-01-05 17:50:45 +05:00
|
|
|
|
{
|
2023-12-05 14:48:56 +05:00
|
|
|
|
Id = p.File.Id,
|
|
|
|
|
Author = null,
|
|
|
|
|
IdAuthor = p.File.IdAuthor ?? 0,
|
|
|
|
|
IdCategory = p.File.IdCategory,
|
|
|
|
|
IdWell = p.File.IdWell,
|
2022-01-05 17:50:45 +05:00
|
|
|
|
Name = p.File.Name,
|
2023-12-05 14:48:56 +05:00
|
|
|
|
Size = p.File.Size,
|
|
|
|
|
UploadDate = p.File.UploadDate.ToRemoteDateTime(timezoneOffset),
|
|
|
|
|
},
|
|
|
|
|
IdWell = p.IdWell,
|
|
|
|
|
Date = p.File.UploadDate.ToRemoteDateTime(timezoneOffset),
|
|
|
|
|
Begin = p.Begin.ToRemoteDateTime(timezoneOffset),
|
|
|
|
|
End = p.End.ToRemoteDateTime(timezoneOffset),
|
|
|
|
|
Step = p.Step,
|
|
|
|
|
Format = p.Format == 0 ? ".pdf" : ".las"
|
|
|
|
|
});
|
|
|
|
|
return dtos;
|
|
|
|
|
}
|
2021-09-23 10:52:10 +05:00
|
|
|
|
|
2023-12-25 10:53:45 +05:00
|
|
|
|
public static IReportGenerator GetReportGenerator(int idWell, DateTime begin,
|
2023-12-05 14:48:56 +05:00
|
|
|
|
DateTime end, int stepSeconds, int format, IAsbCloudDbContext context)
|
|
|
|
|
{
|
|
|
|
|
var dataSource = new ReportDataSourcePgCloud(context, idWell);
|
|
|
|
|
IReportGenerator generator = format switch
|
2021-05-18 12:33:23 +05:00
|
|
|
|
{
|
2023-12-05 14:48:56 +05:00
|
|
|
|
//LAS
|
|
|
|
|
1 => new AsbSaubReportLas.ReprotGeneratorLas(dataSource),
|
|
|
|
|
//PDF
|
|
|
|
|
_ => new AsbSaubReportPdf.ReprotGeneratorPdf(dataSource),
|
|
|
|
|
};
|
2023-09-26 17:05:01 +05:00
|
|
|
|
|
2023-12-25 10:53:45 +05:00
|
|
|
|
if (begin == default || end == default)
|
2023-12-05 14:48:56 +05:00
|
|
|
|
{
|
|
|
|
|
var analyzeResult = dataSource.Analyze();
|
|
|
|
|
begin = begin == default ? analyzeResult.MinDate : begin;
|
|
|
|
|
end = end == default ? begin.AddDays(1) : end;
|
2021-05-18 12:33:23 +05:00
|
|
|
|
}
|
2023-11-08 17:12:53 +05:00
|
|
|
|
|
2023-12-05 14:48:56 +05:00
|
|
|
|
generator.Begin = begin;
|
|
|
|
|
generator.End = end;
|
|
|
|
|
generator.Step = TimeSpan.FromSeconds(stepSeconds);
|
|
|
|
|
generator.WithCharts = true;
|
|
|
|
|
generator.WithEvents = true;
|
2023-11-08 17:12:53 +05:00
|
|
|
|
|
2023-12-05 14:48:56 +05:00
|
|
|
|
return generator;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<int> DeleteAllOldReportsAsync(TimeSpan lifetime, CancellationToken token)
|
|
|
|
|
{
|
|
|
|
|
var lifeTimeStartDate = DateTime.UtcNow.Date - lifetime;
|
|
|
|
|
var fileIds = await db.ReportProperties
|
|
|
|
|
.Where(r => r.File.UploadDate.Date < lifeTimeStartDate)
|
|
|
|
|
.Select(r => r.IdFile)
|
|
|
|
|
.ToArrayAsync(token);
|
|
|
|
|
|
|
|
|
|
return await fileService.DeleteAsync(fileIds, token);
|
2021-05-18 12:33:23 +05:00
|
|
|
|
}
|
|
|
|
|
}
|