Skip to content

Commit 0b5404c

Browse files
[Metrics UI] Optimizations for Snapshot and Inventory Metadata (#83596) (#83751)
* [Metrics UI] Add time range to inventory metadata request * Adding optimizations for snapshot request * Adding sorting to dataset request * Only query inventory metadata for AWS * moving check inside getCloudMetadata * removing unused deps Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>
1 parent f30e0e2 commit 0b5404c

File tree

11 files changed

+69
-20
lines changed

11 files changed

+69
-20
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ export const InventoryMetaResponseRT = rt.type({
2121
export const InventoryMetaRequestRT = rt.type({
2222
sourceId: rt.string,
2323
nodeType: ItemTypeRT,
24+
currentTime: rt.number,
2425
});
2526

2627
export type InventoryMetaRequest = rt.TypeOf<typeof InventoryMetaRequestRT>;

x-pack/plugins/infra/public/pages/metrics/inventory_view/components/layout.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ export const Layout = () => {
124124
<>
125125
<TopActionContainer ref={topActionMeasureRef}>
126126
<EuiFlexGroup justifyContent="spaceBetween" alignItems="center" gutterSize="m">
127-
<Toolbar nodeType={nodeType} />
127+
<Toolbar nodeType={nodeType} currentTime={currentTime} />
128128
<EuiFlexItem grow={false}>
129129
<IntervalLabel intervalAsString={intervalAsString} />
130130
</EuiFlexItem>

x-pack/plugins/infra/public/pages/metrics/inventory_view/components/toolbars/toolbar.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,12 @@ const wrapToolbarItems = (
5454

5555
interface Props {
5656
nodeType: InventoryItemType;
57+
currentTime: number;
5758
}
5859

59-
export const Toolbar = ({ nodeType }: Props) => {
60+
export const Toolbar = ({ nodeType, currentTime }: Props) => {
6061
const { sourceId } = useSourceContext();
61-
const { accounts, regions } = useInventoryMeta(sourceId, nodeType);
62+
const { accounts, regions } = useInventoryMeta(sourceId, nodeType, currentTime);
6263
const ToolbarItems = findToolbar(nodeType);
6364
return wrapToolbarItems(ToolbarItems, accounts, regions);
6465
};

x-pack/plugins/infra/public/pages/metrics/inventory_view/hooks/use_inventory_meta.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,11 @@ import {
1515
} from '../../../../../common/http_api/inventory_meta_api';
1616
import { InventoryItemType } from '../../../../../common/inventory_models/types';
1717

18-
export function useInventoryMeta(sourceId: string, nodeType: InventoryItemType) {
18+
export function useInventoryMeta(
19+
sourceId: string,
20+
nodeType: InventoryItemType,
21+
currentTime: number
22+
) {
1923
const decodeResponse = (response: any) => {
2024
return pipe(
2125
InventoryMetaResponseRT.decode(response),
@@ -29,6 +33,7 @@ export function useInventoryMeta(sourceId: string, nodeType: InventoryItemType)
2933
JSON.stringify({
3034
sourceId,
3135
nodeType,
36+
currentTime,
3237
}),
3338
decodeResponse
3439
);

x-pack/plugins/infra/public/pages/metrics/inventory_view/hooks/use_snaphot.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ export function useSnapshot(
4545
interval: '1m',
4646
to: currentTime,
4747
from: currentTime - 1200 * 1000,
48-
lookbackSize: 20,
48+
lookbackSize: 5,
4949
};
5050

5151
const { error, loading, response, makeRequest } = useHTTPRequest<SnapshotNodeResponse>(

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ export const initInventoryMetaRoute = (libs: InfraBackendLibs) => {
3333
},
3434
async (requestContext, request, response) => {
3535
try {
36-
const { sourceId, nodeType } = pipe(
36+
const { sourceId, nodeType, currentTime } = pipe(
3737
InventoryMetaRequestRT.decode(request.body),
3838
fold(throwErrors(Boom.badRequest), identity)
3939
);
@@ -42,11 +42,13 @@ export const initInventoryMetaRoute = (libs: InfraBackendLibs) => {
4242
requestContext.core.savedObjects.client,
4343
sourceId
4444
);
45+
4546
const awsMetadata = await getCloudMetadata(
4647
framework,
4748
requestContext,
4849
configuration,
49-
nodeType
50+
nodeType,
51+
currentTime
5052
);
5153

5254
return response.ok({

x-pack/plugins/infra/server/routes/inventory_metadata/lib/get_cloud_metadata.ts

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,18 @@ export const getCloudMetadata = async (
2525
framework: KibanaFramework,
2626
req: RequestHandlerContext,
2727
sourceConfiguration: InfraSourceConfiguration,
28-
nodeType: InventoryItemType
28+
nodeType: InventoryItemType,
29+
currentTime: number
2930
): Promise<CloudMetaData> => {
3031
const model = findInventoryModel(nodeType);
32+
// Only run this for AWS modules, eventually we might have more.
33+
if (model.requiredModule !== 'aws') {
34+
return {
35+
accounts: [],
36+
projects: [],
37+
regions: [],
38+
};
39+
}
3140

3241
const metricQuery = {
3342
allowNoIndices: true,
@@ -36,7 +45,18 @@ export const getCloudMetadata = async (
3645
body: {
3746
query: {
3847
bool: {
39-
must: [{ match: { 'event.module': model.requiredModule } }],
48+
must: [
49+
{
50+
range: {
51+
[sourceConfiguration.fields.timestamp]: {
52+
gte: currentTime - 86400000, // 24 hours ago
53+
lte: currentTime,
54+
format: 'epoch_millis',
55+
},
56+
},
57+
},
58+
{ match: { 'event.module': model.requiredModule } },
59+
],
4060
},
4161
},
4262
size: 0,

x-pack/plugins/infra/server/routes/metrics_explorer/lib/find_interval_for_metrics.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ export const findIntervalForMetrics = async (
3434

3535
const modules = await Promise.all(
3636
fields.map(
37-
async (field) => await getDatasetForField(client, field as string, options.indexPattern)
37+
async (field) =>
38+
await getDatasetForField(client, field as string, options.indexPattern, options.timerange)
3839
)
3940
);
4041

x-pack/plugins/infra/server/routes/metrics_explorer/lib/get_dataset_for_field.ts

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,34 @@ interface EventDatasetHit {
1717
export const getDatasetForField = async (
1818
client: ESSearchClient,
1919
field: string,
20-
indexPattern: string
20+
indexPattern: string,
21+
timerange: { field: string; to: number; from: number }
2122
) => {
2223
const params = {
2324
allowNoIndices: true,
2425
ignoreUnavailable: true,
2526
terminateAfter: 1,
2627
index: indexPattern,
2728
body: {
28-
query: { exists: { field } },
29+
query: {
30+
bool: {
31+
filter: [
32+
{ exists: { field } },
33+
{
34+
range: {
35+
[timerange.field]: {
36+
gte: timerange.from,
37+
lte: timerange.to,
38+
format: 'epoch_millis',
39+
},
40+
},
41+
},
42+
],
43+
},
44+
},
2945
size: 1,
3046
_source: ['event.dataset'],
47+
sort: [{ [timerange.field]: { order: 'desc' } }],
3148
},
3249
};
3350

x-pack/plugins/infra/server/routes/snapshot/lib/create_timerange_with_interval.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,10 @@ const aggregationsToModules = async (
7575
const fields = await Promise.all(
7676
uniqueFields.map(
7777
async (field) =>
78-
await getDatasetForField(client, field as string, options.sourceConfiguration.metricAlias)
78+
await getDatasetForField(client, field as string, options.sourceConfiguration.metricAlias, {
79+
...options.timerange,
80+
field: options.sourceConfiguration.fields.timestamp,
81+
})
7982
)
8083
);
8184
return fields.filter((f) => f) as string[];

0 commit comments

Comments
 (0)