Skip to content

Commit a29a9b0

Browse files
committed
[Metrics UI] Replace Snapshot API with Metrics API
- Remove server/lib/snapshot - Replace backend for /api/infra/snapshot with data from Metrics API - Fixing tests with updates to the snapshot node
1 parent d9dc47e commit a29a9b0

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+592
-779
lines changed

x-pack/plugins/infra/common/http_api/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@ export * from './log_entries';
1010
export * from './metrics_explorer';
1111
export * from './metrics_api';
1212
export * from './log_alerts';
13+
export * from './snapshot_api';

x-pack/plugins/infra/common/http_api/metrics_api.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ export const MetricsAPIRequestRT = rt.intersection([
3333
afterKey: rt.union([rt.null, afterKeyObjectRT]),
3434
limit: rt.union([rt.number, rt.null, rt.undefined]),
3535
filters: rt.array(rt.object),
36-
forceInterval: rt.boolean,
3736
dropLastBucket: rt.boolean,
3837
alignDataToEnd: rt.boolean,
3938
}),
@@ -59,7 +58,10 @@ export const MetricsAPIRowRT = rt.intersection([
5958
rt.type({
6059
timestamp: rt.number,
6160
}),
62-
rt.record(rt.string, rt.union([rt.string, rt.number, rt.null, rt.undefined])),
61+
rt.record(
62+
rt.string,
63+
rt.union([rt.string, rt.number, rt.null, rt.undefined, rt.array(rt.object)])
64+
),
6365
]);
6466

6567
export const MetricsAPISeriesRT = rt.intersection([

x-pack/plugins/infra/common/http_api/metrics_explorer.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,10 @@ export const metricsExplorerRowRT = rt.intersection([
8989
rt.type({
9090
timestamp: rt.number,
9191
}),
92-
rt.record(rt.string, rt.union([rt.string, rt.number, rt.null, rt.undefined])),
92+
rt.record(
93+
rt.string,
94+
rt.union([rt.string, rt.number, rt.null, rt.undefined, rt.array(rt.object)])
95+
),
9396
]);
9497

9598
export const metricsExplorerSeriesRT = rt.intersection([

x-pack/plugins/infra/common/http_api/snapshot_api.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
import * as rt from 'io-ts';
88
import { SnapshotMetricTypeRT, ItemTypeRT } from '../inventory_models/types';
9-
import { metricsExplorerSeriesRT } from './metrics_explorer';
9+
import { MetricsAPISeriesRT } from './metrics_api';
1010

1111
export const SnapshotNodePathRT = rt.intersection([
1212
rt.type({
@@ -22,7 +22,7 @@ const SnapshotNodeMetricOptionalRT = rt.partial({
2222
value: rt.union([rt.number, rt.null]),
2323
avg: rt.union([rt.number, rt.null]),
2424
max: rt.union([rt.number, rt.null]),
25-
timeseries: metricsExplorerSeriesRT,
25+
timeseries: MetricsAPISeriesRT,
2626
});
2727

2828
const SnapshotNodeMetricRequiredRT = rt.type({
@@ -36,6 +36,7 @@ export const SnapshotNodeMetricRT = rt.intersection([
3636
export const SnapshotNodeRT = rt.type({
3737
metrics: rt.array(SnapshotNodeMetricRT),
3838
path: rt.array(SnapshotNodePathRT),
39+
name: rt.string,
3940
});
4041

4142
export const SnapshotNodeResponseRT = rt.type({

x-pack/plugins/infra/common/inventory_models/types.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,10 @@ export const ESSumBucketAggRT = rt.type({
281281
}),
282282
});
283283

284+
export const ESTopHitsAggRT = rt.type({
285+
top_hits: rt.object,
286+
});
287+
284288
interface SnapshotTermsWithAggregation {
285289
terms: { field: string };
286290
aggregations: MetricsUIAggregation;
@@ -304,6 +308,7 @@ export const ESAggregationRT = rt.union([
304308
ESSumBucketAggRT,
305309
ESTermsWithAggregationRT,
306310
ESCaridnalityAggRT,
311+
ESTopHitsAggRT,
307312
]);
308313

309314
export const MetricsUIAggregationRT = rt.record(rt.string, ESAggregationRT);

x-pack/plugins/infra/public/pages/metrics/inventory_view/components/waffle/conditional_tooltip.test.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ describe('ConditionalToolTip', () => {
8888
mockedUseSnapshot.mockReturnValue({
8989
nodes: [
9090
{
91+
name: 'host-01',
9192
path: [{ label: 'host-01', value: 'host-01', ip: '192.168.1.10' }],
9293
metrics: [
9394
{ name: 'cpu', value: 0.1, avg: 0.4, max: 0.7 },

x-pack/plugins/infra/public/pages/metrics/inventory_view/lib/calculate_bounds_from_nodes.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { calculateBoundsFromNodes } from './calculate_bounds_from_nodes';
77
import { SnapshotNode } from '../../../../../common/http_api/snapshot_api';
88
const nodes: SnapshotNode[] = [
99
{
10+
name: 'host-01',
1011
path: [{ value: 'host-01', label: 'host-01' }],
1112
metrics: [
1213
{
@@ -18,6 +19,7 @@ const nodes: SnapshotNode[] = [
1819
],
1920
},
2021
{
22+
name: 'host-02',
2123
path: [{ value: 'host-02', label: 'host-02' }],
2224
metrics: [
2325
{

x-pack/plugins/infra/public/pages/metrics/inventory_view/lib/sort_nodes.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { SnapshotNode } from '../../../../../common/http_api/snapshot_api';
99

1010
const nodes: SnapshotNode[] = [
1111
{
12+
name: 'host-01',
1213
path: [{ value: 'host-01', label: 'host-01' }],
1314
metrics: [
1415
{
@@ -20,6 +21,7 @@ const nodes: SnapshotNode[] = [
2021
],
2122
},
2223
{
24+
name: 'host-02',
2325
path: [{ value: 'host-02', label: 'host-02' }],
2426
metrics: [
2527
{

x-pack/plugins/infra/server/lib/alerting/inventory_metric_threshold/evaluate_condition.ts

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,11 @@ import {
1616
} from '../../adapters/framework/adapter_types';
1717
import { Comparator, InventoryMetricConditions } from './types';
1818
import { AlertServices } from '../../../../../alerts/server';
19-
import { InfraSnapshot } from '../../snapshot';
20-
import { parseFilterQuery } from '../../../utils/serialized_query';
2119
import { InventoryItemType, SnapshotMetricType } from '../../../../common/inventory_models/types';
22-
import { InfraTimerangeInput } from '../../../../common/http_api/snapshot_api';
23-
import { InfraSourceConfiguration } from '../../sources';
20+
import { InfraTimerangeInput, SnapshotRequest } from '../../../../common/http_api/snapshot_api';
21+
import { InfraSource } from '../../sources';
2422
import { UNGROUPED_FACTORY_KEY } from '../common/utils';
23+
import { getNodes } from '../../../routes/snapshot/lib/get_nodes';
2524

2625
type ConditionResult = InventoryMetricConditions & {
2726
shouldFire: boolean[];
@@ -33,7 +32,7 @@ type ConditionResult = InventoryMetricConditions & {
3332
export const evaluateCondition = async (
3433
condition: InventoryMetricConditions,
3534
nodeType: InventoryItemType,
36-
sourceConfiguration: InfraSourceConfiguration,
35+
source: InfraSource,
3736
callCluster: AlertServices['callCluster'],
3837
filterQuery?: string,
3938
lookbackSize?: number
@@ -55,7 +54,7 @@ export const evaluateCondition = async (
5554
nodeType,
5655
metric,
5756
timerange,
58-
sourceConfiguration,
57+
source,
5958
filterQuery,
6059
customMetric
6160
);
@@ -94,30 +93,29 @@ const getData = async (
9493
nodeType: InventoryItemType,
9594
metric: SnapshotMetricType,
9695
timerange: InfraTimerangeInput,
97-
sourceConfiguration: InfraSourceConfiguration,
96+
source: InfraSource,
9897
filterQuery?: string,
9998
customMetric?: SnapshotCustomMetricInput
10099
) => {
101-
const snapshot = new InfraSnapshot();
102-
const esClient = <Hit = {}, Aggregation = undefined>(
100+
const client = <Hit = {}, Aggregation = undefined>(
103101
options: CallWithRequestParams
104102
): Promise<InfraDatabaseSearchResponse<Hit, Aggregation>> => callCluster('search', options);
105103

106104
const metrics = [
107105
metric === 'custom' ? (customMetric as SnapshotCustomMetricInput) : { type: metric },
108106
];
109107

110-
const options = {
111-
filterQuery: parseFilterQuery(filterQuery),
108+
const snapshotRequest: SnapshotRequest = {
109+
filterQuery,
112110
nodeType,
113111
groupBy: [],
114-
sourceConfiguration,
112+
sourceId: 'default',
115113
metrics,
116114
timerange,
117115
includeTimeseries: Boolean(timerange.lookbackSize),
118116
};
119117
try {
120-
const { nodes } = await snapshot.getNodes(esClient, options);
118+
const { nodes } = await getNodes(client, snapshotRequest, source);
121119

122120
if (!nodes.length) return { [UNGROUPED_FACTORY_KEY]: null }; // No Data state
123121

x-pack/plugins/infra/server/lib/alerting/inventory_metric_threshold/inventory_metric_threshold_executor.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,7 @@ export const createInventoryMetricThresholdExecutor = (libs: InfraBackendLibs) =
5050
);
5151

5252
const results = await Promise.all(
53-
criteria.map((c) =>
54-
evaluateCondition(c, nodeType, source.configuration, services.callCluster, filterQuery)
55-
)
53+
criteria.map((c) => evaluateCondition(c, nodeType, source, services.callCluster, filterQuery))
5654
);
5755

5856
const inventoryItems = Object.keys(first(results)!);

0 commit comments

Comments
 (0)