2024-03-14 23:08:24 +00:00
// import config from 'config';
import { format } from 'date-fns' ;
import { faker } from '@faker-js/faker' ;
2024-03-21 01:54:05 +00:00
import { indexApi } from '../manticore.js' ;
2024-03-14 23:08:24 +00:00
import { knexOwner as knex } from '../knex.js' ;
import slugify from '../utils/slugify.js' ;
import chunk from '../utils/chunk.js' ;
async function fetchScenes ( ) {
const scenes = await knex . raw ( `
SELECT
releases . id AS id ,
releases . title ,
releases . created _at ,
releases . date ,
releases . shoot _id ,
scenes _meta . stashed ,
entities . id as channel _id ,
entities . slug as channel _slug ,
entities . name as channel _name ,
parents . id as network _id ,
parents . slug as network _slug ,
parents . name as network _name ,
COALESCE ( JSON _AGG ( DISTINCT ( actors . id , actors . name ) ) FILTER ( WHERE actors . id IS NOT NULL ) , '[]' ) as actors ,
COALESCE ( JSON _AGG ( DISTINCT ( tags . id , tags . name , tags . priority , tags _aliases . name ) ) FILTER ( WHERE tags . id IS NOT NULL ) , '[]' ) as tags
FROM releases
LEFT JOIN scenes _meta ON scenes _meta . scene _id = releases . id
LEFT JOIN entities ON releases . entity _id = entities . id
LEFT JOIN entities AS parents ON parents . id = entities . parent _id
LEFT JOIN releases _actors AS local _actors ON local _actors . release _id = releases . id
LEFT JOIN releases _directors AS local _directors ON local _directors . release _id = releases . id
LEFT JOIN releases _tags AS local _tags ON local _tags . release _id = releases . id
LEFT JOIN actors ON local _actors . actor _id = actors . id
LEFT JOIN actors AS directors ON local _directors . director _id = directors . id
LEFT JOIN tags ON local _tags . tag _id = tags . id
LEFT JOIN tags as tags _aliases ON local _tags . tag _id = tags _aliases . alias _for AND tags _aliases . secondary = true
GROUP BY
releases . id ,
releases . title ,
releases . created _at ,
releases . date ,
releases . shoot _id ,
scenes _meta . stashed ,
entities . id ,
entities . name ,
entities . slug ,
entities . alias ,
parents . id ,
parents . name ,
parents . slug ,
parents . alias ;
` );
const actors = Object . fromEntries ( scenes . rows . flatMap ( ( row ) => row . actors . map ( ( actor ) => [ actor . f1 , faker . person . fullName ( ) ] ) ) ) ;
const tags = Object . fromEntries ( scenes . rows . flatMap ( ( row ) => row . tags . map ( ( tag ) => [ tag . f1 , faker . word . adjective ( ) ] ) ) ) ;
return scenes . rows . map ( ( row ) => {
const title = faker . lorem . lines ( 1 ) ;
const channelName = faker . company . name ( ) ;
const channelSlug = slugify ( channelName , '' ) ;
const networkName = faker . company . name ( ) ;
const networkSlug = slugify ( networkName , '' ) ;
const rowActors = row . actors . map ( ( actor ) => ( { f1 : actor . f1 , f2 : actors [ actor . f1 ] } ) ) ;
const rowTags = row . tags . map ( ( tag ) => ( { f1 : tag . f1 , f2 : tags [ tag . f1 ] , f3 : tag . f3 } ) ) ;
return {
... row ,
title ,
actors : rowActors ,
tags : rowTags ,
channel _name : channelName ,
channel _slug : channelSlug ,
network _name : networkName ,
network _slug : networkSlug ,
} ;
} ) ;
}
async function updateStashed ( docs ) {
await chunk ( docs , 1000 ) . reduce ( async ( chain , docsChunk ) => {
await chain ;
const sceneIds = docsChunk . map ( ( doc ) => doc . replace . id ) ;
const stashes = await knex ( 'stashes_scenes' )
. select ( 'stashes_scenes.id as stashed_id' , 'stashes_scenes.scene_id' , 'stashes.id as stash_id' , 'stashes.user_id as user_id' )
. leftJoin ( 'stashes' , 'stashes.id' , 'stashes_scenes.stash_id' )
. whereIn ( 'scene_id' , sceneIds ) ;
if ( stashes . length > 0 ) {
console . log ( stashes ) ;
}
const stashDocs = docsChunk . flatMap ( ( doc ) => {
const sceneStashes = stashes . filter ( ( stash ) => stash . scene _id === doc . replace . id ) ;
if ( sceneStashes . length === 0 ) {
return [ ] ;
}
const stashDoc = sceneStashes . map ( ( stash ) => ( {
replace : {
2024-03-21 01:54:05 +00:00
index : 'scenes_stashed' ,
2024-03-14 23:08:24 +00:00
id : stash . stashed _id ,
doc : {
// ...doc.replace.doc,
2024-03-21 01:54:05 +00:00
scene _id : doc . replace . id ,
2024-03-14 23:08:24 +00:00
user _id : stash . user _id ,
} ,
} ,
} ) ) ;
return stashDoc ;
} ) ;
console . log ( stashDocs ) ;
if ( stashDocs . length > 0 ) {
await indexApi . bulk ( stashDocs . map ( ( doc ) => JSON . stringify ( doc ) ) . join ( '\n' ) ) ;
}
} , Promise . resolve ( ) ) ;
}
async function init ( ) {
const scenes = await fetchScenes ( ) ;
const docs = scenes . map ( ( scene ) => {
const flatActors = scene . actors . flatMap ( ( actor ) => actor . f2 . match ( /[\w']+/g ) ) ; // match word characters to filter out brackets etc.
const flatTags = scene . tags . filter ( ( tag ) => tag . f3 > 6 ) . flatMap ( ( tag ) => ( tag . f4 ? ` ${ tag . f2 } ${ tag . f4 } ` : tag . f2 ) . match ( /[\w']+/g ) ) ; // only make top tags searchable to minimize cluttered results
const filteredTitle = scene . title && [ ... flatActors , ... flatTags ] . reduce ( ( accTitle , tag ) => accTitle . replace ( new RegExp ( tag . replace ( /[^\w\s]+/g , '' ) , 'i' ) , '' ) , scene . title ) . trim ( ) . replace ( /\s{2,}/ , ' ' ) ;
return {
replace : {
2024-03-21 01:54:05 +00:00
index : 'scenes' ,
2024-03-14 23:08:24 +00:00
id : scene . id ,
doc : {
title : scene . title || undefined ,
title _filtered : filteredTitle || undefined ,
date : scene . date ? Math . round ( scene . date . getTime ( ) / 1000 ) : undefined ,
created _at : Math . round ( scene . created _at . getTime ( ) / 1000 ) ,
effective _date : Math . round ( ( scene . date || scene . created _at ) . getTime ( ) / 1000 ) ,
// shoot_id: scene.shoot_id || undefined,
channel _id : scene . channel _id ,
channel _slug : scene . channel _slug ,
channel _name : scene . channel _name ,
network _id : scene . network _id || undefined ,
network _slug : scene . network _slug || undefined ,
network _name : scene . network _name || undefined ,
actor _ids : scene . actors . map ( ( actor ) => actor . f1 ) ,
actors : scene . actors . map ( ( actor ) => actor . f2 ) . join ( ) ,
tag _ids : scene . tags . map ( ( tag ) => tag . f1 ) ,
tags : flatTags . join ( ' ' ) ,
meta : scene . date ? format ( scene . date , 'y yy M MMM MMMM d' ) : undefined ,
liked : scene . stashed || 0 ,
} ,
} ,
} ;
} ) ;
const data = await indexApi . bulk ( docs . map ( ( doc ) => JSON . stringify ( doc ) ) . join ( '\n' ) ) ;
await updateStashed ( docs ) ;
console . log ( 'data' , data ) ;
knex . destroy ( ) ;
}
init ( ) ;