diff --git a/server/src/scripts/__KT_DATABASE_MIGRATION/charts-iidx.ts b/server/src/scripts/__KT_DATABASE_MIGRATION/charts-iidx.ts index 46c1b0034..6719018f6 100644 --- a/server/src/scripts/__KT_DATABASE_MIGRATION/charts-iidx.ts +++ b/server/src/scripts/__KT_DATABASE_MIGRATION/charts-iidx.ts @@ -21,8 +21,7 @@ function ConvertFn(c: any): ChartDocument<"iidx:SP" | "iidx:DP"> { "N-1": c.flags["N-1"], }, data: { - inGameID: c.internals.inGameID, - inGameINTID: c.internals.inGameINTID, + inGameID: c.internals.inGameINTID, notecount: c.notedata.notecount, }, }; @@ -34,12 +33,10 @@ function ConvertFn(c: any): ChartDocument<"iidx:SP" | "iidx:DP"> { newChartDoc.length = `${m}:${s}`; } - if (c.bpmMin && c.bpmMax) { - if (c.monoBPM) { - newChartDoc.bpmString = c.bpmMin; - } else { - newChartDoc.bpmString = `${c.bpmMin}-${c.bpmMax}`; - } + if (c.monoBPM) { + newChartDoc.bpmString = c.bpmMin.toString(); + } else { + newChartDoc.bpmString = `${c.bpmMin}-${c.bpmMax}`; } if (newChartDoc.length === null) { diff --git a/server/src/scripts/validate-database.ts b/server/src/scripts/validate-database.ts index 7d7c90267..40f219351 100644 --- a/server/src/scripts/validate-database.ts +++ b/server/src/scripts/validate-database.ts @@ -1,6 +1,6 @@ import Prudence, { PrudenceSchema } from "prudence"; import db from "../db/db"; -import { PRUDENCE_SCORE_SCHEMAS, STATIC_SCHEMAS } from "../db/schemas"; +import { PRUDENCE_CHART_SCHEMAS, PRUDENCE_SCORE_SCHEMAS, STATIC_SCHEMAS } from "../db/schemas"; import CreateLogCtx from "../logger"; import fs from "fs"; import path from "path"; @@ -9,11 +9,15 @@ import { supportedGames, validPlaytypes } from "kamaitachi-common/js/config"; const BASE_DIR = path.join(__dirname, "./validate-database-errs"); +// note: a hell of a lot of this code is copy pasted +// this is just a script, so, feel free to refactor it. + const logger = CreateLogCtx("validate-database.ts"); async function ValidateStaticSchemas(): Promise { for (const x in STATIC_SCHEMAS) { - const c = x as keyof typeof db; + const c = x as keyof typeof STATIC_SCHEMAS; + const schema = STATIC_SCHEMAS[c] as PrudenceSchema; logger.info(`=== Validating Collection ${c}... ===`); @@ -126,11 +130,67 @@ async function ValidateScores(): Promise { } async function ValidateCharts(): Promise { + let globalStart = process.hrtime.bigint(); + let globalTotal = 0; + let globalSuccess = 0; + let globalFail = 0; for (const game of supportedGames) { - let col = db.charts[game]; - let schema = STATIC_SCHEMAS.charts[game]; + let schema = PRUDENCE_CHART_SCHEMAS[game]; + + let successCount = 0; + let total = 0; + let fails: { err: PrudenceError; doc: unknown }[] = []; + + logger.info(`=== Validating Collection charts (${game}) ... ===`); + let start = process.hrtime.bigint(); + + // eslint-disable-next-line no-await-in-loop + await db.charts[game] + .find({}, { projection: { _id: 0 } }) + // @ts-expect-error shut UP + .each((c: unknown) => { + total++; + let res = Prudence(c, schema); + + if (res === null) { + successCount++; + } else { + logger.error(`[${game}] [${res.keychain}] ${res.message}: ${res.userVal}`); + fails.push({ err: res, doc: c }); + } + }); + + let end = process.hrtime.bigint(); + logger.info(`Validated ${total} objects. Took ${Number(end - start) / 1e9} seconds.`); + logger.info(`Success: ${successCount} (${((successCount * 100) / total).toFixed(2)}%)`); + logger.info(`Fail: ${fails.length} (${((fails.length * 100) / total).toFixed(2)}%)`); + + if (fails.length !== 0) { + logger.severe(`Invalid documents found! Please resolve them.`); + fs.writeFileSync(path.join(BASE_DIR, `charts-${game}.json`), JSON.stringify(fails)); + fs.writeFileSync( + path.join(BASE_DIR, `charts-${game}.log`), + `Validated ${total} objects. Took ${ + Number(end - start) / 1e9 + } seconds.\nSuccess: ${successCount} (${((successCount * 100) / total).toFixed( + 2 + )}%)\nFail: ${fails.length} (${((fails.length * 100) / total).toFixed(2)}%)` + ); + } + + globalTotal += total; + globalFail += fails.length; + globalSuccess += successCount; } + + let globalEnd = process.hrtime.bigint(); + logger.info( + `Validated ${globalTotal} objects. Took ${Number(globalEnd - globalStart) / 1e9} seconds.` + ); + logger.info(`Success: ${globalSuccess} (${((globalSuccess * 100) / globalTotal).toFixed(2)}%)`); + logger.info(`Fail: ${globalFail} (${((globalFail * 100) / globalTotal).toFixed(2)}%)`); } // ValidateStaticSchemas(); -ValidateScores(); +// ValidateScores(); +ValidateCharts();