diff --git a/server/src/internal-api/import/import.ts b/server/src/internal-api/import/import.ts index 295cc7b6a..855274e95 100644 --- a/server/src/internal-api/import/import.ts +++ b/server/src/internal-api/import/import.ts @@ -82,7 +82,7 @@ router.post( }); } - let importDocument = await ScoreImportMain(userDoc, importType, inputParser); + let importDocument = await ScoreImportMain(userDoc, true, importType, inputParser); return res.status(200).json({ success: true, diff --git a/server/src/score-import/framework/score-import-main.ts b/server/src/score-import/framework/score-import-main.ts index 7b84bde02..91429d896 100644 --- a/server/src/score-import/framework/score-import-main.ts +++ b/server/src/score-import/framework/score-import-main.ts @@ -8,8 +8,93 @@ import { ImportInputParser, ScorePlaytypeMap } from "../../types"; import { InsertQueue } from "./core/insert-score"; import { ImportAllIterableData } from "./importing/score-importing"; import { CreateImportLoggerAndID } from "./core/import-logger"; -import { Logger } from "winston"; import { CreateSessions } from "./core/sessions/sessions"; +import { GetMilisecondsSince } from "../../core/hrtime-core"; + +export default async function ScoreImportMain( + user: PublicUserDocument, + userIntent: boolean, + importType: ImportTypes, + InputParser: ImportInputParser +) { + const timeStarted = Date.now(); + const { importID, logger } = CreateImportLoggerAndID(user, importType); + logger.verbose("Received import request."); + + const parseTimeStart = process.hrtime.bigint(); + const { iterable, ConverterFunction, context, idStrings, game } = await InputParser(logger); + + const parseTime = GetMilisecondsSince(parseTimeStart); + + logger.verbose(`Parsing took ${parseTime} miliseconds.`); + + const importTimeStart = process.hrtime.bigint(); + + let importInfo = await ImportAllIterableData( + user.id, + iterable, + ConverterFunction, + context, + logger + ); + + // Flush the score queue out after importing. + let emptied = await InsertQueue(); + + if (emptied) { + logger.verbose(`Emptied ${emptied} documents from score queue.`); + } + + const importTime = GetMilisecondsSince(importTimeStart); + + logger.verbose( + `Importing took ${importTime} miliseconds. (${importTime / importInfo.length}ms/doc)` + ); + + let { scorePlaytypeMap, errors, scoreIDs } = ParseImportInfo(importInfo); + + // Update user's rating information + // @todo + + const sessionTimeStart = process.hrtime.bigint(); + + let sessionInfo = await CreateSessions(user.id, importType, game, scorePlaytypeMap, logger); + + const sessionTime = GetMilisecondsSince(sessionTimeStart); + + logger.verbose( + `Session Processing took ${sessionTime} miliseconds (${ + sessionTime / sessionInfo.length + }ms/doc).` + ); + + // Create import document + const ImportDocument: KTBlackImportDocument = { + importType, + idStrings: idStrings, + scoreIDs, + errors, + importID, + timeFinished: Date.now(), + timeStarted, + createdSessions: sessionInfo, + userID: user.id, + userIntent, + }; + + logger.info( + `Import took: ${ImportDocument.timeFinished - timeStarted}ms, with ${ + importInfo.length + } documents (Fails: ${errors.length}, Successes: ${scoreIDs.length}, Sessions: ${ + sessionInfo.length + }). Aprx ${(ImportDocument.timeFinished - timeStarted) / importInfo.length}ms/doc` + ); + + // Add this to the imports database + // @todo + + return ImportDocument; +} function ParseImportInfo(importInfo: ImportProcessingInfo[]) { let scorePlaytypeMap: ScorePlaytypeMap = Object.create(null); @@ -33,65 +118,3 @@ function ParseImportInfo(importInfo: ImportProcessingInfo[]) { return { scoreIDs, errors, scorePlaytypeMap }; } - -export default async function ScoreImportMain( - user: PublicUserDocument, - importType: ImportTypes, - InputParser: ImportInputParser -) { - const timeStarted = Date.now(); - const { importID, logger } = CreateImportLoggerAndID(user, importType); - - const { iterable, ConverterFunction, context, idStrings, game } = await InputParser(logger); - - // @todo: scope logger properly - logger.verbose("Received import request."); - - let importInfo = await ImportAllIterableData( - user.id, - iterable, - ConverterFunction, - context, - logger - ); - - // Empty anything in the score queue - let emptied = await InsertQueue(); - - if (emptied) { - logger.verbose(`Emptied ${emptied} documents from score queue.`); - } - - let { scorePlaytypeMap, errors, scoreIDs } = ParseImportInfo(importInfo); - - // Update user's rating information - // @todo - - let sessionInfo = await CreateSessions(user.id, importType, game, scorePlaytypeMap, logger); - - // Create import document - const ImportDocument: KTBlackImportDocument = { - importType, - idStrings: idStrings, - scoreIDs, - errors, - importID, - timeFinished: Date.now(), - timeStarted, - createdSessions: [], // @todo, see session ctor - userID: user.id, - }; - - logger.info( - `Import took: ${ImportDocument.timeFinished - timeStarted}ms, with ${ - importInfo.length - } documents (Fails: ${errors.length}, Successes: ${scoreIDs.length}). Aprx ${ - (ImportDocument.timeFinished - timeStarted) / importInfo.length - }ms/doc` - ); - - // Add this to the imports database - // @todo - - return ImportDocument; -}