217836099c
- fix program.cs startup - fix calcolo dim DB - fix IOB count
174 lines
6.8 KiB
C#
174 lines
6.8 KiB
C#
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<MoonProContext> _ctxFactory;
|
|
private readonly IDbContextFactory<MoonPro_FluxContext> _ctxFactoryFluxLog;
|
|
private readonly IDbContextFactory<MoonPro_STATSContext> _ctxFactoryStats;
|
|
|
|
#endregion
|
|
|
|
#region Public Constructors
|
|
|
|
public MpLandRepository(
|
|
IConfiguration configuration,
|
|
IDbContextFactory<MoonProContext> ctxFactory,
|
|
IDbContextFactory<MoonPro_FluxContext> ctxFactoryFL,
|
|
IDbContextFactory<MoonPro_STATSContext> ctxFactorySta)
|
|
{
|
|
_configuration = configuration;
|
|
_ctxFactory = ctxFactory;
|
|
_ctxFactoryFluxLog = ctxFactoryFL;
|
|
_ctxFactoryStats= ctxFactorySta;
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Public Methods
|
|
|
|
/// <inheritdoc />
|
|
public async Task<List<DbSizeModel>> AllDbInfoAsync()
|
|
{
|
|
List<DbSizeModel> dbResult = new List<DbSizeModel>();
|
|
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;
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public async Task<List<ConfigModel>> ConfigGetAllAsync()
|
|
{
|
|
await using var dbCtx = await _ctxFactory.CreateDbContextAsync();
|
|
return await dbCtx
|
|
.DbSetConfig
|
|
.AsNoTracking()
|
|
.OrderBy(x => x.Chiave)
|
|
.ToListAsync() ?? new();
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public async Task<List<MacchineModel>> MacchineGetAllAsync()
|
|
{
|
|
await using var dbCtx = await _ctxFactory.CreateDbContextAsync();
|
|
return await dbCtx
|
|
.DbSetMacchine
|
|
.ToListAsync() ?? new();
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public async Task<List<RemoteRebootLogModel>> RemRebootLogGetAllAsync()
|
|
{
|
|
await using var dbCtx = await _ctxFactory.CreateDbContextAsync();
|
|
return await dbCtx
|
|
.DbSetRemRebLog
|
|
.AsNoTracking()
|
|
.OrderByDescending(x => x.IdxReboot)
|
|
.ToListAsync() ?? new();
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public async Task<List<RemoteRebootLogModel>> RemRebootLogGetLastAsync()
|
|
{
|
|
await using var dbCtx = await _ctxFactory.CreateDbContextAsync();
|
|
return await dbCtx
|
|
.DbSetRemRebLog
|
|
.FromSqlRaw("EXEC stp_RRL_getLast")
|
|
.AsNoTracking()
|
|
.ToListAsync() ?? new();
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public async Task<List<RemoteRebootLogModel>> RemRebootLogGetLastNoMaccAsync()
|
|
{
|
|
await using var dbCtx = await _ctxFactory.CreateDbContextAsync();
|
|
return await dbCtx
|
|
.DbSetRemRebLog
|
|
.FromSqlRaw("EXEC stp_RRL_GetLastNoMachine")
|
|
.AsNoTracking()
|
|
.ToListAsync() ?? new();
|
|
}
|
|
|
|
#endregion
|
|
}
|
|
}
|