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-09-29 10:12:54 +05:00
|
|
|
|
using System.Collections.Generic;
|
2021-09-27 16:47:16 +05:00
|
|
|
|
using System.Linq;
|
2021-11-16 13:14:31 +05:00
|
|
|
|
using System.Threading.Tasks;
|
2021-09-29 10:12:54 +05:00
|
|
|
|
using System;
|
2021-11-16 13:14:31 +05:00
|
|
|
|
using System.Threading;
|
2021-09-29 10:12:54 +05:00
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
2021-12-17 12:48:58 +05:00
|
|
|
|
using AsbCloudDb;
|
2021-04-07 18:01:56 +05:00
|
|
|
|
|
|
|
|
|
namespace AsbCloudInfrastructure.Services
|
|
|
|
|
{
|
|
|
|
|
public class TelemetryService : ITelemetryService
|
|
|
|
|
{
|
2021-05-20 14:14:51 +05:00
|
|
|
|
private readonly CacheTable<Telemetry> cacheTelemetry;
|
2021-12-22 11:41:18 +05:00
|
|
|
|
private readonly CacheTable<Well> cacheWells;//TODO: use wellService insad of this
|
2021-09-27 16:47:16 +05:00
|
|
|
|
private readonly IAsbCloudDbContext db;
|
2021-10-15 12:24:04 +05:00
|
|
|
|
private readonly ITelemetryTracker telemetryTracker;
|
2021-11-22 11:30:08 +05:00
|
|
|
|
private readonly ITimeZoneService timeZoneService;
|
2021-04-07 18:01:56 +05:00
|
|
|
|
|
2021-11-22 11:30:08 +05:00
|
|
|
|
public ITimeZoneService TimeZoneService => timeZoneService;
|
2021-11-22 16:02:15 +05:00
|
|
|
|
public ITelemetryTracker TelemetryTracker => telemetryTracker;
|
2021-11-22 11:30:08 +05:00
|
|
|
|
|
|
|
|
|
public TelemetryService(
|
|
|
|
|
IAsbCloudDbContext db,
|
|
|
|
|
ITelemetryTracker telemetryTracker,
|
|
|
|
|
ITimeZoneService timeZoneService,
|
2021-10-15 12:24:04 +05:00
|
|
|
|
CacheDb cacheDb)
|
2021-04-07 18:01:56 +05:00
|
|
|
|
{
|
|
|
|
|
cacheTelemetry = cacheDb.GetCachedTable<Telemetry>((AsbCloudDbContext)db);
|
2021-12-22 11:41:18 +05:00
|
|
|
|
cacheWells = cacheDb.GetCachedTable<Well>(
|
|
|
|
|
(AsbCloudDbContext)db,
|
|
|
|
|
$"{nameof(Well.Cluster)}.{nameof(Cluster.Deposit)}",
|
|
|
|
|
nameof(Well.Telemetry),
|
|
|
|
|
$"{nameof(Well.RelationCompaniesWells)}.{nameof(RelationCompanyWell.Company)}",
|
|
|
|
|
nameof(Well.WellType));
|
2021-09-27 16:47:16 +05:00
|
|
|
|
this.db = db;
|
2021-10-15 12:24:04 +05:00
|
|
|
|
this.telemetryTracker = telemetryTracker;
|
2021-11-22 11:30:08 +05:00
|
|
|
|
this.timeZoneService = timeZoneService;
|
2021-04-07 18:01:56 +05:00
|
|
|
|
}
|
2021-10-15 12:24:04 +05:00
|
|
|
|
|
2021-12-07 15:55:11 +05:00
|
|
|
|
public IEnumerable<TelemetryDto> GetTransmittingTelemetries()
|
2021-10-15 12:24:04 +05:00
|
|
|
|
{
|
2021-10-21 15:57:20 +05:00
|
|
|
|
var telemetryDtos = new List<TelemetryDto>();
|
2021-12-07 18:27:52 +05:00
|
|
|
|
var activeTelemetriesUids = telemetryTracker.GetTransmittingTelemetriesUids();
|
|
|
|
|
if (!activeTelemetriesUids.Any())
|
|
|
|
|
return telemetryDtos;
|
|
|
|
|
var telemetries = cacheTelemetry
|
|
|
|
|
.Where(t => activeTelemetriesUids.Contains(t.RemoteUid));
|
|
|
|
|
telemetryDtos = telemetries.Adapt<TelemetryDto>().ToList();
|
|
|
|
|
|
2021-10-21 15:57:20 +05:00
|
|
|
|
return telemetryDtos;
|
2021-10-15 12:24:04 +05:00
|
|
|
|
}
|
|
|
|
|
|
2021-10-28 10:56:18 +05:00
|
|
|
|
public void SaveRequestDate(string uid, DateTime remoteDate) =>
|
|
|
|
|
telemetryTracker.SaveRequestDate(uid, remoteDate);
|
2021-04-07 18:01:56 +05:00
|
|
|
|
|
2021-10-20 12:52:31 +05:00
|
|
|
|
public DateTime GetLastTelemetryDate(string telemetryUid) =>
|
|
|
|
|
telemetryTracker.GetLastTelemetryDateByUid(telemetryUid);
|
|
|
|
|
|
|
|
|
|
public DateTime GetLastTelemetryDate(int telemetryId)
|
2021-10-15 15:35:18 +05:00
|
|
|
|
{
|
2021-10-18 10:44:53 +05:00
|
|
|
|
var lastTelemetryDate = DateTime.MinValue;
|
|
|
|
|
var telemetry = cacheTelemetry.FirstOrDefault(t => t.Id == telemetryId);
|
|
|
|
|
|
|
|
|
|
if (telemetry is null)
|
|
|
|
|
return lastTelemetryDate;
|
|
|
|
|
|
|
|
|
|
var uid = telemetry.RemoteUid;
|
|
|
|
|
|
2021-10-20 12:52:31 +05:00
|
|
|
|
lastTelemetryDate = GetLastTelemetryDate(uid);
|
2021-10-18 10:44:53 +05:00
|
|
|
|
return lastTelemetryDate;
|
2021-10-15 15:35:18 +05:00
|
|
|
|
}
|
|
|
|
|
|
2021-12-07 15:55:11 +05:00
|
|
|
|
public int GetOrCreateTelemetryIdByUid(string uid)
|
2021-10-15 15:35:18 +05:00
|
|
|
|
=> GetOrCreateTelemetryByUid(uid).Id;
|
2021-04-07 18:01:56 +05:00
|
|
|
|
|
2021-12-07 15:55:11 +05:00
|
|
|
|
public int? GetIdWellByTelemetryUid(string uid)
|
2021-04-23 10:21:25 +05:00
|
|
|
|
=> GetWellByTelemetryUid(uid)?.Id;
|
2021-04-30 17:35:35 +05:00
|
|
|
|
|
2021-06-25 15:10:05 +05:00
|
|
|
|
public double GetTimezoneOffsetByTelemetryId(int idTelemetry) =>
|
2021-10-04 15:54:58 +05:00
|
|
|
|
cacheTelemetry.FirstOrDefault(t => t.Id == idTelemetry).Info?.TimeZoneOffsetTotalHours ?? 0d;
|
2021-06-25 15:10:05 +05:00
|
|
|
|
|
2021-11-16 13:14:31 +05:00
|
|
|
|
public async Task UpdateInfoAsync(string uid, TelemetryInfoDto info,
|
|
|
|
|
CancellationToken token)
|
2021-04-09 17:59:07 +05:00
|
|
|
|
{
|
2021-04-23 10:21:25 +05:00
|
|
|
|
var telemetry = GetOrCreateTelemetryByUid(uid);
|
2021-07-28 09:46:58 +05:00
|
|
|
|
telemetry.Info = info.Adapt<TelemetryInfo>();
|
2021-11-16 13:14:31 +05:00
|
|
|
|
|
2021-11-22 11:30:08 +05:00
|
|
|
|
if (!string.IsNullOrEmpty(info.TimeZoneId) &&
|
|
|
|
|
telemetry.TelemetryTimeZone?.IsOverride != true)
|
2021-11-16 13:14:31 +05:00
|
|
|
|
telemetry.TelemetryTimeZone = new TelemetryTimeZone()
|
|
|
|
|
{
|
2021-11-16 17:14:36 +05:00
|
|
|
|
Hours = info.TimeZoneOffsetTotalHours,
|
2021-11-16 13:14:31 +05:00
|
|
|
|
TimeZoneId = info.TimeZoneId
|
|
|
|
|
};
|
|
|
|
|
|
2021-11-16 17:14:36 +05:00
|
|
|
|
await cacheTelemetry.UpsertAsync(telemetry, token)
|
|
|
|
|
.ConfigureAwait(false);
|
|
|
|
|
}
|
2021-11-17 15:54:01 +05:00
|
|
|
|
|
2021-11-18 14:25:11 +05:00
|
|
|
|
public async Task<double?> GetTelemetryTimeZoneOffsetAsync(int idTelemetry, CancellationToken token)
|
|
|
|
|
{
|
2021-11-17 15:54:01 +05:00
|
|
|
|
var telemetry =
|
|
|
|
|
await cacheTelemetry.FirstOrDefaultAsync(t => t.Id == idTelemetry, token);
|
2021-11-18 14:25:11 +05:00
|
|
|
|
|
2021-11-22 11:30:08 +05:00
|
|
|
|
if (!string.IsNullOrEmpty(telemetry.TelemetryTimeZone?.TimeZoneId))
|
2021-11-18 14:25:11 +05:00
|
|
|
|
return telemetry.TelemetryTimeZone.Hours;
|
2021-11-17 15:54:01 +05:00
|
|
|
|
|
2021-11-22 11:30:08 +05:00
|
|
|
|
if (!string.IsNullOrEmpty(telemetry.Info?.TimeZoneId))
|
|
|
|
|
{
|
|
|
|
|
telemetry.TelemetryTimeZone = new TelemetryTimeZone
|
|
|
|
|
{
|
|
|
|
|
Hours = telemetry.Info.TimeZoneOffsetTotalHours,
|
|
|
|
|
IsOverride = false,
|
|
|
|
|
TimeZoneId = telemetry.Info.TimeZoneId,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
var well = await cacheWells.FirstOrDefaultAsync(t => t.IdTelemetry == telemetry.Id, token)
|
|
|
|
|
.ConfigureAwait(false);
|
2021-11-17 15:54:01 +05:00
|
|
|
|
|
2021-11-22 11:30:08 +05:00
|
|
|
|
if (well is null)
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
|
|
var coordinates = await GetWellCoordinatesAsync(well.Id, token);
|
|
|
|
|
|
|
|
|
|
if (coordinates is null)
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
|
|
var requestedTimeZone = await timeZoneService.GetByCoordinatesAsync(coordinates.Value.latitude, coordinates.Value.longitude, token)
|
|
|
|
|
.ConfigureAwait(false);
|
|
|
|
|
|
|
|
|
|
if (requestedTimeZone is null)
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
|
|
telemetry.TelemetryTimeZone = requestedTimeZone.Adapt<TelemetryTimeZone>();
|
|
|
|
|
}
|
2021-11-17 15:54:01 +05:00
|
|
|
|
|
2021-11-18 14:25:11 +05:00
|
|
|
|
await cacheTelemetry.UpsertAsync(telemetry, token).ConfigureAwait(false);
|
2021-11-17 15:54:01 +05:00
|
|
|
|
|
2021-11-18 14:25:11 +05:00
|
|
|
|
return telemetry.TelemetryTimeZone.Hours;
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-22 11:30:08 +05:00
|
|
|
|
public async Task<DatesRangeDto> FixDatesRangeByTimeZoneAsync(int idTelemetry, DatesRangeDto range,
|
2021-11-18 14:25:11 +05:00
|
|
|
|
CancellationToken token)
|
|
|
|
|
{
|
2021-11-22 11:30:08 +05:00
|
|
|
|
var offset = await GetTelemetryTimeZoneOffsetAsync(idTelemetry, token);
|
2021-11-18 12:03:59 +05:00
|
|
|
|
|
2021-11-22 11:30:08 +05:00
|
|
|
|
if (offset is null)
|
|
|
|
|
return range;
|
2021-11-18 12:03:59 +05:00
|
|
|
|
|
|
|
|
|
return new DatesRangeDto()
|
|
|
|
|
{
|
2021-11-22 11:30:08 +05:00
|
|
|
|
From = timeZoneService.DateToTimeZone(range.From, offset ?? default),
|
|
|
|
|
To = timeZoneService.DateToTimeZone(range.To, offset ?? default),
|
2021-11-18 12:03:59 +05:00
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-16 13:14:31 +05:00
|
|
|
|
public async Task UpdateTimeZoneAsync(string uid, TelemetryTimeZoneDto timeZoneInfo,
|
|
|
|
|
CancellationToken token)
|
|
|
|
|
{
|
2021-11-22 11:30:08 +05:00
|
|
|
|
var telemetry = GetOrCreateTelemetryByUid(uid);
|
|
|
|
|
var newTelemetryTimeZone = timeZoneInfo.Adapt<TelemetryTimeZone>();
|
|
|
|
|
if (newTelemetryTimeZone?.Equals(telemetry.TelemetryTimeZone) == true)
|
|
|
|
|
return;
|
2021-11-16 17:14:36 +05:00
|
|
|
|
await cacheTelemetry.UpsertAsync(telemetry, token)
|
|
|
|
|
.ConfigureAwait(false);
|
2021-04-07 18:01:56 +05:00
|
|
|
|
}
|
|
|
|
|
|
2021-08-09 14:01:57 +05:00
|
|
|
|
public int? GetIdTelemetryByIdWell(int idWell)
|
2021-05-17 12:53:30 +05:00
|
|
|
|
{
|
2021-07-27 14:43:30 +05:00
|
|
|
|
var well = cacheWells.FirstOrDefault(w => w.Id == idWell);
|
2021-12-07 18:27:52 +05:00
|
|
|
|
return well?.IdTelemetry;
|
2021-11-17 15:54:01 +05:00
|
|
|
|
}
|
|
|
|
|
|
2021-11-22 11:30:08 +05:00
|
|
|
|
private async Task<(double latitude, double longitude)?> GetWellCoordinatesAsync(int idWell,
|
2021-11-17 15:54:01 +05:00
|
|
|
|
CancellationToken token)
|
|
|
|
|
{
|
|
|
|
|
var well = await cacheWells.FirstOrDefaultAsync(w => w.Id == idWell, token)
|
|
|
|
|
.ConfigureAwait(false);
|
|
|
|
|
|
|
|
|
|
if (well is null)
|
|
|
|
|
return null;
|
|
|
|
|
|
2021-12-22 11:41:18 +05:00
|
|
|
|
var latitude = well.Latitude ??
|
|
|
|
|
well.Cluster?.Latitude ??
|
|
|
|
|
well.Cluster?.Deposit?.Latitude;
|
|
|
|
|
|
|
|
|
|
var longitude = well.Longitude ??
|
|
|
|
|
well.Cluster?.Longitude ??
|
|
|
|
|
well.Cluster?.Deposit?.Longitude;
|
|
|
|
|
|
|
|
|
|
if (latitude is not null && longitude is not null)
|
|
|
|
|
return ((double)latitude, (double)longitude);
|
2021-11-17 15:54:01 +05:00
|
|
|
|
|
|
|
|
|
return null;
|
2021-05-17 12:53:30 +05:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-23 10:21:25 +05:00
|
|
|
|
private Well GetWellByTelemetryUid(string uid)
|
2021-04-07 18:01:56 +05:00
|
|
|
|
{
|
2021-10-04 15:54:58 +05:00
|
|
|
|
var tele = cacheTelemetry.FirstOrDefault(t => t.RemoteUid == uid);
|
2021-04-23 10:21:25 +05:00
|
|
|
|
if (tele is null)
|
|
|
|
|
return null;
|
2021-08-24 10:59:10 +05:00
|
|
|
|
|
2021-05-17 12:53:30 +05:00
|
|
|
|
return cacheWells.FirstOrDefault(w => w?.IdTelemetry == tele.Id);
|
2021-04-07 18:01:56 +05:00
|
|
|
|
}
|
2021-04-23 10:21:25 +05:00
|
|
|
|
|
|
|
|
|
private Telemetry GetOrCreateTelemetryByUid(string uid)
|
2021-10-04 15:54:58 +05:00
|
|
|
|
=> cacheTelemetry.GetOrCreate(t => t.RemoteUid == uid, () => new Telemetry { RemoteUid = uid });
|
2021-09-29 10:12:54 +05:00
|
|
|
|
|
2021-12-17 12:48:58 +05:00
|
|
|
|
public async Task<int> MergeAsync(int from, int to, CancellationToken token)
|
2021-09-29 10:12:54 +05:00
|
|
|
|
{
|
2021-12-17 12:48:58 +05:00
|
|
|
|
if (from == to)
|
|
|
|
|
return -2;
|
|
|
|
|
|
|
|
|
|
var stopwath = new System.Diagnostics.Stopwatch();
|
|
|
|
|
stopwath.Start();
|
|
|
|
|
|
|
|
|
|
var transaction = await db.Database.BeginTransactionAsync(token).ConfigureAwait(false);
|
2021-09-29 10:12:54 +05:00
|
|
|
|
try
|
|
|
|
|
{
|
2021-12-17 12:48:58 +05:00
|
|
|
|
var affected = 0;
|
|
|
|
|
|
|
|
|
|
var wellFrom = await db.Wells.FirstOrDefaultAsync(w => w.IdTelemetry == from, token)
|
|
|
|
|
.ConfigureAwait(false);
|
|
|
|
|
var wellTo = await db.Wells.FirstOrDefaultAsync(w => w.IdTelemetry == to, token)
|
|
|
|
|
.ConfigureAwait(false);
|
|
|
|
|
|
|
|
|
|
if (wellTo is not null && wellFrom is not null)
|
|
|
|
|
return -2;
|
2021-09-29 10:12:54 +05:00
|
|
|
|
|
2021-12-17 12:48:58 +05:00
|
|
|
|
if (wellTo is null && wellFrom is not null)
|
2021-09-29 10:12:54 +05:00
|
|
|
|
{
|
2021-12-17 12:48:58 +05:00
|
|
|
|
wellFrom.IdTelemetry = to;
|
|
|
|
|
affected += await db.SaveChangesAsync(token);
|
2021-09-29 10:12:54 +05:00
|
|
|
|
}
|
|
|
|
|
|
2021-12-17 12:48:58 +05:00
|
|
|
|
affected += await MergeEventsAndMessagesAndUsersAsync(from, to, token);
|
|
|
|
|
affected += await MergeDataAsync<TelemetryDataSaub>(from, to, token);
|
|
|
|
|
affected += await MergeDataAsync<TelemetryDataSpin>(from, to, token);
|
|
|
|
|
|
|
|
|
|
affected += await db.Database.ExecuteSqlRawAsync($"DELETE FROM t_telemetry_analysis WHERE id_telemetry = {from} OR id_telemetry = {to};", token)
|
|
|
|
|
.ConfigureAwait(false);
|
|
|
|
|
affected += await db.Database.ExecuteSqlRawAsync($"DELETE FROM t_telemetry WHERE id = {from};", token)
|
|
|
|
|
.ConfigureAwait(false);
|
|
|
|
|
await transaction.CommitAsync(token).ConfigureAwait(false);
|
|
|
|
|
|
|
|
|
|
stopwath.Stop();
|
|
|
|
|
Console.WriteLine($"Successfully commited in {1d * stopwath.ElapsedMilliseconds / 1000d: #0.00} sec. Affected {affected} rows.");
|
|
|
|
|
return affected;
|
2021-09-29 10:12:54 +05:00
|
|
|
|
}
|
|
|
|
|
catch(Exception ex)
|
|
|
|
|
{
|
2021-12-17 12:48:58 +05:00
|
|
|
|
System.Diagnostics.Trace.WriteLine($"Merge() Fail. Rollback. Reason is:{ex.Message}");
|
|
|
|
|
await transaction.RollbackAsync(token).ConfigureAwait(false);
|
|
|
|
|
return -1;
|
2021-09-29 10:12:54 +05:00
|
|
|
|
}
|
|
|
|
|
}
|
2021-11-22 11:30:08 +05:00
|
|
|
|
|
2021-12-17 12:48:58 +05:00
|
|
|
|
private async Task<int> MergeEventsAndMessagesAndUsersAsync(int from, int to, CancellationToken token)
|
|
|
|
|
{
|
|
|
|
|
var messagesFrom = await db.TelemetryMessages
|
|
|
|
|
.Where(d => d.IdTelemetry == from)
|
|
|
|
|
.ToListAsync(token)
|
|
|
|
|
.ConfigureAwait(false);
|
|
|
|
|
|
|
|
|
|
var usersFromQuery = db.TelemetryUsers
|
|
|
|
|
.Where(d => d.IdTelemetry == from);
|
|
|
|
|
var usersFrom = await usersFromQuery
|
|
|
|
|
.ToListAsync(token)
|
|
|
|
|
.ConfigureAwait(false);
|
|
|
|
|
|
|
|
|
|
var usersTo = await db.TelemetryUsers
|
|
|
|
|
.Where(d => d.IdTelemetry == to)
|
|
|
|
|
.AsNoTracking()
|
|
|
|
|
.ToListAsync(token)
|
|
|
|
|
.ConfigureAwait(false);
|
|
|
|
|
|
|
|
|
|
var usersToNextId = usersTo.Max(u => u.IdUser) + 100;
|
|
|
|
|
|
|
|
|
|
messagesFrom.ForEach(m => m.IdTelemetry = to);
|
|
|
|
|
|
|
|
|
|
foreach (var userFrom in usersFrom)
|
|
|
|
|
{
|
|
|
|
|
var userTo = usersTo
|
|
|
|
|
.FirstOrDefault(u=>u.IdUser == userFrom.IdUser);
|
|
|
|
|
|
|
|
|
|
if (userTo is null ||
|
|
|
|
|
userTo.Name != userFrom.Name ||
|
|
|
|
|
userTo.Surname != userFrom.Surname ||
|
|
|
|
|
userTo.Patronymic != userFrom.Patronymic)
|
|
|
|
|
{
|
|
|
|
|
messagesFrom
|
|
|
|
|
.Where(m => m.IdTelemetryUser == userFrom.IdUser)
|
|
|
|
|
.ToList()
|
|
|
|
|
.ForEach(m => m.IdTelemetryUser = usersToNextId);
|
|
|
|
|
userFrom.IdUser = usersToNextId;
|
|
|
|
|
userFrom.IdTelemetry = to;
|
|
|
|
|
usersToNextId++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var eventsFromQuery = db.TelemetryEvents
|
|
|
|
|
.Where(d => d.IdTelemetry == from);
|
|
|
|
|
var eventsFrom = await eventsFromQuery
|
|
|
|
|
.ToListAsync(token)
|
|
|
|
|
.ConfigureAwait(false);
|
|
|
|
|
|
|
|
|
|
var eventsTo = await db.TelemetryEvents
|
|
|
|
|
.Where(d => d.IdTelemetry == to)
|
|
|
|
|
.AsNoTracking()
|
|
|
|
|
.ToListAsync(token)
|
|
|
|
|
.ConfigureAwait(false);
|
|
|
|
|
|
|
|
|
|
var eventsToNextId = eventsTo.Max(e => e.IdEvent) + 1;
|
|
|
|
|
|
|
|
|
|
foreach (var eventFrom in eventsFrom)
|
|
|
|
|
{
|
|
|
|
|
var eventTo = eventsTo
|
|
|
|
|
.FirstOrDefault(e => e.IdEvent == eventFrom.IdEvent);
|
|
|
|
|
|
|
|
|
|
if (eventTo is null ||
|
|
|
|
|
eventTo.IdCategory != eventFrom.IdCategory ||
|
|
|
|
|
eventTo.MessageTemplate != eventFrom.MessageTemplate)
|
|
|
|
|
{
|
|
|
|
|
messagesFrom
|
|
|
|
|
.Where(m => m.IdEvent == eventFrom.IdEvent)
|
|
|
|
|
.ToList()
|
|
|
|
|
.ForEach(m => m.IdEvent = eventsToNextId);
|
|
|
|
|
eventFrom.IdEvent = eventsToNextId;
|
|
|
|
|
eventFrom.IdTelemetry = to;
|
|
|
|
|
eventsToNextId++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
await db.Database.ExecuteSqlRawAsync($"ALTER TABLE t_telemetry_user DISABLE TRIGGER ALL;", token).ConfigureAwait(false);
|
|
|
|
|
await db.Database.ExecuteSqlRawAsync($"ALTER TABLE t_telemetry_event DISABLE TRIGGER ALL;", token).ConfigureAwait(false);
|
|
|
|
|
await db.Database.ExecuteSqlRawAsync($"ALTER TABLE t_telemetry_message DISABLE TRIGGER ALL;", token).ConfigureAwait(false);
|
|
|
|
|
var affected = await db.SaveChangesAsync(token).ConfigureAwait(false);
|
|
|
|
|
await db.Database.ExecuteSqlRawAsync($"ALTER TABLE t_telemetry_user ENABLE TRIGGER ALL;", token).ConfigureAwait(false);
|
|
|
|
|
await db.Database.ExecuteSqlRawAsync($"ALTER TABLE t_telemetry_event ENABLE TRIGGER ALL;", token).ConfigureAwait(false);
|
|
|
|
|
await db.Database.ExecuteSqlRawAsync($"ALTER TABLE t_telemetry_message ENABLE TRIGGER ALL;", token).ConfigureAwait(false);
|
|
|
|
|
db.TelemetryUsers.RemoveRange(usersFromQuery);
|
|
|
|
|
db.TelemetryEvents.RemoveRange(eventsFromQuery);
|
|
|
|
|
affected += await db.SaveChangesAsync(token).ConfigureAwait(false);
|
|
|
|
|
return affected;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async Task<int> MergeDataAsync<TEntity>(int from, int to, CancellationToken token)
|
|
|
|
|
where TEntity: class, AsbCloudDb.Model.ITelemetryData
|
|
|
|
|
{
|
|
|
|
|
const string IdTelemetryColumnName = "\"id_telemetry\"";
|
|
|
|
|
var dbSet = db.Set<TEntity>();
|
|
|
|
|
var tableName = dbSet.GetTableName();
|
|
|
|
|
var columns = dbSet.GetColumnsNames().ToList();
|
|
|
|
|
var index = columns.FindIndex(c => c.ToLower() == IdTelemetryColumnName.ToLower());
|
|
|
|
|
if (index < 0)
|
|
|
|
|
return -5;
|
|
|
|
|
columns[index] = $"{to} as {IdTelemetryColumnName}";
|
|
|
|
|
var columnsString = string.Join(',', columns);
|
|
|
|
|
var sql = $"INSERT INTO {tableName} " +
|
|
|
|
|
$"select {columnsString} " +
|
|
|
|
|
$"from {tableName} " +
|
|
|
|
|
$"where {IdTelemetryColumnName} = {from};" +
|
|
|
|
|
$"delete from {tableName} where {IdTelemetryColumnName} = {from};";
|
|
|
|
|
|
|
|
|
|
var affected = await db.Database.ExecuteSqlRawAsync(sql, token)
|
|
|
|
|
.ConfigureAwait(false);
|
|
|
|
|
|
|
|
|
|
return affected;
|
|
|
|
|
}
|
2021-04-07 18:01:56 +05:00
|
|
|
|
}
|
|
|
|
|
}
|