2024-07-22 12:43:26 +05:00
|
|
|
using AsbCloudApp.Data;
|
2024-07-04 11:02:45 +05:00
|
|
|
using AsbCloudApp.Data.GTR;
|
2024-07-22 12:43:26 +05:00
|
|
|
using AsbCloudApp.Exceptions;
|
2023-04-07 04:51:30 +05:00
|
|
|
using AsbCloudApp.Repositories;
|
2024-07-22 12:43:26 +05:00
|
|
|
using AsbCloudApp.Requests;
|
2023-04-04 21:21:06 +05:00
|
|
|
using AsbCloudApp.Services;
|
2023-09-20 17:50:44 +05:00
|
|
|
using AsbCloudDb;
|
2023-04-04 21:21:06 +05:00
|
|
|
using AsbCloudDb.Model;
|
2023-04-06 04:02:47 +05:00
|
|
|
using AsbCloudDb.Model.GTR;
|
2024-07-22 12:43:26 +05:00
|
|
|
using Mapster;
|
2023-04-06 04:02:47 +05:00
|
|
|
using Microsoft.EntityFrameworkCore;
|
2023-04-04 21:21:06 +05:00
|
|
|
using System;
|
2023-06-30 15:48:24 +05:00
|
|
|
using System.Collections.Concurrent;
|
2023-04-04 21:21:06 +05:00
|
|
|
using System.Collections.Generic;
|
2023-09-20 17:50:44 +05:00
|
|
|
using System.Diagnostics;
|
2023-04-04 21:21:06 +05:00
|
|
|
using System.Linq;
|
|
|
|
using System.Threading;
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
namespace AsbCloudInfrastructure.Repository
|
|
|
|
{
|
2023-04-11 06:34:51 +05:00
|
|
|
public class GtrWitsRepository : IGtrRepository
|
2023-04-04 21:21:06 +05:00
|
|
|
{
|
2024-04-10 11:09:48 +05:00
|
|
|
private static IDictionary<(int IdRecord, int IdItem), string> WitsParameters = new Dictionary<(int, int), string>
|
2024-04-04 09:41:00 +05:00
|
|
|
{
|
2024-04-08 16:32:00 +05:00
|
|
|
{ (1, 8), nameof(GtrWitsDto.DEPTBITM) },
|
|
|
|
{ (1, 10), nameof(GtrWitsDto.DEPTMEAS) },
|
2024-04-04 09:41:00 +05:00
|
|
|
{ (1, 14), nameof(GtrWitsDto.HKLA) },
|
|
|
|
{ (1, 12), nameof(GtrWitsDto.BLKPOS) },
|
|
|
|
{ (1, 16), nameof(GtrWitsDto.WOBA) },
|
|
|
|
{ (1, 18), nameof(GtrWitsDto.TORQA) },
|
|
|
|
{ (1, 21), nameof(GtrWitsDto.SPPA) },
|
|
|
|
{ (2, 15), nameof(GtrWitsDto.RPMA) },
|
|
|
|
{ (1, 13), nameof(GtrWitsDto.ROPA) },
|
|
|
|
{ (3, 16), nameof(GtrWitsDto.RSUX) },
|
|
|
|
{ (3, 17), nameof(GtrWitsDto.RSDX) },
|
|
|
|
{ (1, 30), nameof(GtrWitsDto.MFIA) },
|
|
|
|
{ (1, 29), nameof(GtrWitsDto.MFOA)},
|
|
|
|
{ (1, 34), nameof(GtrWitsDto.MTIA) },
|
|
|
|
{ (1, 33), nameof(GtrWitsDto.MTOA) },
|
|
|
|
{ (1, 23), nameof(GtrWitsDto.SPM1) },
|
|
|
|
{ (1, 24), nameof(GtrWitsDto.SPM2) },
|
|
|
|
{ (1, 25), nameof(GtrWitsDto.SPM3) },
|
|
|
|
{ (1, 26), nameof(GtrWitsDto.TVOLACT) },
|
|
|
|
{ (11, 29), nameof(GtrWitsDto.TTVOL1) },
|
|
|
|
{ (11, 30), nameof(GtrWitsDto.TTVOL2) },
|
|
|
|
{ (11, 15), nameof(GtrWitsDto.TVOL01) },
|
|
|
|
{ (11, 16), nameof(GtrWitsDto.TVOL02) },
|
|
|
|
{ (11, 17), nameof(GtrWitsDto.TVOL03) },
|
|
|
|
{ (11, 18), nameof(GtrWitsDto.TVOL04) },
|
|
|
|
{ (11, 19), nameof(GtrWitsDto.TVOL05) },
|
|
|
|
{ (11, 20), nameof(GtrWitsDto.TVOL06) },
|
|
|
|
{ (11, 21), nameof(GtrWitsDto.TVOL07) },
|
|
|
|
{ (11, 22), nameof(GtrWitsDto.TVOL08) },
|
|
|
|
{ (11, 23), nameof(GtrWitsDto.TVOL09) },
|
|
|
|
{ (11, 24), nameof(GtrWitsDto.TVOL10) },
|
|
|
|
{ (11, 25), nameof(GtrWitsDto.TVOL11) },
|
|
|
|
{ (11, 26), nameof(GtrWitsDto.TVOL12) },
|
|
|
|
{ (11, 27), nameof(GtrWitsDto.TVOL13) },
|
|
|
|
{ (11, 28), nameof(GtrWitsDto.TVOL14) },
|
|
|
|
{ (1, 31), nameof(GtrWitsDto.MDOA) },
|
|
|
|
{ (1, 32), nameof(GtrWitsDto.MDIA) },
|
|
|
|
{ (12, 12), nameof(GtrWitsDto.METHANE) },
|
|
|
|
{ (12, 13), nameof(GtrWitsDto.ETHANE) },
|
|
|
|
{ (12, 14), nameof(GtrWitsDto.PROPANE) },
|
|
|
|
{ (12, 15), nameof(GtrWitsDto.IBUTANE) },
|
|
|
|
{ (12, 16), nameof(GtrWitsDto.NBUTANE) },
|
|
|
|
{ (1, 40), nameof(GtrWitsDto.GASA) },
|
|
|
|
};
|
|
|
|
|
2023-04-04 21:21:06 +05:00
|
|
|
private readonly IAsbCloudDbContext db;
|
|
|
|
private readonly ITelemetryService telemetryService;
|
2023-06-30 15:48:24 +05:00
|
|
|
private static ConcurrentDictionary<int, ConcurrentDictionary<(int, int), WitsItemRecordDto>> cache = new();
|
2023-04-04 21:21:06 +05:00
|
|
|
|
|
|
|
public GtrWitsRepository(
|
|
|
|
IAsbCloudDbContext db,
|
|
|
|
ITelemetryService telemetryService)
|
|
|
|
{
|
|
|
|
this.db = db;
|
|
|
|
this.telemetryService = telemetryService;
|
|
|
|
}
|
2024-04-08 16:32:00 +05:00
|
|
|
|
|
|
|
public async Task<IEnumerable<GtrWitsDto>> GetAsync(int idWell, GtrRequest request, CancellationToken token) =>
|
|
|
|
await GetAsync<WitsItemFloat, float>(idWell, request, token);
|
|
|
|
|
2024-04-17 15:46:14 +05:00
|
|
|
public async Task<DatesRangeDto?> GetRangeAsync(int idWell, DateTimeOffset? geDate, DateTimeOffset? leDate, CancellationToken token)
|
|
|
|
{
|
|
|
|
var telemetry = telemetryService.GetOrDefaultTelemetryByIdWell(idWell);
|
2024-07-22 12:43:26 +05:00
|
|
|
|
2024-04-17 15:46:14 +05:00
|
|
|
if (telemetry is null)
|
|
|
|
return null;
|
|
|
|
|
|
|
|
var rangeQuery = db
|
|
|
|
.Set<WitsItemFloat>()
|
|
|
|
.Where(e => e.IdTelemetry == telemetry.Id);
|
|
|
|
|
|
|
|
if (geDate is not null)
|
|
|
|
rangeQuery = rangeQuery.Where(e => e.DateTime >= geDate);
|
|
|
|
|
|
|
|
if (leDate is not null)
|
|
|
|
rangeQuery = rangeQuery.Where(e => e.DateTime <= leDate);
|
|
|
|
|
|
|
|
var groupedQuery = rangeQuery.GroupBy(e => e.IdTelemetry)
|
|
|
|
.Select(group => new
|
|
|
|
{
|
|
|
|
Min = group.Min(e => e.DateTime),
|
|
|
|
Max = group.Max(e => e.DateTime)
|
|
|
|
});
|
|
|
|
var range = await groupedQuery.FirstOrDefaultAsync(token);
|
|
|
|
|
2024-07-22 12:43:26 +05:00
|
|
|
if (range is null)
|
2024-04-17 15:46:14 +05:00
|
|
|
return null;
|
|
|
|
|
|
|
|
var result = new DatesRangeDto
|
|
|
|
{
|
|
|
|
From = range.Min.ToOffset(telemetry.TimeZone!.Offset),
|
|
|
|
To = range.Max.ToOffset(telemetry.TimeZone!.Offset),
|
|
|
|
};
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2024-04-08 16:32:00 +05:00
|
|
|
private async Task<IEnumerable<GtrWitsDto>> GetAsync<TEntity, TType>(int idWell, GtrRequest request, CancellationToken token)
|
2024-07-22 12:43:26 +05:00
|
|
|
where TEntity : WitsItemBase<TType>
|
2024-04-08 16:32:00 +05:00
|
|
|
where TType : notnull
|
2024-04-04 09:41:00 +05:00
|
|
|
{
|
2024-04-08 16:32:00 +05:00
|
|
|
var telemetry = telemetryService.GetOrDefaultTelemetryByIdWell(idWell);
|
2024-07-22 12:43:26 +05:00
|
|
|
|
2024-04-08 16:32:00 +05:00
|
|
|
if (telemetry is null)
|
|
|
|
return Enumerable.Empty<GtrWitsDto>();
|
2024-04-04 09:41:00 +05:00
|
|
|
|
2024-04-10 11:09:48 +05:00
|
|
|
if (telemetry.TimeZone is null)
|
2024-07-22 12:43:26 +05:00
|
|
|
throw new ArgumentInvalidException(nameof(idWell), $"Telemetry id: {telemetry.Id} can't find timezone");
|
2024-04-10 11:09:48 +05:00
|
|
|
|
2024-04-08 16:32:00 +05:00
|
|
|
var query = BuildQuery<TEntity, TType>(telemetry.Id, request);
|
2024-04-04 09:41:00 +05:00
|
|
|
|
2024-04-10 11:09:48 +05:00
|
|
|
var idsRecord = WitsParameters.Select(p => p.Key.IdRecord);
|
|
|
|
|
2024-04-08 16:32:00 +05:00
|
|
|
var entities = await query
|
2024-04-10 11:09:48 +05:00
|
|
|
.Where(e => idsRecord.Contains(e.IdRecord))
|
2024-04-08 16:32:00 +05:00
|
|
|
.OrderBy(e => e.DateTime)
|
|
|
|
.AsNoTracking()
|
|
|
|
.ToArrayAsync(token);
|
2024-04-04 09:41:00 +05:00
|
|
|
|
2024-04-17 09:18:08 +05:00
|
|
|
if (!entities.Any())
|
|
|
|
return Enumerable.Empty<GtrWitsDto>();
|
|
|
|
|
|
|
|
var interval = TimeSpan.FromSeconds(10);
|
|
|
|
var timezoneOffset = TimeSpan.FromHours(telemetry.TimeZone.Hours);
|
2024-07-22 12:43:26 +05:00
|
|
|
|
2024-04-08 16:32:00 +05:00
|
|
|
var dtos = entities
|
|
|
|
.GroupBy(e => e.DateTime.Ticks / interval.Ticks)
|
|
|
|
.Select(groupByInterval =>
|
|
|
|
{
|
|
|
|
var items = groupByInterval.Select(e => e);
|
|
|
|
var values = items.GroupBy(e => (e.IdRecord, e.IdItem))
|
2024-04-17 15:46:14 +05:00
|
|
|
.Where(group => WitsParameters.ContainsKey(group.Key))
|
|
|
|
.ToDictionary(group => WitsParameters[group.Key], g => (object)g.Last().Value);
|
2024-04-08 16:32:00 +05:00
|
|
|
|
|
|
|
var dto = values.Adapt<GtrWitsDto>();
|
|
|
|
dto.DateTime = items.Last().DateTime.ToOffset(timezoneOffset);
|
|
|
|
return dto;
|
|
|
|
});
|
|
|
|
|
|
|
|
return dtos;
|
2024-04-04 09:41:00 +05:00
|
|
|
}
|
|
|
|
|
2024-04-08 16:32:00 +05:00
|
|
|
private IQueryable<TEntity> BuildQuery<TEntity, TType>(int idTelemetry, GtrRequest request)
|
2024-07-22 12:43:26 +05:00
|
|
|
where TEntity : WitsItemBase<TType>
|
2024-04-04 09:41:00 +05:00
|
|
|
where TType : notnull
|
2024-07-22 12:43:26 +05:00
|
|
|
{
|
2024-04-08 16:32:00 +05:00
|
|
|
var query = db.Set<TEntity>()
|
2024-04-17 09:18:08 +05:00
|
|
|
.Where(e => e.IdTelemetry == idTelemetry);
|
2024-04-04 09:41:00 +05:00
|
|
|
|
|
|
|
if (request.Begin.HasValue)
|
|
|
|
{
|
2024-04-17 09:18:08 +05:00
|
|
|
var dateBegin = request.Begin.Value.ToUniversalTime();
|
|
|
|
var dateEnd = dateBegin.AddSeconds(request.IntervalSec);
|
|
|
|
query = query
|
|
|
|
.Where(e => e.DateTime >= dateBegin)
|
|
|
|
.Where(e => e.DateTime <= dateEnd);
|
2024-04-04 09:41:00 +05:00
|
|
|
}
|
2024-04-17 15:46:14 +05:00
|
|
|
else
|
|
|
|
{
|
|
|
|
var lastDate = query
|
2024-07-22 12:43:26 +05:00
|
|
|
.OrderBy(e => e.DateTime)
|
2024-04-17 15:46:14 +05:00
|
|
|
.LastOrDefault()
|
2024-07-22 12:43:26 +05:00
|
|
|
?.DateTime
|
2024-04-17 15:46:14 +05:00
|
|
|
?? DateTimeOffset.UtcNow;
|
|
|
|
var dateBegin = lastDate.AddSeconds(-request.IntervalSec);
|
|
|
|
var dateEnd = lastDate;
|
|
|
|
query = query
|
|
|
|
.Where(e => e.DateTime >= dateBegin)
|
|
|
|
.Where(e => e.DateTime <= dateEnd);
|
|
|
|
}
|
2024-04-04 09:41:00 +05:00
|
|
|
|
|
|
|
return query;
|
|
|
|
}
|
2023-04-04 21:21:06 +05:00
|
|
|
|
2024-04-08 16:32:00 +05:00
|
|
|
[Obsolete]
|
2023-04-18 16:07:29 +05:00
|
|
|
public async Task<IEnumerable<WitsRecordDto>> GetAsync(int idWell, DateTime? dateBegin, double intervalSec = 600, int approxPointsCount = 1024, CancellationToken token = default)
|
2023-04-04 21:21:06 +05:00
|
|
|
{
|
|
|
|
var telemetry = telemetryService.GetOrDefaultTelemetryByIdWell(idWell);
|
|
|
|
if (telemetry is null)
|
|
|
|
return Enumerable.Empty<WitsRecordDto>();
|
2023-04-07 02:53:57 +05:00
|
|
|
|
2023-04-04 21:21:06 +05:00
|
|
|
var timezone = telemetryService.GetTimezone(telemetry.Id);
|
|
|
|
|
2023-05-18 15:31:27 +05:00
|
|
|
DateTimeOffset? dateBeginUtc = dateBegin?.ToUtcDateTimeOffset(timezone.Hours);
|
2023-04-18 16:07:29 +05:00
|
|
|
var dateEnd = dateBeginUtc?.AddSeconds(intervalSec);
|
|
|
|
|
2023-05-18 15:31:27 +05:00
|
|
|
var witsRequest = new WitsRequest()
|
|
|
|
{
|
|
|
|
IdTelemetry = telemetry.Id,
|
|
|
|
DateBeginUtc = dateBeginUtc,
|
|
|
|
DateEnd = dateEnd,
|
|
|
|
ApproxPointsCount = approxPointsCount,
|
|
|
|
TimezoneHours = timezone.Hours
|
|
|
|
};
|
|
|
|
|
|
|
|
var recordAllInt = await GetItemsOrDefaultAsync<WitsItemInt, int>(witsRequest, token);
|
|
|
|
var recordAllFloat = await GetItemsOrDefaultAsync<WitsItemFloat, float>(witsRequest, token);
|
|
|
|
var recordAllString = await GetItemsOrDefaultAsync<WitsItemString, string>(witsRequest, token);
|
2023-04-04 21:21:06 +05:00
|
|
|
|
2023-04-18 16:07:29 +05:00
|
|
|
var dtos = (recordAllFloat.Union(recordAllInt)).Union(recordAllString)
|
2023-04-14 00:36:11 +05:00
|
|
|
.GroupBy(g => new
|
|
|
|
{
|
2023-04-14 00:40:38 +05:00
|
|
|
g.IdRecord,
|
|
|
|
g.Date
|
2023-04-18 16:07:29 +05:00
|
|
|
})
|
|
|
|
.Select(g => new WitsRecordDto
|
|
|
|
{
|
|
|
|
Id = g.Key.IdRecord,
|
|
|
|
Date = g.Key.Date,
|
2023-05-18 15:31:27 +05:00
|
|
|
Items = g.Select(r => new
|
|
|
|
{
|
|
|
|
Key = r.IdItem,
|
2023-05-19 16:48:00 +05:00
|
|
|
r.Value
|
2023-04-18 16:07:29 +05:00
|
|
|
}).ToDictionary(x => x.Key, x => x.Value)
|
2023-05-18 15:31:27 +05:00
|
|
|
});
|
2023-04-11 00:32:06 +05:00
|
|
|
return dtos;
|
|
|
|
}
|
|
|
|
|
2023-06-30 15:48:24 +05:00
|
|
|
public IEnumerable<WitsItemRecordDto> GetLastDataByRecordId(int idWell, int idRecord)
|
2023-05-16 09:17:17 +05:00
|
|
|
{
|
2023-06-30 15:48:24 +05:00
|
|
|
var result = GetLastData(idWell)
|
|
|
|
.Where(item => item.IdRecord == idRecord);
|
|
|
|
return result;
|
2023-05-16 09:17:17 +05:00
|
|
|
}
|
|
|
|
|
2023-06-30 15:48:24 +05:00
|
|
|
public IEnumerable<WitsItemRecordDto> GetLastData(int idWell)
|
2023-04-11 00:32:06 +05:00
|
|
|
{
|
2023-06-30 15:48:24 +05:00
|
|
|
var telemetry = telemetryService.GetOrDefaultTelemetryByIdWell(idWell);
|
|
|
|
if (telemetry is null)
|
|
|
|
return Enumerable.Empty<WitsItemRecordDto>();
|
2023-05-18 15:37:21 +05:00
|
|
|
|
2023-06-30 15:48:24 +05:00
|
|
|
var lastData = cache.GetValueOrDefault(telemetry.Id);
|
|
|
|
return lastData?.Values ?? Enumerable.Empty<WitsItemRecordDto>();
|
2023-05-19 12:45:07 +05:00
|
|
|
}
|
|
|
|
|
|
|
|
private async Task<IEnumerable<WitsItemRecordDto>> GetItemsOrDefaultAsync<TEntity, TValue>(
|
|
|
|
WitsRequest request,
|
|
|
|
CancellationToken token)
|
|
|
|
where TEntity : WitsItemBase<TValue>
|
|
|
|
where TValue : notnull
|
|
|
|
{
|
|
|
|
var query = BuildQuery<TEntity, TValue>(request);
|
2023-04-18 16:07:29 +05:00
|
|
|
|
|
|
|
var fullDataCount = await query.CountAsync(token);
|
2023-04-17 20:59:27 +05:00
|
|
|
if (fullDataCount == 0)
|
2023-05-19 12:45:07 +05:00
|
|
|
return Enumerable.Empty<WitsItemRecordDto>();
|
2023-04-11 00:32:06 +05:00
|
|
|
|
2023-05-18 15:31:27 +05:00
|
|
|
if (request.ApproxPointsCount is not null && fullDataCount > 1.75 * request.ApproxPointsCount)
|
2023-04-17 20:59:27 +05:00
|
|
|
{
|
2023-05-18 15:31:27 +05:00
|
|
|
var m = (int)Math.Round(1d * fullDataCount / request.ApproxPointsCount!.Value);
|
2023-04-17 20:59:27 +05:00
|
|
|
if (m > 1)
|
|
|
|
query = query.Where((d) => (((d.DateTime.DayOfYear * 24 + d.DateTime.Hour) * 60 + d.DateTime.Minute) * 60 + d.DateTime.Second) % m == 0);
|
|
|
|
}
|
2023-04-06 04:02:47 +05:00
|
|
|
|
2023-04-11 00:32:06 +05:00
|
|
|
var entities = await query
|
2023-05-18 15:37:21 +05:00
|
|
|
.OrderBy(d => d.DateTime)
|
2023-04-11 00:32:06 +05:00
|
|
|
.AsNoTracking()
|
|
|
|
.ToListAsync(token)
|
|
|
|
.ConfigureAwait(false);
|
2023-05-18 15:31:27 +05:00
|
|
|
|
2023-05-19 12:45:07 +05:00
|
|
|
var items = entities.Select(e => new WitsItemRecordDto
|
2023-04-14 00:36:11 +05:00
|
|
|
{
|
|
|
|
IdRecord = e.IdRecord,
|
2023-05-18 15:31:27 +05:00
|
|
|
Date = e.DateTime.ToRemoteDateTime(request.TimezoneHours),
|
2023-04-14 00:36:11 +05:00
|
|
|
IdItem = e.IdItem,
|
2023-05-19 12:45:07 +05:00
|
|
|
Value = new JsonValue(e.Value)
|
2023-04-14 00:36:11 +05:00
|
|
|
});
|
2023-05-16 09:17:17 +05:00
|
|
|
return items;
|
|
|
|
}
|
|
|
|
|
2023-05-19 12:45:07 +05:00
|
|
|
private IQueryable<TEntity> BuildQuery<TEntity, TValue>(WitsRequest request)
|
|
|
|
where TEntity : WitsItemBase<TValue>
|
|
|
|
where TValue : notnull
|
|
|
|
{
|
|
|
|
var query = db.Set<TEntity>().Where(i => i.IdTelemetry == request.IdTelemetry);
|
|
|
|
|
|
|
|
if (request.IdRecord is not null)
|
|
|
|
query = query
|
|
|
|
.Where(d => d.IdRecord == request.IdRecord);
|
|
|
|
|
|
|
|
if (request.DateBeginUtc is not null)
|
|
|
|
query = query
|
|
|
|
.Where(d => d.DateTime >= request.DateBeginUtc);
|
|
|
|
|
|
|
|
if (request.DateEnd is not null)
|
|
|
|
query = query
|
|
|
|
.Where(d => d.DateTime <= request.DateEnd);
|
|
|
|
|
|
|
|
return query;
|
|
|
|
}
|
|
|
|
|
2023-06-07 13:48:40 +05:00
|
|
|
public async Task SaveDataAsync(int idTelemetry, IEnumerable<WitsRecordDto> dtos, CancellationToken token)
|
2023-04-11 06:34:51 +05:00
|
|
|
{
|
2023-04-18 16:07:29 +05:00
|
|
|
var timezoneHours = telemetryService.GetTimezone(idTelemetry).Hours;
|
2023-06-07 13:48:40 +05:00
|
|
|
|
2023-06-30 15:48:24 +05:00
|
|
|
var cacheTelemetryItems = cache.GetValueOrDefault(idTelemetry);
|
|
|
|
|
2023-09-20 17:50:44 +05:00
|
|
|
var strings = new List<WitsItemString>(4);
|
|
|
|
var floats = new List<WitsItemFloat>(4);
|
|
|
|
var ints = new List<WitsItemInt>(4);
|
|
|
|
|
2023-06-30 15:48:24 +05:00
|
|
|
foreach (var record in dtos)
|
2023-04-18 16:07:29 +05:00
|
|
|
{
|
2023-06-30 15:48:24 +05:00
|
|
|
var dateTime = record.Date.ToUtcDateTimeOffset(timezoneHours);
|
|
|
|
foreach (var item in record.Items)
|
2023-04-18 16:07:29 +05:00
|
|
|
{
|
2024-07-22 12:43:26 +05:00
|
|
|
if (cacheTelemetryItems?.TryGetValue((record.Id, item.Key), out var cacheItem) == true)
|
2023-06-30 15:48:24 +05:00
|
|
|
if (Math.Abs((dateTime - cacheItem.Date).TotalSeconds) < 1)
|
2024-07-22 12:43:26 +05:00
|
|
|
continue;
|
|
|
|
|
2023-06-07 13:48:40 +05:00
|
|
|
if (item.Value.Value is string valueString)
|
|
|
|
{
|
2023-06-30 15:48:24 +05:00
|
|
|
var entity = MakeEntity<WitsItemString, string>(record.Id, item.Key, idTelemetry, dateTime, valueString);
|
2023-09-20 17:50:44 +05:00
|
|
|
strings.Add(entity);
|
2023-06-07 13:48:40 +05:00
|
|
|
}
|
|
|
|
if (item.Value.Value is float valueFloat)
|
|
|
|
{
|
2023-06-30 15:48:24 +05:00
|
|
|
var entity = MakeEntity<WitsItemFloat, float>(record.Id, item.Key, idTelemetry, dateTime, valueFloat);
|
2023-09-20 17:50:44 +05:00
|
|
|
floats.Add(entity);
|
2023-06-07 13:48:40 +05:00
|
|
|
}
|
|
|
|
if (item.Value.Value is int valueInt)
|
|
|
|
{
|
2023-06-30 15:48:24 +05:00
|
|
|
var entity = MakeEntity<WitsItemInt, int>(record.Id, item.Key, idTelemetry, dateTime, valueInt);
|
2023-09-20 17:50:44 +05:00
|
|
|
ints.Add(entity);
|
2023-06-07 13:48:40 +05:00
|
|
|
}
|
2023-05-18 15:31:27 +05:00
|
|
|
}
|
|
|
|
}
|
2023-06-30 15:48:24 +05:00
|
|
|
|
|
|
|
try
|
|
|
|
{
|
2023-09-20 17:50:44 +05:00
|
|
|
if (strings.Any())
|
|
|
|
await db.Database.ExecInsertOrIgnoreAsync(db.Set<WitsItemString>(), strings, token);
|
|
|
|
|
|
|
|
if (floats.Any())
|
|
|
|
await db.Database.ExecInsertOrIgnoreAsync(db.Set<WitsItemFloat>(), floats, token);
|
|
|
|
|
|
|
|
if (ints.Any())
|
|
|
|
await db.Database.ExecInsertOrIgnoreAsync(db.Set<WitsItemInt>(), ints, token);
|
2023-06-30 15:48:24 +05:00
|
|
|
}
|
2024-07-22 12:43:26 +05:00
|
|
|
catch (Exception ex)
|
2023-06-30 15:48:24 +05:00
|
|
|
{
|
2023-09-20 17:50:44 +05:00
|
|
|
Trace.TraceError("Exception while saving GTR Wits data", ex);
|
2023-06-30 15:48:24 +05:00
|
|
|
}
|
|
|
|
|
|
|
|
cache.AddOrUpdate(idTelemetry,
|
|
|
|
(_) => MakeNewCache(dtos),
|
2024-07-22 12:43:26 +05:00
|
|
|
(_, oldItemsDictionary) =>
|
|
|
|
{
|
2023-06-30 15:48:24 +05:00
|
|
|
foreach (var record in dtos)
|
|
|
|
foreach (var item in record.Items)
|
|
|
|
{
|
|
|
|
oldItemsDictionary.AddOrUpdate(
|
|
|
|
(record.Id, item.Key),
|
|
|
|
(_) => new WitsItemRecordDto
|
|
|
|
{
|
|
|
|
IdRecord = record.Id,
|
|
|
|
IdItem = item.Key,
|
|
|
|
Date = record.Date,
|
|
|
|
Value = item.Value
|
|
|
|
},
|
|
|
|
(_, _) => new WitsItemRecordDto
|
|
|
|
{
|
|
|
|
IdRecord = record.Id,
|
|
|
|
IdItem = item.Key,
|
|
|
|
Date = record.Date,
|
|
|
|
Value = item.Value
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return oldItemsDictionary;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
private static ConcurrentDictionary<(int, int), WitsItemRecordDto> MakeNewCache(IEnumerable<WitsRecordDto> dtos)
|
|
|
|
{
|
|
|
|
var items = dtos.SelectMany(record =>
|
|
|
|
record.Items.Select(
|
2024-07-22 12:43:26 +05:00
|
|
|
item => new WitsItemRecordDto
|
|
|
|
{
|
2023-06-30 15:48:24 +05:00
|
|
|
IdItem = item.Key,
|
|
|
|
IdRecord = record.Id,
|
|
|
|
Date = record.Date,
|
|
|
|
Value = item.Value,
|
|
|
|
}));
|
|
|
|
|
|
|
|
var groups = items
|
|
|
|
.GroupBy(item => (item.IdRecord, item.IdItem));
|
|
|
|
|
|
|
|
var pairs = groups.Select(group => new KeyValuePair<(int, int), WitsItemRecordDto>(
|
2024-07-22 12:43:26 +05:00
|
|
|
group.Key,
|
2023-06-30 15:48:24 +05:00
|
|
|
group.OrderByDescending(item => item.Date).First()));
|
|
|
|
|
|
|
|
return new ConcurrentDictionary<(int, int), WitsItemRecordDto>(pairs);
|
2023-04-11 06:34:51 +05:00
|
|
|
}
|
2023-04-11 13:49:51 +05:00
|
|
|
|
2023-04-18 16:07:29 +05:00
|
|
|
private static TEntity MakeEntity<TEntity, TValue>(int idRecord, int idItem, int idTelemetry, DateTimeOffset dateTime, TValue value)
|
|
|
|
where TEntity : WitsItemBase<TValue>, new()
|
2023-05-18 15:31:27 +05:00
|
|
|
where TValue : notnull
|
|
|
|
=> new TEntity()
|
|
|
|
{
|
|
|
|
IdRecord = idRecord,
|
|
|
|
IdItem = idItem,
|
|
|
|
IdTelemetry = idTelemetry,
|
|
|
|
DateTime = dateTime,
|
|
|
|
Value = value,
|
|
|
|
};
|
2023-04-18 16:07:29 +05:00
|
|
|
|
2023-06-30 15:48:24 +05:00
|
|
|
private static TEntity MakeEntity<TEntity, TValue>(WitsItemRecordDto dto, int idTelemetry, DateTimeOffset dateTime)
|
|
|
|
where TEntity : WitsItemBase<TValue>, new()
|
|
|
|
where TValue : notnull
|
|
|
|
=> new TEntity()
|
|
|
|
{
|
|
|
|
IdRecord = dto.IdRecord,
|
|
|
|
IdItem = dto.IdItem,
|
|
|
|
IdTelemetry = idTelemetry,
|
|
|
|
DateTime = dateTime,
|
|
|
|
Value = (TValue)dto.Value.Value,
|
|
|
|
};
|
|
|
|
|
2024-07-22 12:43:26 +05:00
|
|
|
private IQueryable<TEntity> BuildQuery<TEntity, TValue>(TelemetryPartDeleteRequest request)
|
|
|
|
where TEntity : WitsItemBase<TValue>
|
|
|
|
where TValue : notnull
|
|
|
|
{
|
|
|
|
var query = db.Set<TEntity>().Where(i => i.IdTelemetry == request.IdTelemetry);
|
|
|
|
|
2024-07-23 17:26:23 +05:00
|
|
|
if (request.LeDate is not null)
|
2024-07-22 12:43:26 +05:00
|
|
|
{
|
2024-07-23 17:26:23 +05:00
|
|
|
var leDate = request.LeDate.Value.ToUniversalTime();
|
|
|
|
query = query.Where(o => o.DateTime <= leDate);
|
2024-07-22 12:43:26 +05:00
|
|
|
}
|
|
|
|
|
2024-07-23 17:26:23 +05:00
|
|
|
if (request.GeDate is not null)
|
2024-07-22 12:43:26 +05:00
|
|
|
{
|
2024-07-23 17:26:23 +05:00
|
|
|
var geDate = request.GeDate.Value.ToUniversalTime();
|
|
|
|
query = query.Where(o => o.DateTime >= geDate);
|
2024-07-22 12:43:26 +05:00
|
|
|
}
|
|
|
|
|
|
|
|
return query;
|
|
|
|
}
|
|
|
|
|
2024-07-23 17:26:23 +05:00
|
|
|
public async Task<int> DeleteAsync(TelemetryPartDeleteRequest request, CancellationToken token)
|
2024-07-22 12:43:26 +05:00
|
|
|
{
|
2024-07-23 17:26:23 +05:00
|
|
|
var result = 0;
|
|
|
|
result += await DeleteAsync<WitsItemFloat, float>(request, token);
|
|
|
|
result += await DeleteAsync<WitsItemInt, int>(request, token);
|
|
|
|
result += await DeleteAsync<WitsItemString, string>(request, token);
|
|
|
|
|
|
|
|
return result;
|
2024-07-22 12:43:26 +05:00
|
|
|
}
|
|
|
|
|
|
|
|
private async Task<int> DeleteAsync<TEntity, TType>(TelemetryPartDeleteRequest request, CancellationToken token)
|
|
|
|
where TEntity : WitsItemBase<TType>
|
|
|
|
where TType : notnull
|
|
|
|
{
|
|
|
|
var query = BuildQuery<TEntity, TType>(request);
|
|
|
|
db.Set<TEntity>().RemoveRange(query);
|
|
|
|
return await db.SaveChangesAsync(token);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-05-18 15:31:27 +05:00
|
|
|
private class WitsRequest
|
|
|
|
{
|
|
|
|
public int IdTelemetry { get; set; }
|
|
|
|
public DateTimeOffset? DateBeginUtc { get; set; }
|
|
|
|
public DateTimeOffset? DateEnd { get; set; }
|
|
|
|
public int? ApproxPointsCount { get; set; }
|
|
|
|
public double TimezoneHours { get; set; }
|
|
|
|
public int? IdRecord { get; set; }
|
|
|
|
}
|
|
|
|
}
|
2023-04-04 21:21:06 +05:00
|
|
|
}
|