Skip to content

Commit

Permalink
Fix types and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisronline committed Feb 5, 2021
1 parent a077d7c commit ca6c7cf
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 30 deletions.
1 change: 1 addition & 0 deletions x-pack/plugins/monitoring/common/types/es.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ export interface ElasticsearchLegacySource {
license?: {
status?: string;
type?: string;
expiry_date_in_millis?: number;
};
logstash_state?: {
pipeline?: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,7 @@ export class LicenseExpirationAlert extends BaseAlert {
if (availableCcs) {
esIndexPattern = getCcsIndexPattern(esIndexPattern, availableCcs);
}
const licenses = await fetchLicenses(
callCluster,
clusters,
esIndexPattern,
Globals.app.config.ui.max_bucket_size
);
const licenses = await fetchLicenses(callCluster, clusters, esIndexPattern);

return licenses.map((license) => {
const { clusterUuid, type, expiryDateMS, status, ccs } = license;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
/*
* 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.
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import { fetchClusterHealth } from './fetch_cluster_health';
Expand Down Expand Up @@ -29,7 +30,7 @@ describe('fetchClusterHealth', () => {
const clusters = [{ clusterUuid, clusterName: 'foo' }];
const index = '.monitoring-es-*';

const health = await fetchClusterHealth(callCluster, clusters, index, 1);
const health = await fetchClusterHealth(callCluster, clusters, index);
expect(health).toEqual([
{
health: status,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
/*
* 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.
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/
import { fetchLicenses } from './fetch_licenses';

describe('fetchLicenses', () => {
const clusterName = 'MyCluster';
const clusterUuid = 'clusterA';
const size = 1000;
const license = {
status: 'active',
expiry_date_in_millis: 1579532493876,
Expand All @@ -30,7 +30,7 @@ describe('fetchLicenses', () => {
}));
const clusters = [{ clusterUuid, clusterName }];
const index = '.monitoring-es-*';
const result = await fetchLicenses(callCluster, clusters, index, size);
const result = await fetchLicenses(callCluster, clusters, index);
expect(result).toEqual([
{
status: license.status,
Expand All @@ -45,7 +45,7 @@ describe('fetchLicenses', () => {
const callCluster = jest.fn();
const clusters = [{ clusterUuid, clusterName }];
const index = '.monitoring-es-*';
await fetchLicenses(callCluster, clusters, index, size);
await fetchLicenses(callCluster, clusters, index);
const params = callCluster.mock.calls[0][1];
expect(params.body.query.bool.filter[0].terms.cluster_uuid).toEqual([clusterUuid]);
});
Expand All @@ -54,7 +54,7 @@ describe('fetchLicenses', () => {
const callCluster = jest.fn();
const clusters = [{ clusterUuid, clusterName }];
const index = '.monitoring-es-*';
await fetchLicenses(callCluster, clusters, index, size);
await fetchLicenses(callCluster, clusters, index);
const params = callCluster.mock.calls[0][1];
expect(params.body.query.bool.filter[2].range.timestamp.gte).toBe('now-2m');
});
Expand Down
32 changes: 17 additions & 15 deletions x-pack/plugins/monitoring/server/lib/alerts/fetch_licenses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
* 2.0.
*/
import { AlertLicense, AlertCluster } from '../../../common/types/alerts';
import { ElasticsearchResponse } from '../../../common/types/es';

export async function fetchLicenses(
callCluster: any,
clusters: AlertCluster[],
index: string,
size: number
index: string
): Promise<AlertLicense[]> {
const params = {
index,
Expand All @@ -20,7 +20,7 @@ export async function fetchLicenses(
'hits.hits._index',
],
body: {
size,
size: clusters.length,
sort: [
{
timestamp: {
Expand Down Expand Up @@ -58,16 +58,18 @@ export async function fetchLicenses(
},
};

const response = await callCluster('search', params);
return response.hits.hits.map((hit: any) => {
const rawLicense: any = hit._source.license;
const license: AlertLicense = {
status: rawLicense.status,
type: rawLicense.type,
expiryDateMS: rawLicense.expiry_date_in_millis,
clusterUuid: hit._source.cluster_uuid,
ccs: hit._index,
};
return license;
});
const response: ElasticsearchResponse = await callCluster('search', params);
return (
response?.hits?.hits.map((hit) => {
const rawLicense = hit._source.license ?? {};
const license: AlertLicense = {
status: rawLicense.status ?? '',
type: rawLicense.type ?? '',
expiryDateMS: rawLicense.expiry_date_in_millis ?? 0,
clusterUuid: hit._source.cluster_uuid,
ccs: hit._index,
};
return license;
}) ?? []
);
}

0 comments on commit ca6c7cf

Please sign in to comment.