diff --git a/packages/backend/routes/video/metadata.ts b/packages/backend/routes/video/metadata.ts index fda1771..4573754 100644 --- a/packages/backend/routes/video/metadata.ts +++ b/packages/backend/routes/video/metadata.ts @@ -6,6 +6,7 @@ import { redis } from "@core/db/redis"; import { ErrorResponseSchema } from "@backend/src/schema"; import type { VideoInfoData } from "@core/net/bilibili.d.ts"; import { BiliAPIVideoMetadataSchema } from "@backend/lib/schema"; +import { snapshotCounter } from "@crawler/metrics"; export async function retrieveVideoInfoFromCache(aid: number) { const cacheKey = `cvsa:videoInfo:av${aid}`; @@ -41,6 +42,7 @@ async function insertVideoSnapshot(data: VideoInfoData) { shares, favorites }); + snapshotCounter.add(1); } export const getVideoMetadataHandler = new Elysia({ prefix: "/video" }).get( diff --git a/packages/crawler/db/snapshot.ts b/packages/crawler/db/snapshot.ts index cb7619d..94121a4 100644 --- a/packages/crawler/db/snapshot.ts +++ b/packages/crawler/db/snapshot.ts @@ -3,9 +3,11 @@ import type { Psql } from "@core/db/psql.d"; import { db, LatestVideoSnapshotType, videoSnapshot, VideoSnapshotType } from "@core/drizzle"; import { PartialBy } from "@core/lib"; import { sql } from "drizzle-orm"; +import { snapshotCounter } from "metrics"; export async function insertVideoSnapshot(data: PartialBy) { await db.insert(videoSnapshot).values(data); + snapshotCounter.add(1); } export async function getVideosNearMilestone() { diff --git a/packages/crawler/metrics/index.ts b/packages/crawler/metrics/index.ts index b4bfbec..9851cfc 100644 --- a/packages/crawler/metrics/index.ts +++ b/packages/crawler/metrics/index.ts @@ -27,3 +27,7 @@ export const queueJobsCounter = meter.createGauge("queue_jobs_count", { export const jobDurationRaw = meter.createGauge("job_duration_raw", { description: "Execution duration of BullMQ jobs in milliseconds" }); + +export const snapshotCounter = meter.createCounter("snapshot_count", { + description: "Number of snapshots taken" +}); \ No newline at end of file diff --git a/packages/crawler/mq/exec/getVideoInfo.ts b/packages/crawler/mq/exec/getVideoInfo.ts index 4bebe33..bcfd393 100644 --- a/packages/crawler/mq/exec/getVideoInfo.ts +++ b/packages/crawler/mq/exec/getVideoInfo.ts @@ -11,6 +11,7 @@ import { insertIntoSongs } from "mq/task/collectSongs"; import { bilibiliUser, db, videoSnapshot } from "@core/drizzle"; import { eq } from "drizzle-orm"; import { GetVideoInfoJobData } from "mq/schema"; +import { snapshotCounter } from "metrics"; interface AddSongEventPayload { eventName: string; @@ -100,6 +101,8 @@ export const getVideoInfoWorker = async (job: Job): Promise favorites: stat.favorite }); + snapshotCounter.add(1); + logger.log(`Inserted video metadata for aid: ${aid}`, "mq"); if (!insertSongs) { diff --git a/packages/crawler/mq/exec/takeBulkSnapshot.ts b/packages/crawler/mq/exec/takeBulkSnapshot.ts index 61b5742..735d7fe 100644 --- a/packages/crawler/mq/exec/takeBulkSnapshot.ts +++ b/packages/crawler/mq/exec/takeBulkSnapshot.ts @@ -15,6 +15,7 @@ import { SnapshotScheduleType } from "@core/db/schema"; import { sql } from "@core/db/dbNew"; import { updateETA } from "db/eta"; import { closetMilestone } from "./snapshotTick"; +import { snapshotCounter } from "metrics"; export const takeBulkSnapshotForVideosWorker = async (job: Job) => { const schedules: SnapshotScheduleType[] = job.data.schedules; @@ -73,6 +74,8 @@ export const takeBulkSnapshotForVideosWorker = async (job: Job) => { ) `; + snapshotCounter.add(1); + logger.log( `Taken snapshot for video ${aid} in bulk.`, "net",