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

113 lines
3.8 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;
2021-04-07 18:01:56 +05:00
using System;
using System.Collections.Generic;
using System.Linq;
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);
}
2021-07-27 14:43:30 +05:00
public IEnumerable<DataSaubBaseDto> Get(int idWell, DateTime dateBegin = default, double intervalSec = 600d, int approxPointsCount = 1024)
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
2021-07-28 09:46:58 +05:00
var fullDataCount = query.Count();
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
}
2021-07-28 09:46:58 +05:00
var entities = query.ToList();
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 void UpdateData(string uid, IEnumerable<DataSaubBaseDto> dtos)
{
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 = (from d in db.DataSaubBases
2021-07-21 15:29:19 +05:00
where d.IdTelemetry == telemetryId
&& d.Date > dtoMinDate
&& d.Date < dtoMaxDate
select d).ToList();
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
}
2021-07-27 14:43:30 +05:00
public DatesRangeDto GetDataDatesRange(int idWell)
{
var telemetryId = telemetryService.GetIdTelemetryByIdWell(idWell);
if (telemetryId is null)
return null;
var (From, To) = db.GetDatesRange<DataSaubBase>((int)telemetryId);
2021-07-21 15:29:19 +05:00
return new DatesRangeDto { From = From, To = To };
}
2021-04-07 18:01:56 +05:00
}
}