Skip to content

Usenet streaming with a WebDAV server and a SABnzbd-compatible API

C#

Improved history api performance.

nzbdav-devdc70dd59545a
diff --git a/backend/Api/SabControllers/GetHistory/GetHistoryController.cs b/backend/Api/SabControllers/GetHistory/GetHistoryController.cs
index 3683942a..956bcd67 100644
--- a/backend/Api/SabControllers/GetHistory/GetHistoryController.cs
+++ b/backend/Api/SabControllers/GetHistory/GetHistoryController.cs
@@ -19,24 +19,31 @@ public class GetHistoryController(
.Where(q => q.Category == request.Category || request.Category == null)
.CountAsync(request.CancellationToken);
- // get history items with joined DavItem data
- var historyItemsWithDavItems = await dbClient.Ctx.HistoryItems
+ // get history items
+ var historyItems = await dbClient.Ctx.HistoryItems
.Where(q => q.Category == request.Category || request.Category == null)
- .GroupJoin(
- dbClient.Ctx.Items,
- h => h.DownloadDirId,
- d => d.Id,
- (h, d) => new { HistoryItem = h, DavItem = d.FirstOrDefault() }
- )
- .OrderByDescending(q => q.HistoryItem.CreatedAt)
+ .OrderByDescending(q => q.CreatedAt)
.Skip(request.Start)
.Take(request.Limit)
.ToArrayAsync(request.CancellationToken);
+ // get download folders
+ var downloadFolderIds = historyItems.Select(x => x.DownloadDirId).ToHashSet();
+ var davItems = await dbClient.Ctx.Items
+ .Where(x => downloadFolderIds.Contains(x.Id))
+ .ToArrayAsync(request.CancellationToken);
+ var davItemsDict = davItems
+ .ToDictionary(x => x.Id, x => x);
+
// get slots
- var slots = historyItemsWithDavItems
- .Select(x => GetHistoryResponse.HistorySlot.FromHistoryItem(
- x.HistoryItem, x.DavItem, configManager.GetRcloneMountDir()))
+ var slots = historyItems
+ .Select(x =>
+ GetHistoryResponse.HistorySlot.FromHistoryItem(
+ x,
+ x.DownloadDirId != null ? davItemsDict.GetValueOrDefault(x.DownloadDirId.Value) : null,
+ configManager.GetRcloneMountDir()
+ )
+ )
.ToList();
// return response