Refactor GetLastTelemetryDate(..) in services (single resp).

Add State and LastTelemetryDate into StatWellDto.
This commit is contained in:
Фролов 2021-10-20 12:52:31 +05:00
parent 701f5b26d2
commit eab95cb7a1
11 changed files with 69 additions and 40 deletions

View File

@ -1,4 +1,5 @@
using System.Collections.Generic; using System;
using System.Collections.Generic;
namespace AsbCloudApp.Data namespace AsbCloudApp.Data
{ {
@ -7,6 +8,8 @@ namespace AsbCloudApp.Data
public int Id { get; set; } public int Id { get; set; }
public string Caption { get; set; } public string Caption { get; set; }
public string WellType { get; set; } public string WellType { get; set; }
public string State { get; set; }
public DateTime LastTelemetryDate { get; set; }
public IEnumerable<StatSectionDto> Sections { get; set; } public IEnumerable<StatSectionDto> Sections { get; set; }
public PlanFactBase<StatOperationsDto> Total { get; set; } public PlanFactBase<StatOperationsDto> Total { get; set; }
public IEnumerable<CompanyDto> Companies { get; set; } public IEnumerable<CompanyDto> Companies { get; set; }

View File

@ -9,10 +9,13 @@ namespace AsbCloudApp.Data
public double? Latitude { get; set; } public double? Latitude { get; set; }
public double? Longitude { get; set; } public double? Longitude { get; set; }
public string WellType { get; set; } public string WellType { get; set; }
// 0 - незвестно,
// 1 - в работе, /// <summary>
// 2 - завершена /// 0 - незвестно,
public int State { get; set; } /// 1 - в работе,
/// 2 - завершена
/// </summary>
public int IdState { get; set; }
public DateTime LastTelemetryDate { get; set; } public DateTime LastTelemetryDate { get; set; }
public TelemetryDto Telemetry { get; set; } public TelemetryDto Telemetry { get; set; }
} }

View File

@ -1,11 +1,11 @@
namespace AsbCloudApp.Data namespace AsbCloudApp.Data
{ {
public class WellUpdateParamsDto public class WellParamsDto
{ {
public string Caption { get; set; } public string Caption { get; set; }
public double? Latitude { get; set; } public double? Latitude { get; set; }
public double? Longitude { get; set; } public double? Longitude { get; set; }
public int IdWellType { get; set; } public int IdWellType { get; set; }
public int State { get; set; } public int IdState { get; set; }
} }
} }

View File

@ -9,7 +9,6 @@ namespace AsbCloudApp.Services
public interface ITelemetryService public interface ITelemetryService
{ {
void SaveRequestDate(string uid); void SaveRequestDate(string uid);
DateTime GetLastTelemetryDateByWellId(int idWell);
int? GetidWellByTelemetryUid(string uid); int? GetidWellByTelemetryUid(string uid);
int GetOrCreateTemetryIdByUid(string uid); int GetOrCreateTemetryIdByUid(string uid);
double GetTimezoneOffsetByTelemetryId(int idTelemetry); double GetTimezoneOffsetByTelemetryId(int idTelemetry);
@ -19,5 +18,7 @@ namespace AsbCloudApp.Services
IEnumerable<(string Key, int[] Ids)> GetRedundentRemoteUids(); IEnumerable<(string Key, int[] Ids)> GetRedundentRemoteUids();
Task<IEnumerable<WellDto>> GetTransmittingWellsAsync(int idCompany, Task<IEnumerable<WellDto>> GetTransmittingWellsAsync(int idCompany,
CancellationToken token); CancellationToken token);
DateTime GetLastTelemetryDate(string telemetryUid);
DateTime GetLastTelemetryDate(int telemetryId);
} }
} }

View File

@ -1,4 +1,5 @@
using AsbCloudApp.Data; using AsbCloudApp.Data;
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
@ -8,12 +9,14 @@ namespace AsbCloudApp.Services
public interface IWellService public interface IWellService
{ {
Task<IEnumerable<WellDto>> GetWellsByCompanyAsync(int idCompany, CancellationToken token); Task<IEnumerable<WellDto>> GetWellsByCompanyAsync(int idCompany, CancellationToken token);
Task<int?> UpdateWellAsync(int idWell, WellUpdateParamsDto dto, CancellationToken token = default); Task<int?> UpdateWellAsync(int idWell, WellParamsDto dto, CancellationToken token = default);
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<string> GetWellCaptionByIdAsync(int idWell, CancellationToken token); Task<string> GetWellCaptionByIdAsync(int idWell, CancellationToken token);
Task<IEnumerable<CompanyDto>> GetCompaniesAsync(int idWell, CancellationToken token); Task<IEnumerable<CompanyDto>> GetCompaniesAsync(int idWell, CancellationToken token);
Task<WellDto> GetAsync(int idWell, CancellationToken token); Task<WellDto> GetAsync(int idWell, CancellationToken token);
bool IsCompanyInvolvedInWell(int idCompany, int idWell); bool IsCompanyInvolvedInWell(int idCompany, int idWell);
string GetStateText(int state);
DateTime GetLastTelemetryDate(int idWell);
} }
} }

View File

@ -13,12 +13,12 @@ namespace AsbCloudInfrastructure.Services
public class ClusterService : IClusterService public class ClusterService : IClusterService
{ {
private readonly IAsbCloudDbContext db; private readonly IAsbCloudDbContext db;
private readonly ITelemetryService telemetryService; private readonly IWellService wellService;
public ClusterService(IAsbCloudDbContext db, ITelemetryService telemetryService) public ClusterService(IAsbCloudDbContext db, IWellService wellService)
{ {
this.db = db; this.db = db;
this.telemetryService = telemetryService; this.wellService = wellService;
} }
public async Task<IEnumerable<DepositDto>> GetDepositsAsync(int idCompany, public async Task<IEnumerable<DepositDto>> GetDepositsAsync(int idCompany,
@ -144,7 +144,7 @@ namespace AsbCloudInfrastructure.Services
Latitude = well.Latitude, Latitude = well.Latitude,
Longitude = well.Longitude, Longitude = well.Longitude,
WellType = well.WellType?.Caption, WellType = well.WellType?.Caption,
LastTelemetryDate = telemetryService.GetLastTelemetryDateByWellId(well.Id), LastTelemetryDate = wellService.GetLastTelemetryDate(well.Id),
Cluster = gCluster.Key.Caption, Cluster = gCluster.Key.Caption,
Deposit = gDeposit.Key.Caption, Deposit = gDeposit.Key.Caption,
}), }),

View File

@ -53,15 +53,12 @@ namespace AsbCloudInfrastructure.Services
public void SaveRequestDate(string uid) => public void SaveRequestDate(string uid) =>
telemetryTracker.SaveRequestDate(uid); telemetryTracker.SaveRequestDate(uid);
public DateTime GetLastTelemetryDateByWellId(int idWell) public DateTime GetLastTelemetryDate(string telemetryUid) =>
telemetryTracker.GetLastTelemetryDateByUid(telemetryUid);
public DateTime GetLastTelemetryDate(int telemetryId)
{ {
var lastTelemetryDate = DateTime.MinValue; var lastTelemetryDate = DateTime.MinValue;
var telemetryId = GetIdTelemetryByIdWell(idWell);
if (telemetryId is null)
return lastTelemetryDate;
var telemetry = cacheTelemetry.FirstOrDefault(t => t.Id == telemetryId); var telemetry = cacheTelemetry.FirstOrDefault(t => t.Id == telemetryId);
if (telemetry is null) if (telemetry is null)
@ -69,10 +66,7 @@ namespace AsbCloudInfrastructure.Services
var uid = telemetry.RemoteUid; var uid = telemetry.RemoteUid;
if(!telemetryTracker.GetTransmittingTelemetryUids().Contains(uid)) lastTelemetryDate = GetLastTelemetryDate(uid);
return lastTelemetryDate;
lastTelemetryDate = telemetryTracker.GetLastTelemetryDateByUid(uid);
return lastTelemetryDate; return lastTelemetryDate;
} }

View File

@ -7,8 +7,8 @@ namespace AsbCloudInfrastructure.Services
{ {
public class TelemetryTracker : ITelemetryTracker public class TelemetryTracker : ITelemetryTracker
{ {
private IDictionary<string, DateTime> requests = new Dictionary<string, DateTime>(); private Dictionary<string, DateTime> requests = new Dictionary<string, DateTime>();
private readonly TimeSpan timeout = TimeSpan.FromMinutes(6); private readonly TimeSpan timeout = TimeSpan.FromDays(7);
public void SaveRequestDate(string uid) public void SaveRequestDate(string uid)
{ {
@ -17,7 +17,7 @@ namespace AsbCloudInfrastructure.Services
} }
public DateTime GetLastTelemetryDateByUid(string uid) => public DateTime GetLastTelemetryDateByUid(string uid) =>
requests[uid]; requests.GetValueOrDefault(uid, DateTime.MinValue);
public IEnumerable<string> GetTransmittingTelemetryUids() public IEnumerable<string> GetTransmittingTelemetryUids()
{ {

View File

@ -101,7 +101,9 @@ namespace AsbCloudInfrastructure.Services.WellOperationService
{ {
Id = well.Id, Id = well.Id,
Caption = well.Caption, Caption = well.Caption,
WellType = wellType.Caption WellType = wellType.Caption,
State = wellService.GetStateText(well.IdState),
LastTelemetryDate = wellService.GetLastTelemetryDate(well.Id),
}; };
statWellDto.Companies = await wellService.GetCompaniesAsync(well.Id, token); statWellDto.Companies = await wellService.GetCompaniesAsync(well.Id, token);

View File

@ -26,7 +26,19 @@ namespace AsbCloudInfrastructure.Services
cacheRelationCompaniesWells = cacheDb.GetCachedTable<RelationCompanyWell>((AsbCloudDbContext)db); cacheRelationCompaniesWells = cacheDb.GetCachedTable<RelationCompanyWell>((AsbCloudDbContext)db);
cacheWells = cacheDb.GetCachedTable<Well>((AsbCloudDbContext)db); cacheWells = cacheDb.GetCachedTable<Well>((AsbCloudDbContext)db);
} }
public DateTime GetLastTelemetryDate(int idWell)
{
var lastTelemetryDate = DateTime.MinValue;
var well = cacheWells.FirstOrDefault(w => w.Id == idWell);
if (well is null || well.IdTelemetry is null)
return lastTelemetryDate;
lastTelemetryDate = telemetryService.GetLastTelemetryDate(well.IdTelemetry??0);
return lastTelemetryDate;
}
public async Task<IEnumerable<WellDto>> GetTransmittingWellsAsync(int idCompany, public async Task<IEnumerable<WellDto>> GetTransmittingWellsAsync(int idCompany,
CancellationToken token) => CancellationToken token) =>
await telemetryService.GetTransmittingWellsAsync(idCompany, token); await telemetryService.GetTransmittingWellsAsync(idCompany, token);
@ -43,29 +55,29 @@ namespace AsbCloudInfrastructure.Services
}); });
} }
public async Task<int?> UpdateWellAsync(int idWell, WellUpdateParamsDto dto, public async Task<int?> UpdateWellAsync(int idWell, WellParamsDto dto,
CancellationToken token = default) CancellationToken token = default)
{ {
if (dto.IdWellType is < 1 or > 2) if (dto.IdWellType is < 1 or > 2)
throw new ArgumentException("Тип секции указан неправильно.", nameof(dto)); throw new ArgumentException("Тип секции указан неправильно.", nameof(dto));
if (dto.State is < 0 or > 2) if (dto.IdState is < 0 or > 2)
throw new ArgumentException("Текущее состояние работы скважины" + throw new ArgumentException("Текущее состояние работы скважины" +
"указано неправильно.", nameof(dto)); "указано неправильно.", nameof(dto));
var well = await db.Wells var entity = await db.Wells
.FirstOrDefaultAsync(w => w.Id == idWell, token) .FirstOrDefaultAsync(w => w.Id == idWell, token)
.ConfigureAwait(false); .ConfigureAwait(false);
if (well is null) if (entity is null)
throw new ArgumentException("Тип секции указан неправильно.", nameof(idWell)); throw new ArgumentException("Тип секции указан неправильно.", nameof(idWell));
well.Caption = dto.Caption; entity.Caption = dto.Caption;
well.Latitude = dto.Latitude; entity.Latitude = dto.Latitude;
well.Longitude = dto.Longitude; entity.Longitude = dto.Longitude;
well.IdWellType = dto.IdWellType; entity.IdWellType = dto.IdWellType;
well.State = dto.State; entity.IdState = dto.IdState;
db.Wells.Update(well); db.Wells.Update(entity);
return await db.SaveChangesAsync(token); return await db.SaveChangesAsync(token);
} }
@ -88,6 +100,7 @@ namespace AsbCloudInfrastructure.Services
var dto = entity.Adapt<WellDto>(); var dto = entity.Adapt<WellDto>();
dto.Cluster = entity.Cluster?.Caption; dto.Cluster = entity.Cluster?.Caption;
dto.Deposit = entity.Cluster?.Deposit?.Caption; dto.Deposit = entity.Cluster?.Deposit?.Caption;
return dto; return dto;
} }
public async Task<string> GetWellCaptionByIdAsync(int idWell, CancellationToken token) public async Task<string> GetWellCaptionByIdAsync(int idWell, CancellationToken token)
@ -107,5 +120,15 @@ namespace AsbCloudInfrastructure.Services
var companies = well.RelationCompaniesWells.Select(r => r.Company); var companies = well.RelationCompaniesWells.Select(r => r.Company);
return companies.Adapt<CompanyDto>(); return companies.Adapt<CompanyDto>();
} }
public string GetStateText(int state)
{
return state switch
{
1 => "В работе",
2 => "Завершена",
_ => "Незвестно",
};
}
} }
} }

View File

@ -81,7 +81,7 @@ namespace AsbCloudWebApi.Controllers
/// <returns></returns> /// <returns></returns>
[HttpPut] [HttpPut]
[ProducesResponseType(typeof(int), (int)System.Net.HttpStatusCode.OK)] [ProducesResponseType(typeof(int), (int)System.Net.HttpStatusCode.OK)]
public async Task<IActionResult> UpdateWellAsync(int idWell, WellUpdateParamsDto dto, public async Task<IActionResult> UpdateWellAsync(int idWell, WellParamsDto dto,
CancellationToken token = default) CancellationToken token = default)
{ {
var idCompany = User.GetCompanyId(); var idCompany = User.GetCompanyId();