Skip to content

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

C#

Improved /health page loading time.

nzbdav-devb916e28389f7
diff --git a/backend/Api/Controllers/GetHealthCheckHistory/GetHealthCheckHistoryController.cs b/backend/Api/Controllers/GetHealthCheckHistory/GetHealthCheckHistoryController.cs
index 3f886bb1..f3589e05 100644
--- a/backend/Api/Controllers/GetHealthCheckHistory/GetHealthCheckHistoryController.cs
+++ b/backend/Api/Controllers/GetHealthCheckHistory/GetHealthCheckHistoryController.cs
@@ -11,17 +11,18 @@ public class GetHealthCheckHistoryController(DavDatabaseClient dbClient) : BaseA
private async Task<GetHealthCheckHistoryResponse> GetHealthCheckHistory(GetHealthCheckHistoryRequest request)
{
var now = DateTime.UtcNow;
+ var tomorrow = now.AddDays(1);
var thirtyDaysAgo = now.AddDays(-30);
- var stats = await dbClient.GetHealthCheckStatsAsync(thirtyDaysAgo, now);
- var items = await dbClient.Ctx.HealthCheckResults
+ var statsPromise = dbClient.GetHealthCheckStatsAsync(thirtyDaysAgo, tomorrow);
+ var itemsPromise = dbClient.Ctx.HealthCheckResults
.OrderByDescending(x => x.CreatedAt)
.Take(request.PageSize)
.ToListAsync();
return new GetHealthCheckHistoryResponse()
{
- Stats = stats,
- Items = items
+ Stats = await statsPromise,
+ Items = await itemsPromise
};
}
diff --git a/backend/Api/Controllers/GetHealthCheckHistory/GetHealthCheckHistoryResponse.cs b/backend/Api/Controllers/GetHealthCheckHistory/GetHealthCheckHistoryResponse.cs
index 7203f7ca..a8667f7f 100644
--- a/backend/Api/Controllers/GetHealthCheckHistory/GetHealthCheckHistoryResponse.cs
+++ b/backend/Api/Controllers/GetHealthCheckHistory/GetHealthCheckHistoryResponse.cs
@@ -4,6 +4,6 @@ namespace NzbWebDAV.Api.Controllers.GetHealthCheckHistory;
public class GetHealthCheckHistoryResponse : BaseApiResponse
{
- public required List<HealthCheckStats> Stats { get; init; }
+ public required List<HealthCheckStat> Stats { get; init; }
public required List<HealthCheckResult> Items { get; init; }
}
\ No newline at end of file
diff --git a/backend/Database/DavDatabaseClient.cs b/backend/Database/DavDatabaseClient.cs
index 4a0c293d..6e975977 100644
--- a/backend/Database/DavDatabaseClient.cs
+++ b/backend/Database/DavDatabaseClient.cs
@@ -158,21 +158,21 @@ public sealed class DavDatabaseClient(DavDatabaseContext ctx)
}
// health check
- public async Task<List<HealthCheckStats>> GetHealthCheckStatsAsync
+ public async Task<List<HealthCheckStat>> GetHealthCheckStatsAsync
(
DateTimeOffset from,
DateTimeOffset to,
CancellationToken ct = default
)
{
- return await Ctx.HealthCheckResults
- .Where(h => h.CreatedAt >= from && h.CreatedAt <= to)
+ return await Ctx.HealthCheckStats
+ .Where(h => h.DateStartInclusive >= from && h.DateStartInclusive <= to)
.GroupBy(h => new { h.Result, h.RepairStatus })
- .Select(g => new HealthCheckStats
+ .Select(g => new HealthCheckStat
{
Result = g.Key.Result,
RepairStatus = g.Key.RepairStatus,
- Count = g.Count()
+ Count = g.Select(r => r.Count).Sum(),
})
.ToListAsync(ct);
}
diff --git a/backend/Database/DavDatabaseContext.cs b/backend/Database/DavDatabaseContext.cs
index 846f0499..3a7a2009 100644
--- a/backend/Database/DavDatabaseContext.cs
+++ b/backend/Database/DavDatabaseContext.cs
@@ -28,6 +28,7 @@ public sealed class DavDatabaseContext() : DbContext(Options.Value)
public DbSet<HistoryItem> HistoryItems => Set<HistoryItem>();
public DbSet<QueueNzbContents> QueueNzbContents => Set<QueueNzbContents>();
public DbSet<HealthCheckResult> HealthCheckResults => Set<HealthCheckResult>();
+ public DbSet<HealthCheckStat> HealthCheckStats => Set<HealthCheckStat>();
public DbSet<ConfigItem> ConfigItems => Set<ConfigItem>();
// tables
@@ -297,7 +298,7 @@ public sealed class DavDatabaseContext() : DbContext(Options.Value)
e.HasIndex(i => new { i.Category, i.DownloadDirId })
.IsUnique(false);
});
-
+
// QueueNzbContents
b.Entity<QueueNzbContents>(e =>
{
@@ -355,11 +356,47 @@ public sealed class DavDatabaseContext() : DbContext(Options.Value)
e.HasIndex(i => new { i.Result, i.RepairStatus, i.CreatedAt })
.IsUnique(false);
+ e.HasIndex(i => new { i.CreatedAt })
+ .IsUnique(false);
+
e.HasIndex(h => h.DavItemId)
.HasFilter("\"RepairStatus\" = 3")
.IsUnique(false);
});
+ // HealthCheckStats
+ b.Entity<HealthCheckStat>(e =>
+ {
+ e.ToTable("HealthCheckStats");
+ e.HasKey(i => new { i.DateStartInclusive, i.DateEndExclusive, i.Result, i.RepairStatus });
+
+ e.Property(i => i.DateStartInclusive)
+ .ValueGeneratedNever()
+ .IsRequired()
+ .HasConversion(
+ x => x.ToUnixTimeSeconds(),
+ x => DateTimeOffset.FromUnixTimeSeconds(x)
+ );
+
+ e.Property(i => i.DateEndExclusive)
+ .ValueGeneratedNever()
+ .IsRequired()
+ .HasConversion(
+ x => x.ToUnixTimeSeconds(),
+ x => DateTimeOffset.FromUnixTimeSeconds(x)
+ );
+
+ e.Property(i => i.Result)
+ .HasConversion<int>()
+ .IsRequired();
+
+ e.Property(i => i.RepairStatus)
+ .HasConversion<int>()
+ .IsRequired();
+
+ e.Property(i => i.Count);
+ });
+
// ConfigItem
b.Entity<ConfigItem>(e =>
{
diff --git a/backend/Database/Migrations/20251105050845_Add-HealthCheckStats-Table.Designer.cs b/backend/Database/Migrations/20251105050845_Add-HealthCheckStats-Table.Designer.cs
new file mode 100644
index 00000000..9b51ecc3
--- /dev/null
+++ b/backend/Database/Migrations/20251105050845_Add-HealthCheckStats-Table.Designer.cs
@@ -0,0 +1,384 @@
+// <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("20251105050845_Add-HealthCheckStats-Table")]
+ partial class AddHealthCheckStatsTable
+ {
+ /// <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.HasIndex("Type", "NextHealthCheck", "ReleaseDate", "Id");
+
+ b.ToTable("DavItems", (string)null);
+ });
+
+ modelBuilder.Entity("NzbWebDAV.Database.Models.DavMultipartFile", b =>
+ {
+ b.Property<Guid>("Id")
+ .HasColumnType("TEXT");
+
+ b.Property<string>("Metadata")
+ .IsRequired()
+ .HasColumnType("TEXT");
+
+ b.HasKey("Id");
+
+ b.ToTable("DavMultipartFiles", (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.HealthCheckStat", b =>
+ {
+ b.Property<long>("DateStartInclusive")
+ .HasColumnType("INTEGER");
+
+ b.Property<long>("DateEndExclusive")
+ .HasColumnType("INTEGER");
+
+ b.Property<int>("Result")
+ .HasColumnType("INTEGER");
+
+ b.Property<int>("RepairStatus")
+ .HasColumnType("INTEGER");
+
+ b.Property<int>("Count")
+ .HasColumnType("INTEGER");
+
+ b.HasKey("DateStartInclusive", "DateEndExclusive", "Result", "RepairStatus");
+
+ b.ToTable("HealthCheckStats", (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<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.QueueNzbContents", b =>
+ {
+ b.Property<Guid>("Id")
+ .HasColumnType("TEXT");
+
+ b.Property<string>("NzbContents")
+ .IsRequired()
+ .HasColumnType("TEXT");
+
+ b.HasKey("Id");
+
+ b.ToTable("QueueNzbContents", (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.DavMultipartFile", b =>
+ {
+ b.HasOne("NzbWebDAV.Database.Models.DavItem", "DavItem")
+ .WithOne()
+ .HasForeignKey("NzbWebDAV.Database.Models.DavMultipartFile", "Id")
+ .OnDelete(DeleteBehavior.Cascade)
+ .IsRequired();
+
+ b.Navigation("DavItem");
+ });
+
+ 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.QueueNzbContents", b =>
+ {
+ b.HasOne("NzbWebDAV.Database.Models.QueueItem", "QueueItem")
+ .WithOne()
+ .HasForeignKey("NzbWebDAV.Database.Models.QueueNzbContents", "Id")
+ .OnDelete(DeleteBehavior.Cascade)
+ .IsRequired();
+
+ b.Navigation("QueueItem");
+ });
+
+ modelBuilder.Entity("NzbWebDAV.Database.Models.DavItem", b =>
+ {
+ b.Navigation("Children");
+ });
+#pragma warning restore 612, 618
+ }
+ }
+}
diff --git a/backend/Database/Migrations/20251105050845_Add-HealthCheckStats-Table.cs b/backend/Database/Migrations/20251105050845_Add-HealthCheckStats-Table.cs
new file mode 100644
index 00000000..1d006bc4
--- /dev/null
+++ b/backend/Database/Migrations/20251105050845_Add-HealthCheckStats-Table.cs
@@ -0,0 +1,144 @@
+using Microsoft.EntityFrameworkCore.Migrations;
+
+#nullable disable
+
+namespace NzbWebDAV.Database.Migrations
+{
+ /// <inheritdoc />
+ public partial class AddHealthCheckStatsTable : Migration
+ {
+ /// <inheritdoc />
+ protected override void Up(MigrationBuilder migrationBuilder)
+ {
+ migrationBuilder.CreateTable(
+ name: "HealthCheckStats",
+ columns: table => new
+ {
+ DateStartInclusive = table.Column<long>(type: "INTEGER", nullable: false),
+ DateEndExclusive = table.Column<long>(type: "INTEGER", nullable: false),
+ Result = table.Column<int>(type: "INTEGER", nullable: false),
+ RepairStatus = table.Column<int>(type: "INTEGER", nullable: false),
+ Count = table.Column<int>(type: "INTEGER", nullable: false)
+ },
+ constraints: table =>
+ {
+ table.PrimaryKey("PK_HealthCheckStats", x => new { x.DateStartInclusive, x.DateEndExclusive, x.Result, x.RepairStatus });
+ });
+
+ // Populate HealthCheckStats from HealthCheckResults
+ // Group by day (truncated to beginning of day in UTC), Result, and RepairStatus
+ migrationBuilder.Sql(
+ """
+ INSERT INTO HealthCheckStats (DateStartInclusive, DateEndExclusive, Result, RepairStatus, Count)
+ SELECT
+ CAST(strftime('%s', date(datetime(CreatedAt, 'unixepoch')) || ' 00:00:00') AS INTEGER) AS DateStartInclusive,
+ CAST(strftime('%s', date(datetime(CreatedAt, 'unixepoch'), '+1 day') || ' 00:00:00') AS INTEGER) AS DateEndExclusive,
+ Result,
+ RepairStatus,
+ COUNT(*) AS Count
+ FROM HealthCheckResults
+ GROUP BY
+ CAST(strftime('%s', date(datetime(CreatedAt, 'unixepoch')) || ' 00:00:00') AS INTEGER),
+ CAST(strftime('%s', date(datetime(CreatedAt, 'unixepoch'), '+1 day') || ' 00:00:00') AS INTEGER),
+ Result,
+ RepairStatus
+ """
+ );
+
+ // Create trigger to automatically update HealthCheckStats when new rows are added to HealthCheckResults
+ migrationBuilder.Sql(
+ """
+ CREATE TRIGGER TR_HealthCheckResults_IncrementStats
+ AFTER INSERT ON HealthCheckResults
+ BEGIN
+ INSERT INTO HealthCheckStats (DateStartInclusive, DateEndExclusive, Result, RepairStatus, Count)
+ VALUES (
+ CAST(strftime('%s', date(datetime(NEW.CreatedAt, 'unixepoch')) || ' 00:00:00') AS INTEGER),
+ CAST(strftime('%s', date(datetime(NEW.CreatedAt, 'unixepoch'), '+1 day') || ' 00:00:00') AS INTEGER),
+ NEW.Result,
+ NEW.RepairStatus,
+ 1
+ )
+ ON CONFLICT(DateStartInclusive, DateEndExclusive, Result, RepairStatus) DO UPDATE SET
+ Count = Count + 1;
+ END
+ """
+ );
+
+ // Create trigger to automatically update HealthCheckStats when rows are deleted from HealthCheckResults
+ migrationBuilder.Sql(
+ """
+ CREATE TRIGGER TR_HealthCheckResults_DecrementStats
+ AFTER DELETE ON HealthCheckResults
+ BEGIN
+ UPDATE HealthCheckStats
+ SET Count = Count - 1
+ WHERE DateStartInclusive = CAST(strftime('%s', date(datetime(OLD.CreatedAt, 'unixepoch')) || ' 00:00:00') AS INTEGER)
+ AND DateEndExclusive = CAST(strftime('%s', date(datetime(OLD.CreatedAt, 'unixepoch'), '+1 day') || ' 00:00:00') AS INTEGER)
+ AND Result = OLD.Result
+ AND RepairStatus = OLD.RepairStatus;
+
+ DELETE FROM HealthCheckStats
+ WHERE DateStartInclusive = CAST(strftime('%s', date(datetime(OLD.CreatedAt, 'unixepoch')) || ' 00:00:00') AS INTEGER)
+ AND DateEndExclusive = CAST(strftime('%s', date(datetime(OLD.CreatedAt, 'unixepoch'), '+1 day') || ' 00:00:00') AS INTEGER)
+ AND Result = OLD.Result
+ AND RepairStatus = OLD.RepairStatus
+ AND Count <= 0;
+ END
+ """
+ );
+
+ // Create trigger to automatically update HealthCheckStats when rows are updated in HealthCheckResults
+ // Only processes updates when Result or RepairStatus changes
+ migrationBuilder.Sql(
+ """
+ CREATE TRIGGER TR_HealthCheckResults_UpdateStats
+ AFTER UPDATE ON HealthCheckResults
+ WHEN OLD.Result != NEW.Result OR OLD.RepairStatus != NEW.RepairStatus
+ BEGIN
+ -- Decrement count for the old combination (using old date range)
+ UPDATE HealthCheckStats
+ SET Count = Count - 1
+ WHERE DateStartInclusive = CAST(strftime('%s', date(datetime(OLD.CreatedAt, 'unixepoch')) || ' 00:00:00') AS INTEGER)
+ AND DateEndExclusive = CAST(strftime('%s', date(datetime(OLD.CreatedAt, 'unixepoch'), '+1 day') || ' 00:00:00') AS INTEGER)
+ AND Result = OLD.Result
+ AND RepairStatus = OLD.RepairStatus;
+
+ -- Delete the old stat row if count reaches 0
+ DELETE FROM HealthCheckStats
+ WHERE DateStartInclusive = CAST(strftime('%s', date(datetime(OLD.CreatedAt, 'unixepoch')) || ' 00:00:00') AS INTEGER)
+ AND DateEndExclusive = CAST(strftime('%s', date(datetime(OLD.CreatedAt, 'unixepoch'), '+1 day') || ' 00:00:00') AS INTEGER)
+ AND Result = OLD.Result
+ AND RepairStatus = OLD.RepairStatus
+ AND Count <= 0;
+
+ -- Increment count for the new combination (using new date range, or old if CreatedAt didn't change)
+ -- If CreatedAt changed, we need to handle both old and new date ranges
+ -- First, handle the case where CreatedAt might have changed
+ INSERT INTO HealthCheckStats (DateStartInclusive, DateEndExclusive, Result, RepairStatus, Count)
+ VALUES (
+ CAST(strftime('%s', date(datetime(NEW.CreatedAt, 'unixepoch')) || ' 00:00:00') AS INTEGER),
+ CAST(strftime('%s', date(datetime(NEW.CreatedAt, 'unixepoch'), '+1 day') || ' 00:00:00') AS INTEGER),
+ NEW.Result,
+ NEW.RepairStatus,
+ 1
+ )
+ ON CONFLICT(DateStartInclusive, DateEndExclusive, Result, RepairStatus) DO UPDATE SET
+ Count = Count + 1;
+ END
+ """
+ );
+ }
+
+ /// <inheritdoc />
+ protected override void Down(MigrationBuilder migrationBuilder)
+ {
+ migrationBuilder.Sql("DROP TRIGGER IF EXISTS TR_HealthCheckResults_UpdateStats");
+ migrationBuilder.Sql("DROP TRIGGER IF EXISTS TR_HealthCheckResults_DecrementStats");
+ migrationBuilder.Sql("DROP TRIGGER IF EXISTS TR_HealthCheckResults_IncrementStats");
+
+ migrationBuilder.DropTable(
+ name: "HealthCheckStats");
+ }
+ }
+}
diff --git a/backend/Database/Migrations/20251105200722_Add-CreatedAt-Index-To-HealthCheckResults-Table.Designer.cs b/backend/Database/Migrations/20251105200722_Add-CreatedAt-Index-To-HealthCheckResults-Table.Designer.cs
new file mode 100644
index 00000000..8f939945
--- /dev/null
+++ b/backend/Database/Migrations/20251105200722_Add-CreatedAt-Index-To-HealthCheckResults-Table.Designer.cs
@@ -0,0 +1,386 @@
+// <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("20251105200722_Add-CreatedAt-Index-To-HealthCheckResults-Table")]
+ partial class AddCreatedAtIndexToHealthCheckResultsTable
+ {
+ /// <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.HasIndex("Type", "NextHealthCheck", "ReleaseDate", "Id");
+
+ b.ToTable("DavItems", (string)null);
+ });
+
+ modelBuilder.Entity("NzbWebDAV.Database.Models.DavMultipartFile", b =>
+ {
+ b.Property<Guid>("Id")
+ .HasColumnType("TEXT");
+
+ b.Property<string>("Metadata")
+ .IsRequired()
+ .HasColumnType("TEXT");
+
+ b.HasKey("Id");
+
+ b.ToTable("DavMultipartFiles", (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("CreatedAt");
+
+ b.HasIndex("DavItemId")
+ .HasFilter("\"RepairStatus\" = 3");
+
+ b.HasIndex("Result", "RepairStatus", "CreatedAt");
+
+ b.ToTable("HealthCheckResults", (string)null);
+ });
+
+ modelBuilder.Entity("NzbWebDAV.Database.Models.HealthCheckStat", b =>
+ {
+ b.Property<long>("DateStartInclusive")
+ .HasColumnType("INTEGER");
+
+ b.Property<long>("DateEndExclusive")
+ .HasColumnType("INTEGER");
+
+ b.Property<int>("Result")
+ .HasColumnType("INTEGER");
+
+ b.Property<int>("RepairStatus")
+ .HasColumnType("INTEGER");
+
+ b.Property<int>("Count")
+ .HasColumnType("INTEGER");
+
+ b.HasKey("DateStartInclusive", "DateEndExclusive", "Result", "RepairStatus");
+
+ b.ToTable("HealthCheckStats", (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<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.QueueNzbContents", b =>
+ {
+ b.Property<Guid>("Id")
+ .HasColumnType("TEXT");
+
+ b.Property<string>("NzbContents")
+ .IsRequired()
+ .HasColumnType("TEXT");
+
+ b.HasKey("Id");
+
+ b.ToTable("QueueNzbContents", (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.DavMultipartFile", b =>
+ {
+ b.HasOne("NzbWebDAV.Database.Models.DavItem", "DavItem")
+ .WithOne()
+ .HasForeignKey("NzbWebDAV.Database.Models.DavMultipartFile", "Id")
+ .OnDelete(DeleteBehavior.Cascade)
+ .IsRequired();
+
+ b.Navigation("DavItem");
+ });
+
+ 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.QueueNzbContents", b =>
+ {
+ b.HasOne("NzbWebDAV.Database.Models.QueueItem", "QueueItem")
+ .WithOne()
+ .HasForeignKey("NzbWebDAV.Database.Models.QueueNzbContents", "Id")
+ .OnDelete(DeleteBehavior.Cascade)
+ .IsRequired();
+
+ b.Navigation("QueueItem");
+ });
+
+ modelBuilder.Entity("NzbWebDAV.Database.Models.DavItem", b =>
+ {
+ b.Navigation("Children");
+ });
+#pragma warning restore 612, 618
+ }
+ }
+}
diff --git a/backend/Database/Migrations/20251105200722_Add-CreatedAt-Index-To-HealthCheckResults-Table.cs b/backend/Database/Migrations/20251105200722_Add-CreatedAt-Index-To-HealthCheckResults-Table.cs
new file mode 100644
index 00000000..1d537ff5
--- /dev/null
+++ b/backend/Database/Migrations/20251105200722_Add-CreatedAt-Index-To-HealthCheckResults-Table.cs
@@ -0,0 +1,27 @@
+using Microsoft.EntityFrameworkCore.Migrations;
+
+#nullable disable
+
+namespace NzbWebDAV.Database.Migrations
+{
+ /// <inheritdoc />
+ public partial class AddCreatedAtIndexToHealthCheckResultsTable : Migration
+ {
+ /// <inheritdoc />
+ protected override void Up(MigrationBuilder migrationBuilder)
+ {
+ migrationBuilder.CreateIndex(
+ name: "IX_HealthCheckResults_CreatedAt",
+ table: "HealthCheckResults",
+ column: "CreatedAt");
+ }
+
+ /// <inheritdoc />
+ protected override void Down(MigrationBuilder migrationBuilder)
+ {
+ migrationBuilder.DropIndex(
+ name: "IX_HealthCheckResults_CreatedAt",
+ table: "HealthCheckResults");
+ }
+ }
+}
diff --git a/backend/Database/Migrations/DavDatabaseContextModelSnapshot.cs b/backend/Database/Migrations/DavDatabaseContextModelSnapshot.cs
index 57062c61..ae7ae249 100644
--- a/backend/Database/Migrations/DavDatabaseContextModelSnapshot.cs
+++ b/backend/Database/Migrations/DavDatabaseContextModelSnapshot.cs
@@ -172,6 +172,8 @@ namespace NzbWebDAV.Database.Migrations
b.HasKey("Id");
+ b.HasIndex("CreatedAt");
+
b.HasIndex("DavItemId")
.HasFilter("\"RepairStatus\" = 3");
@@ -180,6 +182,28 @@ namespace NzbWebDAV.Database.Migrations
b.ToTable("HealthCheckResults", (string)null);
});
+ modelBuilder.Entity("NzbWebDAV.Database.Models.HealthCheckStat", b =>
+ {
+ b.Property<long>("DateStartInclusive")
+ .HasColumnType("INTEGER");
+
+ b.Property<long>("DateEndExclusive")
+ .HasColumnType("INTEGER");
+
+ b.Property<int>("Result")
+ .HasColumnType("INTEGER");
+
+ b.Property<int>("RepairStatus")
+ .HasColumnType("INTEGER");
+
+ b.Property<int>("Count")
+ .HasColumnType("INTEGER");
+
+ b.HasKey("DateStartInclusive", "DateEndExclusive", "Result", "RepairStatus");
+
+ b.ToTable("HealthCheckStats", (string)null);
+ });
+
modelBuilder.Entity("NzbWebDAV.Database.Models.HistoryItem", b =>
{
b.Property<Guid>("Id")
diff --git a/backend/Database/Models/HealthCheckStats.cs b/backend/Database/Models/HealthCheckStat.cs
similarity index 58%
rename from backend/Database/Models/HealthCheckStats.cs
rename to backend/Database/Models/HealthCheckStat.cs
index c04d7d74..b4510ff4 100644
--- a/backend/Database/Models/HealthCheckStats.cs
+++ b/backend/Database/Models/HealthCheckStat.cs
@@ -1,7 +1,9 @@
namespace NzbWebDAV.Database.Models;
-public class HealthCheckStats
+public class HealthCheckStat
{
+ public DateTimeOffset DateStartInclusive { get; set; }
+ public DateTimeOffset DateEndExclusive { get; set; }
public HealthCheckResult.HealthResult Result { get; set; }
public HealthCheckResult.RepairAction RepairStatus { get; set; }
public int Count { get; set; }