This repository has been archived by the owner on Mar 31, 2024. It is now read-only.
forked from elastic/kibana
-
Notifications
You must be signed in to change notification settings - Fork 3
/
get_kibana_usage_collector.js
65 lines (59 loc) · 1.78 KB
/
get_kibana_usage_collector.js
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/
import { get, snakeCase } from 'lodash';
import { KIBANA_USAGE_TYPE } from '../../../common/constants';
import { UsageCollector } from '../classes/usage_collector';
const TYPES = [
'dashboard',
'visualization',
'search',
'index-pattern',
'graph-workspace',
'timelion-sheet',
];
/**
* Fetches saved object client counts by querying the saved object index
*/
export function getKibanaUsageCollector(server, callCluster) {
return new UsageCollector(server, {
type: KIBANA_USAGE_TYPE,
async fetch() {
const index = server.config().get('kibana.index');
const savedObjectCountSearchParams = {
index,
ignoreUnavailable: true,
filterPath: 'aggregations.types.buckets',
body: {
size: 0,
query: {
terms: { type: TYPES },
},
aggs: {
types: {
terms: { field: 'type', size: TYPES.length }
}
}
}
};
const resp = await callCluster('search', savedObjectCountSearchParams);
const buckets = get(resp, 'aggregations.types.buckets', []);
// get the doc_count from each bucket
const bucketCounts = buckets.reduce((acc, bucket) => ({
...acc,
[bucket.key]: bucket.doc_count,
}), {});
return {
index,
...TYPES.reduce((acc, type) => ({ // combine the bucketCounts and 0s for types that don't have documents
...acc,
[snakeCase(type)]: {
total: bucketCounts[type] || 0
}
}), {})
};
}
});
}