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

131 lines
5.0 KiB
C#
Raw Normal View History

2021-04-07 18:01:56 +05:00
using AsbCloudApp.Data;
using AsbCloudApp.Services;
using AsbCloudDb.Model;
using AsbCloudInfrastructure.Services.Cache;
2021-07-28 09:46:58 +05:00
using Mapster;
using Microsoft.EntityFrameworkCore;
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
{
//TODO: delete this
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 ITelemetryAnalyticsService analyticsService;
private readonly CacheTable<Telemetry> cacheTelemetry;
2021-09-03 11:35:49 +05:00
private readonly CacheTable<TelemetryUser> cacheTelemetryUsers;
private readonly CacheTable<Well> cacheWells;
2021-04-07 18:01:56 +05:00
public DataService(IAsbCloudDbContext db, ITelemetryService telemetryService,
ITelemetryAnalyticsService 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);
2021-09-03 11:35:49 +05:00
cacheTelemetryUsers = cacheDb.GetCachedTable<TelemetryUser>((AsbCloudDbContext)db);
2021-04-07 18:01:56 +05:00
cacheWells = cacheDb.GetCachedTable<Well>((AsbCloudDbContext)db);
}
public async Task<IEnumerable<TelemetryDataSaubDto>> 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);
var query = from data in db.TelemetryDataSaub
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)
.ConfigureAwait(false);
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).ConfigureAwait(false);
2021-04-23 10:21:25 +05:00
var dtos = entities.Adapt<TelemetryDataSaubDto>();
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<int> UpdateDataAsync(string uid, IEnumerable<TelemetryDataSaubDto> dtos,
CancellationToken token = default)
2021-04-23 10:21:25 +05:00
{
if (dtos == default || !dtos.Any())
return 0;
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.TelemetryDataSaub
where d.IdTelemetry == telemetryId
&& d.Date > dtoMinDate
&& d.Date < dtoMaxDate
select d).AsNoTracking()
.ToListAsync(token)
.ConfigureAwait(false);
if (oldDataSaubBase.Any())
db.TelemetryDataSaub.RemoveRange(oldDataSaubBase);
foreach (var dto in dtos)
2021-04-23 10:21:25 +05:00
{
var dataSaub = dto.Adapt<TelemetryDataSaub>();
2021-09-03 11:35:49 +05:00
var telemetryUser = await cacheTelemetryUsers
.FirstOrDefaultAsync(u => u.IdTelemetry == telemetryId && (u.Name == dto.User || u.Surname == dto.User), token)
.ConfigureAwait(false);
dataSaub.IdUser = telemetryUser?.IdUser;
dataSaub.IdTelemetry = telemetryId;
db.TelemetryDataSaub.Add(dataSaub);
dto.IdTelemetry = telemetryId;
analyticsService.SaveAnalytics(dto);
2021-04-23 10:21:25 +05:00
}
return await db.SaveChangesAsync(token);
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<TelemetryDataSaub>((int)telemetryId, token)
.ConfigureAwait(false);
2021-07-21 15:29:19 +05:00
return new DatesRangeDto { From = From, To = To };
}
2021-04-07 18:01:56 +05:00
}
}