From 34488a631e4a12fa55a55e70baaf0e5b91fe72b3 Mon Sep 17 00:00:00 2001 From: Maciej Rybarczyk Date: Mon, 4 Jul 2022 19:22:02 +0200 Subject: [PATCH] Database configuration for plcs --- .../Configurations/MongoConfiguration.cs | 53 +++++++++++++++---- 1 file changed, 44 insertions(+), 9 deletions(-) diff --git a/src/Zeus.Infrastructure/Repositories/Configurations/MongoConfiguration.cs b/src/Zeus.Infrastructure/Repositories/Configurations/MongoConfiguration.cs index de53727..8a52232 100644 --- a/src/Zeus.Infrastructure/Repositories/Configurations/MongoConfiguration.cs +++ b/src/Zeus.Infrastructure/Repositories/Configurations/MongoConfiguration.cs @@ -4,6 +4,8 @@ using MongoDB.Driver.Linq; using Zeus.Domain.Devices; using Zeus.Domain.Locations; +using Zeus.Domain.Plcs.Meters; +using Zeus.Domain.Plcs.Rvds; using Zeus.Domain.Users; using Zeus.Enums.Users; using Zeus.Utilities.Extensions; @@ -20,14 +22,20 @@ public static async Task SetupAsync(IMongoDatabase database) private static async Task CreateIndexesAsync(IMongoDatabase database) { - await CreateLocationIndexesAsync(database); - await CreateLocationHistoryIndexesAsync(database); + await Task.WhenAll(new Task[] + { + CreateLocationIndexesAsync(database), + CreateLocationHistoryIndexesAsync(database), + + CreateDeviceIndexesAsync(database), + CreateDeviceHistoryIndexesAsync(database), - await CreateDeviceIndexesAsync(database); - await CreateDeviceHistoryIndexesAsync(database); + CreateUserIndexesAsync(database), + CreateUserHistoryIndexesAsync(database), - await CreateUserIndexesAsync(database); - await CreateUserHistoryIndexesAsync(database); + CreateMeterIndexesAsync(database), + CreateRvd145IndexesAsync(database), + }); } private static Task CreateLocationIndexesAsync(IMongoDatabase database) @@ -120,9 +128,37 @@ private static Task CreateUserHistoryIndexesAsync(IMongoDatabase database) return CreateIndexesAsync(list, database); } - private static async Task CreateDataAsync(IMongoDatabase database) + private static Task CreateMeterIndexesAsync(IMongoDatabase database) { - await CreateUserDataAsync(database); + List> list = new() + { + new CreateIndexModel(Builders.IndexKeys.Combine( + Builders.IndexKeys.Ascending(x => x.DeviceId), + Builders.IndexKeys.Ascending(x => x.Date) + ), + new CreateIndexOptions { Name = $"{nameof(Meter.DeviceId)}-{nameof(Meter.Date)}", Unique = true, Background = true }) + }; + + return CreateIndexesAsync(list, database); + } + + private static Task CreateRvd145IndexesAsync(IMongoDatabase database) + { + List> list = new() + { + new CreateIndexModel(Builders.IndexKeys.Combine( + Builders.IndexKeys.Ascending(x => x.DeviceId), + Builders.IndexKeys.Ascending(x => x.Date) + ), + new CreateIndexOptions { Name = $"{nameof(Rvd145.DeviceId)}-{nameof(Rvd145.Date)}", Unique = true, Background = true }) + }; + + return CreateIndexesAsync(list, database); + } + + private static Task CreateDataAsync(IMongoDatabase database) + { + return CreateUserDataAsync(database); } private static async Task CreateUserDataAsync(IMongoDatabase database) @@ -152,6 +188,5 @@ private static Task CreateIndexesAsync(IEnumerable> index IMongoCollection collection = database.GetCollection(typeof(T).Name); return collection.Indexes.CreateManyAsync(indexModel); } - } }