Skip to content

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

C#

Added HealthCheckResults table.

nzbdav-devf7673d7d3d05
diff --git a/backend/Api/Controllers/GetHealthCheckQueue/GetHealthCheckQueueController.cs b/backend/Api/Controllers/GetHealthCheckQueue/GetHealthCheckQueueController.cs
index 7d468466..8b34178c 100644
--- a/backend/Api/Controllers/GetHealthCheckQueue/GetHealthCheckQueueController.cs
+++ b/backend/Api/Controllers/GetHealthCheckQueue/GetHealthCheckQueueController.cs
@@ -1,7 +1,7 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using NzbWebDAV.Database;
-using NzbWebDAV.Database.Models;
+using NzbWebDAV.Services;
namespace NzbWebDAV.Api.Controllers.GetHealthCheckQueue;
@@ -11,10 +11,7 @@ public class GetHealthCheckQueueController(DavDatabaseClient dbClient) : BaseApi
{
private async Task<GetHealthCheckQueueResponse> GetHealthCheckQueue(GetHealthCheckQueueRequest request)
{
- var davItems = await dbClient.Ctx.Items
- .Where(x => x.Type == DavItem.ItemType.NzbFile || x.Type == DavItem.ItemType.RarFile)
- .OrderBy(x => x.NextHealthCheck)
- .ThenByDescending(x => x.ReleaseDate)
+ var davItems = await HealthCheckService.GetHealthCheckQueueItems(dbClient)
.Take(request.PageSize)
.ToListAsync();
diff --git a/backend/Database/DavDatabaseContext.cs b/backend/Database/DavDatabaseContext.cs
index e7cf4e17..810b63df 100644
--- a/backend/Database/DavDatabaseContext.cs
+++ b/backend/Database/DavDatabaseContext.cs
@@ -25,6 +25,7 @@ public sealed class DavDatabaseContext() : DbContext(Options.Value)
public DbSet<DavRarFile> RarFiles => Set<DavRarFile>();
public DbSet<QueueItem> QueueItems => Set<QueueItem>();
public DbSet<HistoryItem> HistoryItems => Set<HistoryItem>();
+ public DbSet<HealthCheckResult> HealthCheckResults => Set<HealthCheckResult>();
public DbSet<ConfigItem> ConfigItems => Set<ConfigItem>();
// tables
@@ -271,6 +272,50 @@ public sealed class DavDatabaseContext() : DbContext(Options.Value)
.IsUnique(false);
});
+ // HealthCheckResult
+ b.Entity<HealthCheckResult>(e =>
+ {
+ e.ToTable("HealthCheckResults");
+ e.HasKey(i => i.Id);
+
+ e.Property(i => i.Id)
+ .ValueGeneratedNever()
+ .IsRequired();
+
+ e.Property(i => i.CreatedAt)
+ .ValueGeneratedNever()
+ .IsRequired()
+ .HasConversion(
+ x => x.ToUnixTimeSeconds(),
+ x => DateTimeOffset.FromUnixTimeSeconds(x)
+ );
+
+ e.Property(i => i.DavItemId)
+ .ValueGeneratedNever()
+ .IsRequired();
+
+ e.Property(i => i.Path)
+ .IsRequired();
+
+ e.Property(i => i.Result)
+ .HasConversion<int>()
+ .IsRequired();
+
+ e.Property(i => i.RepairStatus)
+ .HasConversion<int>()
+ .IsRequired();
+
+ e.Property(i => i.Message)
+ .IsRequired(false);
+
+ e.HasIndex(i => new { i.Result, i.RepairStatus, i.CreatedAt })
+ .IsUnique(false);
+
+ e.HasIndex(h => h.DavItemId)
+ .HasFilter("\"RepairStatus\" = 3")
+ .IsUnique(false);
+ });
+
// ConfigItem
b.Entity<ConfigItem>(e =>
{
diff --git a/backend/Database/Migrations/20251014060520_Add-HealthCheckResults-Table.Designer.cs b/backend/Database/Migrations/20251014060520_Add-HealthCheckResults-Table.Designer.cs
new file mode 100644
index 00000000..2d6a8f57
--- /dev/null
+++ b/backend/Database/Migrations/20251014060520_Add-HealthCheckResults-Table.Designer.cs
@@ -0,0 +1,314 @@
+// <auto-generated />
+using System;
+using Microsoft.EntityFrameworkCore;
+using Microsoft.EntityFrameworkCore.Infrastructure;
+using Microsoft.EntityFrameworkCore.Migrations;
+using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
+using NzbWebDAV.Database;
+
+#nullable disable
+
+namespace NzbWebDAV.Database.Migrations
+{
+ [DbContext(typeof(DavDatabaseContext))]
+ [Migration("20251014060520_Add-HealthCheckResults-Table")]
+ partial class AddHealthCheckResultsTable
+ {
+ /// <inheritdoc />
+ protected override void BuildTargetModel(ModelBuilder modelBuilder)
+ {
+#pragma warning disable 612, 618
+ modelBuilder.HasAnnotation("ProductVersion", "9.0.4");
+
+ modelBuilder.Entity("NzbWebDAV.Database.Models.Account", b =>
+ {
+ b.Property<int>("Type")
+ .HasColumnType("INTEGER");
+
+ b.Property<string>("Username")
+ .HasMaxLength(255)
+ .HasColumnType("TEXT");
+
+ b.Property<string>("PasswordHash")
+ .IsRequired()
+ .HasColumnType("TEXT");
+
+ b.Property<string>("RandomSalt")
+ .IsRequired()
+ .HasColumnType("TEXT");
+
+ b.HasKey("Type", "Username");
+
+ b.ToTable("Accounts", (string)null);
+ });
+
+ modelBuilder.Entity("NzbWebDAV.Database.Models.ConfigItem", b =>
+ {
+ b.Property<string>("ConfigName")
+ .HasColumnType("TEXT");
+
+ b.Property<string>("ConfigValue")
+ .IsRequired()
+ .HasColumnType("TEXT");
+
+ b.HasKey("ConfigName");
+
+ b.ToTable("ConfigItems", (string)null);
+ });
+
+ modelBuilder.Entity("NzbWebDAV.Database.Models.DavItem", b =>
+ {
+ b.Property<Guid>("Id")
+ .HasColumnType("TEXT");
+
+ b.Property<DateTime>("CreatedAt")
+ .HasColumnType("TEXT");
+
+ b.Property<long?>("FileSize")
+ .HasColumnType("INTEGER");
+
+ b.Property<string>("IdPrefix")
+ .IsRequired()
+ .HasColumnType("TEXT");
+
+ b.Property<long?>("LastHealthCheck")
+ .HasColumnType("INTEGER");
+
+ b.Property<string>("Name")
+ .IsRequired()
+ .HasMaxLength(255)
+ .HasColumnType("TEXT");
+
+ b.Property<long?>("NextHealthCheck")
+ .HasColumnType("INTEGER");
+
+ b.Property<Guid?>("ParentId")
+ .HasColumnType("TEXT");
+
+ b.Property<string>("Path")
+ .IsRequired()
+ .HasColumnType("TEXT");
+
+ b.Property<long?>("ReleaseDate")
+ .HasColumnType("INTEGER");
+
+ b.Property<int>("Type")
+ .HasColumnType("INTEGER");
+
+ b.HasKey("Id");
+
+ b.HasIndex("IdPrefix", "Type");
+
+ b.HasIndex("ParentId", "Name")
+ .IsUnique();
+
+ b.ToTable("DavItems", (string)null);
+ });
+
+ modelBuilder.Entity("NzbWebDAV.Database.Models.DavNzbFile", b =>
+ {
+ b.Property<Guid>("Id")
+ .HasColumnType("TEXT");
+
+ b.Property<string>("SegmentIds")
+ .IsRequired()
+ .HasColumnType("TEXT");
+
+ b.HasKey("Id");
+
+ b.ToTable("DavNzbFiles", (string)null);
+ });
+
+ modelBuilder.Entity("NzbWebDAV.Database.Models.DavRarFile", b =>
+ {
+ b.Property<Guid>("Id")
+ .HasColumnType("TEXT");
+
+ b.Property<string>("RarParts")
+ .IsRequired()
+ .HasColumnType("TEXT");
+
+ b.HasKey("Id");
+
+ b.ToTable("DavRarFiles", (string)null);
+ });
+
+ modelBuilder.Entity("NzbWebDAV.Database.Models.HealthCheckResult", b =>
+ {
+ b.Property<Guid>("Id")
+ .HasColumnType("TEXT");
+
+ b.Property<long>("CreatedAt")
+ .HasColumnType("INTEGER");
+
+ b.Property<Guid>("DavItemId")
+ .HasColumnType("TEXT");
+
+ b.Property<string>("Message")
+ .HasColumnType("TEXT");
+
+ b.Property<string>("Path")
+ .IsRequired()
+ .HasColumnType("TEXT");
+
+ b.Property<int>("RepairStatus")
+ .HasColumnType("INTEGER");
+
+ b.Property<int>("Result")
+ .HasColumnType("INTEGER");
+
+ b.HasKey("Id");
+
+ b.HasIndex("DavItemId")
+ .HasFilter("\"RepairStatus\" = 3");
+
+ b.HasIndex("Result", "RepairStatus", "CreatedAt");
+
+ b.ToTable("HealthCheckResults", (string)null);
+ });
+
+ modelBuilder.Entity("NzbWebDAV.Database.Models.HistoryItem", b =>
+ {
+ b.Property<Guid>("Id")
+ .HasColumnType("TEXT");
+
+ b.Property<string>("Category")
+ .IsRequired()
+ .HasColumnType("TEXT");
+
+ b.Property<DateTime>("CreatedAt")
+ .HasColumnType("TEXT");
+
+ b.Property<Guid?>("DownloadDirId")
+ .HasColumnType("TEXT");
+
+ b.Property<int>("DownloadStatus")
+ .HasColumnType("INTEGER");
+
+ b.Property<int>("DownloadTimeSeconds")
+ .HasColumnType("INTEGER");
+
+ b.Property<string>("FailMessage")
+ .HasColumnType("TEXT");
+
+ b.Property<string>("FileName")
+ .IsRequired()
+ .HasColumnType("TEXT");
+
+ b.Property<string>("JobName")
+ .IsRequired()
+ .HasColumnType("TEXT");
+
+ b.Property<long>("TotalSegmentBytes")
+ .HasColumnType("INTEGER");
+
+ b.HasKey("Id");
+
+ b.HasIndex("Category");
+
+ b.HasIndex("CreatedAt");
+
+ b.HasIndex("Category", "CreatedAt");
+
+ b.HasIndex("Category", "DownloadDirId");
+
+ b.ToTable("HistoryItems", (string)null);
+ });
+
+ modelBuilder.Entity("NzbWebDAV.Database.Models.QueueItem", b =>
+ {
+ b.Property<Guid>("Id")
+ .HasColumnType("TEXT");
+
+ b.Property<string>("Category")
+ .IsRequired()
+ .HasColumnType("TEXT");
+
+ b.Property<DateTime>("CreatedAt")
+ .HasColumnType("TEXT");
+
+ b.Property<string>("FileName")
+ .IsRequired()
+ .HasColumnType("TEXT");
+
+ b.Property<string>("JobName")
+ .IsRequired()
+ .HasColumnType("TEXT");
+
+ b.Property<string>("NzbContents")
+ .IsRequired()
+ .HasColumnType("TEXT");
+
+ b.Property<long>("NzbFileSize")
+ .HasColumnType("INTEGER");
+
+ b.Property<DateTime?>("PauseUntil")
+ .HasColumnType("TEXT");
+
+ b.Property<int>("PostProcessing")
+ .HasColumnType("INTEGER");
+
+ b.Property<int>("Priority")
+ .HasColumnType("INTEGER");
+
+ b.Property<long>("TotalSegmentBytes")
+ .HasColumnType("INTEGER");
+
+ b.HasKey("Id");
+
+ b.HasIndex("Category");
+
+ b.HasIndex("CreatedAt");
+
+ b.HasIndex("FileName")
+ .IsUnique();
+
+ b.HasIndex("Priority");
+
+ b.HasIndex("Priority", "CreatedAt");
+
+ b.HasIndex("Category", "Priority", "CreatedAt");
+
+ b.ToTable("QueueItems", (string)null);
+ });
+
+ modelBuilder.Entity("NzbWebDAV.Database.Models.DavItem", b =>
+ {
+ b.HasOne("NzbWebDAV.Database.Models.DavItem", "Parent")
+ .WithMany("Children")
+ .HasForeignKey("ParentId")
+ .OnDelete(DeleteBehavior.Cascade);
+
+ b.Navigation("Parent");
+ });
+
+ modelBuilder.Entity("NzbWebDAV.Database.Models.DavNzbFile", b =>
+ {
+ b.HasOne("NzbWebDAV.Database.Models.DavItem", "DavItem")
+ .WithOne()
+ .HasForeignKey("NzbWebDAV.Database.Models.DavNzbFile", "Id")
+ .OnDelete(DeleteBehavior.Cascade)
+ .IsRequired();
+
+ b.Navigation("DavItem");
+ });
+
+ modelBuilder.Entity("NzbWebDAV.Database.Models.DavRarFile", b =>
+ {
+ b.HasOne("NzbWebDAV.Database.Models.DavItem", "DavItem")
+ .WithOne()
+ .HasForeignKey("NzbWebDAV.Database.Models.DavRarFile", "Id")
+ .OnDelete(DeleteBehavior.Cascade)
+ .IsRequired();
+
+ b.Navigation("DavItem");
+ });
+
+ modelBuilder.Entity("NzbWebDAV.Database.Models.DavItem", b =>
+ {
+ b.Navigation("Children");
+ });
+#pragma warning restore 612, 618
+ }
+ }
+}
diff --git a/backend/Database/Migrations/20251014060520_Add-HealthCheckResults-Table.cs b/backend/Database/Migrations/20251014060520_Add-HealthCheckResults-Table.cs
new file mode 100644
index 00000000..01caa341
--- /dev/null
+++ b/backend/Database/Migrations/20251014060520_Add-HealthCheckResults-Table.cs
@@ -0,0 +1,50 @@
+using System;
+using Microsoft.EntityFrameworkCore.Migrations;
+
+#nullable disable
+
+namespace NzbWebDAV.Database.Migrations
+{
+ /// <inheritdoc />
+ public partial class AddHealthCheckResultsTable : Migration
+ {
+ /// <inheritdoc />
+ protected override void Up(MigrationBuilder migrationBuilder)
+ {
+ migrationBuilder.CreateTable(
+ name: "HealthCheckResults",
+ columns: table => new
+ {
+ Id = table.Column<Guid>(type: "TEXT", nullable: false),
+ CreatedAt = table.Column<long>(type: "INTEGER", nullable: false),
+ DavItemId = table.Column<Guid>(type: "TEXT", nullable: false),
+ Path = table.Column<string>(type: "TEXT", nullable: false),
+ Result = table.Column<int>(type: "INTEGER", nullable: false),
+ RepairStatus = table.Column<int>(type: "INTEGER", nullable: false),
+ Message = table.Column<string>(type: "TEXT", nullable: true)
+ },
+ constraints: table =>
+ {
+ table.PrimaryKey("PK_HealthCheckResults", x => x.Id);
+ });
+
+ migrationBuilder.CreateIndex(
+ name: "IX_HealthCheckResults_DavItemId",
+ table: "HealthCheckResults",
+ column: "DavItemId",
+ filter: "\"RepairStatus\" = 3");
+
+ migrationBuilder.CreateIndex(
+ name: "IX_HealthCheckResults_Result_RepairStatus_CreatedAt",
+ table: "HealthCheckResults",
+ columns: new[] { "Result", "RepairStatus", "CreatedAt" });
+ }
+
+ /// <inheritdoc />
+ protected override void Down(MigrationBuilder migrationBuilder)
+ {
+ migrationBuilder.DropTable(
+ name: "HealthCheckResults");
+ }
+ }
+}
diff --git a/backend/Database/Migrations/DavDatabaseContextModelSnapshot.cs b/backend/Database/Migrations/DavDatabaseContextModelSnapshot.cs
index 1bcf2156..97907573 100644
--- a/backend/Database/Migrations/DavDatabaseContextModelSnapshot.cs
+++ b/backend/Database/Migrations/DavDatabaseContextModelSnapshot.cs
@@ -130,6 +130,40 @@ namespace NzbWebDAV.Database.Migrations
b.ToTable("DavRarFiles", (string)null);
});
+ modelBuilder.Entity("NzbWebDAV.Database.Models.HealthCheckResult", b =>
+ {
+ b.Property<Guid>("Id")
+ .HasColumnType("TEXT");
+
+ b.Property<long>("CreatedAt")
+ .HasColumnType("INTEGER");
+
+ b.Property<Guid>("DavItemId")
+ .HasColumnType("TEXT");
+
+ b.Property<string>("Message")
+ .HasColumnType("TEXT");
+
+ b.Property<string>("Path")
+ .IsRequired()
+ .HasColumnType("TEXT");
+
+ b.Property<int>("RepairStatus")
+ .HasColumnType("INTEGER");
+
+ b.Property<int>("Result")
+ .HasColumnType("INTEGER");
+
+ b.HasKey("Id");
+
+ b.HasIndex("DavItemId")
+ .HasFilter("\"RepairStatus\" = 3");
+
+ b.HasIndex("Result", "RepairStatus", "CreatedAt");
+
+ b.ToTable("HealthCheckResults", (string)null);
+ });
+
modelBuilder.Entity("NzbWebDAV.Database.Models.HistoryItem", b =>
{
b.Property<Guid>("Id")
diff --git a/backend/Database/Models/HealthCheckResult.cs b/backend/Database/Models/HealthCheckResult.cs
new file mode 100644
index 00000000..a60bc88a
--- /dev/null
+++ b/backend/Database/Models/HealthCheckResult.cs
@@ -0,0 +1,26 @@
+namespace NzbWebDAV.Database.Models;
+
+public class HealthCheckResult
+{
+ public Guid Id { get; init; }
+ public DateTimeOffset CreatedAt { get; init; }
+ public Guid DavItemId { get; init; }
+ public string Path { get; init; }
+ public HealthResult Result { get; init; }
+ public RepairAction RepairStatus { get; set; }
+ public string? Message { get; set; }
+
+ public enum HealthResult
+ {
+ Healthy = 0,
+ Unhealthy = 1,
+ }
+
+ public enum RepairAction
+ {
+ None = 0,
+ Repaired = 1,
+ Deleted = 2,
+ ActionNeeded = 3,
+ }
+}
\ No newline at end of file
diff --git a/backend/Services/HealthCheckService.cs b/backend/Services/HealthCheckService.cs
index 5e550767..746b7d9e 100644
--- a/backend/Services/HealthCheckService.cs
+++ b/backend/Services/HealthCheckService.cs
@@ -58,11 +58,8 @@ public class HealthCheckService
await using var dbContext = new DavDatabaseContext();
var dbClient = new DavDatabaseClient(dbContext);
var currentDateTime = DateTimeOffset.UtcNow;
- var davItem = await dbClient.Ctx.Items
- .Where(x => x.Type == DavItem.ItemType.NzbFile || x.Type == DavItem.ItemType.RarFile)
+ var davItem = await GetHealthCheckQueueItems(dbClient)
.Where(x => x.NextHealthCheck == null || x.NextHealthCheck < currentDateTime)
- .OrderBy(x => x.NextHealthCheck)
- .ThenByDescending(x => x.ReleaseDate)
.FirstOrDefaultAsync(cts.Token);
// if there is no item to health-check, don't do anything
@@ -84,6 +81,17 @@ public class HealthCheckService
}
}
+ public static IOrderedQueryable<DavItem> GetHealthCheckQueueItems(DavDatabaseClient dbClient)
+ {
+ var actionNeeded = HealthCheckResult.RepairAction.ActionNeeded;
+ var healthCheckResults = dbClient.Ctx.HealthCheckResults;
+ return dbClient.Ctx.Items
+ .Where(x => x.Type == DavItem.ItemType.NzbFile || x.Type == DavItem.ItemType.RarFile)
+ .Where(x => !healthCheckResults.Any(h => h.DavItemId == x.Id && h.RepairStatus == actionNeeded))
+ .OrderBy(x => x.NextHealthCheck)
+ .ThenByDescending(x => x.ReleaseDate);
+ }
+
private async Task<bool> PerformHealthCheck
(
DavItem davItem,