aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJP Dillingham <jp@dillingham.me>2023-12-26 20:56:36 -0600
committerJP Dillingham <jp@dillingham.me>2023-12-26 20:56:36 -0600
commit5d99b9e4e80a63e48ed8d226a733f66c0a61d313 (patch)
tree00829dca48d78e4116008a5e4b3efe509a5abcbc
parentbb2f31e9a6f82763d1146c7b64a643192a5ab775 (diff)
downloadslskd-5d99b9e4e80a63e48ed8d226a733f66c0a61d313.tar.gz
add logic to return a list of previous/active scans
-rw-r--r--src/slskd/Shares/IShareRepository.cs8
-rw-r--r--src/slskd/Shares/SqliteShareRepository.cs38
2 files changed, 42 insertions, 4 deletions
diff --git a/src/slskd/Shares/IShareRepository.cs b/src/slskd/Shares/IShareRepository.cs
index f1e59889..5546bff7 100644
--- a/src/slskd/Shares/IShareRepository.cs
+++ b/src/slskd/Shares/IShareRepository.cs
@@ -19,6 +19,7 @@ namespace slskd.Shares
{
using System;
using System.Collections.Generic;
+ using System.Linq.Expressions;
using Soulseek;
/// <summary>
@@ -131,6 +132,13 @@ namespace slskd.Shares
IEnumerable<File> ListFiles(string parentDirectory = null, bool includeFullPath = false);
/// <summary>
+ /// Returns the list of all <see cref="Scan"/> s matching the optionally specified <paramref name="predicate"/>.
+ /// </summary>
+ /// <param name="predicate">An optional expression used to filter scans.</param>
+ /// <returns>The operation context, including the list of found scans.</returns>
+ IEnumerable<Scan> ListScans(Expression<Func<Scan, bool>> predicate = null);
+
+ /// <summary>
/// Deletes directory records with a timestamp prior to the specified <paramref name="olderThanTimestamp"/>.
/// </summary>
/// <param name="olderThanTimestamp">The timestamp before which to delete directories.</param>
diff --git a/src/slskd/Shares/SqliteShareRepository.cs b/src/slskd/Shares/SqliteShareRepository.cs
index f10b55e5..3591522d 100644
--- a/src/slskd/Shares/SqliteShareRepository.cs
+++ b/src/slskd/Shares/SqliteShareRepository.cs
@@ -20,6 +20,7 @@ namespace slskd.Shares
using System;
using System.Collections.Generic;
using System.Linq;
+ using System.Linq.Expressions;
using System.Timers;
using Microsoft.Data.Sqlite;
using Serilog;
@@ -38,11 +39,11 @@ namespace slskd.Shares
{
ConnectionString = connectionString;
- // in-memory databases will be destroyed if at any point the number of connections reaches zero to prevent this,
+ // in-memory databases will be destroyed if at any point the number of connections reaches zero. to prevent this,
// create a connection and hold it open for the duration of the application. SQLite will destroy this database when
- // the connection that created it closes. since this is the first connection, it *should* keep the db alive.
- // it's possible that this connection will be recycled somehow due to pooling; the app will crash if this happens
- // so i'll know to keep digging.
+ // the connection that created it closes. since this is the first connection, it *should* keep the db alive. it's
+ // possible that this connection will be recycled somehow due to pooling; the app will crash if this happens so i'll
+ // know to keep digging.
KeepaliveConnection = GetConnection(ConnectionString);
KeepaliveTimer = new Timer(1000)
{
@@ -420,6 +421,35 @@ namespace slskd.Shares
}
/// <summary>
+ /// Returns the list of all <see cref="Scan"/> s matching the optionally specified <paramref name="predicate"/>.
+ /// </summary>
+ /// <param name="predicate">An optional expression used to filter scans.</param>
+ /// <returns>The operation context, including the list of found scans.</returns>
+ public IEnumerable<Scan> ListScans(Expression<Func<Scan, bool>> predicate = null)
+ {
+ using var conn = GetConnection();
+ using var cmd = new SqliteCommand("SELECT timestamp, options, COALESCE(end, -1), suspect FROM scans", conn);
+
+ var reader = cmd.ExecuteReader();
+ var scans = new List<Scan>();
+
+ while (reader.Read())
+ {
+ var timestamp = reader.GetInt64(0);
+ var optionsJson = reader.GetString(1);
+
+ var endRaw = reader.GetInt64(2);
+ long? end = endRaw == -1 ? null : endRaw;
+
+ var suspect = reader.GetInt32(3) > 0;
+
+ scans.Add(new Scan { StartedAt = timestamp, OptionsJson = optionsJson, EndedAt = end, Suspect = suspect });
+ }
+
+ return scans;
+ }
+
+ /// <summary>
/// Deletes directory records with a timestamp prior to the specified <paramref name="olderThanTimestamp"/>.
/// </summary>
/// <param name="olderThanTimestamp">The timestamp before which to delete directories.</param>