2023-03-15 13:38:06 +00:00
|
|
|
using System.Reactive.Linq;
|
|
|
|
using InnovEnergy.App.Backend.DataTypes;
|
|
|
|
using InnovEnergy.App.Backend.Relations;
|
2023-02-16 12:57:06 +00:00
|
|
|
using InnovEnergy.Lib.Utils;
|
|
|
|
using SQLite;
|
|
|
|
|
2023-03-15 13:38:06 +00:00
|
|
|
|
|
|
|
|
2023-03-08 12:20:33 +00:00
|
|
|
namespace InnovEnergy.App.Backend.Database;
|
2023-02-16 12:57:06 +00:00
|
|
|
|
2023-03-15 13:38:06 +00:00
|
|
|
|
2023-03-13 10:36:50 +00:00
|
|
|
public static partial class Db
|
2023-02-16 12:57:06 +00:00
|
|
|
{
|
|
|
|
internal const String DbPath = "./db.sqlite";
|
2023-03-09 15:32:11 +00:00
|
|
|
|
2023-03-15 13:38:06 +00:00
|
|
|
private static SQLiteConnection Connection { get; } = new SQLiteConnection(DbPath);
|
2023-03-13 10:48:04 +00:00
|
|
|
|
|
|
|
public static TableQuery<Session> Sessions => Connection.Table<Session>();
|
|
|
|
public static TableQuery<Folder> Folders => Connection.Table<Folder>();
|
|
|
|
public static TableQuery<Installation> Installations => Connection.Table<Installation>();
|
|
|
|
public static TableQuery<User> Users => Connection.Table<User>();
|
|
|
|
public static TableQuery<User2Folder> User2Folder => Connection.Table<User2Folder>();
|
|
|
|
public static TableQuery<User2Installation> User2Installation => Connection.Table<User2Installation>();
|
2023-03-09 15:32:11 +00:00
|
|
|
|
|
|
|
|
2023-02-16 12:57:06 +00:00
|
|
|
static Db()
|
|
|
|
{
|
|
|
|
// on startup create/migrate tables
|
2023-03-09 15:32:11 +00:00
|
|
|
|
2023-03-15 13:38:06 +00:00
|
|
|
Connection.RunInTransaction(() =>
|
2023-02-16 12:57:06 +00:00
|
|
|
{
|
2023-03-15 13:38:06 +00:00
|
|
|
Connection.CreateTable<User>();
|
|
|
|
Connection.CreateTable<Installation>();
|
|
|
|
Connection.CreateTable<Folder>();
|
|
|
|
Connection.CreateTable<User2Folder>();
|
|
|
|
Connection.CreateTable<User2Installation>();
|
|
|
|
Connection.CreateTable<Session>();
|
2023-02-16 12:57:06 +00:00
|
|
|
});
|
2023-03-09 15:32:11 +00:00
|
|
|
|
|
|
|
|
2023-03-15 13:38:06 +00:00
|
|
|
Observable.Interval(TimeSpan.FromDays(1))
|
|
|
|
.StartWith(0) // Do it right away (on startup)
|
|
|
|
.Subscribe(Cleanup); // and then daily
|
2023-02-16 12:57:06 +00:00
|
|
|
}
|
2023-03-09 15:32:11 +00:00
|
|
|
|
2023-03-15 13:38:06 +00:00
|
|
|
|
|
|
|
|
|
|
|
private static Boolean RunTransaction(Func<Boolean> func)
|
2023-02-16 12:57:06 +00:00
|
|
|
{
|
2023-03-15 13:38:06 +00:00
|
|
|
var savepoint = Connection.SaveTransactionPoint();
|
|
|
|
var success = false;
|
|
|
|
|
2023-02-16 12:57:06 +00:00
|
|
|
try
|
|
|
|
{
|
2023-03-15 13:38:06 +00:00
|
|
|
success = func();
|
2023-02-16 12:57:06 +00:00
|
|
|
}
|
2023-03-15 13:38:06 +00:00
|
|
|
finally
|
2023-02-16 12:57:06 +00:00
|
|
|
{
|
2023-03-15 13:38:06 +00:00
|
|
|
if (success)
|
|
|
|
Connection.Release(savepoint);
|
|
|
|
else
|
|
|
|
Connection.RollbackTo(savepoint);
|
2023-02-16 12:57:06 +00:00
|
|
|
}
|
2023-03-09 15:32:11 +00:00
|
|
|
|
2023-03-15 13:38:06 +00:00
|
|
|
return success;
|
2023-02-16 12:57:06 +00:00
|
|
|
}
|
2023-03-09 15:32:11 +00:00
|
|
|
|
2023-03-15 13:38:06 +00:00
|
|
|
|
2023-03-09 15:32:11 +00:00
|
|
|
|
2023-03-16 08:15:59 +00:00
|
|
|
|
2023-02-16 12:57:06 +00:00
|
|
|
|
|
|
|
|
2023-03-15 13:38:06 +00:00
|
|
|
private static void Cleanup(Int64 _)
|
2023-02-21 08:58:21 +00:00
|
|
|
{
|
2023-03-15 13:38:06 +00:00
|
|
|
DeleteS3Keys();
|
|
|
|
DeleteStaleSessions();
|
2023-02-21 08:58:21 +00:00
|
|
|
}
|
2023-03-09 11:50:21 +00:00
|
|
|
|
2023-03-15 13:38:06 +00:00
|
|
|
private static void DeleteStaleSessions()
|
2023-03-09 11:50:21 +00:00
|
|
|
{
|
2023-03-15 13:38:06 +00:00
|
|
|
var deadline = DateTime.Now - Session.MaxAge;
|
|
|
|
Sessions.Delete(s => s.LastSeen < deadline);
|
2023-03-09 11:50:21 +00:00
|
|
|
}
|
2023-03-09 12:20:37 +00:00
|
|
|
|
2023-03-15 13:38:06 +00:00
|
|
|
private static void DeleteS3Keys()
|
2023-03-09 12:20:37 +00:00
|
|
|
{
|
2023-03-15 13:38:06 +00:00
|
|
|
void DeleteKeys() => Installations.Do(i => i.S3Key = "").ForEach(Update); // TODO
|
|
|
|
|
|
|
|
Connection.RunInTransaction(DeleteKeys);
|
2023-03-09 12:20:37 +00:00
|
|
|
}
|
2023-03-09 15:32:11 +00:00
|
|
|
}
|