From 5d99b9e4e80a63e48ed8d226a733f66c0a61d313 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Tue, 26 Dec 2023 20:56:36 -0600 Subject: add logic to return a list of previous/active scans --- src/slskd/Shares/IShareRepository.cs | 8 +++++++ src/slskd/Shares/SqliteShareRepository.cs | 38 +++++++++++++++++++++++++++---- 2 files changed, 42 insertions(+), 4 deletions(-) (limited to 'src') 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; /// @@ -130,6 +131,13 @@ namespace slskd.Shares /// The list of files. IEnumerable ListFiles(string parentDirectory = null, bool includeFullPath = false); + /// + /// Returns the list of all s matching the optionally specified . + /// + /// An optional expression used to filter scans. + /// The operation context, including the list of found scans. + IEnumerable ListScans(Expression> predicate = null); + /// /// Deletes directory records with a timestamp prior to the specified . /// 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) { @@ -419,6 +420,35 @@ namespace slskd.Shares } } + /// + /// Returns the list of all s matching the optionally specified . + /// + /// An optional expression used to filter scans. + /// The operation context, including the list of found scans. + public IEnumerable ListScans(Expression> 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(); + + 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; + } + /// /// Deletes directory records with a timestamp prior to the specified . /// -- cgit v1.2.3