diff --git a/x-pack/plugins/infra/public/components/infrastructure_node_metrics_tables/container/use_container_metrics_table.test.ts b/x-pack/plugins/infra/public/components/infrastructure_node_metrics_tables/container/use_container_metrics_table.test.ts new file mode 100644 index 0000000000000..97044636cc7a1 --- /dev/null +++ b/x-pack/plugins/infra/public/components/infrastructure_node_metrics_tables/container/use_container_metrics_table.test.ts @@ -0,0 +1,50 @@ +/* + * 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 { useContainerMetricsTable } from './use_container_metrics_table'; +import { useInfrastructureNodeMetrics } from '../shared'; +import { renderHook } from '@testing-library/react-hooks'; + +jest.mock('../shared', () => ({ + ...jest.requireActual('../shared'), + useInfrastructureNodeMetrics: jest.fn(), +})); + +describe('useContainerMetricsTable hook', () => { + const useInfrastructureNodeMetricsMock = useInfrastructureNodeMetrics as jest.MockedFunction< + typeof useInfrastructureNodeMetrics + >; + + it('should call useInfrastructureNodeMetrics hook with event.module filter in filterClauseDsl query', () => { + const filterClauseDsl = { + bool: { + filter: [{ terms: { 'container.id': 'gke-edge-oblt-pool-1-9a60016d-lgg9' } }], + }, + }; + + const filterClauseWithEventModuleFilter = { + bool: { + filter: [{ term: { 'event.dataset': 'kubernetes.container' } }, { ...filterClauseDsl }], + }, + }; + + renderHook(() => + useContainerMetricsTable({ + timerange: { from: 'now-30d', to: 'now' }, + filterClauseDsl, + }) + ); + + expect(useInfrastructureNodeMetricsMock).toHaveBeenCalledWith( + expect.objectContaining({ + metricsExplorerOptions: expect.objectContaining({ + filterQuery: JSON.stringify(filterClauseWithEventModuleFilter), + }), + }) + ); + }); +}); diff --git a/x-pack/plugins/infra/public/components/infrastructure_node_metrics_tables/container/use_container_metrics_table.ts b/x-pack/plugins/infra/public/components/infrastructure_node_metrics_tables/container/use_container_metrics_table.ts index fe570a80b6615..40cb1e0ee2763 100644 --- a/x-pack/plugins/infra/public/components/infrastructure_node_metrics_tables/container/use_container_metrics_table.ts +++ b/x-pack/plugins/infra/public/components/infrastructure_node_metrics_tables/container/use_container_metrics_table.ts @@ -5,39 +5,47 @@ * 2.0. */ -import { useState } from 'react'; +import { useMemo, useState } from 'react'; import type { MetricsExplorerRow, MetricsExplorerSeries, } from '../../../../common/http_api/metrics_explorer'; -import type { MetricsMap, SortState, UseNodeMetricsTableOptions } from '../shared'; -import { metricsToApiOptions, useInfrastructureNodeMetrics } from '../shared'; +import type { MetricsQueryOptions, SortState, UseNodeMetricsTableOptions } from '../shared'; +import { + metricsToApiOptions, + useInfrastructureNodeMetrics, + createMetricByFieldLookup, +} from '../shared'; type ContainerMetricsField = | 'kubernetes.container.start_time' | 'kubernetes.container.cpu.usage.node.pct' | 'kubernetes.container.memory.usage.bytes'; -const containerMetricsMap: MetricsMap = { - 'kubernetes.container.start_time': { - aggregation: 'max', - field: 'kubernetes.container.start_time', +const containerMetricsQueryConfig: MetricsQueryOptions = { + sourceFilter: { + term: { + 'event.dataset': 'kubernetes.container', + }, }, - 'kubernetes.container.cpu.usage.node.pct': { - aggregation: 'avg', - field: 'kubernetes.container.cpu.usage.node.pct', - }, - 'kubernetes.container.memory.usage.bytes': { - aggregation: 'avg', - field: 'kubernetes.container.memory.usage.bytes', + groupByField: 'container.id', + metricsMap: { + 'kubernetes.container.start_time': { + aggregation: 'max', + field: 'kubernetes.container.start_time', + }, + 'kubernetes.container.cpu.usage.node.pct': { + aggregation: 'avg', + field: 'kubernetes.container.cpu.usage.node.pct', + }, + 'kubernetes.container.memory.usage.bytes': { + aggregation: 'avg', + field: 'kubernetes.container.memory.usage.bytes', + }, }, }; -const { options: containerMetricsOptions, metricByField } = metricsToApiOptions( - containerMetricsMap, - 'container.id' -); -export { metricByField }; +export const metricByField = createMetricByFieldLookup(containerMetricsQueryConfig.metricsMap); export interface ContainerNodeMetricsRow { name: string; @@ -56,6 +64,11 @@ export function useContainerMetricsTable({ direction: 'desc', }); + const { options: containerMetricsOptions } = useMemo( + () => metricsToApiOptions(containerMetricsQueryConfig, filterClauseDsl), + [filterClauseDsl] + ); + const { isLoading, nodes: containers, @@ -63,7 +76,6 @@ export function useContainerMetricsTable({ } = useInfrastructureNodeMetrics({ metricsExplorerOptions: containerMetricsOptions, timerange, - filterClauseDsl, transform: seriesToContainerNodeMetricsRow, sortState, currentPageIndex, diff --git a/x-pack/plugins/infra/public/components/infrastructure_node_metrics_tables/host/use_host_metrics_table.test.ts b/x-pack/plugins/infra/public/components/infrastructure_node_metrics_tables/host/use_host_metrics_table.test.ts new file mode 100644 index 0000000000000..2956ed2d8bc87 --- /dev/null +++ b/x-pack/plugins/infra/public/components/infrastructure_node_metrics_tables/host/use_host_metrics_table.test.ts @@ -0,0 +1,57 @@ +/* + * 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 { useHostMetricsTable } from './use_host_metrics_table'; +import { useInfrastructureNodeMetrics } from '../shared'; +import { renderHook } from '@testing-library/react-hooks'; + +jest.mock('../shared', () => ({ + ...jest.requireActual('../shared'), + useInfrastructureNodeMetrics: jest.fn(), +})); + +describe('useHostMetricsTable hook', () => { + const useInfrastructureNodeMetricsMock = useInfrastructureNodeMetrics as jest.MockedFunction< + typeof useInfrastructureNodeMetrics + >; + + it('should call useInfrastructureNodeMetrics hook with event.module filter in filterClauseDsl query', () => { + const filterClauseDsl = { + bool: { + should: [ + { + terms: { + 'host.name': 'gke-edge-oblt-pool-1-9a60016d-lgg9', + }, + }, + ], + minimum_should_match: 1, + }, + }; + + const filterClauseWithEventModuleFilter = { + bool: { + filter: [{ term: { 'event.module': 'system' } }, { ...filterClauseDsl }], + }, + }; + + renderHook(() => + useHostMetricsTable({ + timerange: { from: 'now-30d', to: 'now' }, + filterClauseDsl, + }) + ); + + expect(useInfrastructureNodeMetricsMock).toHaveBeenCalledWith( + expect.objectContaining({ + metricsExplorerOptions: expect.objectContaining({ + filterQuery: JSON.stringify(filterClauseWithEventModuleFilter), + }), + }) + ); + }); +}); diff --git a/x-pack/plugins/infra/public/components/infrastructure_node_metrics_tables/host/use_host_metrics_table.ts b/x-pack/plugins/infra/public/components/infrastructure_node_metrics_tables/host/use_host_metrics_table.ts index f82463e97a303..04212ce9c215b 100644 --- a/x-pack/plugins/infra/public/components/infrastructure_node_metrics_tables/host/use_host_metrics_table.ts +++ b/x-pack/plugins/infra/public/components/infrastructure_node_metrics_tables/host/use_host_metrics_table.ts @@ -5,13 +5,17 @@ * 2.0. */ -import { useState } from 'react'; +import { useMemo, useState } from 'react'; import type { MetricsExplorerRow, MetricsExplorerSeries, } from '../../../../common/http_api/metrics_explorer'; -import type { MetricsMap, SortState, UseNodeMetricsTableOptions } from '../shared'; -import { metricsToApiOptions, useInfrastructureNodeMetrics } from '../shared'; +import type { MetricsQueryOptions, SortState, UseNodeMetricsTableOptions } from '../shared'; +import { + metricsToApiOptions, + useInfrastructureNodeMetrics, + createMetricByFieldLookup, +} from '../shared'; type HostMetricsField = | 'system.cpu.cores' @@ -19,24 +23,28 @@ type HostMetricsField = | 'system.memory.total' | 'system.memory.used.pct'; -const hostMetricsMap: MetricsMap = { - 'system.cpu.cores': { aggregation: 'max', field: 'system.cpu.cores' }, - 'system.cpu.total.norm.pct': { - aggregation: 'avg', - field: 'system.cpu.total.norm.pct', +const hostsMetricsQueryConfig: MetricsQueryOptions = { + sourceFilter: { + term: { + 'event.module': 'system', + }, }, - 'system.memory.total': { aggregation: 'max', field: 'system.memory.total' }, - 'system.memory.used.pct': { - aggregation: 'avg', - field: 'system.memory.used.pct', + groupByField: 'host.name', + metricsMap: { + 'system.cpu.cores': { aggregation: 'max', field: 'system.cpu.cores' }, + 'system.cpu.total.norm.pct': { + aggregation: 'avg', + field: 'system.cpu.total.norm.pct', + }, + 'system.memory.total': { aggregation: 'max', field: 'system.memory.total' }, + 'system.memory.used.pct': { + aggregation: 'avg', + field: 'system.memory.used.pct', + }, }, }; -const { options: hostMetricsOptions, metricByField } = metricsToApiOptions( - hostMetricsMap, - 'host.name' -); -export { metricByField }; +export const metricByField = createMetricByFieldLookup(hostsMetricsQueryConfig.metricsMap); export interface HostNodeMetricsRow { name: string; @@ -53,6 +61,11 @@ export function useHostMetricsTable({ timerange, filterClauseDsl }: UseNodeMetri direction: 'desc', }); + const { options: hostMetricsOptions } = useMemo( + () => metricsToApiOptions(hostsMetricsQueryConfig, filterClauseDsl), + [filterClauseDsl] + ); + const { isLoading, nodes: hosts, @@ -60,7 +73,6 @@ export function useHostMetricsTable({ timerange, filterClauseDsl }: UseNodeMetri } = useInfrastructureNodeMetrics({ metricsExplorerOptions: hostMetricsOptions, timerange, - filterClauseDsl, transform: seriesToHostNodeMetricsRow, sortState, currentPageIndex, diff --git a/x-pack/plugins/infra/public/components/infrastructure_node_metrics_tables/pod/use_pod_metrics_table.test.ts b/x-pack/plugins/infra/public/components/infrastructure_node_metrics_tables/pod/use_pod_metrics_table.test.ts new file mode 100644 index 0000000000000..d7edd86771ed7 --- /dev/null +++ b/x-pack/plugins/infra/public/components/infrastructure_node_metrics_tables/pod/use_pod_metrics_table.test.ts @@ -0,0 +1,50 @@ +/* + * 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 { usePodMetricsTable } from './use_pod_metrics_table'; +import { useInfrastructureNodeMetrics } from '../shared'; +import { renderHook } from '@testing-library/react-hooks'; + +jest.mock('../shared', () => ({ + ...jest.requireActual('../shared'), + useInfrastructureNodeMetrics: jest.fn(), +})); + +describe('usePodMetricsTable hook', () => { + const useInfrastructureNodeMetricsMock = useInfrastructureNodeMetrics as jest.MockedFunction< + typeof useInfrastructureNodeMetrics + >; + + it('should call useInfrastructureNodeMetrics hook with event.module filter in filterClauseDsl query', () => { + const filterClauseDsl = { + bool: { + filter: [{ terms: { 'container.id': 'gke-edge-oblt-pool-1-9a60016d-lgg9' } }], + }, + }; + + const filterClauseWithEventModuleFilter = { + bool: { + filter: [{ term: { 'event.dataset': 'kubernetes.pod' } }, { ...filterClauseDsl }], + }, + }; + + renderHook(() => + usePodMetricsTable({ + timerange: { from: 'now-30d', to: 'now' }, + filterClauseDsl, + }) + ); + + expect(useInfrastructureNodeMetricsMock).toHaveBeenCalledWith( + expect.objectContaining({ + metricsExplorerOptions: expect.objectContaining({ + filterQuery: JSON.stringify(filterClauseWithEventModuleFilter), + }), + }) + ); + }); +}); diff --git a/x-pack/plugins/infra/public/components/infrastructure_node_metrics_tables/pod/use_pod_metrics_table.ts b/x-pack/plugins/infra/public/components/infrastructure_node_metrics_tables/pod/use_pod_metrics_table.ts index 5e8221d679b9a..116293a09a61c 100644 --- a/x-pack/plugins/infra/public/components/infrastructure_node_metrics_tables/pod/use_pod_metrics_table.ts +++ b/x-pack/plugins/infra/public/components/infrastructure_node_metrics_tables/pod/use_pod_metrics_table.ts @@ -5,39 +5,47 @@ * 2.0. */ -import { useState } from 'react'; +import { useMemo, useState } from 'react'; import type { MetricsExplorerRow, MetricsExplorerSeries, } from '../../../../common/http_api/metrics_explorer'; -import type { MetricsMap, SortState, UseNodeMetricsTableOptions } from '../shared'; -import { metricsToApiOptions, useInfrastructureNodeMetrics } from '../shared'; +import type { MetricsQueryOptions, SortState, UseNodeMetricsTableOptions } from '../shared'; +import { + metricsToApiOptions, + useInfrastructureNodeMetrics, + createMetricByFieldLookup, +} from '../shared'; type PodMetricsField = | 'kubernetes.pod.start_time' | 'kubernetes.pod.cpu.usage.node.pct' | 'kubernetes.pod.memory.usage.bytes'; -const podMetricsMap: MetricsMap = { - 'kubernetes.pod.start_time': { - aggregation: 'max', - field: 'kubernetes.pod.start_time', +const podMetricsQueryConfig: MetricsQueryOptions = { + sourceFilter: { + term: { + 'event.dataset': 'kubernetes.pod', + }, }, - 'kubernetes.pod.cpu.usage.node.pct': { - aggregation: 'avg', - field: 'kubernetes.pod.cpu.usage.node.pct', - }, - 'kubernetes.pod.memory.usage.bytes': { - aggregation: 'avg', - field: 'kubernetes.pod.memory.usage.bytes', + groupByField: ['kubernetes.pod.uid', 'kubernetes.pod.name'], + metricsMap: { + 'kubernetes.pod.start_time': { + aggregation: 'max', + field: 'kubernetes.pod.start_time', + }, + 'kubernetes.pod.cpu.usage.node.pct': { + aggregation: 'avg', + field: 'kubernetes.pod.cpu.usage.node.pct', + }, + 'kubernetes.pod.memory.usage.bytes': { + aggregation: 'avg', + field: 'kubernetes.pod.memory.usage.bytes', + }, }, }; -const { options: podMetricsOptions, metricByField } = metricsToApiOptions(podMetricsMap, [ - 'kubernetes.pod.uid', - 'kubernetes.pod.name', -]); -export { metricByField }; +export const metricByField = createMetricByFieldLookup(podMetricsQueryConfig.metricsMap); export interface PodNodeMetricsRow { id: string; @@ -54,6 +62,11 @@ export function usePodMetricsTable({ timerange, filterClauseDsl }: UseNodeMetric direction: 'desc', }); + const { options: podMetricsOptions } = useMemo( + () => metricsToApiOptions(podMetricsQueryConfig, filterClauseDsl), + [filterClauseDsl] + ); + const { isLoading, nodes: pods, @@ -61,7 +74,6 @@ export function usePodMetricsTable({ timerange, filterClauseDsl }: UseNodeMetric } = useInfrastructureNodeMetrics({ metricsExplorerOptions: podMetricsOptions, timerange, - filterClauseDsl, transform: seriesToPodNodeMetricsRow, sortState, currentPageIndex, diff --git a/x-pack/plugins/infra/public/components/infrastructure_node_metrics_tables/shared/hooks/index.ts b/x-pack/plugins/infra/public/components/infrastructure_node_metrics_tables/shared/hooks/index.ts index b3e04f1122998..06599df168690 100644 --- a/x-pack/plugins/infra/public/components/infrastructure_node_metrics_tables/shared/hooks/index.ts +++ b/x-pack/plugins/infra/public/components/infrastructure_node_metrics_tables/shared/hooks/index.ts @@ -5,7 +5,7 @@ * 2.0. */ -export { metricsToApiOptions } from './metrics_to_api_options'; -export type { MetricsMap } from './metrics_to_api_options'; +export { metricsToApiOptions, createMetricByFieldLookup } from './metrics_to_api_options'; +export type { MetricsMap, MetricsQueryOptions } from './metrics_to_api_options'; export { useInfrastructureNodeMetrics } from './use_infrastructure_node_metrics'; export type { SortState } from './use_infrastructure_node_metrics'; diff --git a/x-pack/plugins/infra/public/components/infrastructure_node_metrics_tables/shared/hooks/metrics_to_api_options.test.ts b/x-pack/plugins/infra/public/components/infrastructure_node_metrics_tables/shared/hooks/metrics_to_api_options.test.ts index da4ccc45ebf7d..79402b8813e08 100644 --- a/x-pack/plugins/infra/public/components/infrastructure_node_metrics_tables/shared/hooks/metrics_to_api_options.test.ts +++ b/x-pack/plugins/infra/public/components/infrastructure_node_metrics_tables/shared/hooks/metrics_to_api_options.test.ts @@ -5,44 +5,62 @@ * 2.0. */ -import type { MetricsMap } from './metrics_to_api_options'; -import { metricsToApiOptions } from './metrics_to_api_options'; +import type { MetricsExplorerOptions } from '../../../../pages/metrics/metrics_explorer/hooks/use_metrics_explorer_options'; +import { + createMetricByFieldLookup, + MetricsQueryOptions, + metricsToApiOptions, +} from './metrics_to_api_options'; describe('metricsToApiOptions', () => { type TestNodeTypeMetricsField = 'test.node.type.field1' | 'test.node.type.field2'; - const testMetricsMapField1First: MetricsMap = { - 'test.node.type.field1': { - aggregation: 'max', - field: 'test.node.type.field1', + const testMetricsMapField1First: MetricsQueryOptions = { + sourceFilter: { + term: { + 'event.module': 'test', + }, }, - 'test.node.type.field2': { - aggregation: 'avg', - field: 'test.node.type.field2', + groupByField: 'test.node.type.groupingField', + metricsMap: { + 'test.node.type.field1': { + aggregation: 'max', + field: 'test.node.type.field1', + }, + 'test.node.type.field2': { + aggregation: 'avg', + field: 'test.node.type.field2', + }, }, }; - const testMetricsMapField1Second: MetricsMap = { - 'test.node.type.field2': { - aggregation: 'avg', - field: 'test.node.type.field2', + const testMetricsMapField1Second: MetricsQueryOptions = { + sourceFilter: { + term: { + 'event.module': 'test', + }, }, - 'test.node.type.field1': { - aggregation: 'max', - field: 'test.node.type.field1', + groupByField: 'test.node.type.groupingField', + metricsMap: { + 'test.node.type.field2': { + aggregation: 'avg', + field: 'test.node.type.field2', + }, + 'test.node.type.field1': { + aggregation: 'max', + field: 'test.node.type.field1', + }, }, }; const fields = ['test.node.type.field1', 'test.node.type.field2']; it('should join the grouping field with the metrics in the APIs expected format', () => { - const { options } = metricsToApiOptions( - testMetricsMapField1First, - 'test.node.type.groupingField' - ); + const { options } = metricsToApiOptions(testMetricsMapField1First); expect(options).toEqual({ aggregation: 'avg', groupBy: 'test.node.type.groupingField', + filterQuery: JSON.stringify({ bool: { filter: [{ term: { 'event.module': 'test' } }] } }), metrics: [ { field: 'test.node.type.field1', @@ -53,28 +71,21 @@ describe('metricsToApiOptions', () => { aggregation: 'avg', }, ], - }); + } as MetricsExplorerOptions); }); it('should provide a mapping object that allows consumer to ignore metric definition order', () => { - const field1First = metricsToApiOptions( - testMetricsMapField1First, - 'test.node.type.groupingField' - ); + const metricByFieldFirst = createMetricByFieldLookup(testMetricsMapField1First.metricsMap); - assertListContentIsEqual(Object.keys(field1First.metricByField), fields); - expect(field1First.metricByField).toEqual({ + assertListContentIsEqual(Object.keys(metricByFieldFirst), fields); + expect(metricByFieldFirst).toEqual({ 'test.node.type.field1': 'metric_0', 'test.node.type.field2': 'metric_1', }); - const field1Second = metricsToApiOptions( - testMetricsMapField1Second, - 'test.node.type.groupingField' - ); + const metricByFieldSecond = createMetricByFieldLookup(testMetricsMapField1Second.metricsMap); - assertListContentIsEqual(Object.keys(field1Second.metricByField), fields); - expect(field1Second.metricByField).toEqual({ + expect(metricByFieldSecond).toEqual({ 'test.node.type.field1': 'metric_1', 'test.node.type.field2': 'metric_0', }); diff --git a/x-pack/plugins/infra/public/components/infrastructure_node_metrics_tables/shared/hooks/metrics_to_api_options.ts b/x-pack/plugins/infra/public/components/infrastructure_node_metrics_tables/shared/hooks/metrics_to_api_options.ts index 67fcb9dcde81b..33d89f8d3c31f 100644 --- a/x-pack/plugins/infra/public/components/infrastructure_node_metrics_tables/shared/hooks/metrics_to_api_options.ts +++ b/x-pack/plugins/infra/public/components/infrastructure_node_metrics_tables/shared/hooks/metrics_to_api_options.ts @@ -5,6 +5,8 @@ * 2.0. */ +import type { QueryDslQueryContainer } from '@elastic/elasticsearch/lib/api/typesWithBodyKey'; +import { ObjectValues } from '../../../../../common/utility_types'; import type { MetricsExplorerOptions, MetricsExplorerOptionsMetric, @@ -40,6 +42,15 @@ If the endpoint where to change its return format to: Then this code would no longer be needed. */ +// MetricsQueryOptions wraps the basic things needed to build the query for metrics +// sourceFilter is used to define filter to get only relevant docs that contain metrics info +// e.g: { 'source.module': 'system' } +export interface MetricsQueryOptions { + sourceFilter: QueryDslQueryContainer; + groupByField: string | string[]; + metricsMap: MetricsMap; +} + // The input to this generic type is a (union) string type that defines all the fields we want to // request metrics for. This input type serves as something like a "source of truth" for which // fields are being used. The resulting MetricsMap and metricByField helper ensures a type safe @@ -58,29 +69,42 @@ export interface NodeMetricsExplorerOptionsMetric } export function metricsToApiOptions( - metricsMap: MetricsMap, - groupBy: string | string[] + metricsQueryOptions: MetricsQueryOptions, + filterClauseDsl?: QueryDslQueryContainer ) { - const metrics = Object.values(metricsMap) as Array>; + const metrics = Object.values(metricsQueryOptions.metricsMap) as Array< + NodeMetricsExplorerOptionsMetric + >; const options: MetricsExplorerOptions = { aggregation: 'avg', - groupBy, + groupBy: metricsQueryOptions.groupByField, metrics, + filterQuery: JSON.stringify( + buildFilterClause(metricsQueryOptions.sourceFilter, filterClauseDsl) + ), }; - const metricByField = createFieldLookup(Object.keys(metricsMap) as T[], metrics); - return { options, - metricByField, }; } -function createFieldLookup( - fields: T[], - metrics: Array> -) { +function buildFilterClause( + sourceFilter: QueryDslQueryContainer, + filterClauseDsl?: QueryDslQueryContainer +): QueryDslQueryContainer { + return { + bool: { + filter: !!filterClauseDsl ? [sourceFilter, filterClauseDsl] : [sourceFilter], + }, + }; +} + +export function createMetricByFieldLookup(metricMap: MetricsMap) { + const fields = Object.keys(metricMap) as Array; + const metrics = Object.values(metricMap) as ObjectValues; + const setMetricIndexToField = (acc: Record, field: T) => { return { ...acc, diff --git a/x-pack/plugins/infra/public/components/infrastructure_node_metrics_tables/shared/hooks/use_infrastructure_node_metrics.ts b/x-pack/plugins/infra/public/components/infrastructure_node_metrics_tables/shared/hooks/use_infrastructure_node_metrics.ts index 374685a374f24..93ddcc8bf4aa8 100644 --- a/x-pack/plugins/infra/public/components/infrastructure_node_metrics_tables/shared/hooks/use_infrastructure_node_metrics.ts +++ b/x-pack/plugins/infra/public/components/infrastructure_node_metrics_tables/shared/hooks/use_infrastructure_node_metrics.ts @@ -6,10 +6,10 @@ */ import { parse } from '@kbn/datemath'; -import type { QueryDslQueryContainer } from '@elastic/elasticsearch/lib/api/typesWithBodyKey'; import { useEffect, useMemo, useState } from 'react'; import { useKibana } from '@kbn/kibana-react-plugin/public'; import type { + MetricsExplorerRequestBody, MetricsExplorerResponse, MetricsExplorerSeries, } from '../../../../../common/http_api/metrics_explorer'; @@ -28,7 +28,6 @@ export interface SortState { interface UseInfrastructureNodeMetricsOptions { metricsExplorerOptions: MetricsExplorerOptions; timerange: Pick; - filterClauseDsl?: QueryDslQueryContainer; transform: (series: MetricsExplorerSeries) => T; sortState: SortState; currentPageIndex: number; @@ -48,14 +47,7 @@ const nullData: MetricsExplorerResponse = { export const useInfrastructureNodeMetrics = ( options: UseInfrastructureNodeMetricsOptions ) => { - const { - metricsExplorerOptions, - timerange, - filterClauseDsl, - transform, - sortState, - currentPageIndex, - } = options; + const { metricsExplorerOptions, timerange, transform, sortState, currentPageIndex } = options; const [transformedNodes, setTransformedNodes] = useState([]); const fetch = useKibanaHttpFetch(); @@ -69,12 +61,12 @@ export const useInfrastructureNodeMetrics = ( return Promise.resolve(nullData); } - const request = { + const request: MetricsExplorerRequestBody = { metrics: metricsExplorerOptions.metrics, groupBy: metricsExplorerOptions.groupBy, limit: NODE_COUNT_LIMIT, indexPattern: source.configuration.metricAlias, - filterQuery: JSON.stringify(filterClauseDsl), + filterQuery: metricsExplorerOptions.filterQuery, timerange: timerangeWithInterval, }; @@ -93,7 +85,7 @@ export const useInfrastructureNodeMetrics = ( }, cancelPreviousOn: 'creation', }, - [source, metricsExplorerOptions, timerangeWithInterval, filterClauseDsl] + [source, metricsExplorerOptions, timerangeWithInterval] ); const isLoadingNodes = promiseState === 'pending' || promiseState === 'uninitialized'; diff --git a/x-pack/plugins/infra/public/components/infrastructure_node_metrics_tables/shared/index.ts b/x-pack/plugins/infra/public/components/infrastructure_node_metrics_tables/shared/index.ts index 8c74b28764d35..25f78647fa026 100644 --- a/x-pack/plugins/infra/public/components/infrastructure_node_metrics_tables/shared/index.ts +++ b/x-pack/plugins/infra/public/components/infrastructure_node_metrics_tables/shared/index.ts @@ -6,8 +6,12 @@ */ export { MetricsNodeDetailsLink, NumberCell, StepwisePagination, UptimeCell } from './components'; -export { metricsToApiOptions, useInfrastructureNodeMetrics } from './hooks'; -export type { MetricsMap, SortState } from './hooks'; +export { + metricsToApiOptions, + useInfrastructureNodeMetrics, + createMetricByFieldLookup, +} from './hooks'; +export type { MetricsMap, SortState, MetricsQueryOptions } from './hooks'; export type { IntegratedNodeMetricsTableProps, SourceProviderProps,