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
{
@ -7,6 +8,8 @@ namespace AsbCloudApp.Data
public int Id { get; set; }
public string Caption { get; set; }
public string WellType { get; set; }
public string State { get; set; }
public DateTime LastTelemetryDate { get; set; }
public IEnumerable<StatSectionDto> Sections { get; set; }
public PlanFactBase<StatOperationsDto> Total { get; set; }
public IEnumerable<CompanyDto> Companies { get; set; }

View File

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

View File

@ -1,11 +1,11 @@
namespace AsbCloudApp.Data
{
public class WellUpdateParamsDto
public class WellParamsDto
{
public string Caption { get; set; }
public double? Latitude { get; set; }
public double? Longitude { 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
{
void SaveRequestDate(string uid);
DateTime GetLastTelemetryDateByWellId(int idWell);
int? GetidWellByTelemetryUid(string uid);
int GetOrCreateTemetryIdByUid(string uid);
double GetTimezoneOffsetByTelemetryId(int idTelemetry);
@ -19,5 +18,7 @@ namespace AsbCloudApp.Services
IEnumerable<(string Key, int[] Ids)> GetRedundentRemoteUids();
Task<IEnumerable<WellDto>> GetTransmittingWellsAsync(int idCompany,
CancellationToken token);
DateTime GetLastTelemetryDate(string telemetryUid);
DateTime GetLastTelemetryDate(int telemetryId);
}
}

View File

@ -1,4 +1,5 @@
using AsbCloudApp.Data;
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
@ -8,12 +9,14 @@ namespace AsbCloudApp.Services
public interface IWellService
{
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<bool> IsCompanyInvolvedInWellAsync(int idCompany, int idWell, CancellationToken token);
Task<string> GetWellCaptionByIdAsync(int idWell, CancellationToken token);
Task<IEnumerable<CompanyDto>> GetCompaniesAsync(int idWell, CancellationToken token);
Task<WellDto> GetAsync(int idWell, CancellationToken token);
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
{
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.telemetryService = telemetryService;
this.wellService = wellService;
}
public async Task<IEnumerable<DepositDto>> GetDepositsAsync(int idCompany,
@ -144,7 +144,7 @@ namespace AsbCloudInfrastructure.Services
Latitude = well.Latitude,
Longitude = well.Longitude,
WellType = well.WellType?.Caption,
LastTelemetryDate = telemetryService.GetLastTelemetryDateByWellId(well.Id),
LastTelemetryDate = wellService.GetLastTelemetryDate(well.Id),
Cluster = gCluster.Key.Caption,
Deposit = gDeposit.Key.Caption,
}),

View File

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

View File

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

View File

@ -101,7 +101,9 @@ namespace AsbCloudInfrastructure.Services.WellOperationService
{
Id = well.Id,
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);

View File

@ -26,7 +26,19 @@ namespace AsbCloudInfrastructure.Services
cacheRelationCompaniesWells = cacheDb.GetCachedTable<RelationCompanyWell>((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,
CancellationToken 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)
{
if (dto.IdWellType is < 1 or > 2)
throw new ArgumentException("Тип секции указан неправильно.", nameof(dto));
if (dto.State is < 0 or > 2)
if (dto.IdState is < 0 or > 2)
throw new ArgumentException("Текущее состояние работы скважины" +
"указано неправильно.", nameof(dto));
var well = await db.Wells
var entity = await db.Wells
.FirstOrDefaultAsync(w => w.Id == idWell, token)
.ConfigureAwait(false);
if (well is null)
if (entity is null)
throw new ArgumentException("Тип секции указан неправильно.", nameof(idWell));
well.Caption = dto.Caption;
well.Latitude = dto.Latitude;
well.Longitude = dto.Longitude;
well.IdWellType = dto.IdWellType;
well.State = dto.State;
entity.Caption = dto.Caption;
entity.Latitude = dto.Latitude;
entity.Longitude = dto.Longitude;
entity.IdWellType = dto.IdWellType;
entity.IdState = dto.IdState;
db.Wells.Update(well);
db.Wells.Update(entity);
return await db.SaveChangesAsync(token);
}
@ -88,6 +100,7 @@ namespace AsbCloudInfrastructure.Services
var dto = entity.Adapt<WellDto>();
dto.Cluster = entity.Cluster?.Caption;
dto.Deposit = entity.Cluster?.Deposit?.Caption;
return dto;
}
public async Task<string> GetWellCaptionByIdAsync(int idWell, CancellationToken token)
@ -107,5 +120,15 @@ namespace AsbCloudInfrastructure.Services
var companies = well.RelationCompaniesWells.Select(r => r.Company);
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>
[HttpPut]
[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)
{
var idCompany = User.GetCompanyId();