2021-11-26 01:39:02 +01:00
|
|
|
const Path = require('path')
|
2024-08-29 00:26:23 +02:00
|
|
|
const uuidv4 = require('uuid').v4
|
2022-07-06 02:53:01 +02:00
|
|
|
const fs = require('../libs/fsExtra')
|
2022-07-07 02:18:27 +02:00
|
|
|
const date = require('../libs/dateAndTime')
|
2021-11-23 02:58:20 +01:00
|
|
|
|
2021-11-26 01:39:02 +01:00
|
|
|
const Logger = require('../Logger')
|
|
|
|
const { LogLevel } = require('../utils/constants')
|
2023-10-21 00:46:18 +02:00
|
|
|
const { secondsToTimestamp, elapsedPretty } = require('../utils/index')
|
2021-11-23 02:58:20 +01:00
|
|
|
|
|
|
|
class LibraryScan {
|
|
|
|
constructor() {
|
|
|
|
this.id = null
|
2022-02-16 01:33:33 +01:00
|
|
|
this.type = null
|
2024-08-29 00:26:23 +02:00
|
|
|
/** @type {import('../models/Library')} */
|
2023-06-10 19:46:57 +02:00
|
|
|
this.library = null
|
2021-11-26 01:39:02 +01:00
|
|
|
this.verbose = false
|
2021-11-23 02:58:20 +01:00
|
|
|
|
|
|
|
this.startedAt = null
|
|
|
|
this.finishedAt = null
|
2021-11-25 03:15:50 +01:00
|
|
|
this.elapsed = null
|
2023-09-19 00:38:45 +02:00
|
|
|
this.error = null
|
2021-11-23 02:58:20 +01:00
|
|
|
|
2021-11-25 03:15:50 +01:00
|
|
|
this.resultsMissing = 0
|
|
|
|
this.resultsAdded = 0
|
|
|
|
this.resultsUpdated = 0
|
2021-11-26 01:39:02 +01:00
|
|
|
|
2023-09-03 00:49:28 +02:00
|
|
|
/** @type {string[]} */
|
|
|
|
this.authorsRemovedFromBooks = []
|
|
|
|
/** @type {string[]} */
|
|
|
|
this.seriesRemovedFromBooks = []
|
|
|
|
|
2021-11-26 01:39:02 +01:00
|
|
|
this.logs = []
|
2021-11-23 02:58:20 +01:00
|
|
|
}
|
|
|
|
|
2024-08-29 00:26:23 +02:00
|
|
|
get libraryId() {
|
|
|
|
return this.library.id
|
|
|
|
}
|
|
|
|
get libraryName() {
|
|
|
|
return this.library.name
|
|
|
|
}
|
|
|
|
get libraryMediaType() {
|
|
|
|
return this.library.mediaType
|
|
|
|
}
|
|
|
|
get libraryFolders() {
|
|
|
|
return this.library.libraryFolders
|
|
|
|
}
|
2023-06-10 19:46:57 +02:00
|
|
|
|
2021-11-26 01:39:02 +01:00
|
|
|
get timestamp() {
|
2024-08-29 00:26:23 +02:00
|
|
|
return new Date().toISOString()
|
2021-11-26 01:39:02 +01:00
|
|
|
}
|
2021-11-23 02:58:20 +01:00
|
|
|
|
2021-11-25 03:15:50 +01:00
|
|
|
get resultStats() {
|
|
|
|
return `${this.resultsAdded} Added | ${this.resultsUpdated} Updated | ${this.resultsMissing} Missing`
|
|
|
|
}
|
|
|
|
get elapsedTimestamp() {
|
|
|
|
return secondsToTimestamp(this.elapsed / 1000)
|
|
|
|
}
|
|
|
|
get getScanEmitData() {
|
|
|
|
return {
|
|
|
|
id: this.libraryId,
|
2022-02-16 01:33:33 +01:00
|
|
|
type: this.type,
|
2021-11-25 03:15:50 +01:00
|
|
|
name: this.libraryName,
|
2023-09-19 00:38:45 +02:00
|
|
|
error: this.error,
|
2021-11-25 03:15:50 +01:00
|
|
|
results: {
|
|
|
|
added: this.resultsAdded,
|
|
|
|
updated: this.resultsUpdated,
|
|
|
|
missing: this.resultsMissing
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-11-26 01:39:02 +01:00
|
|
|
get totalResults() {
|
|
|
|
return this.resultsAdded + this.resultsUpdated + this.resultsMissing
|
|
|
|
}
|
2022-12-16 00:30:45 +01:00
|
|
|
get logFilename() {
|
2021-11-26 01:39:02 +01:00
|
|
|
return date.format(new Date(), 'YYYY-MM-DD') + '_' + this.id + '.txt'
|
|
|
|
}
|
2023-10-21 00:46:18 +02:00
|
|
|
get scanResultsString() {
|
|
|
|
if (this.error) return this.error
|
|
|
|
const strs = []
|
|
|
|
if (this.resultsAdded) strs.push(`${this.resultsAdded} added`)
|
|
|
|
if (this.resultsUpdated) strs.push(`${this.resultsUpdated} updated`)
|
|
|
|
if (this.resultsMissing) strs.push(`${this.resultsMissing} missing`)
|
|
|
|
if (!strs.length) return `Everything was up to date (${elapsedPretty(this.elapsed / 1000)})`
|
|
|
|
return strs.join(', ') + ` (${elapsedPretty(this.elapsed / 1000)})`
|
|
|
|
}
|
2021-11-26 01:39:02 +01:00
|
|
|
|
|
|
|
toJSON() {
|
|
|
|
return {
|
|
|
|
id: this.id,
|
2022-02-16 01:33:33 +01:00
|
|
|
type: this.type,
|
2023-06-10 19:46:57 +02:00
|
|
|
library: this.library.toJSON(),
|
2021-11-26 01:39:02 +01:00
|
|
|
startedAt: this.startedAt,
|
|
|
|
finishedAt: this.finishedAt,
|
|
|
|
elapsed: this.elapsed,
|
2023-09-19 00:38:45 +02:00
|
|
|
error: this.error,
|
2021-11-26 01:39:02 +01:00
|
|
|
resultsAdded: this.resultsAdded,
|
|
|
|
resultsUpdated: this.resultsUpdated,
|
|
|
|
resultsMissing: this.resultsMissing
|
|
|
|
}
|
|
|
|
}
|
2021-11-25 03:15:50 +01:00
|
|
|
|
2024-08-29 00:26:23 +02:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param {import('../models/Library')} library
|
|
|
|
* @param {string} type
|
|
|
|
*/
|
2023-09-04 20:59:37 +02:00
|
|
|
setData(library, type = 'scan') {
|
2023-07-05 01:14:44 +02:00
|
|
|
this.id = uuidv4()
|
2022-02-16 01:33:33 +01:00
|
|
|
this.type = type
|
2024-08-29 00:26:23 +02:00
|
|
|
this.library = library
|
2021-11-23 02:58:20 +01:00
|
|
|
|
|
|
|
this.startedAt = Date.now()
|
|
|
|
}
|
2021-11-25 03:15:50 +01:00
|
|
|
|
2023-09-19 00:38:45 +02:00
|
|
|
/**
|
2024-08-29 00:26:23 +02:00
|
|
|
*
|
|
|
|
* @param {string} error
|
2023-09-19 00:38:45 +02:00
|
|
|
*/
|
|
|
|
setComplete(error = null) {
|
2021-11-25 03:15:50 +01:00
|
|
|
this.finishedAt = Date.now()
|
|
|
|
this.elapsed = this.finishedAt - this.startedAt
|
2023-09-19 00:38:45 +02:00
|
|
|
this.error = error
|
2021-11-25 03:15:50 +01:00
|
|
|
}
|
2021-11-26 01:39:02 +01:00
|
|
|
|
|
|
|
getLogLevelString(level) {
|
|
|
|
for (const key in LogLevel) {
|
|
|
|
if (LogLevel[key] === level) {
|
|
|
|
return key
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 'UNKNOWN'
|
|
|
|
}
|
|
|
|
|
|
|
|
addLog(level, ...args) {
|
|
|
|
const logObj = {
|
|
|
|
timestamp: this.timestamp,
|
|
|
|
message: args.join(' '),
|
|
|
|
levelName: this.getLogLevelString(level),
|
|
|
|
level
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.verbose) {
|
2023-08-29 00:50:21 +02:00
|
|
|
Logger.debug(`[LibraryScan] "${this.libraryName}":`, ...args)
|
2021-11-26 01:39:02 +01:00
|
|
|
}
|
|
|
|
this.logs.push(logObj)
|
|
|
|
}
|
|
|
|
|
2022-12-16 00:30:45 +01:00
|
|
|
async saveLog() {
|
2024-02-15 23:46:19 +01:00
|
|
|
const scanLogDir = Path.join(global.MetadataPath, 'logs', 'scans')
|
2022-12-16 00:30:45 +01:00
|
|
|
|
2024-02-15 23:46:19 +01:00
|
|
|
if (!(await fs.pathExists(scanLogDir))) {
|
|
|
|
await fs.mkdir(scanLogDir)
|
|
|
|
}
|
|
|
|
|
|
|
|
const outputPath = Path.join(scanLogDir, this.logFilename)
|
2022-12-16 00:30:45 +01:00
|
|
|
const logLines = [JSON.stringify(this.toJSON())]
|
2024-08-29 00:26:23 +02:00
|
|
|
this.logs.forEach((l) => {
|
2021-11-26 01:39:02 +01:00
|
|
|
logLines.push(JSON.stringify(l))
|
|
|
|
})
|
|
|
|
await fs.writeFile(outputPath, logLines.join('\n') + '\n')
|
2022-12-16 00:30:45 +01:00
|
|
|
|
2021-11-26 01:39:02 +01:00
|
|
|
Logger.info(`[LibraryScan] Scan log saved "${outputPath}"`)
|
|
|
|
}
|
2021-11-23 02:58:20 +01:00
|
|
|
}
|
2024-08-29 00:26:23 +02:00
|
|
|
module.exports = LibraryScan
|