[O] Better logging

This commit is contained in:
2025-11-25 10:16:59 +08:00
parent 33cabbcefc
commit 1b4de5634a
12 changed files with 42 additions and 9 deletions
+2
View File
@@ -5,11 +5,13 @@ import { ObjectId } from "mongodb"
export async function saveResult(data: Omit<ResultDocument, "_id" | "createdAt">): Promise<string> { export async function saveResult(data: Omit<ResultDocument, "_id" | "createdAt">): Promise<string> {
const doc = { ...data, createdAt: new Date() } const doc = { ...data, createdAt: new Date() }
const res = await dbs.results.insertOne(doc) const res = await dbs.results.insertOne(doc)
console.log(`Saved result ${res.insertedId} for user ${data.userId}`)
return res.insertedId.toString() return res.insertedId.toString()
} }
export async function getResult(id: string): Promise<ResultDocument | null> { export async function getResult(id: string): Promise<ResultDocument | null> {
try { try {
console.log(`Getting result ${id}`)
return await dbs.results.findOne({ _id: new ObjectId(id) }) return await dbs.results.findOne({ _id: new ObjectId(id) })
} catch { } catch {
return null return null
+15 -3
View File
@@ -12,7 +12,12 @@ const API_URL = process.env.AUDIO_SEPARATOR_API
export async function separateSong(inputPath: string, outputDir: string) { export async function separateSong(inputPath: string, outputDir: string) {
const vocalsPath = path.join(outputDir, 'vocals.opus') const vocalsPath = path.join(outputDir, 'vocals.opus')
const instrumentalPath = path.join(outputDir, 'instrumental.opus') const instrumentalPath = path.join(outputDir, 'instrumental.opus')
if (await fs.exists(vocalsPath) && await fs.exists(instrumentalPath)) return if (await fs.exists(vocalsPath) && await fs.exists(instrumentalPath)) {
console.log(`Separation already done for ${inputPath}`)
return
}
console.log(`Starting separation for ${inputPath}`)
// Read file and create FormData // Read file and create FormData
const fileBuffer = await fs.readFile(inputPath) const fileBuffer = await fs.readFile(inputPath)
@@ -40,6 +45,7 @@ export async function separateSong(inputPath: string, outputDir: string) {
await downloadStem('vocals', vocalsPath) await downloadStem('vocals', vocalsPath)
await downloadStem('instrumental', instrumentalPath) await downloadStem('instrumental', instrumentalPath)
console.log(`Separation completed for ${inputPath}`)
// Clean up task on server // Clean up task on server
try { try {
@@ -49,8 +55,14 @@ export async function separateSong(inputPath: string, outputDir: string) {
} }
break break
} }
if (status.status === 'error') throw error(500, status.error || '分离失败') if (status.status === 'error') {
if (status.status === 'not_found') throw error(500, '任务丢失') console.error(`Separation failed for ${inputPath}: ${status.error}`)
throw error(500, status.error || '分离失败')
}
if (status.status === 'not_found') {
console.error(`Separation task lost for ${inputPath}`)
throw error(500, '任务丢失')
}
} }
} }
+10 -2
View File
@@ -50,6 +50,7 @@ function parsePlaylistRef(ref: string): number {
const getPlaylistRaw = cached(dbs.playlistsRaw, const getPlaylistRaw = cached(dbs.playlistsRaw,
async (id: number) => { async (id: number) => {
console.log(`Fetching playlist raw ${id}`)
const pl = ((await ne.playlist_detail({ id })).body as any).playlist const pl = ((await ne.playlist_detail({ id })).body as any).playlist
for (const track of pl.tracks) for (const track of pl.tracks)
await dbs.songsRaw.replaceOne({ _id: track.id }, { _id: track.id, data: track }, { upsert: true }) await dbs.songsRaw.replaceOne({ _id: track.id }, { _id: track.id, data: track }, { upsert: true })
@@ -147,7 +148,11 @@ const songProcessingStatus = new Map<number, SongProcessState>()
export const getSongStatus = (songId: number) => songProcessingStatus.get(songId) || { items: [], status: 'idle' } export const getSongStatus = (songId: number) => songProcessingStatus.get(songId) || { items: [], status: 'idle' }
export const checkLyricsProcessed = async (songId: number) => !!await dbs.lyricsProcessed.findOne({ _id: songId as any }) export const checkLyricsProcessed = async (songId: number) => !!await dbs.lyricsProcessed.findOne({ _id: songId as any })
export const prepareSong = async (songId: number) => { export const prepareSong = async (songId: number) => {
if (songProcessingStatus.has(songId)) return console.log(`Preparing song ${songId}`)
if (songProcessingStatus.has(songId)) {
console.log(`Song ${songId} is already being processed`)
return
}
const state: SongProcessState = { items: [], status: 'running' } const state: SongProcessState = { items: [], status: 'running' }
songProcessingStatus.set(songId, state) songProcessingStatus.set(songId, state)
@@ -196,7 +201,9 @@ export const prepareSong = async (songId: number) => {
try { try {
await Promise.all([processLyrics(), processMusic()]) await Promise.all([processLyrics(), processMusic()])
state.status = 'done' state.status = 'done'
console.log(`Song ${songId} preparation done`)
} catch (e) { } catch (e) {
console.error(`Song ${songId} preparation failed`, e)
addTask('error', eToString(e)).progress = -1 addTask('error', eToString(e)).progress = -1
state.status = 'error' state.status = 'error'
} }
@@ -261,7 +268,6 @@ async function processImport(session: ImportSession, data: any) {
data.tracks = (await Promise.all(session.songs.map(async item => { data.tracks = (await Promise.all(session.songs.map(async item => {
try { try {
const lyrics = await getLyricsRaw(item.song.id) const lyrics = await getLyricsRaw(item.song.id)
console.log(`Song ${item.song.id} lang ${lyrics.lang}`)
if (lyrics.lang === 'jpn') { if (lyrics.lang === 'jpn') {
item.status = 'success' item.status = 'success'
return item.song return item.song
@@ -298,6 +304,7 @@ export const listRecPlaylists = async () => {
const list = await dbs.playlists.find({ const list = await dbs.playlists.find({
_id: { $in: defaultPlaylists } _id: { $in: defaultPlaylists }
} as any).map(it => it.data).toArray() } as any).map(it => it.data).toArray()
console.log(`Listing recommended playlists: ${list.length} found`)
return list.sort((a: any, b: any) => defaultPlaylists.indexOf(a.id) - defaultPlaylists.indexOf(b.id)) return list.sort((a: any, b: any) => defaultPlaylists.indexOf(a.id) - defaultPlaylists.indexOf(b.id))
} }
export const listMyPlaylists = async (user: UserDocument) => (await user.data.myPlaylists?.let(pl => dbs.playlists.find({ export const listMyPlaylists = async (user: UserDocument) => (await user.data.myPlaylists?.let(pl => dbs.playlists.find({
@@ -305,6 +312,7 @@ export const listMyPlaylists = async (user: UserDocument) => (await user.data.my
}).map(it => it.data).toArray())) ?? [] }).map(it => it.data).toArray())) ?? []
export const getPlaylist = async (playlistId: number | string) => { export const getPlaylist = async (playlistId: number | string) => {
console.log(`Getting playlist ${playlistId}`)
const plData = await dbs.playlists.findOne({ _id: +playlistId as any }) const plData = await dbs.playlists.findOne({ _id: +playlistId as any })
if (!plData) throw error(404, 'Playlist not found') if (!plData) throw error(404, 'Playlist not found')
return plData.data return plData.data
+5 -1
View File
@@ -16,6 +16,7 @@ void users.createIndex({ syncCode: 1 }, { name: "users_sync_code_idx" })
export async function createUser(registUA: string): Promise<string> { export async function createUser(registUA: string): Promise<string> {
const ses = `${crypto.randomUUID()}-${Date.now().toString(36)}` const ses = `${crypto.randomUUID()}-${Date.now().toString(36)}`
await users.insertOne({ registUA, createdAt: new Date(), sessions: [ses], data: {} }) await users.insertOne({ registUA, createdAt: new Date(), sessions: [ses], data: {} })
console.log(`Created new user with session ${ses}`)
return ses return ses
} }
@@ -41,6 +42,7 @@ export async function createSyncCode(session: string): Promise<string> {
// Sync code is 4 * 5 numbers // Sync code is 4 * 5 numbers
const code = Array.from({ length: 4 }, () => Math.floor(Math.random() * 100000).toString().padStart(5, '0')).join('-') const code = Array.from({ length: 4 }, () => Math.floor(Math.random() * 100000).toString().padStart(5, '0')).join('-')
await users.updateOne({ _id: user._id }, { $set: { syncCode: code, syncCodeCreated: new Date() } }) await users.updateOne({ _id: user._id }, { $set: { syncCode: code, syncCodeCreated: new Date() } })
console.log(`Created sync code for user ${user._id}`)
return code return code
} }
@@ -52,6 +54,7 @@ export async function createSyncCode(session: string): Promise<string> {
export async function updateUserData(user: UserDocument, data: Partial<UserData>): Promise<void> { export async function updateUserData(user: UserDocument, data: Partial<UserData>): Promise<void> {
const newData = { ...(user.data || {}), ...data } const newData = { ...(user.data || {}), ...data }
await users.updateOne({ _id: user._id }, { $set: { data: newData } }) await users.updateOne({ _id: user._id }, { $set: { data: newData } })
console.log(`Updated user data for ${user._id}`)
} }
/** /**
@@ -80,6 +83,7 @@ export async function loginWithSyncCode(code: string, newUA: string): Promise<st
$unset: { syncCode: "", syncCodeCreated: "" } $unset: { syncCode: "", syncCodeCreated: "" }
} }
) )
console.log(`User ${user._id} logged in with sync code`)
return ses return ses
} }
-2
View File
@@ -13,8 +13,6 @@
const t = getI18n().home const t = getI18n().home
console.log(data.recPlaylists)
const loc = data.user.data.loc const loc = data.user.data.loc
const href = loc?.isFinished && loc?.lastResultId ? `/results/${loc.lastResultId}` : `/song/${data.last?.id}` const href = loc?.isFinished && loc?.lastResultId ? `/results/${loc.lastResultId}` : `/song/${data.last?.id}`
</script> </script>
+1
View File
@@ -7,6 +7,7 @@ export const POST: RequestHandler = async ({ request, cookies }) => {
if (!code) throw error(400, 'Missing sync code') if (!code) throw error(400, 'Missing sync code')
const ua = request.headers.get('user-agent') || 'unknown' const ua = request.headers.get('user-agent') || 'unknown'
console.log(`Login attempt with sync code from UA: ${ua}`)
const session = await loginWithSyncCode(code, ua) const session = await loginWithSyncCode(code, ua)
// Set session cookie // Set session cookie
@@ -7,7 +7,10 @@ export const POST: RequestHandler = async ({ request }) => {
if (!id) throw error(400, 'Import ID is required') if (!id) throw error(400, 'Import ID is required')
const session = getSession(id) const session = getSession(id)
if (!session) throw error(404, 'Session not found') if (!session) {
console.log(`API: Import session ${id} not found`)
throw error(404, 'Session not found')
}
return json(session); return json(session);
}; };
@@ -11,6 +11,7 @@ export const POST: RequestHandler = async ({ request, cookies }) => {
if (!user) throw error(401, 'Unauthorized'); if (!user) throw error(401, 'Unauthorized');
try { try {
console.log(`API: Starting import for link ${link} by user ${user._id}`)
return json(await startImport(link, user._id)) return json(await startImport(link, user._id))
} catch (e) { } catch (e) {
console.error(e) console.error(e)
+1
View File
@@ -10,6 +10,7 @@ export const POST: RequestHandler = async ({ request, cookies }) => {
data.userId = user._id data.userId = user._id
// Validate data here if needed // Validate data here if needed
console.log(`API: Saving result for user ${user._id}`)
const id = await saveResult(data) const id = await saveResult(data)
return json({ id }) return json({ id })
} catch (e) { } catch (e) {
@@ -3,6 +3,7 @@ import { prepareSong, getSongStatus } from '$lib/server/songs'
export async function POST({ params }) { export async function POST({ params }) {
const songId = +params.id const songId = +params.id
console.log(`API: Requesting preparation for song ${songId}`)
prepareSong(songId) // Start in background prepareSong(songId) // Start in background
return json({ status: 'started' }) return json({ status: 'started' })
} }
+1
View File
@@ -9,6 +9,7 @@ export const POST: RequestHandler = async ({ request, cookies }) => {
try { try {
const data = await request.json() const data = await request.json()
console.log(`API: Updating user data for ${user._id}`)
await updateUserData(user, data) await updateUserData(user, data)
return json({ success: true }) return json({ success: true })
} catch (e) { } catch (e) {
+1
View File
@@ -7,5 +7,6 @@ export const POST: RequestHandler = async ({ cookies }) => {
if (!session) throw error(401, 'Unauthorized') if (!session) throw error(401, 'Unauthorized')
const code = await createSyncCode(session) const code = await createSyncCode(session)
console.log(`API: Created sync code for session ${session}`)
return json({ code }) return json({ code })
} }