From 108bf3b16892fd96ec3eb28ab523e455c252c01d Mon Sep 17 00:00:00 2001 From: DebaucheryLibrarian Date: Fri, 13 Mar 2026 05:14:42 +0100 Subject: [PATCH] Integrated manticore stash sync tool. --- src/tools/manticore-stashes.js | 88 ++++++++++++++++++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100644 src/tools/manticore-stashes.js diff --git a/src/tools/manticore-stashes.js b/src/tools/manticore-stashes.js new file mode 100644 index 00000000..98b9d6f9 --- /dev/null +++ b/src/tools/manticore-stashes.js @@ -0,0 +1,88 @@ +'use strict'; + +const config = require('config'); +const manticore = require('manticoresearch'); + +const knex = require('../knex'); +const chunk = require('../utils/chunk'); + +const mantiClient = new manticore.ApiClient(); + +mantiClient.basePath = `http://${config.database.manticore.host}:${config.database.manticore.httpPort}`; + +const utilsApi = new manticore.UtilsApi(mantiClient); +const indexApi = new manticore.IndexApi(mantiClient); + +async function syncStashes(domain = 'scene') { + await utilsApi.sql(`truncate table ${domain}s_stashed`); + + const stashes = await knex(`stashes_${domain}s`) + .select( + `stashes_${domain}s.id as stashed_id`, + `stashes_${domain}s.${domain}_id`, + 'stashes.id as stash_id', + 'stashes.user_id as user_id', + `stashes_${domain}s.created_at as created_at`, + ) + .leftJoin('stashes', 'stashes.id', `stashes_${domain}s.stash_id`); + + await chunk(stashes, 1000).reduce(async (chain, stashChunk, index) => { + await chain; + + const stashDocs = stashChunk.map((stash) => ({ + replace: { + index: `${domain}s_stashed`, + id: stash.stashed_id, + doc: { + [`${domain}_id`]: stash[`${domain}_id`], + stash_id: stash.stash_id, + user_id: stash.user_id, + created_at: Math.round(stash.created_at.getTime() / 1000), + }, + }, + })); + + await indexApi.bulk(stashDocs.map((doc) => JSON.stringify(doc)).join('\n')); + + console.log(`Synced ${index * 1000 + stashChunk.length}/${stashes.length} ${domain} stashes`); + }, Promise.resolve()); +} + +async function init() { + await utilsApi.sql('drop table if exists scenes_stashed'); + + await utilsApi.sql(`create table if not exists scenes_stashed ( + scene_id int, + stash_id int, + user_id int, + created_at timestamp + )`); + + await utilsApi.sql('drop table if exists movies_stashed'); + + await utilsApi.sql(`create table if not exists movies_stashed ( + movie_id int, + stash_id int, + user_id int, + created_at timestamp + )`); + + await utilsApi.sql('drop table if exists actors_stashed'); + + await utilsApi.sql(`create table if not exists actors_stashed ( + actor_id int, + stash_id int, + user_id int, + created_at timestamp + )`); + + await syncStashes('scene'); + await syncStashes('actor'); + await syncStashes('movie'); + + console.log('Done!'); + + knex.destroy(); +} + +init();