diff --git a/src/Febe.Core/Storage/LibraryQueries.cs b/src/Febe.Core/Storage/LibraryQueries.cs
index f78171e..31737aa 100644
--- a/src/Febe.Core/Storage/LibraryQueries.cs
+++ b/src/Febe.Core/Storage/LibraryQueries.cs
@@ -28,6 +28,14 @@ public enum LibraryKindFilter
/// Posts of any media (the «Post» tab of an account).
Posts,
+
+ ///
+ /// «Video fermi» (tappa E4, answer 40 of Alberto, 29 November 2026; in the library only, not in the account tabs):
+ /// posts, stories, highlights and tagged posts with at least one video in the library whose latest analysis
+ /// (the highest algorithm in video_analysis) says «Fermo». A video never analysed is not in it: analysis is on
+ /// request only. In «Tutte le foto» only those videos.
+ ///
+ StillVideos,
}
///
diff --git a/src/Febe.Data/Stores/EntryStore.cs b/src/Febe.Data/Stores/EntryStore.cs
index 76e92f9..51c3972 100644
--- a/src/Febe.Data/Stores/EntryStore.cs
+++ b/src/Febe.Data/Stores/EntryStore.cs
@@ -527,6 +527,10 @@ internal sealed class EntryStore(FebeDatabase db) : IEntryStore
{
LibraryKindFilter.Photos => " AND m.kind = 'Photo'",
LibraryKindFilter.Videos => " AND m.kind = 'Video'",
+ // Over every file (bulk) SQLite goes from the still SHA-256 to their files on ix_media_sha256; for the files
+ // of a few entries the unary + keeps it on the key of the entry: on the SHA-256 index each entry would walk
+ // every still video (in a correlated count, 20 ms per entry with 10,000 of them).
+ LibraryKindFilter.StillVideos => $" AND m.kind = 'Video' AND {(bulk ? string.Empty : "+")}m.sha256 IN ({StillShas})",
_ => string.Empty,
} + (filter.Copies is { } copies ? " AND " + ExportFilterSql.FileTerm(ExportFilterSql.Load(c), copies, bulk) : string.Empty);
@@ -562,8 +566,38 @@ internal sealed class EntryStore(FebeDatabase db) : IEntryStore
/// the files already decides (a post without videos adds 0) and the EXISTS per post of the grouped filter would cost
/// a second over 200,000 entries.
///
- private static LibraryFilter EntriesOfFiles(LibraryFilter filter) =>
- filter.Kind is LibraryKindFilter.Photos or LibraryKindFilter.Videos ? filter with { Kind = LibraryKindFilter.Posts } : filter;
+ private static LibraryFilter EntriesOfFiles(LibraryFilter filter) => filter.Kind switch
+ {
+ LibraryKindFilter.Photos or LibraryKindFilter.Videos => filter with { Kind = LibraryKindFilter.Posts },
+ // «Video fermi» is of every kind of entry of «Tutto»: an entry without still videos adds 0 the same way.
+ LibraryKindFilter.StillVideos => filter with { Kind = LibraryKindFilter.All },
+ _ => filter,
+ };
+
+ ///
+ /// «Video fermi»: the SHA-256 of the videos whose latest analysis (the highest algorithm, D4 of tappa E4) says
+ /// «Fermo», read once on ix_video_analysis_still; an «In movimento» of a newer algorithm takes the video out. The
+ /// primary key (sha256, algorithm) answers the NOT EXISTS of each.
+ ///
+ private const string StillShas =
+ "SELECT vs.sha256 FROM video_analysis vs WHERE vs.outcome = 'Still' "
+ + "AND NOT EXISTS (SELECT 1 FROM video_analysis vn WHERE vn.sha256 = vs.sha256 AND vn.algorithm > vs.algorithm)";
+
+ ///
+ /// «Video fermi» on entry e: the entries with a still video in the library, one list read once per statement,
+ /// then each entry of the publication index tested by key against it. Analysis is on request, so the still videos
+ /// are few and the list is short; an EXISTS per entry would walk the whole index (each entry a lookup of its files and
+ /// their table rows, over a second) whenever the filter matches few entries.
+ /// The list goes from the still outcomes (ix_video_analysis_still) to their files (ix_media_sha256_entry) and checks
+ /// that the file is a video in the library on ix_media_entry_files, all on covering indexes: 44 ms for 10,000 still
+ /// videos, 55 ms reading the table row of each file. CROSS JOIN keeps that order (started from the files, SQLite
+ /// scans all of them on ix_media_state, 90 ms); the unary + on the key keeps the check on the covering index.
+ ///
+ private const string StillVideoEntries =
+ "SELECT ms.entry_id FROM video_analysis vs CROSS JOIN media ms ON ms.sha256 = vs.sha256 WHERE vs.outcome = 'Still' "
+ + "AND NOT EXISTS (SELECT 1 FROM video_analysis vn WHERE vn.sha256 = vs.sha256 AND vn.algorithm > vs.algorithm) "
+ + "AND EXISTS (SELECT 1 FROM media mf WHERE mf.entry_id = ms.entry_id AND mf.state IN ('Downloaded', 'Imported') "
+ + "AND mf.kind = 'Video' AND +mf.id = ms.id)";
/// A correlated count of the files of «Tutte le foto» of the entry e.
private static string FilesOfEntry(SqliteConnection c, LibraryFilter filter) =>
@@ -884,6 +918,9 @@ internal sealed class EntryStore(FebeDatabase db) : IEntryStore
LibraryKindFilter.Highlights => "(e.kind = 'Highlight' OR EXISTS (SELECT 1 FROM highlight_entry he WHERE he.entry_id = e.id))",
LibraryKindFilter.Tagged => "e.kind = 'Tagged'",
LibraryKindFilter.ProfilePictures => "e.kind = 'ProfilePicture'",
+ // The unary + on the key, as for the list of «Foto» and «Video»: the entries stay in the order of the
+ // publication index (a window stops after its first rows), each tested against the list on its key.
+ LibraryKindFilter.StillVideos => $"+e.kind IN ('Post', 'Story', 'Highlight', 'Tagged') AND +e.id IN ({StillVideoEntries})",
_ => throw new ArgumentOutOfRangeException(nameof(filter), filter.Kind, null),
});
diff --git a/src/Febe.Web/Api/ApiMapping.cs b/src/Febe.Web/Api/ApiMapping.cs
index 854375b..25df9d8 100644
--- a/src/Febe.Web/Api/ApiMapping.cs
+++ b/src/Febe.Web/Api/ApiMapping.cs
@@ -172,7 +172,7 @@ public static class ApiMapping
///
/// The library filter from the query string: kind (all, photos, videos, stories, highlights, tagged, posts,
- /// profile), accounts (comma-separated keys), from/to (local days, inclusive),
+ /// profile, stillvideos), accounts (comma-separated keys), from/to (local days, inclusive),
/// provenance (downloaded, imported), q (account name or caption).
///
public static LibraryFilter Filter(IQueryCollection query, TimeZoneInfo zone)
@@ -189,6 +189,7 @@ public static class ApiMapping
"tagged" => LibraryKindFilter.Tagged,
"posts" => LibraryKindFilter.Posts,
"profile" => LibraryKindFilter.ProfilePictures,
+ "stillvideos" => LibraryKindFilter.StillVideos,
_ => throw new BadRequestException("Tipo di contenuto sconosciuto."),
};
diff --git a/src/Febe.Web/LEGGIMI.md b/src/Febe.Web/LEGGIMI.md
index 02a1c90..d645a92 100644
--- a/src/Febe.Web/LEGGIMI.md
+++ b/src/Febe.Web/LEGGIMI.md
@@ -114,7 +114,7 @@ sono anche `day` (`aaaa-mm-gg`) e `clock` calcolati sul fuso di Febe.
| `POST /api/browser/show`, `/api/browser/used` | «Apri il browser» (desktop), uso del browser |
| `GET /api/eventi` | SSE: `motore` (istantanea, al massimo ogni 300 ms, comunque ogni 60 s), `libreria` (al massimo ogni 1,5 s), commento ogni 25 s |
| `GET /api/library/days?mode=grouped\|files` | Totali (sempre elementi: `entries`, `accounts`) e giorni con conteggio e cursore (per la finestra del mosaico); con `mode=files` («Tutte le foto») i giorni contano i file presenti in libreria e `files` è la loro somma (altrimenti `null`). Un altro valore di `mode` dà 400. Stessi filtri di `/api/library` |
-| `GET /api/library?cursor=&direction=older\|newer&limit=&mode=grouped\|files` | Una finestra di tessere, con `older`/`newer` per continuare; filtri `kind`, `accounts`, `from`, `to`, `provenance`, `q`, `copies=notCopied\|copied\|removedOrSuperseded` (solo i file dovuti; un altro valore 400). Con una cartella delle copie in uso le tessere hanno `copies` (`copied`, `partly` con `copiesDone`/`copiesDue`) e `/days` ha `hasDestinations`; il visualizzatore dà le copie di ogni file (`copies`, al più 6) e `canCopy`. Con `mode=files` una tessera per file (con la sua posizione e `files`, i file dell'elemento) e il cursore è `ms.elemento.posizione` (raggruppato `ms.elemento`); un cursore malformato o `mode` sconosciuto danno 400 |
+| `GET /api/library?cursor=&direction=older\|newer&limit=&mode=grouped\|files` | Una finestra di tessere, con `older`/`newer` per continuare; filtri `kind` (con `kind=stillvideos`, «Video fermi» della tappa E4: gli elementi con un video in libreria la cui ultima analisi dice «Fermo», e con `mode=files` solo quei video), `accounts`, `from`, `to`, `provenance`, `q`, `copies=notCopied\|copied\|removedOrSuperseded` (solo i file dovuti; un altro valore 400). Con una cartella delle copie in uso le tessere hanno `copies` (`copied`, `partly` con `copiesDone`/`copiesDue`) e `/days` ha `hasDestinations`; il visualizzatore dà le copie di ogni file (`copies`, al più 6) e `canCopy`. Con `mode=files` una tessera per file (con la sua posizione e `files`, i file dell'elemento) e il cursore è `ms.elemento.posizione` (raggruppato `ms.elemento`); un cursore malformato o `mode` sconosciuto danno 400 |
| `GET /api/library/tiles?ids=` | Tessere per chiave |
| `GET /api/thumbnails/{chiave}`, `GET /api/media/{id}/file[?download=1]` | Miniature (immutabili) e file (con range) |
| `GET /api/entries/{id}`, `POST /api/entries/delete`, `/export`, `/{id}/open-on-instagram`, `POST /api/media/show-in-folder` | Visualizzatore, eliminazione (`remember` o `allowReturn`), CSV (`{ ids }` per la selezione, `{ query }` per il filtro della pagina: «Esporta l'elenco» nella riga degli strumenti) |
diff --git a/tests/Febe.Tests/Data/LibraryScaleTests.cs b/tests/Febe.Tests/Data/LibraryScaleTests.cs
index 621b7e7..e3f8b0e 100644
--- a/tests/Febe.Tests/Data/LibraryScaleTests.cs
+++ b/tests/Febe.Tests/Data/LibraryScaleTests.cs
@@ -81,17 +81,20 @@ public sealed class LibraryScaleTests
}
var copies = await FillCopiesAsync(db, temp, accountIds, ct);
+ var still = await FillStillVideosAsync(db, accountIds, ct);
fill.Stop();
TestContext.WriteLine($"Riempimento: {Entries:N0} elementi in {fill.Elapsed.TotalSeconds:F1} s");
TestContext.WriteLine($"Copie: {copies.Copied:N0} copiate (di cui {copies.Converted:N0} convertite), {copies.Removed:N0} tolte, {copies.Superseded:N0} superate, {copies.Frames:N0} fotogrammi, su {copies.Due:N0} file da copiare");
+ TestContext.WriteLine($"Video fermi: {still.Analyses:N0} analisi, {still.Still:N0} «Fermo» (di cui {still.Overruled:N0} superati da un algoritmo più nuovo), {still.Entries:N0} elementi e {still.Files:N0} file nel filtro");
Assert.IsGreaterThan(0, copies.Converted, "converted copies in the fill");
Assert.IsGreaterThan(0, copies.Frames, "frames in the fill");
+ Assert.IsGreaterThan(0, still.Overruled, "still videos overruled by a newer algorithm in the fill");
// Plans differ with and without ANALYZE statistics: both must be fast.
// The fill stays outside the timings.
- await MeasureAll(db, accountIds, copies, "senza statistiche", ct);
+ await MeasureAll(db, accountIds, copies, still, "senza statistiche", ct);
await db.WriteAsync(w => w.Command("ANALYZE").ExecuteNonQuery(), ct);
- await MeasureAll(db, accountIds, copies, "con ANALYZE", ct);
+ await MeasureAll(db, accountIds, copies, still, "con ANALYZE", ct);
}
///
@@ -103,8 +106,6 @@ public sealed class LibraryScaleTests
///
private static async Task FillCopiesAsync(FebeDatabase db, TempFolder temp, List accountIds, CancellationToken ct)
{
- const string Hash = "((m.id * 2654435761) % 1000003)";
- const string Spread = "((" + Hash + " * " + Hash + " + m.id) % 999983)";
var general = await db.Exports.AddDestinationAsync(
new ExportDestination { Scope = ExportScope.General, RootPath = temp.Combine("usb"), ContentTypes = ContentTypes.All, CreatedAt = Make.T0 }, ct);
var own = await db.Exports.AddDestinationAsync(
@@ -148,6 +149,55 @@ public sealed class LibraryScaleTests
ct);
}
+ // A hash of the media id that mixes the ids (see FillCopiesAsync).
+ private const string Hash = "((m.id * 2654435761) % 1000003)";
+ private const string Spread = "((" + Hash + " * " + Hash + " + m.id) % 999983)";
+
+ ///
+ /// Tappa E4, the filter «Video fermi»: seven videos in ten analysed (about 20,000 of the 28,600 videos), half of them
+ /// «Fermo» (about 10,000, frame at 1 s) and half «In movimento»; one «Fermo» in ten has a newer algorithm that says
+ /// «In movimento», so it is out of the filter. The expected entries and files are counted here with a join on the
+ /// highest algorithm, another form than the filter's own.
+ ///
+ private static Task FillStillVideosAsync(FebeDatabase db, List accountIds, CancellationToken ct) =>
+ db.WriteAsync(
+ w =>
+ {
+ var videos = $"FROM media m WHERE m.kind = 'Video' AND m.sha256 IS NOT NULL AND {Spread} % 10 < 7";
+ var analyses = w.Command($"""
+ INSERT INTO video_analysis (sha256, algorithm, outcome, analyzed_at, frame_at_ms, note)
+ SELECT m.sha256, 1, CASE WHEN {Spread} % 2 = 0 THEN 'Still' ELSE 'Moving' END, 1767225600000,
+ CASE WHEN {Spread} % 2 = 0 THEN 1000 END, NULL
+ {videos}
+ """).ExecuteNonQuery();
+ var overruled = w.Command($"""
+ INSERT INTO video_analysis (sha256, algorithm, outcome, analyzed_at, frame_at_ms, note)
+ SELECT m.sha256, 2, 'Moving', 1767225600000, NULL, NULL
+ {videos} AND {Spread} % 2 = 0 AND {Spread} % 20 < 2
+ """).ExecuteNonQuery();
+ var still = (int)w.Command("SELECT COUNT(*) FROM video_analysis WHERE outcome = 'Still'").ScalarInt64();
+ const string Latest = """
+ FROM media m JOIN video_analysis va ON va.sha256 = m.sha256
+ WHERE m.kind = 'Video' AND m.state IN ('Downloaded', 'Imported') AND va.outcome = 'Still'
+ AND va.algorithm = (SELECT MAX(x.algorithm) FROM video_analysis x WHERE x.sha256 = va.sha256)
+ """;
+ var entries = (int)w.Command($"SELECT COUNT(DISTINCT m.entry_id) {Latest}").ScalarInt64();
+ var files = (int)w.Command($"SELECT COUNT(*) {Latest}").ScalarInt64();
+ var account = (int)w.Command($"SELECT COUNT(DISTINCT m.entry_id) {Latest} AND m.entry_id IN (SELECT id FROM entry WHERE account_id = @a)")
+ .With("@a", accountIds[3]).ScalarInt64();
+ var accountFiles = (int)w.Command($"SELECT COUNT(*) {Latest} AND m.entry_id IN (SELECT id FROM entry WHERE account_id = @a)")
+ .With("@a", accountIds[3]).ScalarInt64();
+ return new StillWorld(analyses + overruled, still, overruled, entries, files, account, accountFiles);
+ },
+ ct);
+
+ ///
+ /// What the fill of the analyses wrote: rows, «Fermo» rows, those overruled by a newer algorithm, and the entries
+ /// and files the filter must find ( and : those of
+ /// account 3).
+ ///
+ private sealed record StillWorld(int Analyses, int Still, int Overruled, int Entries, int Files, int AccountEntries, int AccountFiles);
+
/// The form of the copies an insert writes: variant, format and instant (SQL), name suffix, copy SHA (SQL).
private sealed record Form(string Variant, string Format, string FrameAt, string Suffix, string? CopySha)
{
@@ -162,7 +212,7 @@ public sealed class LibraryScaleTests
_ => 1 + (n % 5 == 0 ? 1 : 0),
});
- private async Task MeasureAll(FebeDatabase db, List accountIds, CopiesWorld copies, string label, CancellationToken ct)
+ private async Task MeasureAll(FebeDatabase db, List accountIds, CopiesWorld copies, StillWorld still, string label, CancellationToken ct)
{
var times = new List<(string Name, double Bound, Timing