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
{
public class StatClusterDto : ClusterDto
public class StatClusterDto //: ClusterDto
{
public List<StatOperationDto> Sections { get; set; }
public List<StatOperationDto> WellsTotals { get; set; }
public int Id { 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
{
public class StatOperationBase
public class StatOperationsDto
{
/// <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
{
public class StatWellDto : WellDto
public class StatWellDto //: WellDto
{
public IEnumerable<StatOperationDto> Sections { get; set; }
public StatOperationDto Total { get; set; }
public int Id { 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; }
}
}

View File

@ -11,5 +11,7 @@ namespace AsbCloudApp.Services
Task<IEnumerable<WellDto>> GetTransmittingWellsAsync(int idCompany, CancellationToken token);
Task<bool> IsCompanyInvolvedInWellAsync(int idCompany, 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.Services;
using AsbCloudDb.Model;
using Mapster;
using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
using System.Linq;
@ -74,13 +75,7 @@ namespace AsbCloudInfrastructure.Services
.ToListAsync(token)
.ConfigureAwait(false);
var dtos = entities.Select(e => new ClusterDto
{
Id = e.Id,
Caption = e.Caption,
Latitude = e.Latitude,
Longitude = e.Longitude,
});
var dtos = entities.Adapt<ClusterDto>();
return dtos;
}
@ -96,13 +91,7 @@ namespace AsbCloudInfrastructure.Services
.ToListAsync(token)
.ConfigureAwait(false);
var dtos = entities.Select(e => new ClusterDto
{
Id = e.Id,
Caption = e.Caption,
Latitude = e.Latitude,
Longitude = e.Longitude,
});
var dtos = entities.Adapt<ClusterDto>();
return dtos;
}

View File

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

View File

@ -17,12 +17,14 @@ namespace AsbCloudInfrastructure.Services
private readonly IAsbCloudDbContext db;
private readonly ITelemetryTracker telemetryTracker;
private readonly CacheTable<RelationCompanyWell> cacheRelationCompaniesWells;
private readonly CacheTable<Well> cacheWells;
public WellService(IAsbCloudDbContext db, ITelemetryTracker telemetryTracker, CacheDb cacheDb)
{
this.db = db;
this.telemetryTracker = telemetryTracker;
cacheRelationCompaniesWells = cacheDb.GetCachedTable<RelationCompanyWell>((AsbCloudDbContext)db);
cacheWells = cacheDb.GetCachedTable<Well>((AsbCloudDbContext)db);
}
public async Task<IEnumerable<WellDto>> GetTransmittingWellsAsync(int idCompany, CancellationToken token)
@ -74,5 +76,23 @@ namespace AsbCloudInfrastructure.Services
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]
[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,
CancellationToken token = default)
{