forked from ddrilling/AsbCloudServer
69 lines
2.3 KiB
C#
69 lines
2.3 KiB
C#
using AsbCloudApp.Data;
|
|
using AsbCloudApp.Services;
|
|
using AsbCloudDb.Model;
|
|
using AsbCloudInfrastructure.Services.Cache;
|
|
using AutoMapper;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace AsbCloudInfrastructure.Services
|
|
{
|
|
public class TelemetryDataService : ITelemetryDataService
|
|
{
|
|
private IAsbCloudDbContext db;
|
|
private IMapper mapper;
|
|
private ICacheTable<Telemetry> cacheTelemetry;
|
|
private ICacheTable<Well> cacheWells;
|
|
|
|
public TelemetryDataService(IAsbCloudDbContext db, CacheDb cacheDb, MapperConfiguration mapperConfiguration)
|
|
{
|
|
this.db = db;
|
|
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;
|
|
|
|
var telemetry = cacheTelemetry.FirstOrDefault(t => t.Id == well.IdTelemetry);
|
|
if (telemetry is null)
|
|
return default;
|
|
|
|
if(dateBegin == default)
|
|
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);
|
|
|
|
foreach (var item in dbData)
|
|
result.Add(mapper.Map<DataSaubBaseDto>(item));
|
|
|
|
return result;
|
|
}
|
|
}
|
|
}
|