forked from elastic/kibana
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Infra UI] Add cloud metrics and cloud/host info to metadata endpoint (…
…elastic#41836) * [Infra UI] Add cloud metrics and cloud/host info to metadata endpoint * Adding cloud metrics * Correcting the pod host/cloud info * Adding tests to metadata for new cloud/host info * Fixing test to include machine.type * Adding aws test data * Refactor metadata container into hook * Functionally complete * updating tests * Removing Metadata GraphQL endpoint and supporting files * Moving types under common/http_api and prefixing with Infra * adding filter for aws.ec2 dataset * move away from fetch to useHTTPRequest * Add decode function to useHTTPRequest; rename data to response; * Changing from Typescript types to IO-TS types and adding checks at client and server
- Loading branch information
1 parent
2355162
commit c143403
Showing
40 changed files
with
18,105 additions
and
937 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
100 changes: 100 additions & 0 deletions
100
x-pack/legacy/plugins/infra/common/http_api/metadata_api.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
/* | ||
* 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 * as rt from 'io-ts'; | ||
import { InfraWrappableRequest } from '../../server/lib/adapters/framework'; | ||
|
||
export const InfraMetadataNodeTypeRT = rt.keyof({ | ||
host: null, | ||
pod: null, | ||
container: null, | ||
}); | ||
|
||
export const InfraMetadataRequestRT = rt.type({ | ||
nodeId: rt.string, | ||
nodeType: InfraMetadataNodeTypeRT, | ||
sourceId: rt.string, | ||
}); | ||
|
||
export const InfraMetadataFeatureRT = rt.type({ | ||
name: rt.string, | ||
source: rt.string, | ||
}); | ||
|
||
export const InfraMetadataOSRT = rt.partial({ | ||
codename: rt.string, | ||
family: rt.string, | ||
kernel: rt.string, | ||
name: rt.string, | ||
platform: rt.string, | ||
version: rt.string, | ||
}); | ||
|
||
export const InfraMetadataHostRT = rt.partial({ | ||
name: rt.string, | ||
os: InfraMetadataOSRT, | ||
architecture: rt.string, | ||
containerized: rt.boolean, | ||
}); | ||
|
||
export const InfraMetadataInstanceRT = rt.partial({ | ||
id: rt.string, | ||
name: rt.string, | ||
}); | ||
|
||
export const InfraMetadataProjectRT = rt.partial({ | ||
id: rt.string, | ||
}); | ||
|
||
export const InfraMetadataMachineRT = rt.partial({ | ||
interface: rt.string, | ||
}); | ||
|
||
export const InfraMetadataCloudRT = rt.partial({ | ||
instance: InfraMetadataInstanceRT, | ||
provider: rt.string, | ||
availability_zone: rt.string, | ||
project: InfraMetadataProjectRT, | ||
machine: InfraMetadataMachineRT, | ||
}); | ||
|
||
export const InfraMetadataInfoRT = rt.partial({ | ||
cloud: InfraMetadataCloudRT, | ||
host: InfraMetadataHostRT, | ||
}); | ||
|
||
const InfraMetadataRequiredRT = rt.type({ | ||
name: rt.string, | ||
features: rt.array(InfraMetadataFeatureRT), | ||
}); | ||
|
||
const InfraMetadataOptionalRT = rt.partial({ | ||
info: InfraMetadataInfoRT, | ||
}); | ||
|
||
export const InfraMetadataRT = rt.intersection([InfraMetadataRequiredRT, InfraMetadataOptionalRT]); | ||
|
||
export type InfraMetadata = rt.TypeOf<typeof InfraMetadataRT>; | ||
|
||
export type InfraMetadataRequest = rt.TypeOf<typeof InfraMetadataRequestRT>; | ||
|
||
export type InfraMetadataWrappedRequest = InfraWrappableRequest<InfraMetadataRequest>; | ||
|
||
export type InfraMetadataFeature = rt.TypeOf<typeof InfraMetadataFeatureRT>; | ||
|
||
export type InfraMetadataInfo = rt.TypeOf<typeof InfraMetadataInfoRT>; | ||
|
||
export type InfraMetadataCloud = rt.TypeOf<typeof InfraMetadataCloudRT>; | ||
|
||
export type InfraMetadataInstance = rt.TypeOf<typeof InfraMetadataInstanceRT>; | ||
|
||
export type InfraMetadataProject = rt.TypeOf<typeof InfraMetadataProjectRT>; | ||
|
||
export type InfraMetadataMachine = rt.TypeOf<typeof InfraMetadataMachineRT>; | ||
|
||
export type InfraMetadataHost = rt.TypeOf<typeof InfraMetadataHostRT>; | ||
|
||
export type InfraMEtadataOS = rt.TypeOf<typeof InfraMetadataOSRT>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
/* | ||
* 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 { Errors } from 'io-ts'; | ||
import { failure } from 'io-ts/lib/PathReporter'; | ||
|
||
export const createPlainError = (message: string) => new Error(message); | ||
|
||
export const throwErrors = (createError: (message: string) => Error) => (errors: Errors) => { | ||
throw createError(failure(errors).join('\n')); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
x-pack/legacy/plugins/infra/public/containers/metadata/lib/get_filtered_layouts.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/* | ||
* 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 { InfraMetadataFeature } from '../../../../common/http_api/metadata_api'; | ||
import { InfraMetricLayout } from '../../../pages/metrics/layouts/types'; | ||
|
||
export const getFilteredLayouts = ( | ||
layouts: InfraMetricLayout[], | ||
metadata: Array<InfraMetadataFeature | null> | undefined | ||
): InfraMetricLayout[] => { | ||
if (!metadata) { | ||
return layouts; | ||
} | ||
|
||
const metricMetadata: Array<string | null> = metadata | ||
.filter(data => data && data.source === 'metrics') | ||
.map(data => data && data.name); | ||
|
||
// After filtering out sections that can't be displayed, a layout may end up empty and can be removed. | ||
const filteredLayouts = layouts | ||
.map(layout => getFilteredLayout(layout, metricMetadata)) | ||
.filter(layout => layout.sections.length > 0); | ||
return filteredLayouts; | ||
}; | ||
|
||
export const getFilteredLayout = ( | ||
layout: InfraMetricLayout, | ||
metricMetadata: Array<string | null> | ||
): InfraMetricLayout => { | ||
// A section is only displayed if at least one of its requirements is met | ||
// All others are filtered out. | ||
const filteredSections = layout.sections.filter( | ||
section => _.intersection(section.requires, metricMetadata).length > 0 | ||
); | ||
return { ...layout, sections: filteredSections }; | ||
}; |
22 changes: 0 additions & 22 deletions
22
x-pack/legacy/plugins/infra/public/containers/metadata/metadata.gql_query.ts
This file was deleted.
Oops, something went wrong.
48 changes: 48 additions & 0 deletions
48
x-pack/legacy/plugins/infra/public/containers/metadata/use_metadata.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/* | ||
* 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 { useEffect } from 'react'; | ||
import { InfraNodeType } from '../../graphql/types'; | ||
import { InfraMetricLayout } from '../../pages/metrics/layouts/types'; | ||
import { InfraMetadata, InfraMetadataRT } from '../../../common/http_api/metadata_api'; | ||
import { getFilteredLayouts } from './lib/get_filtered_layouts'; | ||
import { useHTTPRequest } from '../../hooks/use_http_request'; | ||
import { throwErrors, createPlainError } from '../../../common/runtime_types'; | ||
|
||
export function useMetadata( | ||
nodeId: string, | ||
nodeType: InfraNodeType, | ||
layouts: InfraMetricLayout[], | ||
sourceId: string | ||
) { | ||
const decodeResponse = (response: any) => { | ||
return InfraMetadataRT.decode(response).getOrElseL(throwErrors(createPlainError)); | ||
}; | ||
|
||
const { error, loading, response, makeRequest } = useHTTPRequest<InfraMetadata>( | ||
'/api/infra/metadata', | ||
'POST', | ||
JSON.stringify({ | ||
nodeId, | ||
nodeType, | ||
sourceId, | ||
decodeResponse, | ||
}) | ||
); | ||
|
||
useEffect(() => { | ||
(async () => { | ||
await makeRequest(); | ||
})(); | ||
}, [makeRequest]); | ||
|
||
return { | ||
name: (response && response.name) || '', | ||
filteredLayouts: (response && getFilteredLayouts(layouts, response.features)) || [], | ||
error: (error && error.message) || null, | ||
loading, | ||
}; | ||
} |
Oops, something went wrong.