Work on the datta handler
This commit is contained in:
@@ -1,13 +1,25 @@
|
|||||||
using SzCore;
|
using SzCore;
|
||||||
|
using SzCore.Defaults;
|
||||||
|
|
||||||
namespace SzCli;
|
namespace SzCli;
|
||||||
|
|
||||||
public class Logger(ISzFileManager fileManager) : ISzLogger
|
public class Logger : ISzLogger
|
||||||
{
|
{
|
||||||
|
private DefaultLocalFileManager _fileManager;
|
||||||
public bool LogToFile { get; set; } = true;
|
public bool LogToFile { get; set; } = true;
|
||||||
public string LogFilePath { get; set; } = Path.Combine(fileManager.DataPath, "log.txt");
|
public string LogFilePath { get; set; }
|
||||||
public int LogFileMaxLines { get; set; } = 100;
|
public int LogFileMaxLines { get; set; } = 100;
|
||||||
|
|
||||||
|
public Logger(DefaultLocalFileManager fm)
|
||||||
|
{
|
||||||
|
_fileManager = fm;
|
||||||
|
LogFilePath = Path.Combine(_fileManager.DataPath, "log.txt");
|
||||||
|
AppDomain.CurrentDomain.FirstChanceException += (sender, eventArgs) =>
|
||||||
|
{
|
||||||
|
LogError($"[EXCEPTION] {eventArgs.Exception}");
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
public void Log(string text)
|
public void Log(string text)
|
||||||
{
|
{
|
||||||
Console.WriteLine($"{DateTime.UtcNow} : [SZ LOG] {text}");
|
Console.WriteLine($"{DateTime.UtcNow} : [SZ LOG] {text}");
|
||||||
|
|||||||
@@ -28,11 +28,3 @@ public class Program
|
|||||||
// _logger.Log(loadedDataset?.DataObjects["test"].Fields["value"].Value ?? "Could not get value of test.value");
|
// _logger.Log(loadedDataset?.DataObjects["test"].Fields["value"].Value ?? "Could not get value of test.value");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public class WeirdThing
|
|
||||||
{
|
|
||||||
public void Yuh()
|
|
||||||
{
|
|
||||||
SZ.Logger.Log("Service test success");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -2,21 +2,16 @@ namespace SzCore.Defaults;
|
|||||||
|
|
||||||
public class DefaultLocalFileManager : ISzFileManager
|
public class DefaultLocalFileManager : ISzFileManager
|
||||||
{
|
{
|
||||||
public string DataPath
|
public string DataPath => Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "data");
|
||||||
{
|
|
||||||
get
|
|
||||||
{
|
|
||||||
var path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "data");
|
|
||||||
if (!Directory.Exists(path))
|
|
||||||
{
|
|
||||||
Directory.CreateDirectory(path);
|
|
||||||
}
|
|
||||||
|
|
||||||
return path;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public string DatasetsPath => Path.Combine(DataPath, "datasets");
|
public string DatasetsPath => Path.Combine(DataPath, "datasets");
|
||||||
|
public string TemplatesPath => Path.Combine(DataPath, "templates");
|
||||||
|
|
||||||
|
public DefaultLocalFileManager()
|
||||||
|
{
|
||||||
|
Directory.CreateDirectory(DataPath);
|
||||||
|
Directory.CreateDirectory(DatasetsPath);
|
||||||
|
Directory.CreateDirectory(TemplatesPath);
|
||||||
|
}
|
||||||
|
|
||||||
public bool SaveFile(string path, string fileContent)
|
public bool SaveFile(string path, string fileContent)
|
||||||
{
|
{
|
||||||
|
|||||||
6
SzCore/ISzDatabaseHandler.cs
Normal file
6
SzCore/ISzDatabaseHandler.cs
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
namespace SzCore;
|
||||||
|
|
||||||
|
public interface ISzDatabaseHandler
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
@@ -6,6 +6,7 @@ public interface ISzFileManager
|
|||||||
{
|
{
|
||||||
public string DataPath {get;}
|
public string DataPath {get;}
|
||||||
public string DatasetsPath {get;}
|
public string DatasetsPath {get;}
|
||||||
|
public string TemplatesPath {get;}
|
||||||
|
|
||||||
public bool SaveFile(string path, string fileContent);
|
public bool SaveFile(string path, string fileContent);
|
||||||
public string? LoadFile(string path);
|
public string? LoadFile(string path);
|
||||||
|
|||||||
@@ -32,8 +32,8 @@ public static class SZ
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static Dictionary<Type, object> _services = [];
|
private static readonly Dictionary<Type, object> _services = [];
|
||||||
private static List<Type> _protectedServices = [];
|
private static readonly List<Type> _protectedServices = [];
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Initalizes the SZ singleton in SzCore
|
/// Initalizes the SZ singleton in SzCore
|
||||||
|
|||||||
@@ -1,9 +1,12 @@
|
|||||||
<Project Sdk="Microsoft.NET.Sdk">
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<TargetFramework>net8.0</TargetFramework>
|
<TargetFramework>net8.0</TargetFramework>
|
||||||
<ImplicitUsings>enable</ImplicitUsings>
|
<ImplicitUsings>enable</ImplicitUsings>
|
||||||
<Nullable>enable</Nullable>
|
<Nullable>enable</Nullable>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
<ItemGroup>
|
||||||
|
<PackageReference Include="sqlite-net-pcl" Version="1.9.172" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
|||||||
@@ -1,6 +1,85 @@
|
|||||||
|
using SzCore.DataObjects;
|
||||||
|
|
||||||
namespace SzCore;
|
namespace SzCore;
|
||||||
|
|
||||||
public class SzDataHandler
|
public class SzDataHandler
|
||||||
{
|
{
|
||||||
|
private Dictionary<string, SzDataset> _loadedDatasets = [];
|
||||||
|
|
||||||
|
public SzDataHandler(){}
|
||||||
|
|
||||||
|
public bool SaveDataset(SzDataset dataset)
|
||||||
|
{
|
||||||
|
var datasetPath = Path.Combine(SZ.LocalFileManager.DatasetsPath, dataset.Id, "dataset.json");
|
||||||
|
try
|
||||||
|
{
|
||||||
|
return SZ.LocalFileManager.SaveFile(datasetPath, SZ.SzParser.SerializeDatasetToJson(dataset));
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
SZ.Logger.LogError("Error saving dataset: " + e.Message);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public SzDataset? LoadDataset(string datasetId)
|
||||||
|
{
|
||||||
|
if (_loadedDatasets.TryGetValue(datasetId, out SzDataset? value))
|
||||||
|
{
|
||||||
|
if (value is not null) return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
var datasetPath = Path.Combine(SZ.LocalFileManager.DataPath, "datasets", datasetId, "dataset.json");
|
||||||
|
|
||||||
|
var json = SZ.LocalFileManager.LoadFile(datasetPath);
|
||||||
|
if (json is null)
|
||||||
|
{
|
||||||
|
SZ.Logger.LogWarning($"Could not load dataset with ID '{datasetId}'");
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
var parsedDataset = SZ.SzParser.DeserializeDataset(json);
|
||||||
|
if (parsedDataset is null)
|
||||||
|
{
|
||||||
|
SZ.Logger.LogWarning($"Could not load dataset with ID '{datasetId}'");
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
_loadedDatasets.Add(parsedDataset.Id, parsedDataset);
|
||||||
|
return parsedDataset;
|
||||||
|
}
|
||||||
|
|
||||||
|
// The client should periodically call this to free memory, probably
|
||||||
|
private void ClearLoadedDatasets()
|
||||||
|
{
|
||||||
|
_loadedDatasets.Clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Only use this for test purposes, we probably don't want to load all datasets to memory in production
|
||||||
|
private void LoadAllDatasets()
|
||||||
|
{
|
||||||
|
ClearLoadedDatasets();
|
||||||
|
if (!Directory.Exists(SZ.LocalFileManager.DatasetsPath))
|
||||||
|
{
|
||||||
|
throw new Exception("Could not load datasets, the datase directory does not exist.");
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach (var dir in Directory.GetDirectories(SZ.LocalFileManager.DatasetsPath))
|
||||||
|
{
|
||||||
|
var datasetFilePath = Path.Combine(dir, "dataset.json");
|
||||||
|
if (!File.Exists(datasetFilePath)) continue;
|
||||||
|
|
||||||
|
var datasetJson = SZ.LocalFileManager.LoadFile(datasetFilePath);
|
||||||
|
if (datasetJson == null) continue;
|
||||||
|
|
||||||
|
var parsedDataset = SZ.SzParser.DeserializeDataset(datasetJson);
|
||||||
|
if (parsedDataset == null)
|
||||||
|
{
|
||||||
|
SZ.Logger.LogWarning($"Parse error: Could not parse file {datasetFilePath} as an SzDataset");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
_loadedDatasets.Add(parsedDataset.Id, parsedDataset);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -34,26 +34,5 @@ public class SzParser
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public bool SaveDataset(SzDataset dataset)
|
|
||||||
{
|
|
||||||
var datasetPath = Path.Combine(SZ.LocalFileManager.DatasetsPath, dataset.Id, "dataset.json");
|
|
||||||
try
|
|
||||||
{
|
|
||||||
return SZ.LocalFileManager.SaveFile(datasetPath, SerializeDatasetToJson(dataset));
|
|
||||||
}
|
|
||||||
catch (Exception e)
|
|
||||||
{
|
|
||||||
SZ.Logger.LogError("Error saving dataset: " + e.Message);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public SzDataset? LoadDataset(string datasetId)
|
|
||||||
{
|
|
||||||
var datasetPath = Path.Combine(SZ.LocalFileManager.DataPath, "datasets", datasetId, "dataset.json");
|
|
||||||
var json = SZ.LocalFileManager.LoadFile(datasetPath);
|
|
||||||
return json is null ? null : DeserializeDataset(json);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user