Fix GtrWitsRepository.SaveDataAsync()

ignoring duplicates and other exception
This commit is contained in:
ngfrolov 2023-09-20 17:50:44 +05:00
parent 750e63300c
commit 76b1cc2f1f
Signed by: ng.frolov
GPG Key ID: E99907A0357B29A7

View File

@ -1,12 +1,14 @@
using AsbCloudApp.Data.GTR;
using AsbCloudApp.Repositories;
using AsbCloudApp.Services;
using AsbCloudDb;
using AsbCloudDb.Model;
using AsbCloudDb.Model.GTR;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
@ -149,6 +151,10 @@ namespace AsbCloudInfrastructure.Repository
var cacheTelemetryItems = cache.GetValueOrDefault(idTelemetry);
var strings = new List<WitsItemString>(4);
var floats = new List<WitsItemFloat>(4);
var ints = new List<WitsItemInt>(4);
foreach (var record in dtos)
{
var dateTime = record.Date.ToUtcDateTimeOffset(timezoneHours);
@ -161,30 +167,35 @@ namespace AsbCloudInfrastructure.Repository
if (item.Value.Value is string valueString)
{
var entity = MakeEntity<WitsItemString, string>(record.Id, item.Key, idTelemetry, dateTime, valueString);
db.WitsItemString.Add(entity);
strings.Add(entity);
}
if (item.Value.Value is float valueFloat)
{
var entity = MakeEntity<WitsItemFloat, float>(record.Id, item.Key, idTelemetry, dateTime, valueFloat);
db.WitsItemFloat.Add(entity);
floats.Add(entity);
}
if (item.Value.Value is int valueInt)
{
var entity = MakeEntity<WitsItemInt, int>(record.Id, item.Key, idTelemetry, dateTime, valueInt);
db.WitsItemInt.Add(entity);
ints.Add(entity);
}
}
}
try
{
await db.SaveChangesAsync(token);
if (strings.Any())
await db.Database.ExecInsertOrIgnoreAsync(db.Set<WitsItemString>(), strings, token);
if (floats.Any())
await db.Database.ExecInsertOrIgnoreAsync(db.Set<WitsItemFloat>(), floats, token);
if (ints.Any())
await db.Database.ExecInsertOrIgnoreAsync(db.Set<WitsItemInt>(), ints, token);
}
catch(DbUpdateException ex)
catch(Exception ex)
{
var isRelational = ex.Source == "Microsoft.EntityFrameworkCore.Relational" && ex.Entries.Any();
if (!isRelational)
throw;
Trace.TraceError("Exception while saving GTR Wits data", ex);
}
cache.AddOrUpdate(idTelemetry,