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-07-23 14:55:31 +05:00
|
|
|
|
using AsbSaubReportPdf;
|
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-05-19 14:41:27 +05:00
|
|
|
|
private readonly IBackgroundQueue 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,
|
|
|
|
|
IBackgroundQueue 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-07-21 15:29:19 +05:00
|
|
|
|
DateTime end, Action<float, string, int> progressHandler, Action<string, int> reportNameHandler)
|
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-05-18 12:33:23 +05:00
|
|
|
|
|
2021-05-19 14:41:27 +05:00
|
|
|
|
using (var context = new AsbCloudDbContext(optionsBuilder.Options))
|
|
|
|
|
{
|
2021-07-27 14:43:30 +05:00
|
|
|
|
var generator = GetReportGenerator(idWell, begin, end, stepSeconds, format, context);
|
2021-07-21 15:29:19 +05:00
|
|
|
|
generator.OnProgress += (s, e) => progressHandler.Invoke(e.progress, e.operation, id);
|
2021-05-31 12:33:17 +05:00
|
|
|
|
var newReportName = generator.Make();
|
2021-07-21 15:29:19 +05:00
|
|
|
|
if (newReportName is not null)
|
2021-05-31 12:33:17 +05:00
|
|
|
|
{
|
2021-07-02 12:29:13 +05:00
|
|
|
|
var shorReportName = Path.GetFileName(newReportName);
|
2021-06-07 16:31:14 +05:00
|
|
|
|
reportNameHandler.Invoke(shorReportName, id);
|
|
|
|
|
|
2021-08-13 17:26:19 +05:00
|
|
|
|
var newReportFile = new AsbCloudDb.Model.FileInfo
|
2021-05-31 12:33:17 +05:00
|
|
|
|
{
|
2021-07-27 14:43:30 +05:00
|
|
|
|
IdWell = idWell,
|
2021-07-27 16:55:32 +05:00
|
|
|
|
IdAuthor = idUser,
|
|
|
|
|
IdCategory = ReportCategoryId,
|
|
|
|
|
Name = newReportName,
|
2021-08-13 17:26:19 +05:00
|
|
|
|
UploadDate = DateTime.Now,
|
2021-07-27 16:55:32 +05:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
context.Files.Add(newReportFile);
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
|
2021-08-16 10:38:48 +05:00
|
|
|
|
var newReportProperties = new ReportProperty
|
2021-07-27 16:55:32 +05:00
|
|
|
|
{
|
|
|
|
|
IdWell = idWell,
|
|
|
|
|
IdFile = newReportFile.Id,
|
2021-05-31 12:33:17 +05:00
|
|
|
|
Begin = begin,
|
|
|
|
|
End = end,
|
|
|
|
|
Step = stepSeconds,
|
|
|
|
|
Format = format
|
|
|
|
|
};
|
2021-07-27 16:55:32 +05:00
|
|
|
|
context.ReportProperties.Add(newReportProperties);
|
2021-06-07 16:31:14 +05:00
|
|
|
|
context.SaveChanges();
|
2021-05-31 12:33:17 +05:00
|
|
|
|
}
|
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-18 12:33:23 +05:00
|
|
|
|
|
2021-05-19 14:41:27 +05:00
|
|
|
|
return generator.GetPagesCount();
|
2021-05-18 12:33:23 +05:00
|
|
|
|
}
|
|
|
|
|
|
2021-08-24 10:59:10 +05:00
|
|
|
|
public async Task<IEnumerable<ReportPropertiesDto>> GetSuitableReportsAsync(int idWell,
|
2021-08-11 16:54:42 +05:00
|
|
|
|
DateTime begin, DateTime end, int stepSeconds, int format, CancellationToken token = default)
|
2021-05-31 12:33:17 +05:00
|
|
|
|
{
|
2021-08-24 10:59:10 +05:00
|
|
|
|
var suitableReportsFromDb = await GetSuitableReportsFromDbAsync(idWell,
|
2021-08-11 17:26:02 +05:00
|
|
|
|
begin, end, stepSeconds, format, token).ConfigureAwait(false);
|
2021-05-31 12:33:17 +05:00
|
|
|
|
|
2021-07-21 15:29:19 +05:00
|
|
|
|
var suitableReportsProperties = suitableReportsFromDb.Select(r => new ReportPropertiesDto
|
2021-06-07 16:31:14 +05:00
|
|
|
|
{
|
|
|
|
|
Id = r.Id,
|
2021-07-28 09:47:13 +05:00
|
|
|
|
Name = Path.GetFileName(r.File.Name),
|
|
|
|
|
FullName = r.File.Name,
|
2021-07-28 09:46:58 +05:00
|
|
|
|
IdWell = r.IdWell,
|
2021-08-13 17:26:19 +05:00
|
|
|
|
Date = r.File.UploadDate,
|
2021-06-07 16:31:14 +05:00
|
|
|
|
Begin = r.Begin,
|
|
|
|
|
End = r.End,
|
|
|
|
|
Step = r.Step,
|
|
|
|
|
Format = r.Format == 0 ? ".pdf" : ".las"
|
|
|
|
|
});
|
2021-05-31 12:33:17 +05:00
|
|
|
|
|
2021-06-07 16:31:14 +05:00
|
|
|
|
return suitableReportsProperties;
|
2021-05-31 12:33:17 +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-08-11 16:54:42 +05:00
|
|
|
|
var datesRange = await (from d in db.DataSaubBases
|
|
|
|
|
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-08-24 10:59:10 +05:00
|
|
|
|
private async Task<IEnumerable<ReportProperty>> GetSuitableReportsFromDbAsync(int idWell,
|
2021-08-11 16:54:42 +05:00
|
|
|
|
DateTime begin, DateTime end, int stepSeconds, int format,
|
|
|
|
|
CancellationToken token = default)
|
2021-05-31 12:33:17 +05:00
|
|
|
|
{
|
2021-08-24 10:59:10 +05:00
|
|
|
|
var suitableReportsNames = await (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 r).OrderBy(o => o.File.UploadDate)
|
2021-08-11 16:54:42 +05:00
|
|
|
|
.AsNoTracking()
|
2021-08-11 17:26:02 +05:00
|
|
|
|
.Take(512).ToListAsync(token)
|
|
|
|
|
.ConfigureAwait(false);
|
2021-06-07 16:31:14 +05:00
|
|
|
|
|
2021-05-31 12:33:17 +05:00
|
|
|
|
return suitableReportsNames;
|
2021-05-18 12:33:23 +05:00
|
|
|
|
}
|
|
|
|
|
|
2021-08-24 10:59:10 +05:00
|
|
|
|
private 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-05-19 15:50:09 +05:00
|
|
|
|
|
2021-07-23 14:55:31 +05:00
|
|
|
|
IReportGenerator generator;
|
2021-07-21 15:29:19 +05:00
|
|
|
|
switch (format)
|
2021-05-19 15:50:09 +05:00
|
|
|
|
{
|
2021-07-23 14:55:31 +05:00
|
|
|
|
case 1: //LAS
|
|
|
|
|
generator = new AsbSaubReportLas.LasReprotGenerator(dataSource);
|
|
|
|
|
break;
|
|
|
|
|
case 0: //PDF
|
2021-05-19 15:50:09 +05:00
|
|
|
|
default:
|
2021-07-23 14:55:31 +05:00
|
|
|
|
generator = new PdfReprotGenerator(dataSource);
|
|
|
|
|
break;
|
2021-05-19 15:50:09 +05:00
|
|
|
|
}
|
2021-07-23 14:55:31 +05:00
|
|
|
|
|
2021-07-27 16:55:32 +05:00
|
|
|
|
generator.ReportDirectory = Path.Combine(fileService.RootPath, $"{idWell}", $"{ReportCategoryId}");
|
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
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|