using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Configuration; using MP.Data.DbModels; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; namespace MP.Data.Repository.MpLand { public class MpLandRepository : IMpLandRepository { #region Private Fields private readonly IConfiguration _configuration; private readonly IDbContextFactory _ctxFactory; private readonly IDbContextFactory _ctxFactoryFluxLog; private readonly IDbContextFactory _ctxFactoryStats; #endregion #region Public Constructors public MpLandRepository( IConfiguration configuration, IDbContextFactory ctxFactory, IDbContextFactory ctxFactoryFL, IDbContextFactory ctxFactorySta) { _configuration = configuration; _ctxFactory = ctxFactory; _ctxFactoryFluxLog = ctxFactoryFL; _ctxFactoryStats= ctxFactorySta; } #endregion #region Public Methods /// public async Task> AllDbInfoAsync() { List dbResult = new List(); string stp_DbInfo = @" ;WITH TableRowCounts AS ( SELECT t.name AS TableName, SUM(p.rows) AS RowNum FROM sys.tables t JOIN sys.partitions p ON t.object_id = p.object_id WHERE p.index_id IN (0, 1) GROUP BY t.name ), LargestTable AS ( SELECT TOP 1 RowNum, TableName FROM TableRowCounts ORDER BY RowNum DESC ) SELECT DB_name() as DbName, CAST(SUM(size) * 8.0 / 1024 AS DECIMAL(18,2)) AS DbSizeMb, (SELECT COUNT(*) FROM sys.tables) AS NumTables, (SELECT TableName FROM LargestTable) AS BigTable, (SELECT RowNum FROM LargestTable) AS BigTableRows FROM sys.master_files WHERE database_id = DB_ID(); "; try { if (!string.IsNullOrEmpty(_configuration.GetConnectionString("MP.All"))) { await using var dbCtx = await _ctxFactory.CreateDbContextAsync(); var singleRes = dbCtx .DbSetDbSize .FromSqlRaw(stp_DbInfo) .AsNoTracking() .AsEnumerable() .FirstOrDefault(); if (singleRes != null) { dbResult.Add(singleRes); } } if (!string.IsNullOrEmpty(_configuration.GetConnectionString("MP.Flux"))) { await using var dbCtx = await _ctxFactoryFluxLog.CreateDbContextAsync(); var singleRes = dbCtx .DbSetDbSize .FromSqlRaw(stp_DbInfo) .AsNoTracking() .AsEnumerable() .FirstOrDefault(); if (singleRes != null) { dbResult.Add(singleRes); } } if (!string.IsNullOrEmpty(_configuration.GetConnectionString("MP.Stats"))) { await using var dbCtx = await _ctxFactoryStats.CreateDbContextAsync(); var singleRes = dbCtx .DbSetDbSize .FromSqlRaw(stp_DbInfo) .AsNoTracking() .AsEnumerable() .FirstOrDefault(); if (singleRes != null) { dbResult.Add(singleRes); } } } catch { } return dbResult; } /// public async Task> ConfigGetAllAsync() { await using var dbCtx = await _ctxFactory.CreateDbContextAsync(); return await dbCtx .DbSetConfig .AsNoTracking() .OrderBy(x => x.Chiave) .ToListAsync() ?? new(); } /// public async Task> MacchineGetAllAsync() { await using var dbCtx = await _ctxFactory.CreateDbContextAsync(); return await dbCtx .DbSetMacchine .ToListAsync() ?? new(); } /// public async Task> RemRebootLogGetAllAsync() { await using var dbCtx = await _ctxFactory.CreateDbContextAsync(); return await dbCtx .DbSetRemRebLog .AsNoTracking() .OrderByDescending(x => x.IdxReboot) .ToListAsync() ?? new(); } /// public async Task> RemRebootLogGetLastAsync() { await using var dbCtx = await _ctxFactory.CreateDbContextAsync(); return await dbCtx .DbSetRemRebLog .FromSqlRaw("EXEC stp_RRL_getLast") .AsNoTracking() .ToListAsync() ?? new(); } /// public async Task> RemRebootLogGetLastNoMaccAsync() { await using var dbCtx = await _ctxFactory.CreateDbContextAsync(); return await dbCtx .DbSetRemRebLog .FromSqlRaw("EXEC stp_RRL_GetLastNoMachine") .AsNoTracking() .ToListAsync() ?? new(); } #endregion } }