Skip to content

Commit

Permalink
[Infra] Add getMetricIndices to start contract (#131326)
Browse files Browse the repository at this point in the history
* [Infra] Add getMetricIndices to start contract (#131246)

* Remove temporary return variable, change variable name

* Address review comments

* Use Jest Promise mock shorthand

Co-authored-by: Kibana Machine <[email protected]>
  • Loading branch information
miltonhultgren and kibanamachine authored May 3, 2022
1 parent d2e96be commit 71f2099
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* 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 { savedObjectsClientMock } from '@kbn/core/server/mocks';
import { defaultSourceConfiguration, InfraSource } from '../sources';
import { createInfraSourcesMock } from '../sources/mocks';
import { makeGetMetricIndices } from './make_get_metric_indices';

describe('getMetricIndices', () => {
it('should return the indices from a resolved configuration', async () => {
const sourceConfiguration: InfraSource = {
id: 'default',
origin: 'stored',
configuration: defaultSourceConfiguration,
};
const infraSourcesMock = createInfraSourcesMock();
infraSourcesMock.getSourceConfiguration.mockResolvedValueOnce(sourceConfiguration);

const getMetricIndices = makeGetMetricIndices(infraSourcesMock);

const savedObjectsClient = savedObjectsClientMock.create();
const metricIndices = await getMetricIndices(savedObjectsClient);

expect(metricIndices).toEqual(defaultSourceConfiguration.metricAlias);
});
});
16 changes: 16 additions & 0 deletions x-pack/plugins/infra/server/lib/metrics/make_get_metric_indices.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* 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 type { SavedObjectsClientContract } from '@kbn/core/server';
import type { IInfraSources } from '../sources';

export function makeGetMetricIndices(metricSources: IInfraSources) {
return async (savedObjectsClient: SavedObjectsClientContract, sourceId: string = 'default') => {
const source = await metricSources.getSourceConfiguration(savedObjectsClient, sourceId);
return source.configuration.metricAlias;
};
}
6 changes: 4 additions & 2 deletions x-pack/plugins/infra/server/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@

import { Server } from '@hapi/hapi';
import { schema } from '@kbn/config-schema';
import { i18n } from '@kbn/i18n';
import { Logger } from '@kbn/logging';
import {
CoreStart,
Plugin,
PluginConfigDescriptor,
PluginInitializerContext,
} from '@kbn/core/server';
import { handleEsError } from '@kbn/es-ui-shared-plugin/server';
import { i18n } from '@kbn/i18n';
import { Logger } from '@kbn/logging';
import { LOGS_FEATURE_ID, METRICS_FEATURE_ID } from '../common/constants';
import { defaultLogViewsStaticConfig } from '../common/log_views';
import { publicConfigKeys } from '../common/plugin_config_types';
Expand All @@ -35,6 +35,7 @@ import { InfraFieldsDomain } from './lib/domains/fields_domain';
import { InfraLogEntriesDomain } from './lib/domains/log_entries_domain';
import { InfraMetricsDomain } from './lib/domains/metrics_domain';
import { InfraBackendLibs, InfraDomainLibs } from './lib/infra_types';
import { makeGetMetricIndices } from './lib/metrics/make_get_metric_indices';
import { infraSourceConfigurationSavedObjectType, InfraSources } from './lib/sources';
import { InfraSourceStatus } from './lib/source_status';
import { logViewSavedObjectType } from './saved_objects';
Expand Down Expand Up @@ -236,6 +237,7 @@ export class InfraServerPlugin

return {
logViews,
getMetricIndices: makeGetMetricIndices(this.libs.sources),
};
}

Expand Down
10 changes: 9 additions & 1 deletion x-pack/plugins/infra/server/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@
* 2.0.
*/

import type { CoreSetup, CustomRequestHandlerContext } from '@kbn/core/server';
import type {
CoreSetup,
CustomRequestHandlerContext,
SavedObjectsClientContract,
} from '@kbn/core/server';
import type { SearchRequestHandlerContext } from '@kbn/data-plugin/server';
import type { MlPluginSetup } from '@kbn/ml-plugin/server';
import type { InfraStaticSourceConfiguration } from '../common/source_configuration/source_configuration';
Expand All @@ -27,6 +31,10 @@ export interface InfraPluginSetup {

export interface InfraPluginStart {
logViews: LogViewsServiceStart;
getMetricIndices: (
savedObjectsClient: SavedObjectsClientContract,
sourceId?: string
) => Promise<string>;
}

export type MlSystem = ReturnType<MlPluginSetup['mlSystemProvider']>;
Expand Down

0 comments on commit 71f2099

Please sign in to comment.