forked from ddrilling/AsbCloudServer
Получение данных ГТИ по id record
This commit is contained in:
parent
8d1ae1af84
commit
7514e3132a
@ -36,13 +36,13 @@ namespace AsbCloudApp.Repositories
|
||||
int approxPointsCount = 1024, CancellationToken token = default);
|
||||
|
||||
/// <summary>
|
||||
/// получение данных по record id
|
||||
/// получение данных ГТИ по record id
|
||||
/// </summary>
|
||||
/// <param name="idWell"></param>
|
||||
/// <param name="idRecord"></param>
|
||||
/// <param name="token"></param>
|
||||
/// <returns></returns>
|
||||
Task<IEnumerable<WitsItemRecordDto>> GetByRecordAsync(int idWell, int idRecord, CancellationToken token = default);
|
||||
Task<IEnumerable<WitsItemRecordDto>> GetDataByRecordIdAsync(int idWell, int idRecord, CancellationToken token = default);
|
||||
}
|
||||
#nullable disable
|
||||
}
|
||||
|
@ -3,7 +3,6 @@ using AsbCloudApp.Repositories;
|
||||
using AsbCloudApp.Services;
|
||||
using AsbCloudDb.Model;
|
||||
using AsbCloudDb.Model.GTR;
|
||||
using Mapster;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
@ -23,7 +22,7 @@ namespace AsbCloudInfrastructure.Repository
|
||||
IAsbCloudDbContext db,
|
||||
ITelemetryService telemetryService)
|
||||
{
|
||||
|
||||
|
||||
this.db = db;
|
||||
this.telemetryService = telemetryService;
|
||||
}
|
||||
@ -36,12 +35,21 @@ namespace AsbCloudInfrastructure.Repository
|
||||
|
||||
var timezone = telemetryService.GetTimezone(telemetry.Id);
|
||||
|
||||
DateTimeOffset? dateBeginUtc = dateBegin?.ToUtcDateTimeOffset(timezone.Hours);
|
||||
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);
|
||||
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);
|
||||
|
||||
var dtos = (recordAllFloat.Union(recordAllInt)).Union(recordAllString)
|
||||
.GroupBy(g => new
|
||||
@ -53,15 +61,16 @@ namespace AsbCloudInfrastructure.Repository
|
||||
{
|
||||
Id = g.Key.IdRecord,
|
||||
Date = g.Key.Date,
|
||||
Items = g.Select(r => new {
|
||||
Key = r.IdItem,
|
||||
Value = r.Item
|
||||
Items = g.Select(r => new
|
||||
{
|
||||
Key = r.IdItem,
|
||||
Value = r.Item
|
||||
}).ToDictionary(x => x.Key, x => x.Value)
|
||||
});
|
||||
});
|
||||
return dtos;
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<WitsItemRecordDto>> GetByRecordAsync(int idWell, int idRecord, CancellationToken token = default)
|
||||
public async Task<IEnumerable<WitsItemRecordDto>> GetDataByRecordIdAsync(int idWell, int idRecord, CancellationToken token = default)
|
||||
{
|
||||
var telemetry = telemetryService.GetOrDefaultTelemetryByIdWell(idWell);
|
||||
if (telemetry is null)
|
||||
@ -69,9 +78,17 @@ namespace AsbCloudInfrastructure.Repository
|
||||
|
||||
var timezone = telemetryService.GetTimezone(telemetry.Id);
|
||||
|
||||
var recordAllInt = await GetItemsOrDefaultByRecordAsync<WitsItemInt, int>(telemetry.Id, idRecord, timezone.Hours, token);
|
||||
var recordAllFloat = await GetItemsOrDefaultByRecordAsync<WitsItemFloat, float>(telemetry.Id, idRecord, timezone.Hours, token);
|
||||
var recordAllString = await GetItemsOrDefaultByRecordAsync<WitsItemString, string>(telemetry.Id, idRecord, timezone.Hours, token);
|
||||
var witsRequest = new WitsRequest()
|
||||
{
|
||||
IdTelemetry = telemetry.Id,
|
||||
TimezoneHours = timezone.Hours,
|
||||
IdRecord = idRecord,
|
||||
};
|
||||
|
||||
var recordAllInt = await GetItemsOrDefaultAsync<WitsItemInt, int>(witsRequest, token);
|
||||
var recordAllFloat = await GetItemsOrDefaultAsync<WitsItemFloat, float>(witsRequest, token);
|
||||
var recordAllString = await GetItemsOrDefaultAsync<WitsItemString, string>(witsRequest, token);
|
||||
|
||||
|
||||
var dtos = (recordAllFloat.Union(recordAllInt)).Union(recordAllString)
|
||||
.GroupBy(g => new
|
||||
@ -85,95 +102,68 @@ namespace AsbCloudInfrastructure.Repository
|
||||
IdRecord = g.Key.IdRecord,
|
||||
IdItem = g.Key.IdItem,
|
||||
Date = g.MaxBy(i => i.Date)!.Date,
|
||||
Value = g.MaxBy(i => i.Date)!.Item.Value,
|
||||
Value = g.MaxBy(i => i.Date)!.Item.Value,
|
||||
}).ToArray();
|
||||
return dtos;
|
||||
}
|
||||
|
||||
private async Task<IEnumerable<ItemRecord>> GetItemsOrDefaultAsync<TEntity, TValue>(
|
||||
int idTelemetry,
|
||||
DateTimeOffset? dateBegin,
|
||||
DateTimeOffset? dateEnd,
|
||||
int approxPointsCount,
|
||||
double timezoneHours,
|
||||
WitsRequest request,
|
||||
CancellationToken token)
|
||||
where TEntity: WitsItemBase<TValue>
|
||||
where TValue: notnull
|
||||
where TEntity : WitsItemBase<TValue>
|
||||
where TValue : notnull
|
||||
{
|
||||
var query = db.Set<TEntity>()
|
||||
.Where(i => i.IdTelemetry == idTelemetry);
|
||||
|
||||
if (dateBegin is not null)
|
||||
query = query
|
||||
.Where(d => d.DateTime >= dateBegin);
|
||||
|
||||
if (dateEnd is not null)
|
||||
query = query
|
||||
.Where(d => d.DateTime <= dateEnd);
|
||||
var query = BuildQuery<TEntity, TValue>(request, token);
|
||||
|
||||
var fullDataCount = await query.CountAsync(token);
|
||||
|
||||
if (fullDataCount == 0)
|
||||
return Enumerable.Empty<ItemRecord>();
|
||||
|
||||
if (fullDataCount > 1.75 * approxPointsCount)
|
||||
if (request.ApproxPointsCount is not null && fullDataCount > 1.75 * request.ApproxPointsCount)
|
||||
{
|
||||
var m = (int)Math.Round(1d * fullDataCount / approxPointsCount);
|
||||
var m = (int)Math.Round(1d * fullDataCount / request.ApproxPointsCount!.Value);
|
||||
if (m > 1)
|
||||
query = query.Where((d) => (((d.DateTime.DayOfYear * 24 + d.DateTime.Hour) * 60 + d.DateTime.Minute) * 60 + d.DateTime.Second) % m == 0);
|
||||
}
|
||||
|
||||
var entities = await query
|
||||
.OrderBy(d => d.DateTime)
|
||||
.AsNoTracking()
|
||||
.ToListAsync(token)
|
||||
.ConfigureAwait(false);
|
||||
|
||||
|
||||
var items = entities.Select(e => new ItemRecord
|
||||
{
|
||||
IdRecord = e.IdRecord,
|
||||
IdTelemetry = e.IdTelemetry,
|
||||
Date = e.DateTime.ToRemoteDateTime(timezoneHours),
|
||||
Date = e.DateTime.ToRemoteDateTime(request.TimezoneHours),
|
||||
IdItem = e.IdItem,
|
||||
Item = new JsonValue(e.Value)
|
||||
});
|
||||
return items;
|
||||
}
|
||||
|
||||
private async Task<IEnumerable<ItemRecord>> GetItemsOrDefaultByRecordAsync<TEntity, TValue>(
|
||||
int idTelemetry,
|
||||
int idRecord,
|
||||
double timezoneHours,
|
||||
CancellationToken token)
|
||||
private IQueryable<TEntity> BuildQuery<TEntity, TValue>(WitsRequest request, CancellationToken token)
|
||||
where TEntity : WitsItemBase<TValue>
|
||||
where TValue : notnull
|
||||
{
|
||||
var query = db.Set<TEntity>()
|
||||
.Where(i => i.IdTelemetry == idTelemetry)
|
||||
.Where(i => i.IdRecord == idRecord);
|
||||
var query = db.Set<TEntity>().Where(i => i.IdTelemetry == request.IdTelemetry);
|
||||
|
||||
var fullDataCount = await query.CountAsync(token);
|
||||
if (request.IdRecord is not null)
|
||||
query = query
|
||||
.Where(d => d.IdRecord == request.IdRecord);
|
||||
|
||||
if (fullDataCount == 0)
|
||||
return Enumerable.Empty<ItemRecord>();
|
||||
if (request.DateBeginUtc is not null)
|
||||
query = query
|
||||
.Where(d => d.DateTime >= request.DateBeginUtc);
|
||||
|
||||
var entities = await query
|
||||
.OrderBy(d => d.DateTime)
|
||||
.AsNoTracking()
|
||||
.ToListAsync(token)
|
||||
.ConfigureAwait(false);
|
||||
if (request.DateEnd is not null)
|
||||
query = query
|
||||
.Where(d => d.DateTime <= request.DateEnd);
|
||||
|
||||
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;
|
||||
return query.OrderBy(d => d.DateTime);
|
||||
}
|
||||
|
||||
|
||||
public async Task SaveDataAsync(int idTelemetry, WitsRecordDto dto, CancellationToken token)
|
||||
{
|
||||
var timezoneHours = telemetryService.GetTimezone(idTelemetry).Hours;
|
||||
@ -182,7 +172,7 @@ namespace AsbCloudInfrastructure.Repository
|
||||
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);
|
||||
var entity = MakeEntity<WitsItemString, string>(dto.Id, item.Key, idTelemetry, dateTime, valueString);
|
||||
db.WitsItemString.Add(entity);
|
||||
}
|
||||
if (item.Value.Value is float valueFloat)
|
||||
@ -194,30 +184,40 @@ namespace AsbCloudInfrastructure.Repository
|
||||
{
|
||||
var entity = MakeEntity<WitsItemInt, int>(dto.Id, item.Key, idTelemetry, dateTime, valueInt);
|
||||
db.WitsItemInt.Add(entity);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
await db.SaveChangesAsync(token);
|
||||
}
|
||||
|
||||
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,
|
||||
};
|
||||
where TValue : notnull
|
||||
=> new TEntity()
|
||||
{
|
||||
IdRecord = idRecord,
|
||||
IdItem = idItem,
|
||||
IdTelemetry = idTelemetry,
|
||||
DateTime = dateTime,
|
||||
Value = value,
|
||||
};
|
||||
|
||||
internal class ItemRecord
|
||||
{
|
||||
public int IdRecord { get; set; }
|
||||
public int IdRecord { get; set; }
|
||||
public int IdTelemetry { get; set; }
|
||||
public DateTime Date { get; set; }
|
||||
public int IdItem { get; set; }
|
||||
public JsonValue Item { get; set; } = default!;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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; }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -68,9 +68,16 @@ namespace AsbCloudWebApi.Controllers.SAUB
|
||||
return Ok(content);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// получение данных ГТИ по ключу record
|
||||
/// </summary>
|
||||
/// <param name="idWell">id скважины</param>
|
||||
/// <param name="idRecord">id record</param>
|
||||
/// <param name="token"></param>
|
||||
/// <returns></returns>
|
||||
[HttpGet("{idWell}/{idRecord}")]
|
||||
[Permission]
|
||||
public async Task<ActionResult<IEnumerable<WitsItemRecordDto>>> GetDataByRecordAsync(int idWell, int idRecord, CancellationToken token = default)
|
||||
public async Task<ActionResult<IEnumerable<WitsItemRecordDto>>> GetDataByRecordIdAsync(int idWell, int idRecord, CancellationToken token = default)
|
||||
{
|
||||
int? idCompany = User.GetCompanyId();
|
||||
|
||||
@ -83,7 +90,7 @@ namespace AsbCloudWebApi.Controllers.SAUB
|
||||
if (!isCompanyOwnsWell)
|
||||
return Forbid();
|
||||
|
||||
var content = await gtrRepository.GetByRecordAsync(idWell, idRecord, token).ConfigureAwait(false);
|
||||
var content = await gtrRepository.GetDataByRecordIdAsync(idWell, idRecord, token).ConfigureAwait(false);
|
||||
|
||||
return Ok(content);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user