Integrated manticore stash sync tool.
This commit is contained in:
88
src/tools/manticore-stashes.js
Normal file
88
src/tools/manticore-stashes.js
Normal file
@@ -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();
|
||||||
Reference in New Issue
Block a user