Skip to content

Commit

Permalink
Refactoring v2
Browse files Browse the repository at this point in the history
  • Loading branch information
Ryba1986 committed Oct 23, 2023
1 parent 98e2765 commit fbff712
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 72 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using Phoenix.Models.Reports.Queries;
using Phoenix.Services.Extensions;
using Phoenix.Services.Handlers.Base;
using Phoenix.Services.Helpers;
using Phoenix.Services.Mappings;
using Phoenix.Services.Reports.Base;
using Phoenix.Services.Repositories;
Expand Down Expand Up @@ -90,19 +91,21 @@ private void CreateDeviceSheets(ExcelWorksheets sheets, IReadOnlyCollection<Devi

private async Task FillDataAsync(ExcelWorksheets sheets, IReadOnlyCollection<DeviceReportDto> devices, DateOnly date, ITypeProcessor typeProcessor, CancellationToken cancellationToken)
{
IEnumerable<Task> plcTasks = devices
IEnumerable<PlcType> plcTypes = devices
.Select(x => x.PlcType)
.Distinct()
.Select(x => _plcProcessors[x].FillDataAsync(sheets, date, typeProcessor, cancellationToken));
.Distinct();

await Task.WhenAll(plcTasks);
foreach (PlcType plcType in plcTypes)
{
await _plcProcessors[plcType].FillDataAsync(_uow, sheets, date, typeProcessor, cancellationToken);
};
}

private static void CreateResultSheets(ExcelWorksheets sheets, IReadOnlyCollection<DeviceReportDto> devices, DateOnly date, ITypeProcessor typeProcessor)
{
foreach (IGrouping<string, DeviceReportDto> group in devices.GroupBy(x => x.LocationName))
{
ExcelWorksheet groupSheet = sheets.Copy(PlcProcessorBase.BaseSheet, group.Key);
ExcelWorksheet groupSheet = sheets.Copy(PlcHandlerHelper.BaseSheet, group.Key);

foreach (DeviceReportDto device in group)
{
Expand Down
30 changes: 30 additions & 0 deletions src/Phoenix.Services/Helpers/PlcHandlerHelper.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
Expand All @@ -8,7 +10,10 @@
using Phoenix.Entities.Devices;
using Phoenix.Entities.Users;
using Phoenix.Models.Base.Commands;
using Phoenix.Models.Base.Dto;
using Phoenix.Models.Base.Queries;
using Phoenix.Models.Plcs;
using Phoenix.Services.Reports.Base;
using Phoenix.Services.Repositories;
using Phoenix.Shared.Extensions;
using Phoenix.Shared.Results;
Expand All @@ -17,6 +22,10 @@ namespace Phoenix.Services.Helpers
{
internal static class PlcHandlerHelper
{
public const string BaseSheet = "Base";
public const string MeterSheet = "Meter";
public const string PlcSheet = "Plc";

public static async Task<Result> AddPlcAsync<T>(UnitOfWork uow, CreatePlcCommandBase request, T plc, DateTime serverDate, CancellationToken cancellationToken) where T : PlcBase
{
Client? client = await uow.Client
Expand Down Expand Up @@ -72,6 +81,27 @@ public static IQueryable<T> GetPlcChartQuery<T>(DbSet<T> plcs, GetPlcChartQueryB
);
}

public static async Task<IReadOnlyDictionary<int, R[]>> GetPlcDataAsync<S, R>(DbSet<S> plc, Tuple<DateTime, DateTime> range, ITypeProcessor typeProcessor, Expression<Func<IGrouping<PlcGroupBy, S>, R>> selector, CancellationToken cancellationToken) where S : PlcBase where R : PlcReportDtoBase
{
IReadOnlyCollection<R> result = await plc
.AsNoTracking()
.Include(x => x.Device)
.ThenInclude(x => x.Location)
.Where(x =>
x.Date >= range.Item1 &&
x.Date < range.Item2 &&
x.Device.IncludeReport &&
x.Device.Location.IncludeReport
)
.GroupBy(typeProcessor.GetPlcGroup<S>())
.Select(selector)
.ToArrayAsync(cancellationToken);

return result
.GroupBy(x => x.DeviceId)
.ToDictionary(x => x.Key, x => x.OrderBy(x => x.Date).ToArray());
}

public static IQueryable<T> GetPlcLastQuery<T>(DbSet<T> plcs, GetPlcLastQueryBase request) where T : PlcBase
{
return plcs
Expand Down
3 changes: 2 additions & 1 deletion src/Phoenix.Services/Reports/Base/IPlcProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@
using System.Threading;
using System.Threading.Tasks;
using OfficeOpenXml;
using Phoenix.Services.Repositories;

namespace Phoenix.Services.Reports.Base
{
internal interface IPlcProcessor
{
string TemplateSheetName { get; }

Task FillDataAsync(ExcelWorksheets sheets, DateOnly date, ITypeProcessor typeProcessor, CancellationToken cancellationToken);
Task FillDataAsync(UnitOfWork uow, ExcelWorksheets sheets, DateOnly date, ITypeProcessor typeProcessor, CancellationToken cancellationToken);
}
}
47 changes: 0 additions & 47 deletions src/Phoenix.Services/Reports/Base/PlcProcessorBase.cs

This file was deleted.

11 changes: 6 additions & 5 deletions src/Phoenix.Services/Reports/Plcs/ClimatixPlcProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Threading.Tasks;
using OfficeOpenXml;
using Phoenix.Models.Plcs.Climatixs.Dto;
using Phoenix.Services.Helpers;
using Phoenix.Services.Mappings;
using Phoenix.Services.Reports.Base;
using Phoenix.Services.Repositories;
Expand All @@ -13,20 +14,20 @@

namespace Phoenix.Services.Reports.Plcs
{
internal sealed class ClimatixPlcProcessor : PlcProcessorBase, IPlcProcessor
internal sealed class ClimatixPlcProcessor : IPlcProcessor
{
public string TemplateSheetName { get; }

public ClimatixPlcProcessor(UnitOfWork uow) : base(uow)
public ClimatixPlcProcessor()
{
TemplateSheetName = PlcSheet;
TemplateSheetName = PlcHandlerHelper.PlcSheet;
}

public async Task FillDataAsync(ExcelWorksheets sheets, DateOnly date, ITypeProcessor typeProcessor, CancellationToken cancellationToken)
public async Task FillDataAsync(UnitOfWork uow, ExcelWorksheets sheets, DateOnly date, ITypeProcessor typeProcessor, CancellationToken cancellationToken)
{
Tuple<DateTime, DateTime> range = typeProcessor.GetRange(date);

IReadOnlyDictionary<int, ClimatixReportDto[]> plcData = await GetPlcDataAsync(_uow.Climatix, range, typeProcessor, ClimatixMappings.ToClimatixReportDto, cancellationToken);
IReadOnlyDictionary<int, ClimatixReportDto[]> plcData = await PlcHandlerHelper.GetPlcDataAsync(uow.Climatix, range, typeProcessor, ClimatixMappings.ToClimatixReportDto, cancellationToken);
foreach (KeyValuePair<int, ClimatixReportDto[]> plc in plcData)
{
FillData(sheets[plc.Key.ToString()], plc.Value, typeProcessor);
Expand Down
19 changes: 10 additions & 9 deletions src/Phoenix.Services/Reports/Plcs/KamstrupPlcProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,31 @@
using Microsoft.EntityFrameworkCore;
using OfficeOpenXml;
using Phoenix.Models.Plcs.Meters.Dto;
using Phoenix.Services.Helpers;
using Phoenix.Services.Mappings;
using Phoenix.Services.Reports.Base;
using Phoenix.Services.Repositories;
using Phoenix.Shared.Extensions;

namespace Phoenix.Services.Reports.Plcs
{
internal sealed class KamstrupPlcProcessor : PlcProcessorBase, IPlcProcessor
internal sealed class KamstrupPlcProcessor : IPlcProcessor
{
public string TemplateSheetName { get; }

public KamstrupPlcProcessor(UnitOfWork uow) : base(uow)
public KamstrupPlcProcessor()
{
TemplateSheetName = MeterSheet;
TemplateSheetName = PlcHandlerHelper.MeterSheet;
}

public async Task FillDataAsync(ExcelWorksheets sheets, DateOnly date, ITypeProcessor typeProcessor, CancellationToken cancellationToken)
public async Task FillDataAsync(UnitOfWork uow, ExcelWorksheets sheets, DateOnly date, ITypeProcessor typeProcessor, CancellationToken cancellationToken)
{
Tuple<DateTime, DateTime> range = typeProcessor.GetRange(date);

IReadOnlyDictionary<int, KamstrupReportDto[]> plcData = await GetPlcDataAsync(_uow.Kamstrup, range, typeProcessor, KamstrupMappings.ToKamstrupReportDto, cancellationToken);
IReadOnlyDictionary<int, KamstrupReportDto[]> plcData = await PlcHandlerHelper.GetPlcDataAsync(uow.Kamstrup, range, typeProcessor, KamstrupMappings.ToKamstrupReportDto, cancellationToken);
foreach (KeyValuePair<int, KamstrupReportDto[]> plc in plcData)
{
KamstrupDto before = await GetBeforeDataAsync(plc.Key, range.Item1, cancellationToken);
KamstrupDto before = await GetBeforeDataAsync(uow, plc.Key, range.Item1, cancellationToken);
FillData(sheets[plc.Key.ToString()], before, plc.Value, typeProcessor);
}
}
Expand Down Expand Up @@ -86,9 +87,9 @@ private static void FillData(ExcelWorksheet sheet, KamstrupDto before, IReadOnly
sheet.Cells[sheet.Dimension.Rows, 14].Value = plcData.Max(x => x.EnergySummaryMax) - before.EnergySummary;
}

private async Task<KamstrupDto> GetBeforeDataAsync(int deviceId, DateTime date, CancellationToken cancellationToken)
private async Task<KamstrupDto> GetBeforeDataAsync(UnitOfWork uow, int deviceId, DateTime date, CancellationToken cancellationToken)
{
KamstrupDto? before = await _uow.Kamstrup
KamstrupDto? before = await uow.Kamstrup
.AsNoTracking()
.Include(x => x.Device)
.Where(x =>
Expand All @@ -104,7 +105,7 @@ private async Task<KamstrupDto> GetBeforeDataAsync(int deviceId, DateTime date,
return before;
}

return await _uow.Kamstrup
return await uow.Kamstrup
.AsNoTracking()
.Include(x => x.Device)
.Where(x =>
Expand Down
11 changes: 6 additions & 5 deletions src/Phoenix.Services/Reports/Plcs/Rvd145PlcProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Threading.Tasks;
using OfficeOpenXml;
using Phoenix.Models.Plcs.Rvds.Dto;
using Phoenix.Services.Helpers;
using Phoenix.Services.Mappings;
using Phoenix.Services.Reports.Base;
using Phoenix.Services.Repositories;
Expand All @@ -14,20 +15,20 @@

namespace Phoenix.Services.Reports.Plcs
{
internal sealed class Rvd145PlcProcessor : PlcProcessorBase, IPlcProcessor
internal sealed class Rvd145PlcProcessor : IPlcProcessor
{
public string TemplateSheetName { get; }

public Rvd145PlcProcessor(UnitOfWork uow) : base(uow)
public Rvd145PlcProcessor()
{
TemplateSheetName = PlcSheet;
TemplateSheetName = PlcHandlerHelper.PlcSheet;
}

public async Task FillDataAsync(ExcelWorksheets sheets, DateOnly date, ITypeProcessor typeProcessor, CancellationToken cancellationToken)
public async Task FillDataAsync(UnitOfWork uow, ExcelWorksheets sheets, DateOnly date, ITypeProcessor typeProcessor, CancellationToken cancellationToken)
{
Tuple<DateTime, DateTime> range = typeProcessor.GetRange(date);

IReadOnlyDictionary<int, Rvd145ReportDto[]> plcData = await GetPlcDataAsync(_uow.Rvd145, range, typeProcessor, Rvd145Mappings.ToRvd145ReportDto, cancellationToken);
IReadOnlyDictionary<int, Rvd145ReportDto[]> plcData = await PlcHandlerHelper.GetPlcDataAsync(uow.Rvd145, range, typeProcessor, Rvd145Mappings.ToRvd145ReportDto, cancellationToken);
foreach (KeyValuePair<int, Rvd145ReportDto[]> plc in plcData)
{
FillData(sheets[plc.Key.ToString()], plc.Value, typeProcessor);
Expand Down

0 comments on commit fbff712

Please sign in to comment.