2021-04-07 18:01:56 +05:00
|
|
|
|
using AsbCloudApp.Data;
|
|
|
|
|
using AsbCloudApp.Services;
|
|
|
|
|
using AsbCloudDb.Model;
|
|
|
|
|
using AsbCloudInfrastructure.Services.Cache;
|
|
|
|
|
using AutoMapper;
|
|
|
|
|
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 IMapper mapper;
|
|
|
|
|
private readonly ICacheTable<Telemetry> cacheTelemetry;
|
|
|
|
|
private readonly ICacheTable<Well> cacheWells;
|
2021-04-07 18:01:56 +05:00
|
|
|
|
|
2021-04-23 10:21:25 +05:00
|
|
|
|
public DataService(IAsbCloudDbContext db, ITelemetryService telemetryService, CacheDb cacheDb, MapperConfiguration mapperConfiguration)
|
2021-04-07 18:01:56 +05:00
|
|
|
|
{
|
|
|
|
|
this.db = db;
|
2021-04-23 10:21:25 +05:00
|
|
|
|
this.telemetryService = telemetryService;
|
2021-04-07 18:01:56 +05:00
|
|
|
|
mapper = mapperConfiguration.CreateMapper();
|
|
|
|
|
cacheTelemetry = cacheDb.GetCachedTable<Telemetry>((AsbCloudDbContext)db);
|
|
|
|
|
cacheWells = cacheDb.GetCachedTable<Well>((AsbCloudDbContext)db);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public IEnumerable<DataSaubBaseDto> Get(int wellId, DateTime dateBegin = default, double intervalSec = 600d, int approxPointsCount = 1024)
|
|
|
|
|
{
|
|
|
|
|
var well = cacheWells.FirstOrDefault(w => w.Id == wellId);
|
|
|
|
|
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 fullData = from data in db.DataSaubBases
|
|
|
|
|
where data.IdTelemetry == telemetry.Id
|
|
|
|
|
&& data.Date >= dateBegin && data.Date < datEnd
|
|
|
|
|
select data;
|
|
|
|
|
|
|
|
|
|
var fullDataCount = fullData.Count();
|
|
|
|
|
|
|
|
|
|
if (fullDataCount == 0)
|
|
|
|
|
return default;
|
|
|
|
|
|
|
|
|
|
if (fullDataCount > 1.2 * approxPointsCount)
|
|
|
|
|
{
|
|
|
|
|
var m = approxPointsCount / fullDataCount;
|
|
|
|
|
fullData = fullData.Where(d => d.Id % m == 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var dbData = fullData.ToList();
|
|
|
|
|
var result = new List<DataSaubBaseDto>(dbData.Count);
|
2021-04-23 10:21:25 +05:00
|
|
|
|
|
2021-04-07 18:01:56 +05:00
|
|
|
|
foreach (var item in dbData)
|
|
|
|
|
result.Add(mapper.Map<DataSaubBaseDto>(item));
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
2021-04-23 10:21:25 +05:00
|
|
|
|
|
|
|
|
|
public void UpdateData(string uid, IEnumerable<DataSaubBaseDto> dtos)
|
|
|
|
|
{
|
2021-05-13 12:24:21 +05:00
|
|
|
|
if (dtos == default || dtos.Count() <= 0)
|
|
|
|
|
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
|
|
|
|
|
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
|
|
|
|
|
2021-05-13 12:24:21 +05:00
|
|
|
|
foreach (var item in dtos)
|
2021-04-23 10:21:25 +05:00
|
|
|
|
{
|
2021-05-13 12:24:21 +05:00
|
|
|
|
var dataSaub = mapper.Map<DataSaubBase>(item);
|
|
|
|
|
dataSaub.IdTelemetry = telemetryId;
|
|
|
|
|
db.DataSaubBases.Add(dataSaub);
|
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-04-07 18:01:56 +05:00
|
|
|
|
}
|
|
|
|
|
}
|