import { CONFIG } from '../../initializers/constants' import { listIndexInstancesHost, getMajorInstanceVersion } from '../requests/instances-index' import { client } from '../../helpers/meilisearch' import { logger } from '../../helpers/logger' import Bluebird from 'bluebird' async function buildInstanceHosts () { let indexHosts = await listIndexInstancesHost() if (CONFIG.INSTANCES_INDEX.WHITELIST.ENABLED) { const whitelistHosts = Array.isArray(CONFIG.INSTANCES_INDEX.WHITELIST.HOSTS) ? CONFIG.INSTANCES_INDEX.WHITELIST.HOSTS : [] indexHosts = indexHosts.filter(h => whitelistHosts.includes(h)) } indexHosts = await Bluebird.filter(indexHosts, async indexHost => { const instanceVersion = await getMajorInstanceVersion(indexHost) if (instanceVersion < 4) { logger.info(`Do not index ${indexHost} because the major version is too low (v${instanceVersion} < v4)`) return false } return true }, { concurrency: 10 }) const dbHosts = await listDBInstances() const removedHosts = getRemovedHosts(dbHosts, indexHosts) return { indexHosts, removedHosts } } export { buildInstanceHosts } // ################################################## async function listDBInstances () { const setResult = new Set() const indexes = [ CONFIG.MEILISEARCH.INDEXES.VIDEOS, CONFIG.MEILISEARCH.INDEXES.CHANNELS ] for (const index of indexes) { const result = await client.index(index).searchForFacetValues({ facetName: 'host' }) for (const b of result.facetHits) { setResult.add(b.value) } } return Array.from(setResult) } function getRemovedHosts (dbHosts: string[], indexHosts: string[]) { return dbHosts.filter(dbHost => indexHosts.includes(dbHost) === false) }