2023-04-04 21:21:06 +05:00
|
|
|
|
using AsbCloudApp.Data.GTR;
|
2023-04-07 04:51:30 +05:00
|
|
|
|
using AsbCloudApp.Repositories;
|
2023-04-04 21:21:06 +05:00
|
|
|
|
using AsbCloudApp.Services;
|
|
|
|
|
using AsbCloudDb.Model;
|
2023-04-06 04:02:47 +05:00
|
|
|
|
using AsbCloudDb.Model.GTR;
|
2023-04-11 13:27:42 +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;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace AsbCloudInfrastructure.Repository
|
|
|
|
|
{
|
2023-04-18 16:22:53 +05:00
|
|
|
|
|
2023-04-11 06:34:51 +05:00
|
|
|
|
public class GtrWitsRepository : IGtrRepository
|
2023-04-04 21:21:06 +05:00
|
|
|
|
{
|
|
|
|
|
private readonly IAsbCloudDbContext db;
|
|
|
|
|
private readonly ITelemetryService telemetryService;
|
|
|
|
|
|
|
|
|
|
public GtrWitsRepository(
|
|
|
|
|
IAsbCloudDbContext db,
|
|
|
|
|
ITelemetryService telemetryService)
|
|
|
|
|
{
|
2023-04-11 00:32:06 +05:00
|
|
|
|
|
2023-04-04 21:21:06 +05:00
|
|
|
|
this.db = db;
|
|
|
|
|
this.telemetryService = telemetryService;
|
|
|
|
|
}
|
|
|
|
|
|
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-04-18 16:07:29 +05:00
|
|
|
|
DateTimeOffset? dateBeginUtc = dateBegin?.ToUtcDateTimeOffset(timezone.Hours);
|
|
|
|
|
var dateEnd = dateBeginUtc?.AddSeconds(intervalSec);
|
|
|
|
|
|
|
|
|
|
var recordAllInt = await GetItemsOrDefaultAsync<WitsItemInt, int>(telemetry.Id, dateBeginUtc, dateEnd, approxPointsCount, timezone.Hours, token);
|
|
|
|
|
var recordAllFloat = await GetItemsOrDefaultAsync<WitsItemFloat, float>(telemetry.Id, dateBeginUtc, dateEnd, approxPointsCount,timezone.Hours, token);
|
|
|
|
|
var recordAllString = await GetItemsOrDefaultAsync<WitsItemString, string>(telemetry.Id, dateBeginUtc, dateEnd, approxPointsCount, timezone.Hours, 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,
|
|
|
|
|
Items = g.Select(r => new {
|
|
|
|
|
Key = r.IdItem,
|
|
|
|
|
Value = r.Item
|
|
|
|
|
}).ToDictionary(x => x.Key, x => x.Value)
|
|
|
|
|
});
|
2023-04-11 00:32:06 +05:00
|
|
|
|
return dtos;
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-18 16:07:29 +05:00
|
|
|
|
private async Task<IEnumerable<ItemRecord>> GetItemsOrDefaultAsync<TEntity, TValue>(
|
|
|
|
|
int idTelemetry,
|
|
|
|
|
DateTimeOffset? dateBegin,
|
|
|
|
|
DateTimeOffset? dateEnd,
|
|
|
|
|
int approxPointsCount,
|
|
|
|
|
double timezoneHours,
|
|
|
|
|
CancellationToken token)
|
|
|
|
|
where TEntity: WitsItemBase<TValue>
|
|
|
|
|
where TValue: notnull
|
2023-04-11 00:32:06 +05:00
|
|
|
|
{
|
2023-04-18 16:07:29 +05:00
|
|
|
|
var query = db.Set<TEntity>()
|
|
|
|
|
.Where(i => i.IdTelemetry == idTelemetry);
|
2023-04-11 00:32:06 +05:00
|
|
|
|
|
2023-04-18 16:07:29 +05:00
|
|
|
|
if (dateBegin is not null)
|
|
|
|
|
query = query
|
|
|
|
|
.Where(d => d.DateTime >= dateBegin);
|
2023-04-04 21:21:06 +05:00
|
|
|
|
|
2023-04-18 16:07:29 +05:00
|
|
|
|
if (dateEnd is not null)
|
|
|
|
|
query = query
|
|
|
|
|
.Where(d => d.DateTime <= dateEnd);
|
|
|
|
|
|
|
|
|
|
var fullDataCount = await query.CountAsync(token);
|
2023-04-04 21:21:06 +05:00
|
|
|
|
|
2023-04-17 20:59:27 +05:00
|
|
|
|
if (fullDataCount == 0)
|
|
|
|
|
return Enumerable.Empty<ItemRecord>();
|
2023-04-11 00:32:06 +05:00
|
|
|
|
|
2023-04-17 20:59:27 +05:00
|
|
|
|
if (fullDataCount > 1.75 * approxPointsCount)
|
|
|
|
|
{
|
|
|
|
|
var m = (int)Math.Round(1d * fullDataCount / approxPointsCount);
|
|
|
|
|
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
|
|
|
|
|
.OrderBy(d => d.DateTime)
|
|
|
|
|
.AsNoTracking()
|
|
|
|
|
.ToListAsync(token)
|
|
|
|
|
.ConfigureAwait(false);
|
2023-04-14 00:40:38 +05:00
|
|
|
|
|
2023-04-14 00:36:11 +05:00
|
|
|
|
var items = entities.Select(e => new ItemRecord
|
|
|
|
|
{
|
|
|
|
|
IdRecord = e.IdRecord,
|
|
|
|
|
IdTelemetry = e.IdTelemetry,
|
|
|
|
|
Date = e.DateTime.ToRemoteDateTime(timezoneHours),
|
|
|
|
|
IdItem = e.IdItem,
|
|
|
|
|
Item = new JsonValue(e.Value)
|
|
|
|
|
});
|
|
|
|
|
return items;
|
2023-04-04 21:21:06 +05:00
|
|
|
|
}
|
2023-04-18 16:07:29 +05:00
|
|
|
|
|
|
|
|
|
public async Task SaveDataAsync(int idTelemetry, WitsRecordDto dto, CancellationToken token)
|
2023-04-11 06:34:51 +05:00
|
|
|
|
{
|
2023-04-18 16:07:29 +05:00
|
|
|
|
var timezoneHours = telemetryService.GetTimezone(idTelemetry).Hours;
|
|
|
|
|
foreach (var item in dto.Items)
|
|
|
|
|
{
|
|
|
|
|
var dateTime = dto.Date.ToUtcDateTimeOffset(timezoneHours);
|
|
|
|
|
if (item.Value.Value is string valueString)
|
|
|
|
|
{
|
|
|
|
|
var entity = MakeEntity<WitsItemString, string>( dto.Id, item.Key, idTelemetry, dateTime, valueString);
|
|
|
|
|
db.WitsItemString.Add(entity);
|
|
|
|
|
}
|
|
|
|
|
if (item.Value.Value is float valueFloat)
|
|
|
|
|
{
|
|
|
|
|
var entity = MakeEntity<WitsItemFloat, float>(dto.Id, item.Key, idTelemetry, dateTime, valueFloat);
|
|
|
|
|
db.WitsItemFloat.Add(entity);
|
|
|
|
|
}
|
|
|
|
|
if (item.Value.Value is int valueInt)
|
|
|
|
|
{
|
|
|
|
|
var entity = MakeEntity<WitsItemInt, int>(dto.Id, item.Key, idTelemetry, dateTime, valueInt);
|
|
|
|
|
db.WitsItemInt.Add(entity);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
await db.SaveChangesAsync(token);
|
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()
|
|
|
|
|
where TValue: notnull
|
|
|
|
|
=> new TEntity() {
|
|
|
|
|
IdRecord = idRecord,
|
|
|
|
|
IdItem = idItem,
|
|
|
|
|
IdTelemetry = idTelemetry,
|
|
|
|
|
DateTime = dateTime,
|
|
|
|
|
Value = value,
|
|
|
|
|
};
|
|
|
|
|
|
2023-04-14 00:36:11 +05:00
|
|
|
|
internal class ItemRecord
|
2023-04-11 13:49:51 +05:00
|
|
|
|
{
|
2023-04-14 00:36:11 +05:00
|
|
|
|
public int IdRecord { get; set; }
|
|
|
|
|
public int IdTelemetry { get; set; }
|
|
|
|
|
public DateTime Date { get; set; }
|
|
|
|
|
public int IdItem { get; set; }
|
2023-04-17 18:02:36 +05:00
|
|
|
|
public JsonValue Item { get; set; } = default!;
|
2023-04-14 00:36:11 +05:00
|
|
|
|
}
|
2023-04-04 21:21:06 +05:00
|
|
|
|
}
|
2023-04-18 16:22:53 +05:00
|
|
|
|
|
2023-04-04 21:21:06 +05:00
|
|
|
|
}
|