🏗️ Refactored the searchIssue method

This commit is contained in:
2023-12-28 22:52:33 -05:00
parent c926758db6
commit 78e0e9f8ce
2 changed files with 148 additions and 82 deletions

View File

@@ -33,7 +33,13 @@ SOFTWARE.
"use strict"; "use strict";
import { isNil } from "lodash"; import { isNil } from "lodash";
import { Context, Service, ServiceBroker, ServiceSchema, Errors } from "moleculer"; import {
Context,
Service,
ServiceBroker,
ServiceSchema,
Errors,
} from "moleculer";
import { DbMixin } from "../mixins/db.mixin"; import { DbMixin } from "../mixins/db.mixin";
import Comic from "../models/comic.model"; import Comic from "../models/comic.model";
import { walkFolder, getSizeOfDirectory } from "../utils/file.utils"; import { walkFolder, getSizeOfDirectory } from "../utils/file.utils";
@@ -95,7 +101,9 @@ export default class ImportService extends Service {
uncompressFullArchive: { uncompressFullArchive: {
rest: "POST /uncompressFullArchive", rest: "POST /uncompressFullArchive",
params: {}, params: {},
handler: async (ctx: Context<{ filePath: string; options: any }>) => { handler: async (
ctx: Context<{ filePath: string; options: any }>
) => {
await broker.call("importqueue.uncompressResize", { await broker.call("importqueue.uncompressResize", {
filePath: ctx.params.filePath, filePath: ctx.params.filePath,
options: ctx.params.options, options: ctx.params.options,
@@ -113,7 +121,8 @@ export default class ImportService extends Service {
}); });
// Determine source where the comic was added from // Determine source where the comic was added from
// and gather identifying information about it // and gather identifying information about it
const sourceName = referenceComicObject[0].acquisition.source.name; const sourceName =
referenceComicObject[0].acquisition.source.name;
const { sourcedMetadata } = referenceComicObject[0]; const { sourcedMetadata } = referenceComicObject[0];
const filePath = `${COMICS_DIRECTORY}/${ctx.params.bundle.data.name}`; const filePath = `${COMICS_DIRECTORY}/${ctx.params.bundle.data.name}`;
@@ -157,8 +166,14 @@ export default class ImportService extends Service {
// 1.1 Filter on .cb* extensions // 1.1 Filter on .cb* extensions
.pipe( .pipe(
through2.obj(function (item, enc, next) { through2.obj(function (item, enc, next) {
let fileExtension = path.extname(item.path); let fileExtension = path.extname(
if ([".cbz", ".cbr", ".cb7"].includes(fileExtension)) { item.path
);
if (
[".cbz", ".cbr", ".cb7"].includes(
fileExtension
)
) {
this.push(item); this.push(item);
} }
next(); next();
@@ -167,7 +182,10 @@ export default class ImportService extends Service {
// 1.2 Pipe filtered results to the next step // 1.2 Pipe filtered results to the next step
// Enqueue the job in the queue // Enqueue the job in the queue
.on("data", async (item) => { .on("data", async (item) => {
console.info("Found a file at path: %s", item.path); console.info(
"Found a file at path: %s",
item.path
);
let comicExists = await Comic.exists({ let comicExists = await Comic.exists({
"rawFileDetails.name": `${path.basename( "rawFileDetails.name": `${path.basename(
item.path, item.path,
@@ -176,8 +194,14 @@ export default class ImportService extends Service {
}); });
if (!comicExists) { if (!comicExists) {
// 2.1 Reset the job counters in Redis // 2.1 Reset the job counters in Redis
await pubClient.set("completedJobCount", 0); await pubClient.set(
await pubClient.set("failedJobCount", 0); "completedJobCount",
0
);
await pubClient.set(
"failedJobCount",
0
);
// 2.2 Send the extraction job to the queue // 2.2 Send the extraction job to the queue
this.broker.call("jobqueue.enqueue", { this.broker.call("jobqueue.enqueue", {
fileObject: { fileObject: {
@@ -188,7 +212,9 @@ export default class ImportService extends Service {
importType: "new", importType: "new",
}); });
} else { } else {
console.log("Comic already exists in the library."); console.log(
"Comic already exists in the library."
);
} }
}) })
.on("end", () => { .on("end", () => {
@@ -240,19 +266,28 @@ export default class ImportService extends Service {
// we solicit volume information and add that to mongo // we solicit volume information and add that to mongo
if ( if (
comicMetadata.sourcedMetadata.comicvine && comicMetadata.sourcedMetadata.comicvine &&
!isNil(comicMetadata.sourcedMetadata.comicvine.volume) !isNil(
comicMetadata.sourcedMetadata.comicvine
.volume
)
) { ) {
volumeDetails = await this.broker.call("comicvine.getVolumes", { volumeDetails = await this.broker.call(
volumeURI: "comicvine.getVolumes",
comicMetadata.sourcedMetadata.comicvine.volume {
.api_detail_url, volumeURI:
}); comicMetadata.sourcedMetadata
.comicvine.volume
.api_detail_url,
}
);
comicMetadata.sourcedMetadata.comicvine.volumeInformation = comicMetadata.sourcedMetadata.comicvine.volumeInformation =
volumeDetails.results; volumeDetails.results;
} }
console.log("Saving to Mongo..."); console.log("Saving to Mongo...");
console.log(`Import type: [${ctx.params.importType}]`); console.log(
`Import type: [${ctx.params.importType}]`
);
switch (ctx.params.importType) { switch (ctx.params.importType) {
case "new": case "new":
return await Comic.create(comicMetadata); return await Comic.create(comicMetadata);
@@ -273,7 +308,10 @@ export default class ImportService extends Service {
} }
} catch (error) { } catch (error) {
console.log(error); console.log(error);
throw new Errors.MoleculerError("Import failed.", 500); throw new Errors.MoleculerError(
"Import failed.",
500
);
} }
}, },
}, },
@@ -291,7 +329,9 @@ export default class ImportService extends Service {
) { ) {
// 1. Find mongo object by id // 1. Find mongo object by id
// 2. Import payload into sourcedMetadata.comicvine // 2. Import payload into sourcedMetadata.comicvine
const comicObjectId = new ObjectId(ctx.params.comicObjectId); const comicObjectId = new ObjectId(
ctx.params.comicObjectId
);
return new Promise(async (resolve, reject) => { return new Promise(async (resolve, reject) => {
let volumeDetails = {}; let volumeDetails = {};
@@ -300,15 +340,18 @@ export default class ImportService extends Service {
const volumeDetails = await this.broker.call( const volumeDetails = await this.broker.call(
"comicvine.getVolumes", "comicvine.getVolumes",
{ {
volumeURI: matchedResult.volume.api_detail_url, volumeURI:
matchedResult.volume.api_detail_url,
} }
); );
matchedResult.volumeInformation = volumeDetails.results; matchedResult.volumeInformation =
volumeDetails.results;
Comic.findByIdAndUpdate( Comic.findByIdAndUpdate(
comicObjectId, comicObjectId,
{ {
$set: { $set: {
"sourcedMetadata.comicvine": matchedResult, "sourcedMetadata.comicvine":
matchedResult,
}, },
}, },
{ new: true }, { new: true },
@@ -339,7 +382,9 @@ export default class ImportService extends Service {
}> }>
) { ) {
console.log(JSON.stringify(ctx.params, null, 2)); console.log(JSON.stringify(ctx.params, null, 2));
const comicObjectId = new ObjectId(ctx.params.comicObjectId); const comicObjectId = new ObjectId(
ctx.params.comicObjectId
);
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
Comic.findByIdAndUpdate( Comic.findByIdAndUpdate(
@@ -385,6 +430,7 @@ export default class ImportService extends Service {
rest: "POST /getComicBookById", rest: "POST /getComicBookById",
params: { id: "string" }, params: { id: "string" },
async handler(ctx: Context<{ id: string }>) { async handler(ctx: Context<{ id: string }>) {
console.log(ctx.params.id);
return await Comic.findById(ctx.params.id); return await Comic.findById(ctx.params.id);
}, },
}, },
@@ -393,7 +439,9 @@ export default class ImportService extends Service {
params: { ids: "array" }, params: { ids: "array" },
handler: async (ctx: Context<{ ids: [string] }>) => { handler: async (ctx: Context<{ ids: [string] }>) => {
console.log(ctx.params.ids); console.log(ctx.params.ids);
const queryIds = ctx.params.ids.map((id) => new ObjectId(id)); const queryIds = ctx.params.ids.map(
(id) => new ObjectId(id)
);
return await Comic.find({ return await Comic.find({
_id: { _id: {
$in: queryIds, $in: queryIds,
@@ -409,7 +457,8 @@ export default class ImportService extends Service {
const volumes = await Comic.aggregate([ const volumes = await Comic.aggregate([
{ {
$project: { $project: {
volumeInfo: "$sourcedMetadata.comicvine.volumeInformation", volumeInfo:
"$sourcedMetadata.comicvine.volumeInformation",
}, },
}, },
{ {
@@ -455,46 +504,52 @@ export default class ImportService extends Service {
const { queryObjects } = ctx.params; const { queryObjects } = ctx.params;
// construct the query for ElasticSearch // construct the query for ElasticSearch
let elasticSearchQuery = {}; let elasticSearchQuery = {};
const elasticSearchQueries = queryObjects.map((queryObject) => { const elasticSearchQueries = queryObjects.map(
console.log("Volume: ", queryObject.volumeName); (queryObject) => {
console.log("Issue: ", queryObject.issueName); console.log("Volume: ", queryObject.volumeName);
if (queryObject.issueName === null) { console.log("Issue: ", queryObject.issueName);
queryObject.issueName = ""; if (queryObject.issueName === null) {
} queryObject.issueName = "";
if (queryObject.volumeName === null) { }
queryObject.volumeName = ""; if (queryObject.volumeName === null) {
} queryObject.volumeName = "";
elasticSearchQuery = { }
bool: { elasticSearchQuery = {
must: [ bool: {
{ must: [
match_phrase: { {
"rawFileDetails.name": queryObject.volumeName, match_phrase: {
"rawFileDetails.name":
queryObject.volumeName,
},
}, },
}, {
{ term: {
term: { "inferredMetadata.issue.number":
"inferredMetadata.issue.number": parseInt( parseInt(
queryObject.issueNumber, queryObject.issueNumber,
10 10
), ),
},
}, },
}, ],
], },
}, };
};
return [ return [
{ {
index: "comics", index: "comics",
search_type: "dfs_query_then_fetch", search_type: "dfs_query_then_fetch",
}, },
{ {
query: elasticSearchQuery, query: elasticSearchQuery,
}, },
]; ];
}); }
console.log(JSON.stringify(elasticSearchQueries, null, 2)); );
console.log(
JSON.stringify(elasticSearchQueries, null, 2)
);
return await ctx.broker.call("search.searchComic", { return await ctx.broker.call("search.searchComic", {
elasticSearchQueries, elasticSearchQueries,
@@ -507,11 +562,10 @@ export default class ImportService extends Service {
rest: "GET /libraryStatistics", rest: "GET /libraryStatistics",
params: {}, params: {},
handler: async (ctx: Context<{}>) => { handler: async (ctx: Context<{}>) => {
const comicDirectorySize = await getSizeOfDirectory(COMICS_DIRECTORY, [ const comicDirectorySize = await getSizeOfDirectory(
".cbz", COMICS_DIRECTORY,
".cbr", [".cbz", ".cbr", ".cb7"]
".cb7", );
]);
const totalCount = await Comic.countDocuments({}); const totalCount = await Comic.countDocuments({});
const statistics = await Comic.aggregate([ const statistics = await Comic.aggregate([
{ {
@@ -520,7 +574,11 @@ export default class ImportService extends Service {
{ {
$match: { $match: {
"rawFileDetails.extension": { "rawFileDetails.extension": {
$in: [".cbr", ".cbz", ".cb7"], $in: [
".cbr",
".cbz",
".cb7",
],
}, },
}, },
}, },
@@ -534,9 +592,10 @@ export default class ImportService extends Service {
issues: [ issues: [
{ {
$match: { $match: {
"sourcedMetadata.comicvine.volumeInformation": { "sourcedMetadata.comicvine.volumeInformation":
$gt: {}, {
}, $gt: {},
},
}, },
}, },
{ {
@@ -599,16 +658,23 @@ export default class ImportService extends Service {
.drop() .drop()
.then(async (data) => { .then(async (data) => {
console.info(data); console.info(data);
const coversFolderDeleteResult = fsExtra.emptyDirSync( const coversFolderDeleteResult =
path.resolve(`${USERDATA_DIRECTORY}/covers`) fsExtra.emptyDirSync(
); path.resolve(
const expandedFolderDeleteResult = fsExtra.emptyDirSync( `${USERDATA_DIRECTORY}/covers`
path.resolve(`${USERDATA_DIRECTORY}/expanded`) )
); );
const eSIndicesDeleteResult = await ctx.broker.call( const expandedFolderDeleteResult =
"search.deleteElasticSearchIndices", fsExtra.emptyDirSync(
{} path.resolve(
); `${USERDATA_DIRECTORY}/expanded`
)
);
const eSIndicesDeleteResult =
await ctx.broker.call(
"search.deleteElasticSearchIndices",
{}
);
return { return {
data, data,
coversFolderDeleteResult, coversFolderDeleteResult,

View File

@@ -75,9 +75,9 @@ export default class SettingsService extends Service {
) => { ) => {
try { try {
console.log(ctx.params); console.log(ctx.params);
const { query, pagination } = ctx.params; const { query, pagination, type } = ctx.params;
let eSQuery = {}; let eSQuery = {};
switch (ctx.params.type) { switch (type) {
case "all": case "all":
Object.assign(eSQuery, { Object.assign(eSQuery, {
match_all: {}, match_all: {},