2021-05-18 12:33:23 +05:00
|
|
|
|
using AsbCloudApp.Services;
|
|
|
|
|
using AsbCloudApp.Data;
|
|
|
|
|
using AsbCloudDb.Model;
|
|
|
|
|
using System;
|
2021-05-31 12:33:17 +05:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
2021-05-18 12:33:23 +05:00
|
|
|
|
using System.IO;
|
2021-05-19 14:41:27 +05:00
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
|
using Microsoft.Extensions.Configuration;
|
2021-05-18 12:33:23 +05:00
|
|
|
|
using AsbSaubReport;
|
|
|
|
|
|
|
|
|
|
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-05-19 14:41:27 +05:00
|
|
|
|
private readonly IBackgroundQueue queue;
|
2021-05-18 12:33:23 +05:00
|
|
|
|
|
2021-05-19 15:21:19 +05:00
|
|
|
|
public ReportService(IAsbCloudDbContext db, IConfiguration configuration, ITelemetryService telemetryService, 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-05-19 14:41:27 +05:00
|
|
|
|
this.queue = queue;
|
|
|
|
|
RootPath = "reports";
|
2021-05-18 12:33:23 +05:00
|
|
|
|
}
|
|
|
|
|
|
2021-05-19 14:41:27 +05:00
|
|
|
|
public string RootPath { get ; private set; }
|
|
|
|
|
|
2021-05-20 11:07:45 +05:00
|
|
|
|
public int CreateReport(int wellId, int stepSeconds, int format, DateTime begin,
|
2021-05-20 12:38:25 +05:00
|
|
|
|
DateTime end, Action<float , string , 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-05-18 12:33:23 +05:00
|
|
|
|
|
2021-05-19 14:41:27 +05:00
|
|
|
|
using (var context = new AsbCloudDbContext(optionsBuilder.Options))
|
|
|
|
|
{
|
|
|
|
|
var generator = GetReportGenerator(wellId, begin, end, stepSeconds, format, context);
|
2021-05-24 16:31:02 +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();
|
|
|
|
|
if(newReportName is not null)
|
|
|
|
|
{
|
|
|
|
|
var newReportProperties = new Report
|
|
|
|
|
{
|
|
|
|
|
Name = newReportName,
|
|
|
|
|
WellId = wellId,
|
|
|
|
|
Date = DateTime.Now,
|
|
|
|
|
Begin = begin,
|
|
|
|
|
End = end,
|
|
|
|
|
Step = stepSeconds,
|
|
|
|
|
Format = format
|
|
|
|
|
};
|
|
|
|
|
context.Reports.Add(newReportProperties);
|
|
|
|
|
db.SaveChanges();
|
|
|
|
|
}
|
2021-05-19 14:41:27 +05:00
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
return newReportId;
|
2021-05-18 12:33:23 +05:00
|
|
|
|
}
|
|
|
|
|
|
2021-05-19 14:41:27 +05:00
|
|
|
|
public int GetReportPagesCount(int wellId, DateTime begin, DateTime end, int stepSeconds, int format)
|
2021-05-18 12:33:23 +05:00
|
|
|
|
{
|
2021-05-19 14:41:27 +05:00
|
|
|
|
var generator = GetReportGenerator(wellId, 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-05-31 12:33:17 +05:00
|
|
|
|
public IEnumerable<string> GetExistingReportNames(int wellId, DateTime begin, DateTime end, int stepSeconds, int format)
|
|
|
|
|
{
|
|
|
|
|
var suitableReportsFromDb = GetSuitableReportNamesFromDb(wellId, begin, end, stepSeconds, format);
|
|
|
|
|
|
|
|
|
|
var reportsFolder = Path.Combine(RootPath, $"{wellId}");
|
|
|
|
|
|
|
|
|
|
var suitableReportNames = Directory.GetFiles(reportsFolder)
|
|
|
|
|
.Select(Path.GetFileName)
|
|
|
|
|
.Where(name => suitableReportsFromDb.Contains(name));
|
|
|
|
|
|
|
|
|
|
return suitableReportNames;
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-18 12:33:23 +05:00
|
|
|
|
public DatesRangeDto GetReportsDatesRange(int wellId)
|
|
|
|
|
{
|
|
|
|
|
var telemetry = telemetryService.GetTelemetryByWellId(wellId);
|
|
|
|
|
|
|
|
|
|
if (telemetry is null)
|
|
|
|
|
return null;
|
|
|
|
|
|
2021-05-31 12:33:17 +05:00
|
|
|
|
// Убран общий с другими сущностями интерфейс IIdTelemetryDate,
|
|
|
|
|
// т.к. у Report нет IdTelemetry. GetDatesRange() уже не работает для Report.
|
|
|
|
|
// Будет исправлено следом за этим коммитом
|
|
|
|
|
//var (From, To) = db.GetDatesRange<Report>(telemetry.Id);
|
2021-05-18 12:33:23 +05:00
|
|
|
|
|
2021-05-31 12:33:17 +05:00
|
|
|
|
return new DatesRangeDto {
|
|
|
|
|
From = DateTime.MinValue,
|
|
|
|
|
To = DateTime.MaxValue
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private IEnumerable<string> GetSuitableReportNamesFromDb(int wellId, DateTime begin, DateTime end, int stepSeconds, int format)
|
|
|
|
|
{
|
|
|
|
|
var suitableReportsNames = (from r in db.Reports
|
|
|
|
|
where r.WellId == wellId && r.Begin == begin
|
|
|
|
|
&& r.End == end && r.Step == stepSeconds
|
|
|
|
|
&& r.Format == format
|
|
|
|
|
select r.Name).ToList();
|
|
|
|
|
return suitableReportsNames;
|
2021-05-18 12:33:23 +05:00
|
|
|
|
}
|
|
|
|
|
|
2021-05-19 14:41:27 +05:00
|
|
|
|
private IReportGenerator GetReportGenerator(int wellId, DateTime begin, DateTime end, int stepSeconds, int format, AsbCloudDbContext context)
|
2021-05-18 12:33:23 +05:00
|
|
|
|
{
|
2021-05-19 14:41:27 +05:00
|
|
|
|
var dataSource = new ReportDataSourcePgCloud(context, wellId);
|
2021-05-19 15:50:09 +05:00
|
|
|
|
var generator = new PdfGenerator(dataSource)
|
|
|
|
|
{
|
|
|
|
|
ReportDirectory = Path.Combine(RootPath, $"{wellId}"),
|
|
|
|
|
Begin = begin,
|
|
|
|
|
End = end,
|
|
|
|
|
Step = TimeSpan.FromSeconds(stepSeconds),
|
|
|
|
|
WithCharts = true,
|
|
|
|
|
WithEvents = true
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
switch(format)
|
|
|
|
|
{
|
|
|
|
|
case 0:
|
|
|
|
|
return generator;
|
|
|
|
|
case 1:
|
|
|
|
|
return generator;
|
|
|
|
|
default:
|
|
|
|
|
return generator;
|
|
|
|
|
}
|
2021-05-18 12:33:23 +05:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|