Skip to content

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

C#

Added GetHealthCheckHistory api to backend.

nzbdav-devbd905b3dba11
diff --git a/backend/Api/Controllers/GetHealthCheckHistory/GetHealthCheckHistoryController.cs b/backend/Api/Controllers/GetHealthCheckHistory/GetHealthCheckHistoryController.cs
new file mode 100644
index 00000000..3f886bb1
--- /dev/null
+++ b/backend/Api/Controllers/GetHealthCheckHistory/GetHealthCheckHistoryController.cs
@@ -0,0 +1,34 @@
+using Microsoft.AspNetCore.Mvc;
+using Microsoft.EntityFrameworkCore;
+using NzbWebDAV.Database;
+
+namespace NzbWebDAV.Api.Controllers.GetHealthCheckHistory;
+
+[ApiController]
+[Route("api/get-health-check-history")]
+public class GetHealthCheckHistoryController(DavDatabaseClient dbClient) : BaseApiController
+{
+ private async Task<GetHealthCheckHistoryResponse> GetHealthCheckHistory(GetHealthCheckHistoryRequest request)
+ {
+ var now = DateTime.UtcNow;
+ var thirtyDaysAgo = now.AddDays(-30);
+ var stats = await dbClient.GetHealthCheckStatsAsync(thirtyDaysAgo, now);
+ var items = await dbClient.Ctx.HealthCheckResults
+ .OrderByDescending(x => x.CreatedAt)
+ .Take(request.PageSize)
+ .ToListAsync();
+
+ return new GetHealthCheckHistoryResponse()
+ {
+ Stats = stats,
+ Items = items
+ };
+ }
+
+ protected override async Task<IActionResult> HandleRequest()
+ {
+ var request = new GetHealthCheckHistoryRequest(HttpContext);
+ var response = await GetHealthCheckHistory(request);
+ return Ok(response);
+ }
+}
\ No newline at end of file
diff --git a/backend/Api/Controllers/GetHealthCheckHistory/GetHealthCheckHistoryRequest.cs b/backend/Api/Controllers/GetHealthCheckHistory/GetHealthCheckHistoryRequest.cs
new file mode 100644
index 00000000..c16316d8
--- /dev/null
+++ b/backend/Api/Controllers/GetHealthCheckHistory/GetHealthCheckHistoryRequest.cs
@@ -0,0 +1,23 @@
+using Microsoft.AspNetCore.Http;
+using NzbWebDAV.Extensions;
+
+namespace NzbWebDAV.Api.Controllers.GetHealthCheckHistory;
+
+public class GetHealthCheckHistoryRequest
+{
+ public int PageSize { get; init; } = 20;
+ public CancellationToken CancellationToken { get; init; }
+
+ public GetHealthCheckHistoryRequest(HttpContext context)
+ {
+ var pageSizeParam = context.GetQueryParam("pageSize");
+ CancellationToken = context.RequestAborted;
+
+ if (pageSizeParam is not null)
+ {
+ var isValidStartParam = int.TryParse(pageSizeParam, out int pageSize);
+ if (!isValidStartParam) throw new BadHttpRequestException("Invalid pageSize parameter");
+ PageSize = pageSize;
+ }
+ }
+}
\ No newline at end of file
diff --git a/backend/Api/Controllers/GetHealthCheckHistory/GetHealthCheckHistoryResponse.cs b/backend/Api/Controllers/GetHealthCheckHistory/GetHealthCheckHistoryResponse.cs
new file mode 100644
index 00000000..7203f7ca
--- /dev/null
+++ b/backend/Api/Controllers/GetHealthCheckHistory/GetHealthCheckHistoryResponse.cs
@@ -0,0 +1,9 @@
+using NzbWebDAV.Database.Models;
+
+namespace NzbWebDAV.Api.Controllers.GetHealthCheckHistory;
+
+public class GetHealthCheckHistoryResponse : BaseApiResponse
+{
+ public required List<HealthCheckStats> Stats { get; init; }
+ public required List<HealthCheckResult> Items { get; init; }
+}
\ No newline at end of file
diff --git a/backend/Api/Controllers/GetHealthCheckQueue/GetHealthCheckQueueController.cs b/backend/Api/Controllers/GetHealthCheckQueue/GetHealthCheckQueueController.cs
index 8b34178c..5ac62a15 100644
--- a/backend/Api/Controllers/GetHealthCheckQueue/GetHealthCheckQueueController.cs
+++ b/backend/Api/Controllers/GetHealthCheckQueue/GetHealthCheckQueueController.cs
@@ -19,6 +19,7 @@ public class GetHealthCheckQueueController(DavDatabaseClient dbClient) : BaseApi
{
Items = davItems.Select(x => new GetHealthCheckQueueResponse.HealthCheckQueueItem()
{
+ Id = x.Id.ToString(),
Name = x.Name,
Path = x.Path,
ReleaseDate = x.ReleaseDate,
diff --git a/backend/Api/Controllers/GetHealthCheckQueue/GetHealthCheckQueueResponse.cs b/backend/Api/Controllers/GetHealthCheckQueue/GetHealthCheckQueueResponse.cs
index c2528486..1a12cee1 100644
--- a/backend/Api/Controllers/GetHealthCheckQueue/GetHealthCheckQueueResponse.cs
+++ b/backend/Api/Controllers/GetHealthCheckQueue/GetHealthCheckQueueResponse.cs
@@ -6,6 +6,7 @@ public class GetHealthCheckQueueResponse : BaseApiResponse
public class HealthCheckQueueItem
{
+ public required string Id { get; init; }
public required string Name { get; init; }
public required string Path { get; init; }
public required DateTimeOffset? ReleaseDate { get; init; }
diff --git a/backend/Database/DavDatabaseClient.cs b/backend/Database/DavDatabaseClient.cs
index abbbc028..06b6274a 100644
--- a/backend/Database/DavDatabaseClient.cs
+++ b/backend/Database/DavDatabaseClient.cs
@@ -150,6 +150,26 @@ public sealed class DavDatabaseClient(DavDatabaseContext ctx)
public long TotalSize { get; init; }
}
+ // health check
+ public async Task<List<HealthCheckStats>> GetHealthCheckStatsAsync
+ (
+ DateTimeOffset from,
+ DateTimeOffset to,
+ CancellationToken ct = default
+ )
+ {
+ return await Ctx.HealthCheckResults
+ .Where(h => h.CreatedAt >= from && h.CreatedAt <= to)
+ .GroupBy(h => new { h.Result, h.RepairStatus })
+ .Select(g => new HealthCheckStats
+ {
+ Result = g.Key.Result,
+ RepairStatus = g.Key.RepairStatus,
+ Count = g.Count()
+ })
+ .ToListAsync(ct);
+ }
+
// completed-symlinks
public async Task<List<DavItem>> GetCompletedSymlinkCategoryChildren(string category,
CancellationToken ct = default)
diff --git a/backend/Database/Models/HealthCheckStats.cs b/backend/Database/Models/HealthCheckStats.cs
new file mode 100644
index 00000000..c04d7d74
--- /dev/null
+++ b/backend/Database/Models/HealthCheckStats.cs
@@ -0,0 +1,8 @@
+namespace NzbWebDAV.Database.Models;
+
+public class HealthCheckStats
+{
+ public HealthCheckResult.HealthResult Result { get; set; }
+ public HealthCheckResult.RepairAction RepairStatus { get; set; }
+ public int Count { get; set; }
+}
\ No newline at end of file