Files
AI-Studio/app/MindWork AI Studio/Tools/Databases/DatabaseClient.cs
T

59 lines
1.8 KiB
C#
Raw Normal View History

2026-02-03 14:32:17 +01:00
namespace AIStudio.Tools.Databases;
public abstract class DatabaseClient(string name, string path)
2026-02-03 14:32:17 +01:00
{
public string Name => name;
public virtual string CacheKey => name;
public virtual DatabaseClientStatus Status => DatabaseClientStatus.AVAILABLE;
public bool IsAvailable => this.Status is DatabaseClientStatus.AVAILABLE;
2026-02-03 14:32:17 +01:00
private string Path => path;
2026-02-08 17:46:38 +01:00
private ILogger<DatabaseClient>? logger;
2026-02-03 14:32:17 +01:00
public abstract IAsyncEnumerable<(string Label, string Value)> GetDisplayInfo();
2026-02-08 17:46:38 +01:00
protected string GetStorageSize()
2026-02-03 14:32:17 +01:00
{
if (string.IsNullOrWhiteSpace(this.Path))
{
this.logger!.LogError($"Error: Database path '{this.Path}' cannot be null or empty.");
return "0 B";
}
if (!Directory.Exists(this.Path))
{
this.logger!.LogError($"Error: Database path '{this.Path}' does not exist.");
return "0 B";
}
var files = Directory.EnumerateFiles(this.Path, "*", SearchOption.AllDirectories)
.Where(file => !System.IO.Path.GetDirectoryName(file)!.Contains("cert", StringComparison.OrdinalIgnoreCase));
var size = files.Sum(file => new FileInfo(file).Length);
return FormatBytes(size);
}
2026-02-08 17:46:38 +01:00
private static string FormatBytes(long size)
2026-02-03 14:32:17 +01:00
{
string[] suffixes = { "B", "KB", "MB", "GB", "TB", "PB" };
int suffixIndex = 0;
2026-05-13 18:13:34 +02:00
double convertedSize = size;
2026-02-03 14:32:17 +01:00
2026-05-13 18:13:34 +02:00
while (convertedSize >= 1024 && suffixIndex < suffixes.Length - 1)
2026-02-03 14:32:17 +01:00
{
2026-05-13 18:13:34 +02:00
convertedSize /= 1024;
2026-02-03 14:32:17 +01:00
suffixIndex++;
}
2026-05-13 18:13:34 +02:00
return $"{convertedSize:0.##} {suffixes[suffixIndex]}";
2026-02-03 14:32:17 +01:00
}
public void SetLogger(ILogger<DatabaseClient> logService)
2026-02-03 14:32:17 +01:00
{
this.logger = logService;
}
public abstract void Dispose();
}