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

81 lines
3.0 KiB
C#

using AsbCloudApp.Services;
using AsbCloudApp.Data;
using AsbCloudDb.Model;
using System;
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 IServiceProvider serviceProvider;
private readonly IBackgroundQueue queue;
public ReportService(IAsbCloudDbContext db, IConfiguration configuration, ITelemetryService telemetryService, IServiceProvider serviceProvider, IBackgroundQueue queue)
{
this.db = db;
this.configuration = configuration;
this.telemetryService = telemetryService;
this.queue = queue;
this.serviceProvider = serviceProvider;
RootPath = "reports";
}
public string RootPath { get ; private set; }
public int CreateReport(int wellId, int stepSeconds, int format, DateTime begin, DateTime end)
{
var newReportId = queue.EnqueueTask(() =>
{
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.Make();
}
});
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 DatesRangeDto GetReportsDatesRange(int wellId)
{
var telemetry = telemetryService.GetTelemetryByWellId(wellId);
if (telemetry is null)
return null;
var result = db.GetDatesRange<Report>(telemetry.Id);
return new DatesRangeDto { From = result.From, To = result.To };
}
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);
generator.ReportDirectory = Path.Combine(RootPath, $"{wellId}");
generator.Begin = begin;
generator.End = end;
generator.Step = TimeSpan.FromSeconds(stepSeconds);
generator.WithCharts = true;
generator.WithEvents = true;
return generator;
}
}
}