Compute the number of entries available for a show

This commit is contained in:
Zoe Roux 2025-03-06 21:55:55 +01:00
parent dcf26b94cd
commit 194428ecfe
No known key found for this signature in database
5 changed files with 42 additions and 5 deletions

View File

@ -1,7 +1,7 @@
import { eq, sql } from "drizzle-orm"; import { and, count, eq, exists, sql } from "drizzle-orm";
import { db } from "~/db"; import { db } from "~/db";
import { showTranslations, shows } from "~/db/schema"; import { entries, entryVideoJoin, showTranslations, shows } from "~/db/schema";
import { conflictUpdateAllExcept } from "~/db/utils"; import { conflictUpdateAllExcept, sqlarr } from "~/db/utils";
import type { SeedCollection } from "~/models/collections"; import type { SeedCollection } from "~/models/collections";
import type { SeedMovie } from "~/models/movie"; import type { SeedMovie } from "~/models/movie";
import type { SeedSerie } from "~/models/serie"; import type { SeedSerie } from "~/models/serie";
@ -93,3 +93,34 @@ async function insertBaseShow(
slug: show.slug, slug: show.slug,
}; };
} }
export async function updateAvailableCount(
showPks: number[],
updateEntryCount = true,
) {
return await db
.update(shows)
.set({
availableCount: db
.select({ availableCount: count() })
.from(entries)
.where(
and(
eq(entries.showPk, shows.pk),
exists(
db
.select()
.from(entryVideoJoin)
.where(eq(entryVideoJoin.entryPk, entries.pk)),
),
),
),
...(updateEntryCount && {
entriesCount: db
.select({ entriesCount: count() })
.from(entries)
.where(eq(entries.showPk, shows.pk)),
}),
})
.where(eq(shows.pk, sql`any(${sqlarr(showPks)})`));
}

View File

@ -3,7 +3,7 @@ import type { SeedMovie } from "~/models/movie";
import { getYear } from "~/utils"; import { getYear } from "~/utils";
import { insertCollection } from "./insert/collection"; import { insertCollection } from "./insert/collection";
import { insertEntries } from "./insert/entries"; import { insertEntries } from "./insert/entries";
import { insertShow } from "./insert/shows"; import { insertShow, updateAvailableCount } from "./insert/shows";
import { insertStudios } from "./insert/studios"; import { insertStudios } from "./insert/studios";
import { guessNextRefresh } from "./refresh"; import { guessNextRefresh } from "./refresh";
@ -81,6 +81,7 @@ export const seedMovie = async (
videos, videos,
}, },
]); ]);
await updateAvailableCount([show.pk], false);
const retStudios = await insertStudios(studios, show.pk); const retStudios = await insertStudios(studios, show.pk);

View File

@ -4,7 +4,7 @@ import { getYear } from "~/utils";
import { insertCollection } from "./insert/collection"; import { insertCollection } from "./insert/collection";
import { insertEntries } from "./insert/entries"; import { insertEntries } from "./insert/entries";
import { insertSeasons } from "./insert/seasons"; import { insertSeasons } from "./insert/seasons";
import { insertShow } from "./insert/shows"; import { insertShow, updateAvailableCount } from "./insert/shows";
import { insertStudios } from "./insert/studios"; import { insertStudios } from "./insert/studios";
import { guessNextRefresh } from "./refresh"; import { guessNextRefresh } from "./refresh";
@ -107,6 +107,7 @@ export const seedSerie = async (
show, show,
(extras ?? []).map((x) => ({ ...x, kind: "extra", extraKind: x.kind })), (extras ?? []).map((x) => ({ ...x, kind: "extra", extraKind: x.kind })),
); );
await updateAvailableCount([show.pk]);
const retStudios = await insertStudios(studios, show.pk); const retStudios = await insertStudios(studios, show.pk);

View File

@ -73,6 +73,7 @@ export const shows = schema.table(
onDelete: "set null", onDelete: "set null",
}), }),
entriesCount: integer().notNull(), entriesCount: integer().notNull(),
availableCount: integer().notNull().default(0),
externalId: externalid(), externalId: externalid(),

View File

@ -48,6 +48,9 @@ const BaseSerie = t.Object({
entriesCount: t.Integer({ entriesCount: t.Integer({
description: "The number of episodes in this serie", description: "The number of episodes in this serie",
}), }),
availableCount: t.Integer({
description: "The number of episodes that can be played right away",
}),
externalId: ExternalId(), externalId: ExternalId(),
}); });