2022-04-08 13:10:06 +05:00
|
|
|
|
using AsbCloudApp.Services;
|
2022-07-19 10:29:38 +05:00
|
|
|
|
using AsbCloudDb;
|
2022-04-08 13:10:06 +05:00
|
|
|
|
using AsbCloudDb.Model;
|
|
|
|
|
using Mapster;
|
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace AsbCloudInfrastructure.Services
|
|
|
|
|
{
|
|
|
|
|
public class WitsRecordRepository<TDto, TEntity> : IWitsRecordRepository<TDto>
|
|
|
|
|
where TEntity : class, ITelemetryData
|
2022-04-11 18:00:34 +05:00
|
|
|
|
where TDto : AsbCloudApp.Data.ITelemetryData
|
2022-04-08 13:10:06 +05:00
|
|
|
|
{
|
|
|
|
|
private readonly DbSet<TEntity> dbset;
|
|
|
|
|
private readonly IAsbCloudDbContext db;
|
|
|
|
|
private readonly ITelemetryService telemetryService;
|
|
|
|
|
|
|
|
|
|
public WitsRecordRepository(IAsbCloudDbContext db, ITelemetryService telemetryService)
|
|
|
|
|
{
|
|
|
|
|
dbset = db.Set<TEntity>();
|
|
|
|
|
this.db = db;
|
|
|
|
|
this.telemetryService = telemetryService;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<(DateTime begin, DateTime end, int count)?> GetStatAsync(int idTelemetry, CancellationToken token)
|
|
|
|
|
{
|
|
|
|
|
var timezoneHours = telemetryService.GetTimezone(idTelemetry).Hours;
|
|
|
|
|
var stat = await dbset.Where(d => d.IdTelemetry == idTelemetry)
|
|
|
|
|
.GroupBy(d => d.IdTelemetry)
|
|
|
|
|
.Select(g => new Tuple<DateTimeOffset, DateTimeOffset, int>(g.Min(d => d.DateTime), g.Max(d => d.DateTime), g.Count()))
|
|
|
|
|
.FirstOrDefaultAsync(token);
|
|
|
|
|
|
|
|
|
|
if (stat is null || stat.Item3 == 0)
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
stat.Item1.ToRemoteDateTime(timezoneHours),
|
|
|
|
|
stat.Item2.ToRemoteDateTime(timezoneHours),
|
|
|
|
|
stat.Item3);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<IEnumerable<TDto>> GetAsync(int idTelemetry, DateTime begin, DateTime end, CancellationToken token)
|
|
|
|
|
{
|
|
|
|
|
var timezoneHours = telemetryService.GetTimezone(idTelemetry).Hours;
|
|
|
|
|
var query = dbset
|
|
|
|
|
.Where(d => d.IdTelemetry == idTelemetry)
|
|
|
|
|
.Where(d => d.DateTime >= begin)
|
|
|
|
|
.Where(d => d.DateTime <= end)
|
|
|
|
|
.AsNoTracking();
|
|
|
|
|
var data = await query.ToListAsync(token);
|
|
|
|
|
return data.Select(d => Convert(d, timezoneHours));
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-15 14:45:49 +05:00
|
|
|
|
public async Task<IEnumerable<TDto>> GetLastAsync(int idTelemetry, CancellationToken token)
|
|
|
|
|
{
|
|
|
|
|
var timezoneHours = telemetryService.GetTimezone(idTelemetry).Hours;
|
|
|
|
|
var query = dbset
|
|
|
|
|
.Where(d => d.IdTelemetry == idTelemetry)
|
|
|
|
|
.OrderBy(d => d.DateTime)
|
|
|
|
|
.AsNoTracking();
|
|
|
|
|
var data = await query.LastOrDefaultAsync(token);
|
|
|
|
|
return new TDto[] { Convert(data, timezoneHours) };
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-08 13:10:06 +05:00
|
|
|
|
public Task SaveDataAsync(int idTelemetry, IEnumerable<TDto> dtos, CancellationToken token)
|
|
|
|
|
{
|
|
|
|
|
if (dtos?.Any() != true)
|
|
|
|
|
return Task.CompletedTask;
|
|
|
|
|
|
|
|
|
|
var timezoneHours = telemetryService.GetTimezone(idTelemetry).Hours;
|
2022-07-19 10:29:38 +05:00
|
|
|
|
var entities = dtos
|
|
|
|
|
.DistinctBy(d => d.DateTime)
|
|
|
|
|
.Select(dto => Convert(dto, idTelemetry, timezoneHours));
|
2022-04-08 13:10:06 +05:00
|
|
|
|
dbset.AddRange(entities);
|
|
|
|
|
return db.SaveChangesAsync(token);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static TEntity Convert(TDto dto, int idTelemetry, double timezoneHours)
|
|
|
|
|
{
|
|
|
|
|
if (dto is null)
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
|
|
var entity = dto.Adapt<TEntity>();
|
|
|
|
|
entity.IdTelemetry = idTelemetry;
|
|
|
|
|
entity.DateTime = dto.DateTime.ToUtcDateTimeOffset(timezoneHours);
|
|
|
|
|
return entity;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static TDto Convert(TEntity entity, double timezoneHours)
|
|
|
|
|
{
|
|
|
|
|
if (entity is null)
|
|
|
|
|
return default;
|
|
|
|
|
|
|
|
|
|
var data = entity.Adapt<TDto>();
|
|
|
|
|
data.DateTime = entity.DateTime.ToRemoteDateTime(timezoneHours);
|
|
|
|
|
return data;
|
|
|
|
|
}
|
2022-04-15 14:45:49 +05:00
|
|
|
|
|
2022-04-08 13:10:06 +05:00
|
|
|
|
}
|
|
|
|
|
}
|