Skip to content

Commit

Permalink
[Metrics UI] Register function for Observability homepage (#70529) (#…
Browse files Browse the repository at this point in the history
…70647)

* [Metrics UI] Register function for Observability homepage

* Updating types; removing relative path from appLink

Co-authored-by: Elastic Machine <[email protected]>

Co-authored-by: Elastic Machine <[email protected]>
  • Loading branch information
simianhacker and elasticmachine authored Jul 2, 2020
1 parent df4e455 commit e1f017e
Show file tree
Hide file tree
Showing 6 changed files with 783 additions and 1 deletion.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

91 changes: 91 additions & 0 deletions x-pack/plugins/infra/public/metrics_overview_fetchers.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/*
* 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 { coreMock } from 'src/core/public/mocks';
import { createMetricsHasData, createMetricsFetchData } from './metrics_overview_fetchers';
import { CoreStart } from 'kibana/public';
import { InfraClientStartDeps, InfraClientStartExports } from './types';
import moment from 'moment';
import { FAKE_SNAPSHOT_RESPONSE } from './test_utils';

function setup() {
const core = coreMock.createStart();
const mockedGetStartServices = jest.fn(() => {
const deps = {};
return Promise.resolve([
core as CoreStart,
deps as InfraClientStartDeps,
void 0 as InfraClientStartExports,
]) as Promise<[CoreStart, InfraClientStartDeps, InfraClientStartExports]>;
});
return { core, mockedGetStartServices };
}

describe('Metrics UI Observability Homepage Functions', () => {
describe('createMetricsHasData()', () => {
it('should return true when true', async () => {
const { core, mockedGetStartServices } = setup();
core.http.get.mockResolvedValue({
status: {
indexFields: [],
logIndicesExist: false,
metricIndicesExist: true,
},
});
const hasData = createMetricsHasData(mockedGetStartServices);
const response = await hasData();
expect(core.http.get).toHaveBeenCalledTimes(1);
expect(response).toBeTruthy();
});
it('should return false when false', async () => {
const { core, mockedGetStartServices } = setup();
core.http.get.mockResolvedValue({
status: {
indexFields: [],
logIndicesExist: false,
metricIndicesExist: false,
},
});
const hasData = createMetricsHasData(mockedGetStartServices);
const response = await hasData();
expect(core.http.get).toHaveBeenCalledTimes(1);
expect(response).toBeFalsy();
});
});

describe('createMetricsFetchData()', () => {
it('should just work', async () => {
const { core, mockedGetStartServices } = setup();
core.http.post.mockResolvedValue(FAKE_SNAPSHOT_RESPONSE);
const fetchData = createMetricsFetchData(mockedGetStartServices);
const endTime = moment();
const startTime = endTime.clone().subtract(1, 'h');
const bucketSize = '300s';
const response = await fetchData({
startTime: startTime.toISOString(),
endTime: endTime.toISOString(),
bucketSize,
});
expect(core.http.post).toHaveBeenCalledTimes(1);
expect(core.http.post).toHaveBeenCalledWith('/api/metrics/snapshot', {
body: JSON.stringify({
sourceId: 'default',
metrics: [{ type: 'cpu' }, { type: 'memory' }, { type: 'rx' }, { type: 'tx' }],
groupBy: [],
nodeType: 'host',
timerange: {
from: startTime.valueOf(),
to: endTime.valueOf(),
interval: '300s',
forceInterval: true,
ignoreLookback: true,
},
}),
});
expect(response).toMatchSnapshot();
});
});
});
Loading

0 comments on commit e1f017e

Please sign in to comment.