1
0
mirror of https://framagit.org/framasoft/peertube/search-index/ synced 2024-12-12 00:56:23 +01:00
sepia-search-motore-di-rice.../server/lib/elastic-search-instances.ts

62 lines
1.5 KiB
TypeScript
Raw Normal View History

2020-02-19 15:39:35 +01:00
import { elasticSearch } from '../helpers/elastic-search'
import { CONFIG } from '../initializers/constants'
2020-09-03 14:25:57 +02:00
import { listIndexInstancesHost } from './instances-index'
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.ELASTIC_SEARCH.INDEXES.VIDEOS,
CONFIG.ELASTIC_SEARCH.INDEXES.CHANNELS
]
for (const index of indexes) {
const res = await elasticSearch.search({
index,
body: {
size: 0,
aggs: {
hosts: {
terms: {
size: 5000,
field: 'host'
}
}
}
}
})
for (const b of res.body.aggregations.hosts.buckets) {
setResult.add(b.key)
}
}
return Array.from(setResult)
}
function getRemovedHosts (dbHosts: string[], indexHosts: string[]) {
return dbHosts.filter(dbHost => indexHosts.includes(dbHost) === false)
}