Refactor Stat* models

This commit is contained in:
Фролов 2021-08-25 11:13:56 +05:00
parent 94b7dd3ef7
commit e55f505ea7
11 changed files with 116 additions and 68 deletions

View File

@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AsbCloudApp.Data
{
public class PlanFactBase<T>
{
public T Plan { get; set; }
public T Fact { get; set; }
}
}

View File

@ -2,9 +2,10 @@
namespace AsbCloudApp.Data namespace AsbCloudApp.Data
{ {
public class StatClusterDto : ClusterDto public class StatClusterDto //: ClusterDto
{ {
public List<StatOperationDto> Sections { get; set; } public int Id { get; set; }
public List<StatOperationDto> WellsTotals { get; set; } public string Caption { get; set; }
public IEnumerable<StatWellDto> StatsWells { get; set; }
} }
} }

View File

@ -1,10 +0,0 @@
namespace AsbCloudApp.Data
{
public class StatOperationDto: IId
{
public int Id { get; set; }
public string Caption { get; set; }
public StatOperationBase Plan { get; set; }
public StatOperationBase Fact { get; set; }
}
}

View File

@ -2,7 +2,7 @@
namespace AsbCloudApp.Data namespace AsbCloudApp.Data
{ {
public class StatOperationBase public class StatOperationsDto
{ {
/// <summary> /// <summary>
/// Дата и время начала /// Дата и время начала

View File

@ -0,0 +1,8 @@
namespace AsbCloudApp.Data
{
public class StatSectionDto : PlanFactBase<StatOperationsDto>
{
public int Id { get; set; }
public string Caption { get; set; }
}
}

View File

@ -2,10 +2,12 @@
namespace AsbCloudApp.Data namespace AsbCloudApp.Data
{ {
public class StatWellDto : WellDto public class StatWellDto //: WellDto
{ {
public IEnumerable<StatOperationDto> Sections { get; set; } public int Id { get; set; }
public StatOperationDto Total { get; set; } public string Caption { get; set; }
public IEnumerable<StatSectionDto> Sections { get; set; }
public PlanFactBase<StatOperationsDto> Total { get; set; }
public IEnumerable<CompanyDto> Companies { get; set; } public IEnumerable<CompanyDto> Companies { get; set; }
} }
} }

View File

@ -11,5 +11,7 @@ namespace AsbCloudApp.Services
Task<IEnumerable<WellDto>> GetTransmittingWellsAsync(int idCompany, CancellationToken token); Task<IEnumerable<WellDto>> GetTransmittingWellsAsync(int idCompany, CancellationToken token);
Task<bool> IsCompanyInvolvedInWellAsync(int idCompany, int idWell, CancellationToken token); Task<bool> IsCompanyInvolvedInWellAsync(int idCompany, int idWell, CancellationToken token);
Task<IEnumerable<WellOperationDto>> GetOperationsAsync(int idWell, CancellationToken token); Task<IEnumerable<WellOperationDto>> GetOperationsAsync(int idWell, CancellationToken token);
Task<string> GetWellCaptionByIdAsync(int idWell, CancellationToken token);
Task<IEnumerable<CompanyDto>> GetCompaniesAsync(int idWell, CancellationToken token);
} }
} }

View File

@ -1,6 +1,7 @@
using AsbCloudApp.Data; using AsbCloudApp.Data;
using AsbCloudApp.Services; using AsbCloudApp.Services;
using AsbCloudDb.Model; using AsbCloudDb.Model;
using Mapster;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
@ -74,13 +75,7 @@ namespace AsbCloudInfrastructure.Services
.ToListAsync(token) .ToListAsync(token)
.ConfigureAwait(false); .ConfigureAwait(false);
var dtos = entities.Select(e => new ClusterDto var dtos = entities.Adapt<ClusterDto>();
{
Id = e.Id,
Caption = e.Caption,
Latitude = e.Latitude,
Longitude = e.Longitude,
});
return dtos; return dtos;
} }
@ -96,13 +91,7 @@ namespace AsbCloudInfrastructure.Services
.ToListAsync(token) .ToListAsync(token)
.ConfigureAwait(false); .ConfigureAwait(false);
var dtos = entities.Select(e => new ClusterDto var dtos = entities.Adapt<ClusterDto>();
{
Id = e.Id,
Caption = e.Caption,
Latitude = e.Latitude,
Longitude = e.Longitude,
});
return dtos; return dtos;
} }

View File

@ -56,7 +56,10 @@ namespace AsbCloudInfrastructure.Services
public class WellOperationsStatService : IWellOperationsStatService public class WellOperationsStatService : IWellOperationsStatService
{ {
private readonly IAsbCloudDbContext db; private readonly IAsbCloudDbContext db;
private readonly IWellService wellService;
private readonly CacheTable<WellSectionType> cachedSectionsTypes; private readonly CacheTable<WellSectionType> cachedSectionsTypes;
private readonly CacheTable<Well> cachedWell;
private readonly CacheTable<Cluster> cacheCluster;
private const int idOperationBhaAssembly = 1025; private const int idOperationBhaAssembly = 1025;
private const int idOperationBhaDisassembly = 1026; private const int idOperationBhaDisassembly = 1026;
private const int idOperationNonProductiveTime = 1043; private const int idOperationNonProductiveTime = 1043;
@ -65,36 +68,40 @@ namespace AsbCloudInfrastructure.Services
private const int idOperationBhaUp = 1047; private const int idOperationBhaUp = 1047;
private const int IdOperationCasingDown = 1048; private const int IdOperationCasingDown = 1048;
public WellOperationsStatService(IAsbCloudDbContext db, Cache.CacheDb cache) public WellOperationsStatService(IAsbCloudDbContext db, CacheDb cache, IWellService wellService)
{ {
this.db = db; this.db = db;
this.wellService = wellService;
cachedSectionsTypes = cache.GetCachedTable<WellSectionType>((DbContext)db); cachedSectionsTypes = cache.GetCachedTable<WellSectionType>((DbContext)db);
cachedWell = cache.GetCachedTable<Well>((DbContext)db);
cacheCluster = cache.GetCachedTable<Cluster>((DbContext)db);
} }
public async Task<StatClusterDto> GetOperationStatByClusterAsync(int idCluster, CancellationToken token = default) public async Task<StatClusterDto> GetOperationStatByClusterAsync(int idCluster, CancellationToken token = default)
{ {
var operations = await db.WellOperations var operations = await db.WellOperations
.Include(o => o.Well)
.Where(o => o.Well.IdCluster == idCluster) .Where(o => o.Well.IdCluster == idCluster)
.OrderBy(o => o.StartDate) .OrderBy(o => o.StartDate)
.AsNoTracking() .AsNoTracking()
.ToListAsync(token); .ToListAsync(token);
var wellsIds = operations.Select(o => o.IdWell).Distinct(); var cluster = await cacheCluster.FirstOrDefaultAsync(c => c.Id == idCluster, token);
var sectionsStats = new List<StatOperationDto>(wellsIds.Count() * 3);
var wellsTotals = new List<StatOperationDto>(wellsIds.Count());
foreach(var idWell in wellsIds) var wellsIds = operations.Select(o => o.IdWell).Distinct();
var statsWells = new List<StatWellDto>(wellsIds.Count());
foreach (var idWell in wellsIds)
{ {
var wellOperations = operations.Where(o => o.IdWell == idWell); var statWellDto = await CalcStatWell(operations, idWell, token);
sectionsStats.AddRange(GetWellSectionsStats(wellOperations)); statsWells.Add(statWellDto);
wellsTotals.Add(GetWellStats(wellOperations));
} }
var statClusterDto = new StatClusterDto var statClusterDto = new StatClusterDto
{ {
Sections = sectionsStats, Id = idCluster,
WellsTotals = wellsTotals, Caption = cluster.Caption,
StatsWells = statsWells,
}; };
return statClusterDto; return statClusterDto;
} }
@ -108,36 +115,62 @@ namespace AsbCloudInfrastructure.Services
.AsNoTracking() .AsNoTracking()
.ToListAsync(token); .ToListAsync(token);
var statWellDto = await CalcStatWell(operations, idWell, token);
return statWellDto;
}
private async Task<StatWellDto> CalcStatWell(IEnumerable<WellOperation> operations, int idWell,
CancellationToken token = default)
{
var wellOperations = operations
.Where(o => o.IdWell == idWell);
var well = await cachedWell.FirstOrDefaultAsync(w => w.Id == idWell, token);
var statWellDto = new StatWellDto var statWellDto = new StatWellDto
{ {
Sections = GetWellSectionsStats(operations), Id = idWell,
Total = GetWellStats(operations), Caption = well.Caption,
Companies = await wellService.GetCompaniesAsync(idWell, token),
Sections = CalcSectionsStats(operations),
Total = GetStat(operations),
}; };
return statWellDto; return statWellDto;
} }
private IEnumerable<StatOperationDto> GetWellSectionsStats(IEnumerable<WellOperation> operations) private IEnumerable<StatSectionDto> CalcSectionsStats(IEnumerable<WellOperation> operations)
{ {
var sectionTypesIds = operations.Select(o => o.IdWellSectionType).Distinct(); var sectionTypeIds = operations
var sections = new List<StatOperationDto>(sectionTypesIds.Count()); .Select(o => o.IdWellSectionType)
.Distinct();
var sectionTypes = cachedSectionsTypes
.Where(s => sectionTypeIds.Contains(s.Id))
.ToDictionary(s => s.Id);
var sections = new List<StatSectionDto>(sectionTypes.Count);
var operationsPlan = MakeOperationsExt(operations.Where(o => o.IdType == 0)); var operationsPlan = MakeOperationsExt(operations.Where(o => o.IdType == 0));
var operationsFact = MakeOperationsExt(operations.Where(o => o.IdType == 0)); var operationsFact = MakeOperationsExt(operations.Where(o => o.IdType == 0));
foreach (var idSectionType in sectionTypesIds) foreach ((var id, var sectionType) in sectionTypes)
{ {
var statPlan = CalcSectionStat(operationsPlan, idSectionType); StatSectionDto section = new StatSectionDto
var statFact = CalcSectionStat(operationsFact, idSectionType); {
StatOperationDto section = MakeWellSectionDto(idSectionType, statPlan, statFact); Id = id,
Caption = sectionType.Caption,
Plan = CalcSectionStat(operationsPlan, id),
Fact = CalcSectionStat(operationsFact, id),
};
sections.Add(section); sections.Add(section);
} }
return sections; return sections;
} }
private static StatOperationDto GetWellStats(IEnumerable<WellOperation> operations) private static PlanFactBase<StatOperationsDto> GetStat(IEnumerable<WellOperation> operations)
{ {
var operationsPlan = MakeOperationsExt(operations.Where(o => o.IdType == 0)); var operationsPlan = MakeOperationsExt(operations.Where(o => o.IdType == 0));
var operationsFact = MakeOperationsExt(operations.Where(o => o.IdType == 0)); var operationsFact = MakeOperationsExt(operations.Where(o => o.IdType == 0));
var section = new StatOperationDto var section = new PlanFactBase<StatOperationsDto>
{ {
Plan = CalcStat(operationsPlan), Plan = CalcStat(operationsPlan),
Fact = CalcStat(operationsFact), Fact = CalcStat(operationsFact),
@ -145,18 +178,7 @@ namespace AsbCloudInfrastructure.Services
return section; return section;
} }
private StatOperationDto MakeWellSectionDto(int idSectionType, StatOperationBase statPlan, StatOperationBase statFact) private static StatOperationsDto CalcSectionStat(IEnumerable<OperationParams> operations, int idSectionType)
{
return new StatOperationDto
{
Id = idSectionType,
Caption = cachedSectionsTypes.FirstOrDefault(s => s.Id == idSectionType)?.Caption,
Plan = statPlan,
Fact = statFact,
};
}
private static StatOperationBase CalcSectionStat(IEnumerable<OperationParams> operations, int idSectionType)
{ {
var sectionOperations = operations var sectionOperations = operations
.Where(o => o.IdWellSectionType == idSectionType) .Where(o => o.IdWellSectionType == idSectionType)
@ -165,9 +187,9 @@ namespace AsbCloudInfrastructure.Services
return CalcStat(sectionOperations); return CalcStat(sectionOperations);
} }
private static StatOperationBase CalcStat(IEnumerable<OperationParams> operations) private static StatOperationsDto CalcStat(IEnumerable<OperationParams> operations)
{ {
var section = new StatOperationBase var section = new StatOperationsDto
{ {
Start = operations.First().Start, Start = operations.First().Start,
End = operations.Max(o => (o.Start.AddHours(o.DurationHours))), End = operations.Max(o => (o.Start.AddHours(o.DurationHours))),

View File

@ -17,12 +17,14 @@ namespace AsbCloudInfrastructure.Services
private readonly IAsbCloudDbContext db; private readonly IAsbCloudDbContext db;
private readonly ITelemetryTracker telemetryTracker; private readonly ITelemetryTracker telemetryTracker;
private readonly CacheTable<RelationCompanyWell> cacheRelationCompaniesWells; private readonly CacheTable<RelationCompanyWell> cacheRelationCompaniesWells;
private readonly CacheTable<Well> cacheWells;
public WellService(IAsbCloudDbContext db, ITelemetryTracker telemetryTracker, CacheDb cacheDb) public WellService(IAsbCloudDbContext db, ITelemetryTracker telemetryTracker, CacheDb cacheDb)
{ {
this.db = db; this.db = db;
this.telemetryTracker = telemetryTracker; this.telemetryTracker = telemetryTracker;
cacheRelationCompaniesWells = cacheDb.GetCachedTable<RelationCompanyWell>((AsbCloudDbContext)db); cacheRelationCompaniesWells = cacheDb.GetCachedTable<RelationCompanyWell>((AsbCloudDbContext)db);
cacheWells = cacheDb.GetCachedTable<Well>((AsbCloudDbContext)db);
} }
public async Task<IEnumerable<WellDto>> GetTransmittingWellsAsync(int idCompany, CancellationToken token) public async Task<IEnumerable<WellDto>> GetTransmittingWellsAsync(int idCompany, CancellationToken token)
@ -74,5 +76,23 @@ namespace AsbCloudInfrastructure.Services
return wellDto; return wellDto;
} }
public async Task<string> GetWellCaptionByIdAsync(int idWell, CancellationToken token)
{
var entity = await cacheWells.FirstOrDefaultAsync(w => w.Id == idWell, token).ConfigureAwait(false);
var dto = entity.Adapt<WellDto>();
return dto.Caption;
}
public async Task<IEnumerable<CompanyDto>> GetCompaniesAsync(int idWell, CancellationToken token)
{
var well = await db.Wells
.Include(w=>w.RelationCompaniesWells)
.ThenInclude(r=>r.Company)
.FirstOrDefaultAsync(w => w.Id == idWell, token)
.ConfigureAwait(false);
var companies = well.RelationCompaniesWells.Select(r => r.Company);
return companies.Adapt<CompanyDto>();
}
} }
} }

View File

@ -27,7 +27,7 @@ namespace AsbCloudWebApi.Controllers
[HttpGet] [HttpGet]
[Route("well/{idWell}/stat")] [Route("well/{idWell}/stat")]
[ProducesResponseType(typeof(IEnumerable<StatOperationDto>), (int)System.Net.HttpStatusCode.OK)] [ProducesResponseType(typeof(IEnumerable<StatOperationsDto>), (int)System.Net.HttpStatusCode.OK)]
public async Task<IActionResult> GetOperationStatByWellAsync(int idWell, public async Task<IActionResult> GetOperationStatByWellAsync(int idWell,
CancellationToken token = default) CancellationToken token = default)
{ {