DD.WellWorkover.Cloud/AsbCloudInfrastructure/Services/ReportService.cs

138 lines
5.4 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using AsbCloudApp.Services;
using AsbCloudApp.Data;
using AsbCloudDb.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using AsbSaubReport;
namespace AsbCloudInfrastructure.Services
{
public class ReportService : IReportService
{
private readonly IAsbCloudDbContext db;
private readonly IConfiguration configuration;
private readonly ITelemetryService telemetryService;
private readonly IBackgroundQueue queue;
public ReportService(IAsbCloudDbContext db, IConfiguration configuration, ITelemetryService telemetryService, IBackgroundQueue queue)
{
this.db = db;
this.configuration = configuration;
this.telemetryService = telemetryService;
this.queue = queue;
RootPath = "reports";
}
public string RootPath { get ; private set; }
public int CreateReport(int wellId, int stepSeconds, int format, DateTime begin,
DateTime end, Action<float , string , int> progressHandler)
{
var newReportId = queue.EnqueueTask((id) =>
{
var optionsBuilder = new DbContextOptionsBuilder<AsbCloudDbContext>();
optionsBuilder.UseNpgsql(configuration.GetConnectionString("DefaultConnection"));
using (var context = new AsbCloudDbContext(optionsBuilder.Options))
{
var generator = GetReportGenerator(wellId, begin, end, stepSeconds, format, context);
generator.OnProgress += (s,e) => progressHandler.Invoke(e.progress, e.operation, id);
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();
}
}
});
return newReportId;
}
public int GetReportPagesCount(int wellId, DateTime begin, DateTime end, int stepSeconds, int format)
{
var generator = GetReportGenerator(wellId, begin, end, stepSeconds, format, (AsbCloudDbContext)db);
return generator.GetPagesCount();
}
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;
}
public DatesRangeDto GetReportsDatesRange(int wellId)
{
var telemetry = telemetryService.GetTelemetryByWellId(wellId);
if (telemetry is null)
return null;
// Убран общий с другими сущностями интерфейс IIdTelemetryDate,
// т.к. у Report нет IdTelemetry. GetDatesRange() уже не работает для Report.
// Будет исправлено следом за этим коммитом
//var (From, To) = db.GetDatesRange<Report>(telemetry.Id);
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;
}
private IReportGenerator GetReportGenerator(int wellId, DateTime begin, DateTime end, int stepSeconds, int format, AsbCloudDbContext context)
{
var dataSource = new ReportDataSourcePgCloud(context, wellId);
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;
}
}
}
}