DD.WellWorkover.Cloud/AsbCloudInfrastructure/Services/SAUB/TelemetryService.cs

348 lines
14 KiB
C#
Raw Normal View History

2021-04-07 18:01:56 +05:00
using AsbCloudApp.Data;
2022-04-11 18:00:34 +05:00
using AsbCloudApp.Data.SAUB;
2021-04-07 18:01:56 +05:00
using AsbCloudApp.Services;
2022-04-11 18:00:34 +05:00
using AsbCloudDb;
2021-04-07 18:01:56 +05:00
using AsbCloudDb.Model;
2021-07-28 09:46:58 +05:00
using Mapster;
2022-04-11 18:00:34 +05:00
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Caching.Memory;
2022-04-11 18:00:34 +05:00
using System;
2021-09-29 10:12:54 +05:00
using System.Collections.Generic;
using System.Linq;
using System.Threading;
2022-04-11 18:00:34 +05:00
using System.Threading.Tasks;
2021-04-07 18:01:56 +05:00
2022-04-11 18:00:34 +05:00
namespace AsbCloudInfrastructure.Services.SAUB
2021-04-07 18:01:56 +05:00
{
2021-04-07 18:01:56 +05:00
public class TelemetryService : ITelemetryService
{
private readonly IAsbCloudDbContext db;
private readonly IMemoryCache memoryCache;
private readonly ITelemetryTracker telemetryTracker;
private readonly ITimezoneService timezoneService;
2021-04-07 18:01:56 +05:00
public ITimezoneService TimeZoneService => timezoneService;
public ITelemetryTracker TelemetryTracker => telemetryTracker;
2021-11-22 11:30:08 +05:00
public TelemetryService(
2022-04-11 18:00:34 +05:00
IAsbCloudDbContext db,
IMemoryCache memoryCache,
2021-11-22 11:30:08 +05:00
ITelemetryTracker telemetryTracker,
ITimezoneService timezoneService)
2021-04-07 18:01:56 +05:00
{
this.db = db;
this.memoryCache = memoryCache;
this.telemetryTracker = telemetryTracker;
this.timezoneService = timezoneService;
2021-04-07 18:01:56 +05:00
}
2022-04-11 18:00:34 +05:00
private IEnumerable<Telemetry> GetTelemetryCache()
=> memoryCache.GetOrCreateBasic(
db.Set<Telemetry>()
.Include(t => t.Well));
private void DropTelemetryCache()
{
memoryCache.DropBasic<Telemetry>();
}
2021-04-07 18:01:56 +05:00
public DateTime GetLastTelemetryDate(int idTelemetry)
{
var telemetry = GetTelemetryCache().FirstOrDefault(t => t.Id == idTelemetry);
if (telemetry is null)
throw new Exception($"Telemetry id:{idTelemetry} does not exist");
var uid = telemetry.RemoteUid;
var timzone = GetTimezone(idTelemetry);
var lastTelemetryDate = telemetryTracker.GetLastTelemetryDateByUid(uid);
return lastTelemetryDate.ToRemoteDateTime(timzone.Hours);
}
public DatesRangeDto GetDatesRange(int idTelemetry)
{
var telemetry = GetTelemetryCache().FirstOrDefault(t => t.Id == idTelemetry);
if (telemetry is null)
throw new Exception($"Telemetry id:{idTelemetry} does not exist");
var dto = TelemetryTracker.GetTelemetryDateRangeByUid(telemetry.RemoteUid);
if (dto is null)
throw new Exception($"Telemetry id:{idTelemetry} has no data");
2022-04-11 18:00:34 +05:00
var timezone = GetTimezone(idTelemetry);
dto.From = dto.From.ToTimeZoneOffsetHours(timezone.Hours);
dto.To = dto.To.ToTimeZoneOffsetHours(timezone.Hours);
return dto;
}
public int GetOrCreateTelemetryIdByUid(string uid)
=> GetOrCreateTelemetryByUid(uid).Id;
2021-04-07 18:01:56 +05:00
public int? GetIdWellByTelemetryUid(string uid)
2021-04-23 10:21:25 +05:00
=> GetWellByTelemetryUid(uid)?.Id;
public async Task UpdateInfoAsync(string uid, TelemetryInfoDto info,
CancellationToken token)
{
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-22 11:30:08 +05:00
if (!string.IsNullOrEmpty(info.TimeZoneId) &&
telemetry.TimeZone?.IsOverride != true)
telemetry.TimeZone = new SimpleTimezone()
{
Hours = info.TimeZoneOffsetTotalHours,
TimezoneId = info.TimeZoneId
};
2022-04-11 18:00:34 +05:00
db.Telemetries.Upsert(telemetry);
await db.SaveChangesAsync(token);
DropTelemetryCache();
}
public SimpleTimezoneDto GetTimezone(int idTelemetry)
{
var telemetry = GetTelemetryCache().FirstOrDefault(t => t.Id == idTelemetry);
if (telemetry is null)
throw new Exception($"Telemetry id: {idTelemetry} does not exist.");
if (telemetry.TimeZone is not null)
return telemetry.TimeZone.Adapt<SimpleTimezoneDto>();
if (telemetry.Info is not null)
2021-11-22 11:30:08 +05:00
{
telemetry.TimeZone = new SimpleTimezone
2021-11-22 11:30:08 +05:00
{
Hours = telemetry.Info.TimeZoneOffsetTotalHours,
IsOverride = false,
TimezoneId = telemetry.Info.TimeZoneId,
2021-11-22 11:30:08 +05:00
};
db.Telemetries.Upsert(telemetry);
db.SaveChanges();
DropTelemetryCache();
return telemetry.TimeZone.Adapt<SimpleTimezoneDto>();
2021-11-22 11:30:08 +05:00
}
if (telemetry.Well?.Timezone is not null)
{
telemetry.TimeZone = telemetry.Well.Timezone;
db.Telemetries.Upsert(telemetry);
db.SaveChanges();
DropTelemetryCache();
return telemetry.TimeZone.Adapt<SimpleTimezoneDto>();
2021-11-22 11:30:08 +05:00
}
throw new Exception($"Telemetry id: {idTelemetry} can't find timezone.");
}
public TelemetryBaseDto? GetOrDefaultTelemetryByIdWell(int idWell)
{
var telemetry = GetTelemetryCache()
.FirstOrDefault(t => t.Well?.Id == idWell);
return telemetry?.Adapt<TelemetryBaseDto>();
2021-04-07 18:01:56 +05:00
}
private Well? GetWellByTelemetryUid(string uid)
{
var telemetry = GetOrDefaultTelemetryByUid(uid);
return telemetry?.Well;
}
private Telemetry? GetOrDefaultTelemetryByUid(string uid)
2021-04-07 18:01:56 +05:00
{
var telemetry = GetTelemetryCache().FirstOrDefault(t => t.RemoteUid == uid);
return telemetry;
2021-04-07 18:01:56 +05:00
}
2021-04-23 10:21:25 +05:00
private Telemetry GetOrCreateTelemetryByUid(string uid)
{
var telemetry = GetOrDefaultTelemetryByUid(uid);
if (telemetry is null)
{
var newTelemetry = new Telemetry
2022-04-11 18:00:34 +05:00
{
RemoteUid = uid,
TimeZone = new SimpleTimezone
{
Hours = 5,
IsOverride = false,
TimezoneId = "default",
}
};
var entry = db.Telemetries.Add(newTelemetry);
db.SaveChanges();
DropTelemetryCache();
return entry.Entity;
}
return telemetry;
}
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();
2022-04-11 18:00:34 +05:00
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;
2022-04-11 18:00:34 +05:00
2021-12-17 12:48:58 +05:00
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);
2022-04-11 18:00:34 +05:00
2021-12-17 12:48:58 +05:00
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 committed in {1d * stopwath.ElapsedMilliseconds / 1000d: #0.00} sec. Affected {affected} rows.");
DropTelemetryCache();
2021-12-17 12:48:58 +05:00
return affected;
2021-09-29 10:12:54 +05:00
}
2022-04-11 18:00:34 +05:00
catch (Exception ex)
2021-09-29 10:12:54 +05:00
{
2021-12-17 12:48:58 +05:00
System.Diagnostics.Trace.WriteLine($"Merge() Fail. Rollback. Reason is:{ex.Message}");
await transaction.RollbackAsync(CancellationToken.None);
2021-12-17 12:48:58 +05:00
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
2022-04-11 18:00:34 +05:00
.FirstOrDefault(u => u.IdUser == userFrom.IdUser);
2021-12-17 12:48:58 +05:00
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)
2022-04-11 18:00:34 +05:00
where TEntity : class, AsbCloudDb.Model.ITelemetryData
2021-12-17 12:48:58 +05:00
{
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
}
2021-04-07 18:01:56 +05:00
}