forked from ddrilling/AsbCloudServer
Fix GtrWitsRepository.SaveDataAsync()
ignoring duplicates and other exception
This commit is contained in:
parent
750e63300c
commit
76b1cc2f1f
@ -1,12 +1,14 @@
|
|||||||
using AsbCloudApp.Data.GTR;
|
using AsbCloudApp.Data.GTR;
|
||||||
using AsbCloudApp.Repositories;
|
using AsbCloudApp.Repositories;
|
||||||
using AsbCloudApp.Services;
|
using AsbCloudApp.Services;
|
||||||
|
using AsbCloudDb;
|
||||||
using AsbCloudDb.Model;
|
using AsbCloudDb.Model;
|
||||||
using AsbCloudDb.Model.GTR;
|
using AsbCloudDb.Model.GTR;
|
||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Concurrent;
|
using System.Collections.Concurrent;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.Diagnostics;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
@ -149,6 +151,10 @@ namespace AsbCloudInfrastructure.Repository
|
|||||||
|
|
||||||
var cacheTelemetryItems = cache.GetValueOrDefault(idTelemetry);
|
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)
|
foreach (var record in dtos)
|
||||||
{
|
{
|
||||||
var dateTime = record.Date.ToUtcDateTimeOffset(timezoneHours);
|
var dateTime = record.Date.ToUtcDateTimeOffset(timezoneHours);
|
||||||
@ -161,30 +167,35 @@ namespace AsbCloudInfrastructure.Repository
|
|||||||
if (item.Value.Value is string valueString)
|
if (item.Value.Value is string valueString)
|
||||||
{
|
{
|
||||||
var entity = MakeEntity<WitsItemString, string>(record.Id, item.Key, idTelemetry, dateTime, 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)
|
if (item.Value.Value is float valueFloat)
|
||||||
{
|
{
|
||||||
var entity = MakeEntity<WitsItemFloat, float>(record.Id, item.Key, idTelemetry, dateTime, 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)
|
if (item.Value.Value is int valueInt)
|
||||||
{
|
{
|
||||||
var entity = MakeEntity<WitsItemInt, int>(record.Id, item.Key, idTelemetry, dateTime, valueInt);
|
var entity = MakeEntity<WitsItemInt, int>(record.Id, item.Key, idTelemetry, dateTime, valueInt);
|
||||||
db.WitsItemInt.Add(entity);
|
ints.Add(entity);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
try
|
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();
|
Trace.TraceError("Exception while saving GTR Wits data", ex);
|
||||||
if (!isRelational)
|
|
||||||
throw;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
cache.AddOrUpdate(idTelemetry,
|
cache.AddOrUpdate(idTelemetry,
|
||||||
|
Loading…
Reference in New Issue
Block a user