Merge pull request #99 from LD-Reborn/85-add-database-size-to-stats

Added database size to stats, added total ram size to stats
This commit is contained in:
LD50
2026-01-19 03:40:25 +01:00
committed by GitHub
4 changed files with 53 additions and 5 deletions

View File

@@ -1,12 +1,8 @@
namespace Server.Controllers; namespace Server.Controllers;
using System.Reflection;
using System.Text.Json;
using AdaptiveExpressions;
using ElmahCore; using ElmahCore;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options; using Microsoft.Extensions.Options;
using Server.Exceptions;
using Server.Helper; using Server.Helper;
using Server.Models; using Server.Models;
using Shared; using Shared;
@@ -73,6 +69,7 @@ public class ServerController : ControllerBase
embeddingsCount += entry.Keys.Count; embeddingsCount += entry.Keys.Count;
} }
var sqlHelper = DatabaseHelper.GetSQLHelper(_options.Value); var sqlHelper = DatabaseHelper.GetSQLHelper(_options.Value);
var databaseTotalSize = DatabaseHelper.GetTotalDatabaseSize(sqlHelper);
Task<long> entityCountTask = DatabaseHelper.CountEntities(sqlHelper); Task<long> entityCountTask = DatabaseHelper.CountEntities(sqlHelper);
long queryCacheUtilization = 0; long queryCacheUtilization = 0;
long queryCacheElementCount = 0; long queryCacheElementCount = 0;
@@ -95,6 +92,10 @@ public class ServerController : ControllerBase
} }
}; };
long entityCount = await entityCountTask; long entityCount = await entityCountTask;
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
long ramTotalSize = GC.GetTotalMemory(false);
return new ServerGetStatsResult() { return new ServerGetStatsResult() {
Success = true, Success = true,
@@ -106,7 +107,9 @@ public class ServerController : ControllerBase
EmbeddingCacheUtilization = size, EmbeddingCacheUtilization = size,
EmbeddingCacheMaxElementCount = _searchdomainManager.EmbeddingCacheMaxCount, EmbeddingCacheMaxElementCount = _searchdomainManager.EmbeddingCacheMaxCount,
EmbeddingCacheElementCount = elementCount, EmbeddingCacheElementCount = elementCount,
EmbeddingsCount = embeddingsCount EmbeddingsCount = embeddingsCount,
DatabaseTotalSize = databaseTotalSize,
RamTotalSize = ramTotalSize
}; };
} catch (Exception ex) } catch (Exception ex)
{ {

View File

@@ -224,6 +224,21 @@ public class DatabaseHelper(ILogger<DatabaseHelper> logger)
return result; return result;
} }
public static long GetTotalDatabaseSize(SQLHelper helper)
{
Dictionary<string, dynamic> parameters = [];
DbDataReader searchdomainSumReader = helper.ExecuteSQLCommand("SELECT SUM(Data_length) FROM information_schema.tables", parameters);
try
{
bool success = searchdomainSumReader.Read();
long result = success && !searchdomainSumReader.IsDBNull(0) ? searchdomainSumReader.GetInt64(0) : 0;
return result;
} finally
{
searchdomainSumReader.Close();
}
}
public static async Task<long> CountEntities(SQLHelper helper) public static async Task<long> CountEntities(SQLHelper helper)
{ {
DbDataReader searchdomainSumReader = helper.ExecuteSQLCommand("SELECT COUNT(*) FROM entity;", []); DbDataReader searchdomainSumReader = helper.ExecuteSQLCommand("SELECT COUNT(*) FROM entity;", []);

View File

@@ -24,6 +24,24 @@
<div class="row g-4"> <div class="row g-4">
<!-- Server -->
<div class="col-md-6">
<div class="card shadow-sm h-100">
<div class="card-body">
<h2 class="card-title fs-5">@T["Server"]</h2>
<div class="d-flex justify-content-between mt-2">
<span>@T["Total RAM usage"]</span>
<strong id="serverMemorySize"></strong>
</div>
<div class="d-flex justify-content-between mt-2">
<span>@T["Total Database size"]</span>
<strong id="serverDatabaseSize"></strong>
</div>
</div>
</div>
</div>
<!-- Embedding Cache --> <!-- Embedding Cache -->
<div class="col-md-6"> <div class="col-md-6">
<div class="card shadow-sm h-100"> <div class="card shadow-sm h-100">
@@ -175,6 +193,10 @@
showThrobber(querycacheLoadedMaxElementCount); showThrobber(querycacheLoadedMaxElementCount);
let querycacheLoadedElementCountProgressBar = document.getElementById("querycacheLoadedElementCountProgressBar"); let querycacheLoadedElementCountProgressBar = document.getElementById("querycacheLoadedElementCountProgressBar");
let serverMemorySize = document.getElementById("serverMemorySize");
showThrobber(serverMemorySize);
let serverDatabaseSize = document.getElementById("serverDatabaseSize");
showThrobber(serverDatabaseSize);
let healthchecksServer = document.getElementById("healthchecksServer"); let healthchecksServer = document.getElementById("healthchecksServer");
let healthchecksAiProvider = document.getElementById("healthchecksAiProvider"); let healthchecksAiProvider = document.getElementById("healthchecksAiProvider");
@@ -214,6 +236,10 @@
hideThrobber(querycacheLoadedMaxElementCount); hideThrobber(querycacheLoadedMaxElementCount);
querycacheLoadedMaxElementCount.textContent = queryCacheMaxElementCountLoadedSearchdomainsOnly.toLocaleString(); querycacheLoadedMaxElementCount.textContent = queryCacheMaxElementCountLoadedSearchdomainsOnly.toLocaleString();
querycacheLoadedMaxElementCountProgressBar.style.width = `${queryCacheElementCount / queryCacheMaxElementCountLoadedSearchdomainsOnly * 100}%`; querycacheLoadedMaxElementCountProgressBar.style.width = `${queryCacheElementCount / queryCacheMaxElementCountLoadedSearchdomainsOnly * 100}%`;
serverMemorySize.textContent = NumberOfBytesAsHumanReadable(result.RamTotalSize);
hideThrobber(serverMemorySize);
serverDatabaseSize.textContent = NumberOfBytesAsHumanReadable(result.DatabaseTotalSize);
hideThrobber(serverDatabaseSize);
}); });
getHealthCheckStatusAndApply(healthchecksServer, "/healthz/Database"); getHealthCheckStatusAndApply(healthchecksServer, "/healthz/Database");
getHealthCheckStatusAndApply(healthchecksAiProvider, "/healthz/AIProvider"); getHealthCheckStatusAndApply(healthchecksAiProvider, "/healthz/AIProvider");

View File

@@ -28,4 +28,8 @@ public class ServerGetStatsResult : SuccesMessageBaseModel
public long? QueryCacheMaxElementCountLoadedSearchdomainsOnly { get; set; } public long? QueryCacheMaxElementCountLoadedSearchdomainsOnly { get; set; }
[JsonPropertyName("QueryCacheUtilization")] [JsonPropertyName("QueryCacheUtilization")]
public long? QueryCacheUtilization { get; set; } public long? QueryCacheUtilization { get; set; }
[JsonPropertyName("DatabaseTotalSize")]
public long? DatabaseTotalSize { get; set; }
[JsonPropertyName("RamTotalSize")]
public long? RamTotalSize { get; set; }
} }