From 2bd1c00406c177140ce3903299f72ab92fe4b8e7 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Thu, 3 Nov 2022 09:25:17 -0500 Subject: recompute statistics and update state when hosts change --- src/slskd/Shares/IShareService.cs | 14 ++++---- src/slskd/Shares/ShareService.cs | 68 +++++++++++++++++++++++++++------------ 2 files changed, 55 insertions(+), 27 deletions(-) (limited to 'src') diff --git a/src/slskd/Shares/IShareService.cs b/src/slskd/Shares/IShareService.cs index 34f1bde8..dff6a8ce 100644 --- a/src/slskd/Shares/IShareService.cs +++ b/src/slskd/Shares/IShareService.cs @@ -53,6 +53,13 @@ namespace slskd.Shares /// The entire contents of the share. Task> BrowseAsync(Share share = null); + /// + /// Gets statistical information for the specified . + /// + /// The share to summarize. + /// The summary information. + Task<(int Directories, int Files)> ComputeShareStatisticsAsync(Share share); + /// /// Dumps the local share cache to a file. /// @@ -112,13 +119,6 @@ namespace slskd.Shares /// Thrown when a scan is already in progress. Task ScanAsync(); - /// - /// Gets summary information for the specified . - /// - /// The share to summarize. - /// The summary information. - Task<(int Directories, int Files)> SummarizeShareAsync(Share share); - /// /// Cancels the currently running scan on the local host, if one is running. /// diff --git a/src/slskd/Shares/ShareService.cs b/src/slskd/Shares/ShareService.cs index d6c6b977..a497012f 100644 --- a/src/slskd/Shares/ShareService.cs +++ b/src/slskd/Shares/ShareService.cs @@ -57,9 +57,7 @@ namespace slskd.Shares AllRepositories = new List(new[] { repository }); - Scanner = scanner ?? new ShareScanner( - shareRepository: repository, - workerCount: options.Shares.Cache.Workers); + Scanner = scanner ?? new ShareScanner(workerCount: options.Shares.Cache.Workers); Scanner.StateMonitor.OnChange(cacheState => { @@ -130,6 +128,12 @@ namespace slskd.Shares AllRepositories = HostDictionary.Values .Select(value => value.Repository) .Prepend(Local.Repository); + + State.SetValue(state => state with + { + Directories = Hosts.Sum(host => host.Shares.Sum(share => share.Directories)), + Files = Hosts.Sum(host => host.Shares.Sum(share => share.Directories)), + }); } /// @@ -183,6 +187,23 @@ namespace slskd.Shares return Task.FromResult(results); } + /// + /// Gets summary information for the specified . + /// + /// The share to summarize. + /// The summary information. + public Task<(int Directories, int Files)> ComputeShareStatisticsAsync(Share share) + { + var prefix = share.RemotePath + (share.RemotePath.EndsWith('\\') ? string.Empty : '\\'); + + var dirs = AllRepositories.Select(r => r.CountDirectories(prefix)).Sum(); + var files = AllRepositories.Select(r => r.CountFiles(prefix)).Sum(); + + share.UpdateStatistics(dirs, files); + + return Task.FromResult((dirs, files)); + } + /// /// Dumps the local share cache to a file. /// @@ -218,7 +239,20 @@ namespace slskd.Shares /// The name of the host. /// A value indicating whether the host was removed. public bool TryRemoveHost(string name) - => HostDictionary.TryRemove(name, out _); + { + if (HostDictionary.TryRemove(name, out _)) + { + State.SetValue(state => state with + { + Directories = Hosts.Sum(host => host.Shares.Sum(share => share.Directories)), + Files = Hosts.Sum(host => host.Shares.Sum(share => share.Directories)), + }); + + return true; + } + + return false; + } /// /// Returns the contents of the specified . @@ -293,25 +327,15 @@ namespace slskd.Shares /// Thrown when a scan is already in progress. public async Task ScanAsync() { - await Scanner.ScanAsync(Local.Host.Shares, OptionsMonitor.CurrentValue.Shares); + await Scanner.ScanAsync(Local.Host.Shares, OptionsMonitor.CurrentValue.Shares, Local.Repository); Log.Debug("Backing up shared file cache database..."); Local.Repository.BackupTo(ShareRepositoryFactory.CreateFromHostBackup(Local.Host.Name)); Log.Debug("Shared file cache database backup complete"); - } - /// - /// Gets summary information for the specified . - /// - /// The share to summarize. - /// The summary information. - public Task<(int Directories, int Files)> SummarizeShareAsync(Share share) - { - var prefix = share.RemotePath + (share.RemotePath.EndsWith('\\') ? string.Empty : '\\'); - - var dirs = AllRepositories.Select(r => r.CountDirectories(prefix)).Sum(); - var files = AllRepositories.Select(r => r.CountFiles(prefix)).Sum(); - return Task.FromResult((dirs, files)); + Log.Debug("Recomputing share statistics..."); + await Task.WhenAll(Local.Host.Shares.Select(share => ComputeShareStatisticsAsync(share))); + Log.Debug("Share statistics updated"); } /// @@ -391,6 +415,10 @@ namespace slskd.Shares Local.Repository.EnableKeepalive(true); + Log.Debug("Recomputing share statistics..."); + await Task.WhenAll(Local.Host.Shares.Select(share => ComputeShareStatisticsAsync(share))); + Log.Debug("Share statistics updated"); + // one of several thigns happened above before we got here: // this method was called with forceRescan = true // the storage mode is memory, and we loaded the in-memory db from a valid backup @@ -404,8 +432,8 @@ namespace slskd.Shares Faulted = false, Ready = true, ScanProgress = 1, - Directories = Local.Repository.CountDirectories(), - Files = Local.Repository.CountFiles(), + Directories = Hosts.Sum(host => host.Shares.Sum(share => share.Directories)), + Files = Hosts.Sum(host => host.Shares.Sum(share => share.Directories)), }); } catch (Exception ex) -- cgit v1.2.3