2022-06-03 10:54:30 +02:00
|
|
|
import { AggregationsStringTermsAggregate } from '@elastic/elasticsearch/lib/api/types'
|
2021-06-24 15:18:54 +02:00
|
|
|
import { elasticSearch } from '../../helpers/elastic-search'
|
|
|
|
import { CONFIG } from '../../initializers/constants'
|
|
|
|
import { listIndexInstancesHost } from '../requests/instances-index'
|
2022-06-03 10:54:30 +02:00
|
|
|
import { extractBucketsFromAggregation } from './elastic-search-queries'
|
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) {
|
2022-06-03 10:54:30 +02:00
|
|
|
const res = await elasticSearch.search<unknown, Record<'hosts', AggregationsStringTermsAggregate>>({
|
2020-09-03 14:25:57 +02:00
|
|
|
index,
|
|
|
|
body: {
|
|
|
|
size: 0,
|
|
|
|
aggs: {
|
|
|
|
hosts: {
|
|
|
|
terms: {
|
|
|
|
size: 5000,
|
|
|
|
field: 'host'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2022-06-03 10:54:30 +02:00
|
|
|
for (const b of extractBucketsFromAggregation<string>(res.aggregations.hosts.buckets)) {
|
2020-09-03 14:25:57 +02:00
|
|
|
setResult.add(b.key)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return Array.from(setResult)
|
|
|
|
}
|
|
|
|
|
|
|
|
function getRemovedHosts (dbHosts: string[], indexHosts: string[]) {
|
|
|
|
return dbHosts.filter(dbHost => indexHosts.includes(dbHost) === false)
|
|
|
|
}
|