-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmcm-filesize.js
More file actions
47 lines (41 loc) · 1.49 KB
/
mcm-filesize.js
File metadata and controls
47 lines (41 loc) · 1.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
const algoliasearch = require('algoliasearch')
const CallEnum = require('@algolia/transporter').CallEnum
const bytesToGB = (bytesNumber) => Math.round(bytesNumber / 1000 / 1000 / 1000)
const getHosts = (clusterName, domain) => {
// https://www.algolia.com/doc/api-client/getting-started/upgrade-guides/javascript/#the-hosts-parameter
return [1, 2, 3].map(n => `${clusterName}-${n}.${domain}`).map(host => {
return {
protocol: 'https',
url: host,
accept: CallEnum.Read // CallEnum.Any or CallEnum.Write
}
})
}
const mcmFileSize = (appId, adminKey, domain = 'algolia.net') => {
return algoliasearch(appId, adminKey).listClusters().then(({ clusters }) => {
const clusterNames = clusters.map(cluster => cluster.clusterName)
const indexLists = clusters.map(cluster => {
const hosts = getHosts(cluster.clusterName, domain)
const client = algoliasearch(appId, adminKey, { hosts })
return client.listIndices()
})
return Promise.all(indexLists)
.then(indices => {
const report = {}
let total = 0
clusterNames.forEach((clusterName, i) => {
const clusterFileSize = indices[i].items.reduce(
(fileSize, index) => fileSize + bytesToGB(index.fileSize),
0
)
total += clusterFileSize
report[clusterName] = clusterFileSize
})
return JSON.stringify({ ...report, total })
})
.catch(err => {
throw err
})
})
}
module.exports = mcmFileSize