Skip to content

Commit

Permalink
[Infra] Legacy metrics changes (elastic#189974)
Browse files Browse the repository at this point in the history
Closes elastic#189628 
Closes elastic#190118

## Summary

This PR changes the way we show the legacy metrics. It changes the hosts
table `rx` and `tx` values to use the `v2` and renames
`availablefieldsOptions` to `availableFieldsOptions`

It covers `1.` and `2.` from this
[comment](elastic#189261 (review))

## Testing: 

Legacy metrics changes in inventory


https://github.com/user-attachments/assets/d2a74b0f-8a63-49f2-8d4b-e9c738440609

The legacy metrics are currently visible only for hosts so in other
asset types they should not be visible (Inventory + Alerts)

<img width="1624" alt="Screenshot 2024-08-06 at 14 34 49"
src="https://github.com/user-attachments/assets/b24df6b1-5a0d-4a01-ac88-412a419c5d6c">

Alert rules creation and metrics drop-down (with/without legacy metric)


https://github.com/user-attachments/assets/a403c96a-764d-4451-b370-e05a9ae1c5b4

On the Alerts page when creating a rule the default metric is CPU Usage
(not the legacy one):


![image](https://github.com/user-attachments/assets/08ed4a7a-6f6c-4aa2-8f10-5b76d6e84ea6)

Align CPU usage (Legacy) label and position with RX / TX metrics: 

<img width="1415" alt="image"
src="https://github.com/user-attachments/assets/2ce9269e-6f88-47ca-b76c-105f1daab152">
  • Loading branch information
jennypavlova authored Aug 13, 2024
1 parent c53cb3c commit fe592d4
Show file tree
Hide file tree
Showing 29 changed files with 139 additions and 75 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export const METRIC_FORMATTERS: MetricFormatters = {
formatter: InfraFormatterType.percent,
template: '{{value}}',
},
['cpuTotal']: {
['cpuV2']: {
formatter: InfraFormatterType.percent,
template: '{{value}}',
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import * as rt from 'io-ts';

export const InfraMetricTypeRT = rt.keyof({
cpu: null,
cpuTotal: null,
cpuV2: null,
normalizedLoad1m: null,
diskSpaceUsage: null,
memory: null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export const staticInventoryViewAttributes: InventoryViewAttributes = {
isDefault: false,
isStatic: true,
metric: {
type: 'cpuTotal',
type: 'cpuV2',
},
groupBy: [],
nodeType: 'host',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const TranslationsLowercase = {
}),

CPUUsageLegacy: i18n.translate('xpack.infra.waffle.metricOptions.cpuUsageLegacyText', {
defaultMessage: 'CPU usage (legacy)',
defaultMessage: 'CPU usage (Legacy)',
}),

MemoryUsage: i18n.translate('xpack.infra.waffle.metricOptions.memoryUsageText', {
Expand Down Expand Up @@ -113,16 +113,18 @@ export const toMetricOpt = (
nodeType?: InventoryItemType
): { text: string; textLC: string; value: SnapshotMetricType } | undefined => {
switch (metric) {
case 'cpuTotal':
case 'cpuV2':
return {
text: Translations.CPUUsageTotal,
textLC: TranslationsLowercase.CPUUsageTotal,
value: 'cpuTotal',
value: 'cpuV2',
};
case 'cpu':
return {
text: Translations.CPUUsageLegacy,
textLC: TranslationsLowercase.CPUUsageLegacy,
text: showLegacyLabel(nodeType) ? Translations.CPUUsageLegacy : Translations.CPUUsageTotal,
textLC: showLegacyLabel(nodeType)
? TranslationsLowercase.CPUUsageLegacy
: TranslationsLowercase.CPUUsageTotal,
value: 'cpu',
};
case 'memory':
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ describe('Expression', () => {
const ruleParams = {
criteria: [
{
metric: 'cpu',
metric: 'cpuV2',
timeSize: 1,
timeUnit: 'm',
threshold: [10],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ type Props = Omit<
>;

export const defaultExpression = {
metric: 'cpu' as SnapshotMetricType,
metric: 'cpuV2' as SnapshotMetricType,
comparator: COMPARATORS.GREATER_THAN,
threshold: [],
timeSize: 1,
Expand Down Expand Up @@ -772,7 +772,7 @@ export const nodeTypes: { [key: string]: any } = {
const metricUnit: Record<string, { label: string }> = {
count: { label: '' },
cpu: { label: '%' },
cpuTotal: { label: '%' },
cpuV2: { label: '%' },
memory: { label: '%' },
rx: { label: 'bits/s' },
tx: { label: 'bits/s' },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -231,8 +231,10 @@ const convertMetricValue = (metric: SnapshotMetricType, value: number) => {
};
const converters: Record<string, (n: number) => number> = {
cpu: (n) => Number(n) / 100,
cpuTotal: (n) => Number(n) / 100,
cpuV2: (n) => Number(n) / 100,
memory: (n) => Number(n) / 100,
tx: (n) => Number(n) / 8,
rx: (n) => Number(n) / 8,
rxV2: (n) => Number(n) / 8,
txV2: (n) => Number(n) / 8,
};
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,14 @@ interface Props {
| 'rightDown';
}

type V2MetricType = 'txV2' | 'rxV2' | 'cpuV2';

const V2ToLegacyMapping: Record<V2MetricType, string> = {
txV2: 'tx',
rxV2: 'rx',
cpuV2: 'cpu',
};

const AGGREGATION_LABELS = {
['avg']: i18n.translate('xpack.infra.waffle.customMetrics.aggregationLables.avg', {
defaultMessage: 'Average',
Expand Down Expand Up @@ -164,9 +172,25 @@ export const MetricExpression = ({
[customMetric, debouncedOnChangeCustom]
);

const availablefieldsOptions = metrics.map((m) => {
return { label: m.text, value: m.value };
}, []);
const metricsToRemove: string[] = metrics
.map((currentMetric) => {
return V2ToLegacyMapping[currentMetric.value as V2MetricType];
})
.filter((m): m is string => !!m);

const availableFieldsOptions = useMemo(
() =>
metrics
.filter(
(availableMetric) =>
metric?.value === availableMetric.value ||
!metricsToRemove.includes(availableMetric.value)
)
.map((m) => {
return { label: m.text, value: m.value };
}),
[metric?.value, metrics, metricsToRemove]
);

return (
<EuiPopover
Expand Down Expand Up @@ -293,14 +317,14 @@ export const MetricExpression = ({
<EuiComboBox
fullWidth
singleSelection={{ asPlainText: true }}
data-test-subj="availablefieldsOptionsComboBox"
data-test-subj="availableFieldsOptionsComboBox"
// @ts-expect-error upgrade typescript v5.1.6
isInvalid={errors.metric.length > 0}
placeholder={firstFieldOption.text}
options={availablefieldsOptions}
noSuggestions={!availablefieldsOptions.length}
options={availableFieldsOptions}
noSuggestions={!availableFieldsOptions.length}
selectedOptions={
metric ? availablefieldsOptions.filter((a) => a.value === metric.value) : []
metric ? availableFieldsOptions.filter((a) => a.value === metric.value) : []
}
renderOption={(o: any) => o.label}
onChange={(selectedOptions) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
export const useInventoryAlertPrefill = () => {
const [nodeType, setNodeType] = useState<InventoryItemType>('host');
const [filterQuery, setFilterQuery] = useState<string | undefined>();
const [metric, setMetric] = useState<SnapshotMetricInput>({ type: 'cpuTotal' });
const [metric, setMetric] = useState<SnapshotMetricInput>({ type: 'cpuV2' });
const [customMetrics, setCustomMetrics] = useState<SnapshotCustomMetricInput[]>([]);
// only shows for AWS when there are regions info
const [region, setRegion] = useState('');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ const mockHostNode: InfraAssetMetricsItem[] = [
{
metrics: [
{
name: 'cpuTotal',
name: 'cpuV2',
value: 0.6353277777777777,
},
{
Expand Down Expand Up @@ -79,7 +79,7 @@ const mockHostNode: InfraAssetMetricsItem[] = [
{
metrics: [
{
name: 'cpuTotal',
name: 'cpuV2',
value: 0.8647805555555556,
},
{
Expand Down Expand Up @@ -169,7 +169,7 @@ describe('useHostTable hook', () => {
rx: 252456.92916666667,
tx: 252758.425,
memory: 0.94525,
cpuTotal: 0.6353277777777777,
cpuV2: 0.6353277777777777,
diskSpaceUsage: 0.2040001,
memoryFree: 34359.738368,
normalizedLoad1m: 239.2040001,
Expand All @@ -187,7 +187,7 @@ describe('useHostTable hook', () => {
rx: 95.86339715321859,
tx: 110.38566859563191,
memory: 0.5400000214576721,
cpuTotal: 0.8647805555555556,
cpuV2: 0.8647805555555556,
diskSpaceUsage: 0.5400000214576721,
memoryFree: 9.194304,
normalizedLoad1m: 100,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -287,10 +287,10 @@ export const useHostsTable = () => {
/>
),
width: metricColumnsWidth,
field: 'cpuTotal',
field: 'cpuV2',
sortable: true,
'data-test-subj': 'hostsView-tableRow-cpuUsage',
render: (avg: number) => formatMetric('cpuTotal', avg),
render: (avg: number) => formatMetric('cpuV2', avg),
align: 'right',
},
{
Expand Down Expand Up @@ -362,7 +362,7 @@ export const useHostsTable = () => {
/>
),
width: '12%',
field: 'rx',
field: 'rxV2',
sortable: true,
'data-test-subj': 'hostsView-tableRow-rx',
render: (avg: number) => formatMetric('rx', avg),
Expand All @@ -377,7 +377,7 @@ export const useHostsTable = () => {
/>
),
width: '12%',
field: 'tx',
field: 'txV2',
sortable: true,
'data-test-subj': 'hostsView-tableRow-tx',
render: (avg: number) => formatMetric('tx', avg),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@ import {
import { StringDateRange } from './use_unified_search_url_state';

const HOST_TABLE_METRICS: Array<{ type: InfraAssetMetricType }> = [
{ type: 'cpuTotal' },
{ type: 'cpuV2' },
{ type: 'diskSpaceUsage' },
{ type: 'memory' },
{ type: 'memoryFree' },
{ type: 'normalizedLoad1m' },
{ type: 'rx' },
{ type: 'tx' },
{ type: 'rxV2' },
{ type: 'txV2' },
];

const BASE_INFRA_METRICS_PATH = '/api/metrics/infra';
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ const NODE: InfraWaffleMapNode = {
id: 'host-01',
name: 'host-01',
path: [{ value: 'host-01', label: 'host-01' }],
metrics: [{ name: 'cpuTotal' }],
metrics: [{ name: 'cpuV2' }],
};

export const nextTick = () => new Promise((res) => process.nextTick(res));
Expand All @@ -45,11 +45,13 @@ describe('ConditionalToolTip', () => {
name: 'host-01',
path: [{ label: 'host-01', value: 'host-01', ip: '192.168.1.10' }],
metrics: [
{ name: 'cpuTotal', value: 0.1, avg: 0.4, max: 0.7 },
{ name: 'cpuV2', value: 0.1, avg: 0.4, max: 0.7 },
{ name: 'cpu', value: 0.1, avg: 0.4, max: 0.7 },
{ name: 'memory', value: 0.8, avg: 0.8, max: 1 },
{ name: 'txV2', value: 1000000, avg: 1000000, max: 1000000 },
{ name: 'rxV2', value: 1000000, avg: 1000000, max: 1000000 },
{ name: 'txV2', value: 1000000, avg: 1000000, max: 1000000 },
{ name: 'rx', value: 1000000, avg: 1000000, max: 1000000 },
{ name: 'tx', value: 1000000, avg: 1000000, max: 1000000 },
{
name: 'cedd6ca0-5775-11eb-a86f-adb714b6c486',
max: 0.34164999922116596,
Expand Down Expand Up @@ -79,14 +81,16 @@ describe('ConditionalToolTip', () => {
},
});
const expectedMetrics = [
{ type: 'cpuTotal' },
{ type: 'cpu' },
{ type: 'cpuV2' },
{ type: 'memory' },
{ type: 'txV2' },
{ type: 'rxV2' },
{ type: 'cpu' },
{ type: 'tx' },
{ type: 'rx' },
{
aggregation: 'avg',
field: 'host.cpuTotal.pct',
field: 'host.cpuV2.pct',
id: 'cedd6ca0-5775-11eb-a86f-adb714b6c486',
label: 'My Custom Label',
type: 'custom',
Expand Down Expand Up @@ -141,11 +145,11 @@ const mockedUseWaffleOptionsContexReturnValue: ReturnType<typeof useWaffleOption
nodeType: 'host',
customOptions: [],
view: 'map',
metric: { type: 'cpuTotal' },
metric: { type: 'cpuV2' },
customMetrics: [
{
aggregation: 'avg',
field: 'host.cpuTotal.pct',
field: 'host.cpuV2.pct',
id: 'cedd6ca0-5775-11eb-a86f-adb714b6c486',
label: 'My Custom Label',
type: 'custom',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export const ConditionalToolTip = ({ node, nodeType, currentTime }: Props) => {
const dataNode = first(nodes);
const metrics = (dataNode && dataNode.metrics) || [];
return (
<div style={{ minWidth: 200 }} data-test-subj={`conditionalTooltipContent-${node.name}`}>
<div style={{ minWidth: 220 }} data-test-subj={`conditionalTooltipContent-${node.name}`}>
<div
style={{
borderBottom: `${euiTheme.border.thin}`,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export const DEFAULT_LEGEND: WaffleLegendOptions = {
};

export const DEFAULT_WAFFLE_OPTIONS_STATE: WaffleOptionsState = {
metric: { type: 'cpuTotal' },
metric: { type: 'cpuV2' },
groupBy: [],
nodeType: 'host',
view: 'map',
Expand Down
Loading

0 comments on commit fe592d4

Please sign in to comment.