2022-04-08 13:10:06 +05:00
|
|
|
|
using AsbCloudApp.Data.SAUB;
|
2021-04-23 10:21:25 +05:00
|
|
|
|
using AsbCloudApp.Services;
|
2022-06-10 17:36:03 +05:00
|
|
|
|
using AsbCloudDb;
|
2021-04-23 10:21:25 +05:00
|
|
|
|
using AsbCloudDb.Model;
|
2023-02-21 18:01:03 +05:00
|
|
|
|
using Microsoft.Extensions.Caching.Memory;
|
2021-04-23 10:21:25 +05:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
2021-08-11 16:54:42 +05:00
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
2021-04-23 10:21:25 +05:00
|
|
|
|
|
2022-04-11 18:00:34 +05:00
|
|
|
|
namespace AsbCloudInfrastructure.Services.SAUB
|
2021-04-23 10:21:25 +05:00
|
|
|
|
{
|
2023-04-18 16:22:53 +05:00
|
|
|
|
|
2021-04-23 10:21:25 +05:00
|
|
|
|
public class EventService : IEventService
|
|
|
|
|
{
|
2022-06-10 17:36:03 +05:00
|
|
|
|
private readonly IAsbCloudDbContext db;
|
2023-02-21 18:01:03 +05:00
|
|
|
|
private readonly IMemoryCache memoryCache;
|
2021-04-23 10:21:25 +05:00
|
|
|
|
private readonly ITelemetryService telemetryService;
|
|
|
|
|
|
2023-02-21 18:01:03 +05:00
|
|
|
|
public EventService(IAsbCloudDbContext db, IMemoryCache memoryCache, ITelemetryService telemetryService)
|
2021-04-23 10:21:25 +05:00
|
|
|
|
{
|
2022-06-10 17:36:03 +05:00
|
|
|
|
this.db = db;
|
2023-02-21 18:01:03 +05:00
|
|
|
|
this.memoryCache = memoryCache;
|
2021-04-23 10:21:25 +05:00
|
|
|
|
this.telemetryService = telemetryService;
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-11 16:54:42 +05:00
|
|
|
|
public async Task UpsertAsync(string uid, IEnumerable<EventDto> dtos,
|
|
|
|
|
CancellationToken token = default)
|
2021-04-23 10:21:25 +05:00
|
|
|
|
{
|
2021-05-20 11:17:55 +05:00
|
|
|
|
if (!dtos.Any())
|
2021-04-23 10:21:25 +05:00
|
|
|
|
return;
|
|
|
|
|
|
2021-12-07 18:27:52 +05:00
|
|
|
|
var telemetryId = telemetryService.GetOrCreateTelemetryIdByUid(uid);
|
2021-04-23 10:21:25 +05:00
|
|
|
|
|
2021-08-10 16:37:13 +05:00
|
|
|
|
var entities = dtos.Select(dto => new TelemetryEvent
|
2021-04-23 10:21:25 +05:00
|
|
|
|
{
|
2021-07-16 09:15:10 +05:00
|
|
|
|
IdEvent = dto.Id,
|
|
|
|
|
IdTelemetry = telemetryId,
|
|
|
|
|
IdCategory = dto.IdCategory,
|
|
|
|
|
MessageTemplate = dto.Message
|
|
|
|
|
});
|
2022-06-10 17:36:03 +05:00
|
|
|
|
var result = await db.Database.ExecInsertOrUpdateAsync(db.TelemetryEvents, entities, token);
|
2023-02-21 18:01:03 +05:00
|
|
|
|
memoryCache.DropBasic<TelemetryEvent>();
|
2021-04-23 10:21:25 +05:00
|
|
|
|
}
|
|
|
|
|
}
|
2023-04-18 16:22:53 +05:00
|
|
|
|
|
2021-04-23 10:21:25 +05:00
|
|
|
|
}
|