diff --git a/apps/server/src/collector/match-youtube.ts b/apps/server/src/collector/match-youtube.ts index 67def9e..a8cc004 100644 --- a/apps/server/src/collector/match-youtube.ts +++ b/apps/server/src/collector/match-youtube.ts @@ -45,6 +45,8 @@ export async function matchYoutube(db: Database, ytm: Pick { return Object.fromEntries(REVIEW_TABS.map((t) => [t, r[t] ?? 0])) as Record; } -/** The latest match-youtube run, with what it has decided so far. */ -export function reviewJob(db: Database) { - const job = db - .query<{ id: number; status: string; done: number; total: number; started_at: number; finished_at: number | null }, []>( - "SELECT id, status, done, total, started_at, finished_at FROM collect_jobs WHERE kind = 'match-youtube' ORDER BY id DESC LIMIT 1", - ) - .get(); +const pidAlive = (pid: number) => { + try { + process.kill(pid, 0); + return true; + } catch (e) { + return (e as NodeJS.ErrnoException).code === 'EPERM'; // there, owned by someone else + } +}; + +/** Speed and ETA are taken over the last 10 minutes, so a slow start or a 60s retry wait fades out. */ +export const RATE_WINDOW = 10 * 60_000; + +/** + * The latest match-youtube run, in detail: what it decided so far, how fast it goes, when it should end, + * whether its process is still there, the last songs it looked at, its log, and the runs before it. + */ +export function reviewJob(db: Database, now = Date.now(), alive: (pid: number) => boolean = pidAlive) { + type JobRow = { id: number; status: string; done: number; total: number; started_at: number; finished_at: number | null }; + const history = db + .query("SELECT id, status, done, total, started_at, finished_at FROM collect_jobs WHERE kind = 'match-youtube' ORDER BY id DESC LIMIT 6") + .all(); + const job = history[0]; if (!job) return null; - const end = job.finished_at ?? Date.now() + 1; + const { pid, log } = db.query<{ pid: number | null; log: string }, [number]>('SELECT pid, log FROM collect_jobs WHERE id = ?').get(job.id)!; + const running = job.status === 'running'; + const end = job.finished_at ?? now + 1; + const tried = `t.kind LIKE 'yt_tried_%' AND t.value >= ? AND t.value <= ?`; const r = db .query<{ matched: number | null; candidate: number | null; none: number | null }, [number, number]>( `SELECT SUM(s.youtube_status = 'matched') matched, SUM(s.youtube_status = 'candidate') candidate, SUM(s.youtube_status = 'none') none - FROM songs s WHERE s.deleted_at IS NULL AND EXISTS (SELECT 1 FROM signals t WHERE t.song_id = s.id AND t.kind LIKE 'yt_tried_%' AND t.value >= ? AND t.value <= ?)`, + FROM songs s WHERE s.deleted_at IS NULL AND EXISTS (SELECT 1 FROM signals t WHERE t.song_id = s.id AND ${tried})`, ) .get(job.started_at, end)!; - return { ...job, result: { matched: r.matched ?? 0, candidate: r.candidate ?? 0, none: r.none ?? 0 } }; + const recent = db + .query< + { id: number; title: string; artist: string; youtube_status: string; youtube_source: string | null; youtube_title: string | null; youtube_channel: string | null; kind: string; at: number }, + [number, number] + >( + `SELECT s.id, s.title, s.artist, s.youtube_status, s.youtube_source, s.youtube_title, s.youtube_channel, t.kind, t.value at + FROM signals t JOIN songs s ON s.id = t.song_id WHERE ${tried} ORDER BY t.value DESC LIMIT 8`, + ) + .all(job.started_at, end); + // speed: songs per minute over the last 10 minutes of the run (the whole run while it is younger than that) + const runEnd = running ? now : end; + const from = Math.max(job.started_at, runEnd - RATE_WINDOW); + const inWindow = db.query<{ n: number }, [number, number]>(`SELECT COUNT(*) n FROM signals t WHERE ${tried}`).get(from, end)!.n; + const perMin = runEnd > from ? (inWindow / (runEnd - from)) * 60_000 : 0; + const left = Math.max(0, job.total - job.done); + const lines = parseLog(log, job.started_at); + const deadline = lines.reduce((d, l) => (l.deadline ? l.deadline : d), null); + const { kind } = recent[0] ?? { kind: '' }; + return { + ...job, + result: { matched: r.matched ?? 0, candidate: r.candidate ?? 0, none: r.none ?? 0 }, + /** Search used: ytmusic (with the cookie) or the youtube fallback. */ + source: kind ? kind.slice('yt_tried_'.length) : null, + lastAt: recent[0]?.at ?? null, + perMin: Math.round(perMin * 10) / 10, + eta: running && perMin > 0 && left > 0 ? Math.round(now + (left / perMin) * 60_000) : null, + /** Running but its process is gone (container restarted, killed): the row is stale. */ + alive: running && pid ? alive(pid) : null, + deadline, + recent: recent.map(({ kind: _, ...x }) => x), + log: lines.slice(-12).map(({ at, text }) => ({ at, text })), + history: history.slice(1), + }; +} + +/** + * Job log lines are "HH:MM:SS text" in UTC; turn the time back into epoch ms (the run may cross midnight). + * The matcher's start line carries its deadline ("deadline 2026-10-20T07:55:00.000Z"). + */ +export function parseLog(log: string, startedAt: number) { + const out: { at: number; text: string; deadline?: number }[] = []; + let day = Math.floor(startedAt / 86400_000) * 86400_000; + let prev = startedAt - 60_000; + for (const line of log.split('\n')) { + const m = /^(\d\d):(\d\d):(\d\d) (.*)$/.exec(line); + if (!m) continue; + let at = day + ((+m[1]! * 60 + +m[2]!) * 60 + +m[3]!) * 1000; + if (at < prev - 3600_000) { + day += 86400_000; + at += 86400_000; + } + prev = at; + const d = / deadline (\S+)/.exec(m[4]!); + out.push({ at, text: m[4]!, ...(d && Number.isFinite(Date.parse(d[1]!)) ? { deadline: Date.parse(d[1]!) } : {}) }); + } + return out; } export interface ReviewRow { diff --git a/apps/server/src/youtube-review.test.ts b/apps/server/src/youtube-review.test.ts index 14a7543..cdf05c0 100644 --- a/apps/server/src/youtube-review.test.ts +++ b/apps/server/src/youtube-review.test.ts @@ -3,6 +3,7 @@ import { afterEach, expect, test } from 'bun:test'; import { Catalog } from './collector/catalog'; import { matchYoutube } from './collector/match-youtube'; import { quizSong } from './songs/quiz'; +import { parseLog, reviewJob } from './songs/youtube-review'; import { api, boot, discordLogin, TEST_SUPERADMINS } from './test-utils'; let s: ReturnType; @@ -77,3 +78,36 @@ test('원곡 찾기 tabs and actions; admin choices stick across matcher runs', expect(audit).toEqual(expect.arrayContaining(['song.youtube.unconfirm', 'song.youtube.none', 'song.update'])); expect((await call('/youtube?tab=nope')).body.tab).toBe('candidate'); }); + +test('원곡 찾기 진행 상황: speed over the last 10 minutes, ETA, stale process, last songs, log across midnight, earlier runs', () => { + s = boot(); + const c = new Catalog(s.db); + const ids = Array.from({ length: 30 }, (_, i) => c.upsert({ title: `곡${i}`, artists: [{ name: '가수' }], source: 't', ref: String(i) })[0]!); + const day = Date.UTC(2026, 9, 19); + const start = day + 23 * 3600_000 + 50 * 60_000; // 23:50 UTC + s.db.query("INSERT INTO collect_jobs (kind, status, done, total, started_at, finished_at) VALUES ('match-youtube', 'done', 5, 5, ?, ?)").run(start - 86400_000, start - 86000_000); + const log = ['23:50:00 start 100 songs via ytmusic deadline 2026-10-20T07:55:00.000Z', '23:59:59 youtube 50/100 matched 40 candidate 5 none 5', '00:01:00 search failed (429), waiting 60s', ''].join('\n'); + const job = Number(s.db.query("INSERT INTO collect_jobs (kind, status, done, total, started_at, pid, log) VALUES ('match-youtube', 'running', 30, 100, ?, 4242, ?)").run(start, log).lastInsertRowid); + // 20 songs in the first 10 minutes, 10 in the last 10: the ETA follows the recent pace (1 song a minute) + const now = start + 20 * 60_000; + ids.forEach((id, i) => { + const at = i < 20 ? start + i * 30_000 : now - 10 * 60_000 + (i - 19) * 60_000 - 1; + s.db.query("INSERT INTO signals (song_id, kind, value, fetched_at) VALUES (?, 'yt_tried_ytmusic', ?, ?)").run(id, at, at); + }); + s.db.query("UPDATE songs SET youtube_status = 'matched', youtube_id = 'v', youtube_title = '영상' WHERE id = ?").run(ids[29]!); + const alive: number[] = []; + const r = reviewJob(s.db, now, (pid) => (alive.push(pid), false))!; + expect(r).toMatchObject({ id: job, status: 'running', source: 'ytmusic', alive: false, lastAt: now - 1, deadline: Date.parse('2026-10-20T07:55:00.000Z') }); + expect(alive).toEqual([4242]); + expect(r.perMin).toBe(1); + expect(r.eta).toBe(now + 70 * 60_000); + expect(r.result).toEqual({ matched: 1, candidate: 0, none: 29 }); + expect(r.recent.map((x) => x.id)).toEqual(ids.slice(22).reverse()); + expect(r.recent[0]).toMatchObject({ youtube_status: 'matched', youtube_title: '영상' }); + expect(r.log.map((l) => l.at)).toEqual([start, day + 86400_000 - 1000, day + 86400_000 + 60_000]); + expect(r.history).toHaveLength(1); + // a finished run: no ETA, no process check + s.db.query("UPDATE collect_jobs SET status = 'done', finished_at = ? WHERE id = ?").run(now, job); + expect(reviewJob(s.db, now + 3600_000, () => true)).toMatchObject({ eta: null, alive: null, perMin: 1 }); + expect(parseLog('garbage\n10:00:00 hi', Date.UTC(2026, 0, 1, 9))).toEqual([{ at: Date.UTC(2026, 0, 1, 10), text: 'hi' }]); +}); diff --git a/apps/web/src/pages/admin/YoutubePage.tsx b/apps/web/src/pages/admin/YoutubePage.tsx index 89c6f00..0c0e840 100644 --- a/apps/web/src/pages/admin/YoutubePage.tsx +++ b/apps/web/src/pages/admin/YoutubePage.tsx @@ -35,14 +35,25 @@ interface Row { touched: number; candidates: Cand[]; } -interface Job { +interface JobBase { id: number; status: string; done: number; total: number; started_at: number; finished_at: number | null; +} +interface Job extends JobBase { result: { matched: number; candidate: number; none: number }; + source: string | null; + lastAt: number | null; + perMin: number; + eta: number | null; + alive: boolean | null; + deadline: number | null; + recent: { id: number; title: string; artist: string; youtube_status: string; youtube_source: string | null; youtube_title: string | null; youtube_channel: string | null; at: number }[]; + log: { at: number; text: string }[]; + history: JobBase[]; } interface Data { tab: Tab; @@ -79,6 +90,189 @@ function ago(ms: number, now: number): string { if (s < 86400) return `${Math.floor(s / 3600)}시간 전`; return new Date(ms).toLocaleDateString('ko-KR', { month: 'short', day: 'numeric' }); } +/** 1시간 43분 · 12분 · 40초 */ +function span(ms: number): string { + const m = Math.round(ms / 60_000); + if (ms < 60_000) return `${Math.max(0, Math.round(ms / 1000))}초`; + return m >= 60 ? `${Math.floor(m / 60)}시간${m % 60 ? ` ${m % 60}분` : ''}` : `${m}분`; +} +/** 23:46, or 내일 00:12 / 10. 21. 08:10 when it is not today. */ +function when(ms: number, now: number): string { + const day = (t: number) => new Date(t).toDateString(); + if (day(ms) === day(now)) return clock(ms); + if (day(ms) === day(now + 86400_000)) return `내일 ${clock(ms)}`; + return new Date(ms).toLocaleString('ko-KR', { month: 'numeric', day: 'numeric', hour: '2-digit', minute: '2-digit' }); +} +/** The daily cron: 08:10 local. */ +function nextCron(now: number): number { + const d = new Date(now); + d.setHours(8, 10, 0, 0); + if (d.getTime() <= now) d.setDate(d.getDate() + 1); + return d.getTime(); +} +const SEARCH: Record = { ytmusic: '유튜브 뮤직 (쿠키 사용)', youtube: '유튜브 검색 (쿠키 없이, 정확도 낮음)' }; +/** The matcher's log in plain words. */ +function logText(t: string): string { + let m: RegExpExecArray | null; + const n = (x: string) => Number(x).toLocaleString(); + if ((m = /^start (\d+) songs via (\w+)(?: deadline (\S+))?/.exec(t))) return `시작: ${n(m[1]!)}곡 · ${SEARCH[m[2]!] ?? m[2]}${m[3] ? ` · ${clock(Date.parse(m[3]))}에 마감` : ''}`; + if ((m = /^youtube (done )?(\d+)\/(\d+):? matched (\d+) candidate (\d+) none (\d+)/.exec(t))) + return `${m[1] ? '끝: ' : ''}${n(m[2]!)} / ${n(m[3]!)}곡 · 확정 ${n(m[4]!)} · 후보 ${n(m[5]!)} · 못 찾음 ${n(m[6]!)}`; + if ((m = /^search failed \((.*)\), waiting 60s/.exec(t))) return `⚠ 검색 실패 (${m[1]}) → 60초 쉬고 다시 해요`; + if ((m = /^deadline reached after (\d+)\/(\d+)/.exec(t))) return `마감 시각이 돼서 ${n(m[1]!)} / ${n(m[2]!)}곡에서 멈췄어요`; + if ((m = /^stopped: (.*)/.exec(t))) return `⚠ 멈춤: ${m[1]}`; + return t; +} +/** No song finished for this long while running: the run is probably stuck (normal pace is a few seconds a song). */ +const STALL_MS = 3 * 60_000; + +/** 진행 상황: is it running, how far, how fast, when it ends, what it decided, and what it just did. */ +function JobStatus({ job, counts, now, onPick }: { job: Job | null | undefined; counts: Record | undefined; now: number; onPick: (id: number) => void }) { + const all = counts ? Object.values(counts).reduce((a, b) => a + b, 0) : 0; + const footer = ( +

+ {counts && all > 0 && ( + <> + 전체 {all.toLocaleString()}곡 중 게임에 나오는 곡(확정) {counts.matched.toLocaleString()}곡 ({Math.floor((counts.matched / all) * 100)}%) · 아직 안 찾은 곡{' '} + {counts.pending.toLocaleString()}곡 ·{' '} + + )} + 다음 자동 실행 {when(nextCron(now), now)} (매일 08:10, 3,000곡까지). 확정한 영상은 저장하는 즉시 새 게임과 다음 문제부터 나와요(지금 재생 중인 곡은 그대로). +

+ ); + if (!job) + return ( +
+

아직 돌린 원곡 찾기 작업이 없어요.

+ {footer} +
+ ); + const running = job.status === 'running'; + const left = Math.max(0, job.total - job.done); + const pct = job.total ? Math.floor((job.done / job.total) * 100) : 0; + const since = job.lastAt ?? job.started_at; + const gone = running && job.alive === false; + const stalled = running && !gone && now - since > STALL_MS; + const lastLog = job.log[job.log.length - 1]; + // a search failed and no song has finished since: the matcher waits 60s and tries again + const waiting = running && !!lastLog && /^search failed/.test(lastLog.text) && (job.lastAt ?? 0) < lastLog.at; + const elapsed = (job.finished_at ?? now) - job.started_at; + const r = job.result; + const decided = r.matched + r.candidate + r.none; + const share = (n: number) => (decided ? `${Math.round((n / decided) * 100)}%` : '-'); + const cutByDeadline = running && job.deadline && job.eta && job.eta > job.deadline; + let badge: [string, string]; + if (gone) badge = ['⚠ 멈춘 것 같아요', 'warn']; + else if (stalled) badge = [`⚠ ${span(now - since)}째 진행 없음`, 'warn']; + else if (waiting) badge = ['⏳ 검색 실패, 잠깐 쉬는 중', 'warn']; + else if (running) badge = ['● 찾는 중', 'live']; + else badge = [JOB_STATUS[job.status] ?? job.status, job.status === 'done' ? 'ok' : 'warn']; + const stats: [string, string, string?][] = [ + ['처리', `${job.done.toLocaleString()} / ${job.total.toLocaleString()}`, `${pct}%`], + ['남은 곡', `${left.toLocaleString()}곡`], + ['속도', job.perMin ? `${job.perMin.toLocaleString()}곡/분` : '-', job.perMin ? `1곡에 ${(60 / job.perMin).toFixed(1)}초 · 최근 10분` : undefined], + [running ? '지난 시간' : '걸린 시간', span(elapsed), `시작 ${when(job.started_at, now)}`], + ...(running + ? ([ + ['남은 시간', job.eta ? `약 ${span(job.eta - now)}` : '-'], + ['끝 예상', job.eta ? when(job.eta, now) : '-', cutByDeadline ? `${clock(job.deadline!)} 마감에 멈춰요` : undefined], + ] as [string, string, string?][]) + : ([['끝난 시각', job.finished_at ? when(job.finished_at, now) : '-']] as [string, string, string?][])), + ['마지막 처리', job.lastAt ? ago(job.lastAt, now) : '-', job.lastAt ? clock(job.lastAt) : undefined], + ]; + return ( +
+
+ {badge[0]} + + 작업 #{job.id} {pct}% + + {job.source && 검색: {SEARCH[job.source] ?? job.source}} +
+ {gone &&

작업 기록은 '찾는 중'인데 작업 프로세스가 없어요(서버 재시작 등). 수집 화면에서 다시 실행하면 이어서 찾아요.

} + {stalled &&

보통 1곡에 몇 초면 끝나는데 {span(now - since)} 동안 처리한 곡이 없어요. 아래 작업 기록을 확인하고, 계속 그러면 수집 화면에서 멈췄다 다시 실행해 주세요.

} + {!running && job.status !== 'done' && lastLog &&

{logText(lastLog.text)}

} + +
+ {stats.map(([k, v, sub]) => ( +
+ {k} + {v} + {sub && {sub}} +
+ ))} +
+ {cutByDeadline && ( +

+ {clock(job.deadline!)}가 되면 정기 작업과 겹치지 않게 멈춰요. 그때까지 약 {Math.round(((job.deadline! - now) / 60_000) * job.perMin).toLocaleString()}곡 더 찾고, 남은 곡은 다음 작업이 이어서 찾아요. +

+ )} +
+ + 이번 작업 결과 ({decided.toLocaleString()}곡): 확정 {r.matched.toLocaleString()} ({share(r.matched)}) · 후보 {r.candidate.toLocaleString()} ({share(r.candidate)}) · 못 찾음{' '} + {r.none.toLocaleString()} ({share(r.none)}) + + {decided > 0 && ( + + )} +
+ {job.recent.length > 0 && ( +
+ 방금 처리한 곡 +
    + {job.recent.map((x) => { + const [label, cls] = x.youtube_status === 'none' && x.youtube_source === 'admin' ? ['원곡 없음', ''] : x.youtube_status === 'none' ? ['못 찾음', ''] : (STATUS[x.youtube_status] ?? [x.youtube_status, '']); + return ( +
  • + {ago(x.at, now)} + {label} + + + {x.artist} + {x.youtube_title ? ` → ${x.youtube_title}${x.youtube_channel ? ` · ${x.youtube_channel}` : ''}` : ''} + +
  • + ); + })} +
+
+ )} + {job.log.length > 0 && ( +
+ 작업 기록 (최근 {job.log.length}줄) +
    + {job.log.map((l, i) => ( +
  • + {clock(l.at)} {logText(l.text)} +
  • + ))} +
+
+ )} + {job.history.length > 0 && ( +
+ 지난 작업 {job.history.length}개 +
    + {job.history.map((h) => ( +
  • + #{h.id} {when(h.started_at, now)} 시작 · {JOB_STATUS[h.status] ?? h.status} · {h.done.toLocaleString()} / {h.total.toLocaleString()}곡 + {h.finished_at ? ` · ${span(h.finished_at - h.started_at)} 걸림` : ''} +
  • + ))} +
+
+ )} + {footer} +
+ ); +} + /** Which tab a song belongs to now (same rules as the server). */ function tabOf(d: Detail): Tab { const s = d.song; @@ -157,11 +351,8 @@ export function YoutubePage() { } }; - const job = data?.job; - const running = job?.status === 'running'; const pages = data ? Math.max(1, Math.ceil(data.total / data.pageSize)) : 1; const now = Date.now(); - const eta = running && job.done > 0 && job.total > job.done ? now + ((now - job.started_at) / job.done) * (job.total - job.done) : null; return (
@@ -172,33 +363,7 @@ export function YoutubePage() { 수집 화면에서 실행·멈추기
-
- {job ? ( - <> -
- - {running ? '🔎 지금 찾는 중' : `마지막 작업: ${JOB_STATUS[job.status] ?? job.status}`}{' '} - - {job.done.toLocaleString()} / {job.total.toLocaleString()} - {job.total ? ` (${Math.floor((job.done / job.total) * 100)}%)` : ''} - - - - 시작 {clock(job.started_at)} - {running && eta && ` · 끝 예상 ${clock(eta)}`} - {!running && job.finished_at && ` · 끝 ${new Date(job.finished_at).toLocaleString('ko-KR', { dateStyle: 'short', timeStyle: 'short' })}`} - -
- -

- 이번 작업 결과: 확정 {job.result.matched.toLocaleString()} · 후보 {job.result.candidate.toLocaleString()} · 못 찾음 {job.result.none.toLocaleString()} -

- - ) : ( -

아직 돌린 원곡 찾기 작업이 없어요.

- )} -

매일 08:10에 3,000곡씩 자동으로 찾아요. 확정한 영상은 저장하는 즉시 새 게임과 다음 문제부터 나와요(지금 재생 중인 곡은 그대로).

-
+ (setSelected(id), setEditing(false))} />
{TABS.map(([t, label]) => (