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;
|
2021-06-24 13:02:31 +05:00
|
|
|
|
private readonly IAnalyticsService analyticsService;
|
2021-05-20 14:14:51 +05:00
|
|
|
|
private readonly CacheTable<Telemetry> cacheTelemetry;
|
|
|
|
|
private readonly CacheTable<Well> cacheWells;
|
2021-04-07 18:01:56 +05:00
|
|
|
|
|
2021-06-24 13:02:31 +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;
|
2021-06-24 13:02:31 +05:00
|
|
|
|
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
|
2021-08-09 15:41:42 +05:00
|
|
|
|
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;
|
|
|
|
|
|
2021-05-28 11:25:07 +05:00
|
|
|
|
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)
|
|
|
|
|
{
|
2021-05-21 11:02:43 +05:00
|
|
|
|
if (dtos == default || !dtos.Any())
|
2021-05-13 12:24:21 +05:00
|
|
|
|
return;
|
|
|
|
|
|
2021-04-23 10:21:25 +05:00
|
|
|
|
var telemetryId = telemetryService.GetOrCreateTemetryIdByUid(uid);
|
2021-05-13 12:24:21 +05:00
|
|
|
|
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();
|
2021-05-13 12:24:21 +05:00
|
|
|
|
|
|
|
|
|
if (oldDataSaubBase.Any())
|
|
|
|
|
db.DataSaubBases.RemoveRange(oldDataSaubBase);
|
2021-08-09 15:41:42 +05:00
|
|
|
|
|
2021-04-23 10:21:25 +05:00
|
|
|
|
|
2021-08-09 14:01:57 +05:00
|
|
|
|
foreach (var dto in dtos)
|
2021-04-23 10:21:25 +05:00
|
|
|
|
{
|
2021-08-09 14:01:57 +05:00
|
|
|
|
var dataSaub = dto.Adapt<DataSaubBase>();
|
2021-05-13 12:24:21 +05:00
|
|
|
|
dataSaub.IdTelemetry = telemetryId;
|
|
|
|
|
db.DataSaubBases.Add(dataSaub);
|
2021-06-24 13:02:31 +05:00
|
|
|
|
|
2021-08-09 14:01:57 +05:00
|
|
|
|
dto.IdTelemetry = telemetryId;
|
|
|
|
|
analyticsService.SaveAnalytics(dto);
|
2021-04-23 10:21:25 +05:00
|
|
|
|
}
|
2021-05-13 12:24:21 +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)
|
2021-05-17 12:53:30 +05:00
|
|
|
|
{
|
2021-08-09 14:01:57 +05:00
|
|
|
|
var telemetryId = telemetryService.GetIdTelemetryByIdWell(idWell);
|
|
|
|
|
if (telemetryId is null)
|
2021-05-17 12:53:30 +05:00
|
|
|
|
return null;
|
|
|
|
|
|
2021-08-09 14:01:57 +05:00
|
|
|
|
var (From, To) = db.GetDatesRange<DataSaubBase>((int)telemetryId);
|
2021-05-17 12:53:30 +05:00
|
|
|
|
|
2021-07-21 15:29:19 +05:00
|
|
|
|
return new DatesRangeDto { From = From, To = To };
|
2021-05-17 12:53:30 +05:00
|
|
|
|
}
|
2021-04-07 18:01:56 +05:00
|
|
|
|
}
|
|
|
|
|
}
|