sepia-search-motore-di-rice.../server/lib/meilisearch/meilisearch-instances.ts

49 lines
1.3 KiB
TypeScript
Raw Normal View History

2021-06-24 15:18:54 +02:00
import { CONFIG } from '../../initializers/constants'
import { listIndexInstancesHost } from '../requests/instances-index'
import { client } from '../../helpers/meilisearch'
2020-02-19 15:39:35 +01:00
async function buildInstanceHosts () {
2020-05-28 15:00:37 +02:00
let indexHosts = await listIndexInstancesHost()
2020-06-11 11:04:00 +02:00
if (CONFIG.INSTANCES_INDEX.WHITELIST.ENABLED) {
2020-09-03 14:25:57 +02:00
const whitelistHosts = Array.isArray(CONFIG.INSTANCES_INDEX.WHITELIST.HOSTS)
? CONFIG.INSTANCES_INDEX.WHITELIST.HOSTS
: []
2020-05-28 15:00:37 +02:00
indexHosts = indexHosts.filter(h => whitelistHosts.includes(h))
}
2020-02-19 15:39:35 +01:00
2020-09-03 14:25:57 +02:00
const dbHosts = await listDBInstances()
2020-02-19 15:39:35 +01:00
const removedHosts = getRemovedHosts(dbHosts, indexHosts)
return { indexHosts, removedHosts }
}
export {
buildInstanceHosts
}
2020-09-03 14:25:57 +02:00
// ##################################################
async function listDBInstances () {
const setResult = new Set<string>()
const indexes = [
CONFIG.MEILISEARCH.INDEXES.VIDEOS,
CONFIG.MEILISEARCH.INDEXES.CHANNELS
2020-09-03 14:25:57 +02:00
]
for (const index of indexes) {
const result = await client.index(index).searchForFacetValues({ facetName: 'host' })
for (const b of result.facetHits) {
setResult.add(b.value)
2020-09-03 14:25:57 +02:00
}
}
return Array.from(setResult)
}
function getRemovedHosts (dbHosts: string[], indexHosts: string[]) {
return dbHosts.filter(dbHost => indexHosts.includes(dbHost) === false)
}