DD.WellWorkover.Cloud/AsbCloudInfrastructure/Services/DataService.cs

120 lines
4.2 KiB
C#
Raw Normal View History

2021-04-07 18:01:56 +05:00
using AsbCloudApp.Data;
using AsbCloudApp.Services;
using AsbCloudDb.Model;
using Microsoft.EntityFrameworkCore;
2021-04-07 18:01:56 +05:00
using AsbCloudInfrastructure.Services.Cache;
2021-07-28 09:46:58 +05:00
using Mapster;
2021-04-07 18:01:56 +05:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
2021-04-07 18:01:56 +05:00
namespace AsbCloudInfrastructure.Services
{
2021-04-23 10:21:25 +05:00
public class DataService : IDataService
2021-04-07 18:01:56 +05:00
{
2021-04-23 10:21:25 +05:00
private readonly IAsbCloudDbContext db;
private readonly ITelemetryService telemetryService;
private readonly IAnalyticsService analyticsService;
private readonly CacheTable<Telemetry> cacheTelemetry;
private readonly CacheTable<Well> cacheWells;
2021-04-07 18:01:56 +05:00
public DataService(IAsbCloudDbContext db, ITelemetryService telemetryService,
2021-07-28 09:46:58 +05:00
IAnalyticsService analyticsService, CacheDb cacheDb)
2021-04-07 18:01:56 +05:00
{
this.db = db;
2021-04-23 10:21:25 +05:00
this.telemetryService = telemetryService;
this.analyticsService = analyticsService;
2021-04-07 18:01:56 +05:00
cacheTelemetry = cacheDb.GetCachedTable<Telemetry>((AsbCloudDbContext)db);
cacheWells = cacheDb.GetCachedTable<Well>((AsbCloudDbContext)db);
}
public async Task<IEnumerable<DataSaubBaseDto>> GetAsync(int idWell,
DateTime dateBegin = default, double intervalSec = 600d,
int approxPointsCount = 1024, CancellationToken token = default)
2021-04-07 18:01:56 +05:00
{
2021-07-27 14:43:30 +05:00
var well = cacheWells.FirstOrDefault(w => w.Id == idWell);
2021-04-07 18:01:56 +05:00
if (well is null)
return default;
2021-04-23 10:21:25 +05:00
2021-04-07 18:01:56 +05:00
var telemetry = cacheTelemetry.FirstOrDefault(t => t.Id == well.IdTelemetry);
if (telemetry is null)
return default;
2021-04-23 10:21:25 +05:00
if (dateBegin == default)
2021-04-07 18:01:56 +05:00
dateBegin = DateTime.Now.AddSeconds(-intervalSec);
var datEnd = dateBegin.AddSeconds(intervalSec);
2021-07-28 09:46:58 +05:00
var query = from data in db.DataSaubBases
where data.IdTelemetry == telemetry.Id
&& data.Date >= dateBegin && data.Date < datEnd
select data;
2021-04-07 18:01:56 +05:00
var fullDataCount = await query.CountAsync(token);
2021-04-07 18:01:56 +05:00
if (fullDataCount == 0)
return default;
if (fullDataCount > 1.75 * approxPointsCount)
2021-04-07 18:01:56 +05:00
{
2021-07-21 15:29:19 +05:00
var m = (int)Math.Round(1d * fullDataCount / approxPointsCount);
if (m > 1)
2021-07-28 09:46:58 +05:00
query = query.Where(d => d.Id % m == 0);
2021-04-07 18:01:56 +05:00
}
var entities = await query.AsNoTracking().ToListAsync(token);
2021-04-23 10:21:25 +05:00
2021-07-28 09:46:58 +05:00
var dtos = entities.Adapt<DataSaubBaseDto>();
2021-04-07 18:01:56 +05:00
2021-07-28 09:46:58 +05:00
return dtos;
2021-04-07 18:01:56 +05:00
}
2021-04-23 10:21:25 +05:00
public async Task UpdateDataAsync(string uid, IEnumerable<DataSaubBaseDto> dtos,
CancellationToken token = default)
2021-04-23 10:21:25 +05:00
{
if (dtos == default || !dtos.Any())
return;
2021-04-23 10:21:25 +05:00
var telemetryId = telemetryService.GetOrCreateTemetryIdByUid(uid);
var dtoMinDate = dtos.Min(d => d.Date);
var dtoMaxDate = dtos.Max(d => d.Date);
var oldDataSaubBase = await (from d in db.DataSaubBases
where d.IdTelemetry == telemetryId
&& d.Date > dtoMinDate
&& d.Date < dtoMaxDate
select d).AsNoTracking().ToListAsync(token);
if (oldDataSaubBase.Any())
db.DataSaubBases.RemoveRange(oldDataSaubBase);
2021-04-23 10:21:25 +05:00
foreach (var dto in dtos)
2021-04-23 10:21:25 +05:00
{
var dataSaub = dto.Adapt<DataSaubBase>();
dataSaub.IdTelemetry = telemetryId;
db.DataSaubBases.Add(dataSaub);
dto.IdTelemetry = telemetryId;
analyticsService.SaveAnalytics(dto);
2021-04-23 10:21:25 +05:00
}
db.SaveChanges();
2021-04-23 10:21:25 +05:00
}
public async Task<DatesRangeDto> GetDataDatesRangeAsync(int idWell,
CancellationToken token = default)
{
var telemetryId = telemetryService.GetIdTelemetryByIdWell(idWell);
if (telemetryId is null)
return null;
var (From, To) = await db.GetDatesRangeAsync<DataSaubBase>((int)telemetryId, token);
2021-07-21 15:29:19 +05:00
return new DatesRangeDto { From = From, To = To };
}
2021-04-07 18:01:56 +05:00
}
}