Inizio gestione calcolo spazio DB

This commit is contained in:
Samuele Locatelli
2025-07-04 10:54:30 +02:00
parent 4f841a09ad
commit 0829144478
18 changed files with 345 additions and 5 deletions
+83
View File
@@ -31,6 +31,89 @@ namespace MP.Data.Controllers
#region Public Methods
/// <summary>
/// Restituisce info dimensione, tabelle e num righe DB gestiti
/// </summary>
/// <returns></returns>
public List<DbSizeModel> AllDbInfo()
{
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) -- heap or clustered index
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();
";
// leggo per DB principale
if (!string.IsNullOrEmpty(_configuration.GetConnectionString("MP.All")))
{
using (var dbCtx = new MoonProContext(_configuration))
{
var singleRes = dbCtx
.DbSetDbSize
.FromSqlRaw(stp_DbInfo)
.AsEnumerable()
.FirstOrDefault();
if (singleRes != null)
{
dbResult.Add(singleRes);
}
}
}
// leggo per FluxLog
if (!string.IsNullOrEmpty(_configuration.GetConnectionString("MP.FluxLog")))
{
using (var dbCtx = new MoonPro_FluxContext(_configuration))
{
var singleRes = dbCtx
.DbSetDbSize
.FromSqlRaw(stp_DbInfo)
.AsEnumerable()
.FirstOrDefault();
if (singleRes != null)
{
dbResult.Add(singleRes);
}
}
}
// leggo per Stats
if (!string.IsNullOrEmpty(_configuration.GetConnectionString("MP.Stats")))
{
using (var dbCtx = new MoonPro_STATSContext(_configuration))
{
var singleRes = dbCtx
.DbSetDbSize
.FromSqlRaw(stp_DbInfo)
.AsEnumerable()
.FirstOrDefault();
if (singleRes != null)
{
dbResult.Add(singleRes);
}
}
}
return dbResult;
}
/// <summary>
/// Elenco da tabella Config
/// </summary>
+1
View File
@@ -9,6 +9,7 @@ using System.Collections.Generic;
using System.Data;
using System.Drawing.Drawing2D;
using System.Linq;
using ZXing;
using static MP.Core.Objects.Enums;
namespace MP.Data.Controllers