Add entriesCount & availableCount to seasons

This commit is contained in:
Zoe Roux 2025-07-14 00:47:00 +02:00
parent 287b5350ff
commit aea2535cc6
8 changed files with 1943 additions and 3 deletions

View File

@ -0,0 +1,2 @@
ALTER TABLE "kyoo"."seasons" ADD COLUMN "entries_count" integer NOT NULL;--> statement-breakpoint
ALTER TABLE "kyoo"."seasons" ADD COLUMN "available_count" integer DEFAULT 0 NOT NULL;

File diff suppressed because it is too large Load Diff

View File

@ -155,6 +155,13 @@
"when": 1747727831649,
"tag": "0021_crew",
"breakpoints": true
},
{
"idx": 22,
"version": "7",
"when": 1752446736231,
"tag": "0022_seasons-count",
"breakpoints": true
}
]
}

View File

@ -24,6 +24,8 @@ const seasonFilters: FilterDef = {
seasonNumber: { column: seasons.seasonNumber, type: "int" },
startAir: { column: seasons.startAir, type: "date" },
endAir: { column: seasons.endAir, type: "date" },
entriesCount: { column: seasons.entriesCount, type: "int" },
availableCount: { column: seasons.availableCount, type: "int" },
};
const seasonSort = Sort(
@ -31,6 +33,8 @@ const seasonSort = Sort(
seasonNumber: seasons.seasonNumber,
startAir: seasons.startAir,
endAir: seasons.endAir,
entriesCount: seasons.entriesCount,
availableCount: seasons.availableCount,
nextRefresh: seasons.nextRefresh,
},
{

View File

@ -9,7 +9,13 @@ import {
sql,
} from "drizzle-orm";
import { db, type Transaction } from "~/db";
import { entries, entryVideoJoin, shows, showTranslations } from "~/db/schema";
import {
entries,
entryVideoJoin,
seasons,
shows,
showTranslations,
} from "~/db/schema";
import { conflictUpdateAllExcept, sqlarr } from "~/db/utils";
import type { SeedCollection } from "~/models/collections";
import type { SeedMovie } from "~/models/movie";
@ -151,7 +157,7 @@ export async function updateAvailableCount(
updateEntryCount = false,
) {
const showPkQ = Array.isArray(showPks) ? sqlarr(showPks) : showPks;
return await tx
await tx
.update(shows)
.set({
availableCount: sql`${db
@ -179,6 +185,35 @@ export async function updateAvailableCount(
}),
})
.where(eq(shows.pk, sql`any(${showPkQ})`));
await tx
.update(seasons)
.set({
availableCount: sql`${db
.select({ count: count() })
.from(entries)
.where(
and(
eq(entries.showPk, seasons.showPk),
eq(entries.seasonNumber, seasons.seasonNumber),
ne(entries.kind, "extra"),
exists(
db
.select()
.from(entryVideoJoin)
.where(eq(entryVideoJoin.entryPk, entries.pk)),
),
),
)}`,
...(updateEntryCount && {
entriesCount: sql`${db
.select({ count: count() })
.from(entries)
.where(
and(eq(entries.showPk, seasons.showPk), ne(entries.kind, "extra")),
)}`,
}),
})
.where(eq(seasons.showPk, sql`any(${showPkQ})`));
}
export async function updateAvailableSince(

View File

@ -122,7 +122,15 @@ export const seedSerie = async (
);
if ("status" in show) return show;
const retSeasons = await insertSeasons(show, seasons);
const retSeasons = await insertSeasons(
show,
seasons.map((s) => ({
...s,
entriesCount: entries.filter(
(x) => x.kind === "episode" && x.seasonNumber === s.seasonNumber,
).length,
})),
);
const retEntries = await insertEntries(show, entries);
const retExtras = await insertEntries(
show,

View File

@ -40,6 +40,9 @@ export const seasons = schema.table(
startAir: date(),
endAir: date(),
entriesCount: integer().notNull(),
availableCount: integer().notNull().default(0),
externalId: season_extid(),
createdAt: timestamp({ withTimezone: true, mode: "iso" })

View File

@ -32,6 +32,15 @@ export const Season = t.Composite([
SeasonTranslation,
BaseSeason,
DbMetadata,
t.Object({
entriesCount: t.Integer({
description: "The number of episodes in this season",
}),
availableCount: t.Integer({
description: "The number of episodes that can be played right away",
}),
}),
]);
export type Season = Prettify<typeof Season.static>;