Skip to content

Commit

Permalink
fetchClusters creates index pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
neptunian committed Jan 18, 2022
1 parent 2b841e4 commit 6878713
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 17 deletions.
9 changes: 2 additions & 7 deletions x-pack/plugins/monitoring/server/alerts/base_rule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ import { fetchClusters } from '../lib/alerts/fetch_clusters';
import { AlertSeverity } from '../../common/enums';
import { parseDuration } from '../../../alerting/common';
import { Globals } from '../static_globals';
import { getNewIndexPatterns } from '../lib/cluster/get_index_patterns';

type ExecutedState =
| {
Expand Down Expand Up @@ -230,12 +229,8 @@ export class BaseRule {
}

protected async fetchClusters(esClient: ElasticsearchClient, params: CommonAlertParams) {
const indexPatterns = getNewIndexPatterns({
config: Globals.app.config,
moduleType: 'elasticsearch',
});
if (!params.limit) {
return await fetchClusters(esClient, indexPatterns);
return await fetchClusters(esClient);
}
const limit = parseDuration(params.limit);
const rangeFilter = this.ruleOptions.fetchClustersRange
Expand All @@ -246,7 +241,7 @@ export class BaseRule {
},
}
: undefined;
return await fetchClusters(esClient, indexPatterns, rangeFilter);
return await fetchClusters(esClient, rangeFilter);
}

protected async fetchData(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,7 @@ export function getMonitoringUsageCollector(
: getClient().asInternalUser;
const usageClusters: MonitoringClusterStackProductUsage[] = [];
const availableCcs = config.ui.ccs.enabled;
const elasticsearchIndex = getCcsIndexPattern(INDEX_PATTERN_ELASTICSEARCH, availableCcs);
const clusters = await fetchClusters(callCluster, elasticsearchIndex);
const clusters = await fetchClusters(callCluster);
for (const cluster of clusters) {
const license = await fetchLicenseType(callCluster, availableCcs, cluster.clusterUuid);
const stackProducts = await getStackProductsUsage(
Expand Down
21 changes: 15 additions & 6 deletions x-pack/plugins/monitoring/server/lib/alerts/fetch_clusters.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,18 @@ import { elasticsearchClientMock } from 'src/core/server/elasticsearch/client/mo
import { elasticsearchServiceMock } from 'src/core/server/mocks';
import { fetchClusters } from './fetch_clusters';

jest.mock('../../static_globals', () => ({
Globals: {
app: {
config: {
ui: {
ccs: { enabled: true },
},
},
},
},
}));

describe('fetchClusters', () => {
const clusterUuid = '1sdfds734';
const clusterName = 'monitoring';
Expand All @@ -31,8 +43,7 @@ describe('fetchClusters', () => {
},
} as estypes.SearchResponse)
);
const index = '.monitoring-es-*';
const result = await fetchClusters(esClient, index);
const result = await fetchClusters(esClient);
expect(result).toEqual([{ clusterUuid, clusterName }]);
});

Expand Down Expand Up @@ -60,15 +71,13 @@ describe('fetchClusters', () => {
},
} as estypes.SearchResponse)
);
const index = '.monitoring-es-*';
const result = await fetchClusters(esClient, index);
const result = await fetchClusters(esClient);
expect(result).toEqual([{ clusterUuid, clusterName: metadataName }]);
});

it('should limit the time period in the query', async () => {
const esClient = elasticsearchServiceMock.createScopedClusterClient().asCurrentUser;
const index = '.monitoring-es-*';
await fetchClusters(esClient, index);
await fetchClusters(esClient);
const params = esClient.search.mock.calls[0][0] as any;
expect(params?.body?.query.bool.filter[1].range.timestamp.gte).toBe('now-2m');
});
Expand Down
11 changes: 9 additions & 2 deletions x-pack/plugins/monitoring/server/lib/alerts/fetch_clusters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@
import { ElasticsearchClient } from 'kibana/server';
import { get } from 'lodash';
import { AlertCluster } from '../../../common/types/alerts';
import { getNewIndexPatterns } from '../cluster/get_index_patterns';
import { createDatasetFilter } from './create_dataset_query_filter';
import { Globals } from '../../static_globals';
import { getConfigCcs } from '../../../common/ccs_utils';

interface RangeFilter {
[field: string]: {
Expand All @@ -19,11 +22,15 @@ interface RangeFilter {

export async function fetchClusters(
esClient: ElasticsearchClient,
index: string,
rangeFilter: RangeFilter = { timestamp: { gte: 'now-2m' } }
): Promise<AlertCluster[]> {
const indexPatterns = getNewIndexPatterns({
config: Globals.app.config,
moduleType: 'elasticsearch',
ccs: getConfigCcs(Globals.app.config) ? '*' : undefined,
});
const params = {
index,
index: indexPatterns,
filter_path: [
'hits.hits._source.cluster_settings.cluster.metadata.display_name',
'hits.hits._source.cluster_uuid',
Expand Down

0 comments on commit 6878713

Please sign in to comment.