Skip to content

Commit

Permalink
[Metrics UI] Fix hasData call to ensure it has data not just indices (#…
Browse files Browse the repository at this point in the history
…72969) (#73668)

* [Metrics UI] Fix hasData call to ensure it has data not just indices

* Fixing formatting

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

Co-authored-by: Elastic Machine <[email protected]>
  • Loading branch information
simianhacker and elasticmachine authored Jul 30, 2020
1 parent fcf0b63 commit 1eddefa
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 12 deletions.
16 changes: 16 additions & 0 deletions x-pack/plugins/infra/server/lib/create_search_client.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;
* you may not use this file except in compliance with the Elastic License.
*/
import { RequestHandlerContext } from 'src/core/server';
import { CallWithRequestParams, InfraDatabaseSearchResponse } from './adapters/framework';
import { KibanaFramework } from './adapters/framework/kibana_framework_adapter';

export const createSearchClient = (
requestContext: RequestHandlerContext,
framework: KibanaFramework
) => <Hit = {}, Aggregation = undefined>(
opts: CallWithRequestParams
): Promise<InfraDatabaseSearchResponse<Hit, Aggregation>> =>
framework.callWithRequest(requestContext, 'search', opts);
21 changes: 21 additions & 0 deletions x-pack/plugins/infra/server/lib/sources/has_data.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* 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 { ESSearchClient } from '../snapshot';

export const hasData = async (index: string, client: ESSearchClient) => {
const params = {
index,
allowNoIndices: true,
terminate_after: 1,
ignoreUnavailable: true,
body: {
size: 0,
},
};
const results = await client(params);
return results.hits.total.value !== 0;
};
10 changes: 3 additions & 7 deletions x-pack/plugins/infra/server/routes/snapshot/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { UsageCollector } from '../../usage/usage_collector';
import { parseFilterQuery } from '../../utils/serialized_query';
import { SnapshotRequestRT, SnapshotNodeResponseRT } from '../../../common/http_api/snapshot_api';
import { throwErrors } from '../../../common/runtime_types';
import { CallWithRequestParams, InfraDatabaseSearchResponse } from '../../lib/adapters/framework';
import { createSearchClient } from '../../lib/create_search_client';

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

Expand Down Expand Up @@ -63,12 +63,8 @@ export const initSnapshotRoute = (libs: InfraBackendLibs) => {
overrideCompositeSize,
};

const searchES = <Hit = {}, Aggregation = undefined>(
opts: CallWithRequestParams
): Promise<InfraDatabaseSearchResponse<Hit, Aggregation>> =>
framework.callWithRequest(requestContext, 'search', opts);

const nodesWithInterval = await libs.snapshot.getNodes(searchES, options);
const client = createSearchClient(requestContext, framework);
const nodesWithInterval = await libs.snapshot.getNodes(client, options);
return response.ok({
body: SnapshotNodeResponseRT.encode(nodesWithInterval),
});
Expand Down
16 changes: 11 additions & 5 deletions x-pack/plugins/infra/server/routes/source/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import { schema } from '@kbn/config-schema';
import { SourceResponseRuntimeType } from '../../../common/http_api/source_api';
import { InfraBackendLibs } from '../../lib/infra_types';
import { InfraIndexType } from '../../graphql/types';
import { hasData } from '../../lib/sources/has_data';
import { createSearchClient } from '../../lib/create_search_client';

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

const hasData =
type === 'metrics'
? await libs.sourceStatus.hasMetricIndices(requestContext, sourceId)
: (await libs.sourceStatus.getLogIndexStatus(requestContext, sourceId)) !== 'missing';
const client = createSearchClient(requestContext, framework);
const source = await libs.sources.getSourceConfiguration(
requestContext.core.savedObjects.client,
sourceId
);
const indexPattern =
type === 'metrics' ? source.configuration.metricAlias : source.configuration.logAlias;
const results = await hasData(indexPattern, client);

return response.ok({
body: { hasData },
body: { hasData: results },
});
} catch (error) {
return response.internalError({
Expand Down

0 comments on commit 1eddefa

Please sign in to comment.