File tree Expand file tree Collapse file tree
x-pack/plugins/infra/server Expand file tree Collapse file tree Original file line number Diff line number Diff line change 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 ) ;
Original file line number Diff line number Diff line change 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+ } ;
Original file line number Diff line number Diff line change @@ -13,7 +13,7 @@ import { UsageCollector } from '../../usage/usage_collector';
1313import { parseFilterQuery } from '../../utils/serialized_query' ;
1414import { SnapshotRequestRT , SnapshotNodeResponseRT } from '../../../common/http_api/snapshot_api' ;
1515import { throwErrors } from '../../../common/runtime_types' ;
16- import { CallWithRequestParams , InfraDatabaseSearchResponse } from '../../lib/adapters/framework ' ;
16+ import { createSearchClient } from '../../lib/create_search_client ' ;
1717
1818const 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 } ) ;
Original file line number Diff line number Diff line change @@ -7,6 +7,8 @@ import { schema } from '@kbn/config-schema';
77import { SourceResponseRuntimeType } from '../../../common/http_api/source_api' ;
88import { InfraBackendLibs } from '../../lib/infra_types' ;
99import { InfraIndexType } from '../../graphql/types' ;
10+ import { hasData } from '../../lib/sources/has_data' ;
11+ import { createSearchClient } from '../../lib/create_search_client' ;
1012
1113const 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 ( {
You can’t perform that action at this time.
0 commit comments