fix TelemetryService.GetTimezone()

This commit is contained in:
ngfrolov 2023-06-20 14:41:19 +05:00
parent 1489c60675
commit 2e0206c6a0
Signed by: ng.frolov
GPG Key ID: E99907A0357B29A7
10 changed files with 50 additions and 64 deletions

View File

@ -1,6 +1,5 @@
using AsbCloudApp.Data;
using AsbCloudApp.Data.SAUB;
using System;
using System.Threading;
using System.Threading.Tasks;
@ -28,7 +27,7 @@ namespace AsbCloudApp.Services
/// </summary>
/// <param name="uid"></param>
/// <returns></returns>
int GetOrCreateTelemetryIdByUid(string uid);
TelemetryDto GetOrCreateTelemetryByUid(string uid);
/// <summary>
/// получить временную зону скважины по idTelemetry

View File

@ -8,9 +8,9 @@ using System.Linq;
namespace AsbCloudInfrastructure
{
public class ReportDataSourcePgCloud : IReportDataSource
{
private const string DefaultTimezoneId = "Asia/Yekaterinburg";
private readonly IAsbCloudDbContext context;
private readonly int? idTelemetry;
@ -36,14 +36,11 @@ namespace AsbCloudInfrastructure
.Include(w => w.RelationCompaniesWells)
.ThenInclude(r => r.Company)
.Include(w => w.Telemetry)
.FirstOrDefault(w => w.Id == idWell);
.FirstOrDefault(w => w.Id == idWell)
?? throw new ArgumentInvalidException("idWell doesn`t exist", nameof(idWell));
if(well is null)
throw new ArgumentInvalidException("idWell doesn`t exist", nameof(idWell));
idTelemetry = well?.IdTelemetry;
if (idTelemetry is null)
throw new ArgumentInvalidException($"Well {idWell} doesn't contain telemetry", nameof(idWell));
idTelemetry = well?.IdTelemetry
?? throw new ArgumentInvalidException($"Well {idWell} doesn't contain telemetry", nameof(idWell));
events = context.TelemetryEvents
.Where(e => e.IdTelemetry == idTelemetry)
@ -62,7 +59,7 @@ namespace AsbCloudInfrastructure
Well = well.Caption,
Customer = well.RelationCompaniesWells.FirstOrDefault(c => c.Company.IdCompanyType == 1)?.Company.Caption,
DrillingStartDate = well.Telemetry?.Info?.DrillingStartDate.ToRemoteDateTime(timezoneOffset) ?? default,
TimeZoneId = well.Telemetry?.Info?.TimeZoneId ?? well.Timezone?.TimezoneId ?? default,
TimeZoneId = well.Telemetry?.Info?.TimeZoneId ?? well.Timezone?.TimezoneId ?? DefaultTimezoneId,
TimeZoneOffsetTotalHours = timezoneOffset,
};
}

View File

@ -31,7 +31,7 @@ namespace AsbCloudInfrastructure.Repository
/// <inheritdoc/>
public async Task<int> AddOrUpdateAsync(string uid, TelemetryWirelineRunOutBaseDto dto, CancellationToken token)
{
var idTelemetry = telemetryService.GetOrCreateTelemetryIdByUid(uid);
var idTelemetry = telemetryService.GetOrCreateTelemetryByUid(uid).Id;
var timezoneOffset = telemetryService.GetTimezone(idTelemetry).Hours;
var entity = Convert(idTelemetry, dto, timezoneOffset);

View File

@ -30,12 +30,12 @@ namespace AsbCloudInfrastructure.Services.SAUB
if (!dtos.Any())
return;
var telemetryId = telemetryService.GetOrCreateTelemetryIdByUid(uid);
var telemetry = telemetryService.GetOrCreateTelemetryByUid(uid);
var entities = dtos.Select(dto => new TelemetryEvent
{
IdEvent = dto.Id,
IdTelemetry = telemetryId,
IdTelemetry = telemetry.Id,
IdCategory = dto.IdCategory,
MessageTemplate = dto.Message
});
@ -43,5 +43,4 @@ namespace AsbCloudInfrastructure.Services.SAUB
memoryCache.DropBasic<TelemetryEvent>();
}
}
}

View File

@ -139,14 +139,14 @@ namespace AsbCloudInfrastructure.Services.SAUB
if (!dtos.Any())
return Task.CompletedTask;
var telemetryId = telemetryService.GetOrCreateTelemetryIdByUid(uid);
var timezone = telemetryService.GetTimezone(telemetryId);
var telemetry = telemetryService.GetOrCreateTelemetryByUid(uid);
var timezone = telemetryService.GetTimezone(telemetry.Id);
foreach (var dto in dtos)
{
var entity = dto.Adapt<TelemetryMessage>();
entity.Id = 0;
entity.IdTelemetry = telemetryId;
entity.IdTelemetry = telemetry.Id;
entity.DateTime = dto.Date.ToUtcDateTimeOffset(timezone.Hours);
db.TelemetryMessages.Add(entity);
}

View File

@ -9,7 +9,6 @@ using System.Linq;
using System.Threading;
using System.Threading.Tasks;
namespace AsbCloudInfrastructure.Services.SAUB
{
public abstract class TelemetryDataBaseService<TDto, TEntity> : ITelemetryDataService<TDto>
@ -51,15 +50,15 @@ namespace AsbCloudInfrastructure.Services.SAUB
dtosList.Remove(duplicate);
}
var idTelemetry = telemetryService.GetOrCreateTelemetryIdByUid(uid);
var timezone = telemetryService.GetTimezone(idTelemetry);
var telemetry = telemetryService.GetOrCreateTelemetryByUid(uid);
var timezone = telemetryService.GetTimezone(telemetry.Id);
telemetryDataCache.AddRange(idTelemetry, dtos);
telemetryDataCache.AddRange(telemetry.Id, dtos);
var entities = dtosList.Select(dto =>
{
var entity = Convert(dto, timezone.Hours);
entity.IdTelemetry = idTelemetry;
entity.IdTelemetry = telemetry.Id;
return entity;
});
@ -76,7 +75,7 @@ namespace AsbCloudInfrastructure.Services.SAUB
stopwatch.Stop();
Trace.WriteLine($"Fail to save data telemetry " +
$"uid: {uid}, " +
$"idTelemetry {idTelemetry}, " +
$"idTelemetry {telemetry.Id}, " +
$"count: {entities.Count()}, " +
$"dataDate: {entities.FirstOrDefault()?.DateTime}, " +
$"dbSaveDurationTime:{stopwatch.ElapsedMilliseconds}ms. " +

View File

@ -14,7 +14,6 @@ using System.Threading.Tasks;
namespace AsbCloudInfrastructure.Services.SAUB
{
public class TelemetryService : ITelemetryService
{
private readonly IAsbCloudDbContext db;
@ -53,16 +52,13 @@ namespace AsbCloudInfrastructure.Services.SAUB
return cacheDataRange;
}
public int GetOrCreateTelemetryIdByUid(string uid)
=> GetOrCreateTelemetryByUid(uid).Id;
public int? GetIdWellByTelemetryUid(string uid)
=> GetWellByTelemetryUid(uid)?.Id;
public async Task UpdateInfoAsync(string uid, TelemetryInfoDto info,
CancellationToken token)
{
var telemetry = GetOrCreateTelemetryByUid(uid);
var telemetry = GetOrCreateTelemetry(uid);
telemetry.Info = info.Adapt<TelemetryInfo>();
if (!string.IsNullOrEmpty(info.TimeZoneId) &&
@ -78,6 +74,7 @@ namespace AsbCloudInfrastructure.Services.SAUB
DropTelemetryCache();
}
[Obsolete("This method will be private. Use TelemetryDto.TimeZone prop.")]
public SimpleTimezoneDto GetTimezone(int idTelemetry)
{
var telemetry = GetTelemetryCache().FirstOrDefault(t => t.Id == idTelemetry);
@ -85,41 +82,37 @@ namespace AsbCloudInfrastructure.Services.SAUB
if (telemetry is null)
throw new Exception($"Telemetry id: {idTelemetry} does not exist.");
if (telemetry.Well?.Timezone is not null)
{
telemetry.TimeZone = telemetry.Well.Timezone;
db.Telemetries.Upsert(telemetry);
db.SaveChanges();
DropTelemetryCache();
if (telemetry.Well?.Timezone is not null)
return telemetry.TimeZone.Adapt<SimpleTimezoneDto>();
}
if (telemetry.TimeZone is not null)
if (telemetry.TimeZone is not null)
return telemetry.TimeZone.Adapt<SimpleTimezoneDto>();
if (telemetry.Info is not null)
{
telemetry.TimeZone = new SimpleTimezone
{
Hours = telemetry.Info.TimeZoneOffsetTotalHours,
IsOverride = false,
TimezoneId = telemetry.Info.TimeZoneId,
};
db.Telemetries.Upsert(telemetry);
db.SaveChanges();
DropTelemetryCache();
return telemetry.TimeZone.Adapt<SimpleTimezoneDto>();
}
throw new Exception($"Telemetry id: {idTelemetry} can't find timezone.");
}
public TelemetryBaseDto? GetOrDefaultTelemetryByIdWell(int idWell)
{
var telemetry = GetTelemetryCache()
var entity = GetTelemetryCache()
.FirstOrDefault(t => t.Well?.Id == idWell);
return telemetry?.Adapt<TelemetryBaseDto>();
if (entity.Well?.Timezone is not null && entity.TimeZone.Hours != entity.Well.Timezone.Hours)
{
entity.TimeZone = entity.Well.Timezone;
//TODO: выдаем предупреждение!
}
return entity?.Adapt<TelemetryBaseDto>();
}
public TelemetryDto GetOrCreateTelemetryByUid(string uid)
{
var entity = GetOrCreateTelemetry(uid);
if(entity.Well?.Timezone is not null && entity.TimeZone.Hours != entity.Well.Timezone.Hours)
{
entity.TimeZone = entity.Well.Timezone;
//TODO: выдаем предупреждение!
}
var dto = entity.Adapt<TelemetryDto>();
return dto;
}
private Well? GetWellByTelemetryUid(string uid)
@ -134,7 +127,7 @@ namespace AsbCloudInfrastructure.Services.SAUB
return telemetry;
}
private Telemetry GetOrCreateTelemetryByUid(string uid)
private Telemetry GetOrCreateTelemetry(string uid)
{
var telemetry = GetOrDefaultTelemetryByUid(uid);
if (telemetry is null)
@ -320,5 +313,4 @@ namespace AsbCloudInfrastructure.Services.SAUB
return affected;
}
}
}

View File

@ -63,12 +63,12 @@ namespace AsbCloudInfrastructure.Services.SAUB
if (!dtos.Any())
return;
var telemetryId = telemetryService.GetOrCreateTelemetryIdByUid(uid);
var telemetry = telemetryService.GetOrCreateTelemetryByUid(uid);
var entities = dtos.Distinct(new TelemetryUserDtoComparer()).Select(dto => {
var entity = dto.Adapt<TelemetryUser>();
entity.IdUser = dto.Id;
entity.IdTelemetry = telemetryId;
entity.IdTelemetry = telemetry.Id;
return entity;
});
var result = await db.Database.ExecInsertOrUpdateAsync(db.TelemetryUsers, entities, token);

View File

@ -109,8 +109,8 @@ namespace AsbCloudWebApi.Controllers.SAUB
[FromBody] IEnumerable<WitsRecordDto> dtos,
CancellationToken token = default)
{
var idTelemetry = telemetryService.GetOrCreateTelemetryIdByUid(uid);
await gtrRepository.SaveDataAsync(idTelemetry, dtos, token).ConfigureAwait(false);
var telemetry = telemetryService.GetOrCreateTelemetryByUid(uid);
await gtrRepository.SaveDataAsync(telemetry.Id, dtos, token).ConfigureAwait(false);
var idWell = telemetryService.GetIdWellByTelemetryUid(uid);
if (idWell is not null && dtos is not null)
_ = Task.Run(() => telemetryHubContext.Clients.Group($"well_{idWell}_gtr")

View File

@ -49,8 +49,8 @@ namespace AsbCloudWebApi.Controllers.WITS
[FromServices] IWitsRecordRepository<TDto> witsRecordRepository,
CancellationToken token = default)
{
var idTelemetry = telemetryService.GetOrCreateTelemetryIdByUid(uid);
await witsRecordRepository.SaveDataAsync(idTelemetry, dtos, token).ConfigureAwait(false);
var telemetry = telemetryService.GetOrCreateTelemetryByUid(uid);
await witsRecordRepository.SaveDataAsync(telemetry.Id, dtos, token).ConfigureAwait(false);
var idWell = telemetryService.GetIdWellByTelemetryUid(uid);
if (idWell != null && dtos.Any())
_ = Task.Run(() => telemetryHubContext.Clients.Group($"well_{idWell}_wits")