2021-07-21 15:29:19 +05:00
|
|
|
|
using AsbCloudApp.Data;
|
|
|
|
|
using AsbCloudApp.Services;
|
2021-05-18 12:33:23 +05:00
|
|
|
|
using AsbCloudDb.Model;
|
2021-07-21 15:29:19 +05:00
|
|
|
|
using AsbSaubReport;
|
2021-09-23 10:52:10 +05:00
|
|
|
|
using Mapster;
|
2021-07-21 15:29:19 +05:00
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
|
using Microsoft.Extensions.Configuration;
|
2021-05-18 12:33:23 +05:00
|
|
|
|
using System;
|
2021-05-31 12:33:17 +05:00
|
|
|
|
using System.Collections.Generic;
|
2021-05-18 12:33:23 +05:00
|
|
|
|
using System.IO;
|
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
|
|
|
|
|
|
|
|
|
namespace AsbCloudInfrastructure.Services
|
|
|
|
|
{
|
|
|
|
|
public class ReportService : IReportService
|
|
|
|
|
{
|
|
|
|
|
private readonly IAsbCloudDbContext db;
|
2021-05-19 14:41:27 +05:00
|
|
|
|
private readonly IConfiguration configuration;
|
2021-05-18 12:33:23 +05:00
|
|
|
|
private readonly ITelemetryService telemetryService;
|
2021-07-27 16:55:32 +05:00
|
|
|
|
private readonly IFileService fileService;
|
2021-09-27 11:47:39 +05:00
|
|
|
|
private readonly IReportsBackgroundQueue queue;
|
2021-05-18 12:33:23 +05:00
|
|
|
|
|
2021-07-28 09:47:13 +05:00
|
|
|
|
public ReportService(IAsbCloudDbContext db, IConfiguration configuration,
|
2021-07-27 16:55:32 +05:00
|
|
|
|
ITelemetryService telemetryService, IFileService fileService,
|
2021-09-27 11:47:39 +05:00
|
|
|
|
IReportsBackgroundQueue queue)
|
2021-05-18 12:33:23 +05:00
|
|
|
|
{
|
|
|
|
|
this.db = db;
|
2021-05-19 14:41:27 +05:00
|
|
|
|
this.configuration = configuration;
|
2021-05-18 12:33:23 +05:00
|
|
|
|
this.telemetryService = telemetryService;
|
2021-07-27 16:55:32 +05:00
|
|
|
|
this.fileService = fileService;
|
2021-05-19 14:41:27 +05:00
|
|
|
|
this.queue = queue;
|
2021-08-11 10:16:01 +05:00
|
|
|
|
ReportCategoryId = db.FileCategories.AsNoTracking()
|
|
|
|
|
.FirstOrDefault(c =>
|
2021-07-27 16:55:32 +05:00
|
|
|
|
c.Name.Equals("Рапорт")).Id;
|
2021-05-18 12:33:23 +05:00
|
|
|
|
}
|
|
|
|
|
|
2021-07-27 16:55:32 +05:00
|
|
|
|
public int ReportCategoryId { get; private set; }
|
2021-05-19 14:41:27 +05:00
|
|
|
|
|
2021-07-27 16:55:32 +05:00
|
|
|
|
public int CreateReport(int idWell, int idUser, int stepSeconds, int format, DateTime begin,
|
2021-09-23 10:52:10 +05:00
|
|
|
|
DateTime end, Action<object, int> progressHandler)
|
2021-05-18 12:33:23 +05:00
|
|
|
|
{
|
2021-05-20 12:38:25 +05:00
|
|
|
|
var newReportId = queue.EnqueueTask((id) =>
|
2021-05-19 14:41:27 +05:00
|
|
|
|
{
|
|
|
|
|
var optionsBuilder = new DbContextOptionsBuilder<AsbCloudDbContext>();
|
|
|
|
|
optionsBuilder.UseNpgsql(configuration.GetConnectionString("DefaultConnection"));
|
2021-09-23 10:52:10 +05:00
|
|
|
|
var tempDir = Path.Combine(Path.GetTempPath(), "report");
|
2021-05-18 12:33:23 +05:00
|
|
|
|
|
2021-09-23 10:52:10 +05:00
|
|
|
|
using var context = new AsbCloudDbContext(optionsBuilder.Options);
|
|
|
|
|
|
|
|
|
|
var generator = GetReportGenerator(idWell, begin, end, stepSeconds, format, context);
|
|
|
|
|
var reportFileName = Path.Combine(tempDir, generator.GetReportDefaultFileName());
|
|
|
|
|
var totalPages = generator.GetPagesCount();
|
|
|
|
|
|
|
|
|
|
generator.OnProgress += (s, e) =>
|
|
|
|
|
{
|
|
|
|
|
progressHandler.Invoke(e.Adapt<ReportProgressDto>(), id);
|
|
|
|
|
};
|
|
|
|
|
generator.Make(reportFileName);
|
|
|
|
|
|
|
|
|
|
var fileService = new FileService(context);
|
2021-09-23 11:55:25 +05:00
|
|
|
|
var fileInfo = fileService.MoveAsync(idWell, idUser, ReportCategoryId, reportFileName, reportFileName).Result;
|
|
|
|
|
|
2021-09-23 10:52:10 +05:00
|
|
|
|
progressHandler.Invoke(new
|
|
|
|
|
{
|
|
|
|
|
Operation = "done",
|
|
|
|
|
Progress = 100f,
|
|
|
|
|
TotalPages = totalPages,
|
|
|
|
|
CurrentPage = totalPages,
|
|
|
|
|
file = fileInfo,
|
|
|
|
|
}, id);
|
|
|
|
|
|
|
|
|
|
var newReportProperties = new ReportProperty
|
2021-05-19 14:41:27 +05:00
|
|
|
|
{
|
2021-09-23 10:52:10 +05:00
|
|
|
|
IdWell = idWell,
|
|
|
|
|
IdFile = fileInfo.Id,
|
|
|
|
|
Begin = begin,
|
|
|
|
|
End = end,
|
|
|
|
|
Step = stepSeconds,
|
|
|
|
|
Format = format
|
|
|
|
|
};
|
|
|
|
|
context.ReportProperties.Add(newReportProperties);
|
|
|
|
|
context.SaveChanges();
|
2021-05-19 14:41:27 +05:00
|
|
|
|
});
|
2021-09-23 10:52:10 +05:00
|
|
|
|
progressHandler.Invoke(new ReportProgressDto
|
|
|
|
|
{
|
|
|
|
|
Operation = "Ожидает начала в очереди.",
|
|
|
|
|
Progress = 0f,
|
|
|
|
|
}, newReportId);
|
2021-05-19 14:41:27 +05:00
|
|
|
|
return newReportId;
|
2021-05-18 12:33:23 +05:00
|
|
|
|
}
|
|
|
|
|
|
2021-07-27 14:43:30 +05:00
|
|
|
|
public int GetReportPagesCount(int idWell, DateTime begin, DateTime end, int stepSeconds, int format)
|
2021-05-18 12:33:23 +05:00
|
|
|
|
{
|
2021-07-27 14:43:30 +05:00
|
|
|
|
var generator = GetReportGenerator(idWell, begin, end, stepSeconds, format, (AsbCloudDbContext)db);
|
2021-05-19 14:41:27 +05:00
|
|
|
|
return generator.GetPagesCount();
|
2021-05-18 12:33:23 +05:00
|
|
|
|
}
|
|
|
|
|
|
2021-08-11 16:54:42 +05:00
|
|
|
|
public async Task<DatesRangeDto> GetReportsDatesRangeAsync(int idWell,
|
|
|
|
|
CancellationToken token = default)
|
2021-05-18 12:33:23 +05:00
|
|
|
|
{
|
2021-08-09 14:01:57 +05:00
|
|
|
|
var telemetryId = telemetryService.GetIdTelemetryByIdWell(idWell);
|
2021-05-31 16:28:50 +05:00
|
|
|
|
|
2021-08-09 14:01:57 +05:00
|
|
|
|
if (telemetryId is null)
|
2021-05-18 12:33:23 +05:00
|
|
|
|
return null;
|
|
|
|
|
|
2021-09-14 17:17:33 +05:00
|
|
|
|
var datesRange = await (from d in db.TelemetryDataSaub
|
2021-08-11 16:54:42 +05:00
|
|
|
|
where d.IdTelemetry == telemetryId
|
|
|
|
|
select d.Date).Union(
|
|
|
|
|
from m in db.TelemetryMessages
|
|
|
|
|
where m.IdTelemetry == telemetryId
|
|
|
|
|
select m.Date).DefaultIfEmpty()
|
|
|
|
|
.GroupBy(g => true)
|
|
|
|
|
.AsNoTracking()
|
|
|
|
|
.Select(g => new
|
|
|
|
|
{
|
|
|
|
|
From = g.Min(),
|
|
|
|
|
To = g.Max()
|
|
|
|
|
}).OrderBy(gr => gr.From)
|
2021-08-11 17:26:02 +05:00
|
|
|
|
.FirstOrDefaultAsync(token)
|
|
|
|
|
.ConfigureAwait(false);
|
2021-07-01 17:54:29 +05:00
|
|
|
|
|
2021-07-21 15:29:19 +05:00
|
|
|
|
return new DatesRangeDto
|
|
|
|
|
{
|
|
|
|
|
From = datesRange.From,
|
2021-07-01 17:54:29 +05:00
|
|
|
|
To = datesRange.To.Year == 1 ? DateTime.MaxValue : datesRange.To
|
|
|
|
|
};
|
2021-05-31 12:33:17 +05:00
|
|
|
|
}
|
|
|
|
|
|
2021-05-18 12:33:23 +05:00
|
|
|
|
|
2021-09-23 10:52:10 +05:00
|
|
|
|
[Obsolete]
|
|
|
|
|
Task<List<ReportPropertiesDto>> IReportService.GetSuitableReportsAsync(int idWell, DateTime begin, DateTime end, int stepSeconds, int format, CancellationToken token) =>
|
|
|
|
|
(from r in db.ReportProperties.Include(r => r.File)
|
|
|
|
|
where r.IdWell == idWell
|
|
|
|
|
&& r.Begin >= begin
|
|
|
|
|
&& r.End <= end
|
|
|
|
|
&& r.Step <= stepSeconds
|
|
|
|
|
&& r.Format == format
|
|
|
|
|
select new ReportPropertiesDto
|
|
|
|
|
{
|
|
|
|
|
Id = r.Id,
|
|
|
|
|
Name = r.File.Name,
|
|
|
|
|
Url = fileService.GetUrl(r.IdFile),
|
|
|
|
|
IdWell = r.IdWell,
|
|
|
|
|
Date = r.File.UploadDate,
|
|
|
|
|
Begin = r.Begin,
|
|
|
|
|
End = r.End,
|
|
|
|
|
Step = r.Step,
|
|
|
|
|
Format = r.Format == 0 ? ".pdf" : ".las"
|
|
|
|
|
})
|
|
|
|
|
.OrderBy(o => o.Date)
|
|
|
|
|
.AsNoTracking()
|
|
|
|
|
.Take(512).ToListAsync(token);
|
|
|
|
|
|
|
|
|
|
public Task<List<ReportPropertiesDto>> GetAllReportsByWellAsync(int idWell, CancellationToken token) =>
|
|
|
|
|
(from r in db.ReportProperties.Include(r => r.File)
|
|
|
|
|
where r.IdWell == idWell
|
|
|
|
|
select new ReportPropertiesDto
|
|
|
|
|
{
|
|
|
|
|
Id = r.Id,
|
|
|
|
|
Name = r.File.Name,
|
|
|
|
|
#pragma warning disable IDE0057 // Use range operator
|
|
|
|
|
Url = fileService.GetUrl(r.IdWell, ReportCategoryId, r.File.Id, r.File.Name.Substring(r.File.Name.LastIndexOf(".") > 0 ? r.File.Name.LastIndexOf(".") : r.File.Name.Length)),
|
|
|
|
|
#pragma warning restore IDE0057 // Use range operator
|
|
|
|
|
IdWell = r.IdWell,
|
|
|
|
|
Date = r.File.UploadDate,
|
|
|
|
|
Begin = r.Begin,
|
|
|
|
|
End = r.End,
|
|
|
|
|
Step = r.Step,
|
|
|
|
|
Format = r.Format == 0 ? ".pdf" : ".las"
|
|
|
|
|
})
|
|
|
|
|
.OrderBy(o => o.Date)
|
|
|
|
|
.AsNoTracking()
|
|
|
|
|
.Take(1024).ToListAsync(token);
|
|
|
|
|
|
|
|
|
|
private static IReportGenerator GetReportGenerator(int idWell, DateTime begin,
|
2021-08-11 16:54:42 +05:00
|
|
|
|
DateTime end, int stepSeconds, int format, AsbCloudDbContext context)
|
2021-05-18 12:33:23 +05:00
|
|
|
|
{
|
2021-07-27 14:43:30 +05:00
|
|
|
|
var dataSource = new ReportDataSourcePgCloud(context, idWell);
|
2021-09-23 11:56:12 +05:00
|
|
|
|
IReportGenerator generator = format switch
|
2021-05-19 15:50:09 +05:00
|
|
|
|
{
|
2021-09-23 11:56:12 +05:00
|
|
|
|
//LAS
|
|
|
|
|
1 => new AsbSaubReportLas.ReprotGeneratorLas(dataSource),
|
|
|
|
|
//PDF
|
|
|
|
|
_ => new AsbSaubReportPdf.ReprotGeneratorPdf(dataSource),
|
|
|
|
|
};
|
2021-07-23 14:55:31 +05:00
|
|
|
|
generator.Begin = begin;
|
|
|
|
|
generator.End = end;
|
|
|
|
|
generator.Step = TimeSpan.FromSeconds(stepSeconds);
|
|
|
|
|
generator.WithCharts = true;
|
|
|
|
|
generator.WithEvents = true;
|
2021-07-28 09:47:13 +05:00
|
|
|
|
|
2021-07-23 14:55:31 +05:00
|
|
|
|
return generator;
|
2021-05-18 12:33:23 +05:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|