Skip to content

Commit 8bed4b4

Browse files
committed
[Metrics UI] Fix hasData call to ensure it has data not just indices
1 parent 9374a42 commit 8bed4b4

4 files changed

Lines changed: 51 additions & 12 deletions

File tree

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License;
4+
* you may not use this file except in compliance with the Elastic License.
5+
*/
6+
import { RequestHandlerContext } from 'src/core/server';
7+
import { CallWithRequestParams, InfraDatabaseSearchResponse } from './adapters/framework';
8+
import { KibanaFramework } from './adapters/framework/kibana_framework_adapter';
9+
10+
export const createSearchClient = (
11+
requestContext: RequestHandlerContext,
12+
framework: KibanaFramework
13+
) => <Hit = {}, Aggregation = undefined>(
14+
opts: CallWithRequestParams
15+
): Promise<InfraDatabaseSearchResponse<Hit, Aggregation>> =>
16+
framework.callWithRequest(requestContext, 'search', opts);
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License;
4+
* you may not use this file except in compliance with the Elastic License.
5+
*/
6+
7+
import { ESSearchClient } from '../snapshot';
8+
9+
export const hasData = async (index: string, client: ESSearchClient) => {
10+
const params = {
11+
index,
12+
allowNoIndices: true,
13+
terminate_after: 1,
14+
ignoreUnavailable: true,
15+
body: {
16+
size: 0,
17+
},
18+
};
19+
const results = await client(params);
20+
return results.hits.total.value !== 0;
21+
};

x-pack/plugins/infra/server/routes/snapshot/index.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import { UsageCollector } from '../../usage/usage_collector';
1313
import { parseFilterQuery } from '../../utils/serialized_query';
1414
import { SnapshotRequestRT, SnapshotNodeResponseRT } from '../../../common/http_api/snapshot_api';
1515
import { throwErrors } from '../../../common/runtime_types';
16-
import { CallWithRequestParams, InfraDatabaseSearchResponse } from '../../lib/adapters/framework';
16+
import { createSearchClient } from '../../lib/create_search_client';
1717

1818
const escapeHatch = schema.object({}, { unknowns: 'allow' });
1919

@@ -61,12 +61,8 @@ export const initSnapshotRoute = (libs: InfraBackendLibs) => {
6161
includeTimeseries,
6262
};
6363

64-
const searchES = <Hit = {}, Aggregation = undefined>(
65-
opts: CallWithRequestParams
66-
): Promise<InfraDatabaseSearchResponse<Hit, Aggregation>> =>
67-
framework.callWithRequest(requestContext, 'search', opts);
68-
69-
const nodesWithInterval = await libs.snapshot.getNodes(searchES, options);
64+
const client = createSearchClient(requestContext, framework);
65+
const nodesWithInterval = await libs.snapshot.getNodes(client, options);
7066
return response.ok({
7167
body: SnapshotNodeResponseRT.encode(nodesWithInterval),
7268
});

x-pack/plugins/infra/server/routes/source/index.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import { schema } from '@kbn/config-schema';
77
import { SourceResponseRuntimeType } from '../../../common/http_api/source_api';
88
import { InfraBackendLibs } from '../../lib/infra_types';
99
import { InfraIndexType } from '../../graphql/types';
10+
import { hasData } from '../../lib/sources/has_data';
11+
import { createSearchClient } from '../../lib/create_search_client';
1012

1113
const typeToInfraIndexType = (value: string | undefined) => {
1214
switch (value) {
@@ -80,13 +82,17 @@ export const initSourceRoute = (libs: InfraBackendLibs) => {
8082
try {
8183
const { type, sourceId } = request.params;
8284

83-
const hasData =
84-
type === 'metrics'
85-
? await libs.sourceStatus.hasMetricIndices(requestContext, sourceId)
86-
: await libs.sourceStatus.hasLogIndices(requestContext, sourceId);
85+
const client = createSearchClient(requestContext, framework);
86+
const source = await libs.sources.getSourceConfiguration(
87+
requestContext.core.savedObjects.client,
88+
sourceId
89+
);
90+
const indexPattern =
91+
type === 'metrics' ? source.configuration.metricAlias : source.configuration.logAlias;
92+
const results = await hasData(indexPattern, client);
8793

8894
return response.ok({
89-
body: { hasData },
95+
body: { hasData: results },
9096
});
9197
} catch (error) {
9298
return response.internalError({

0 commit comments

Comments
 (0)