Skip to content

Commit a819d73

Browse files
upcoming: [DI-26793] - Legend row label shown as per group by order (linode#12742)
* upcoming: [DI-26793] - Update logic to show legend rows based on the group by order * Added changeset * Fixed typecheck failures
1 parent 1ad25dd commit a819d73

File tree

7 files changed

+79
-21
lines changed

7 files changed

+79
-21
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@linode/manager": Upcoming Features
3+
---
4+
5+
ACLP: Order of each legend row label value is based on group by sequence ([#12742](https://github.com/linode/manager/pull/12742))

packages/manager/cypress/e2e/core/cloudpulse/dbaas-widgets-verification.spec.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ const getWidgetLegendRowValuesFromResponse = (
145145
status: 'success',
146146
unit,
147147
serviceType,
148+
groupBy: ['entity_id'],
148149
});
149150

150151
// Destructure metrics data from the first legend row

packages/manager/cypress/e2e/core/cloudpulse/linode-widget-verification.spec.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ const getWidgetLegendRowValuesFromResponse = (
130130
status: 'success',
131131
unit,
132132
serviceType,
133+
groupBy: ['entity_id'],
133134
});
134135

135136
// Destructure metrics data from the first legend row

packages/manager/cypress/e2e/core/cloudpulse/nodebalancer-widget-verification.spec.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ const getWidgetLegendRowValuesFromResponse = (
123123
status: 'success',
124124
unit,
125125
serviceType,
126+
groupBy: ['entity_id'],
126127
});
127128

128129
// Destructure metrics data from the first legend row

packages/manager/src/features/CloudPulse/Utils/CloudPulseWidgetUtils.test.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ describe('getLabelName method', () => {
7070
resources: [{ id: '123', label: 'linode-1' }],
7171
serviceType: 'linode',
7272
unit: '%',
73+
groupBy: ['entity_id'],
7374
};
7475

7576
it('returns resource label when all data is valid', () => {
@@ -121,6 +122,7 @@ it('test generateGraphData with metrics data', () => {
121122
status: 'success',
122123
unit: '%',
123124
serviceType: 'linode',
125+
groupBy: ['entity_id'],
124126
});
125127

126128
expect(result.areas[0].dataKey).toBe('linode-1');
@@ -148,6 +150,7 @@ describe('getDimensionName method', () => {
148150
serviceType: 'linode',
149151
metric: { entity_id: '123' },
150152
resources: [{ id: '123', label: 'linode-1' }],
153+
groupBy: ['entity_id'],
151154
};
152155

153156
it('returns resource label when all data is valid', () => {

packages/manager/src/features/CloudPulse/Utils/CloudPulseWidgetUtils.ts

Lines changed: 67 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ import type { AreaProps } from 'src/components/AreaChart/AreaChart';
2929
import type { MetricsDisplayRow } from 'src/components/LineGraph/MetricsDisplay';
3030

3131
export interface LabelNameOptionsProps {
32+
/**
33+
* array of group by fields
34+
*/
35+
groupBy: string[];
36+
3237
/**
3338
* Boolean to check if metric name should be hidden
3439
*/
@@ -61,6 +66,11 @@ export interface LabelNameOptionsProps {
6166
}
6267

6368
interface GraphDataOptionsProps {
69+
/**
70+
* array of group by fields
71+
*/
72+
groupBy: string[];
73+
6474
/**
6575
* label for the graph title
6676
*/
@@ -120,6 +130,10 @@ interface MetricRequestProps {
120130
}
121131

122132
export interface DimensionNameProperties {
133+
/**
134+
* array of group by fields
135+
*/
136+
groupBy: string[];
123137
/**
124138
* Boolean to check if metric name should be hidden
125139
*/
@@ -168,7 +182,8 @@ interface GraphData {
168182
* @returns parameters which will be necessary to populate graph & legends
169183
*/
170184
export const generateGraphData = (props: GraphDataOptionsProps): GraphData => {
171-
const { label, metricsList, resources, serviceType, status, unit } = props;
185+
const { label, metricsList, resources, serviceType, status, unit, groupBy } =
186+
props;
172187
const legendRowsData: MetricsDisplayRow[] = [];
173188
const dimension: { [timestamp: number]: { [label: string]: number } } = {};
174189
const areas: AreaProps[] = [];
@@ -205,6 +220,7 @@ export const generateGraphData = (props: GraphDataOptionsProps): GraphData => {
205220
unit,
206221
hideMetricName,
207222
serviceType,
223+
groupBy,
208224
};
209225
const labelName = getLabelName(labelOptions);
210226
const data = seriesDataFormatter(transformedData.values, start, end);
@@ -331,14 +347,21 @@ export const getLabelName = (props: LabelNameOptionsProps): string => {
331347
unit,
332348
hideMetricName = false,
333349
serviceType,
350+
groupBy,
334351
} = props;
335352
// aggregated metric, where metric keys will be 0
336353
if (!Object.keys(metric).length) {
337354
// in this case return widget label and unit
338355
return `${label} (${unit})`;
339356
}
340357

341-
return getDimensionName({ metric, resources, hideMetricName, serviceType });
358+
return getDimensionName({
359+
metric,
360+
resources,
361+
hideMetricName,
362+
serviceType,
363+
groupBy,
364+
});
342365
};
343366

344367
/**
@@ -347,30 +370,53 @@ export const getLabelName = (props: LabelNameOptionsProps): string => {
347370
*/
348371
// ... existing code ...
349372
export const getDimensionName = (props: DimensionNameProperties): string => {
350-
const { metric, resources, hideMetricName = false, serviceType } = props;
351-
return Object.entries(metric)
352-
.map(([key, value]) => {
353-
if (key === 'entity_id') {
354-
return mapResourceIdToName(value, resources);
373+
const {
374+
metric,
375+
resources,
376+
hideMetricName = false,
377+
serviceType,
378+
groupBy,
379+
} = props;
380+
const labels: string[] = new Array(groupBy.length).fill('');
381+
Object.entries(metric).forEach(([key, value]) => {
382+
if (key === 'entity_id') {
383+
const resourceName = mapResourceIdToName(value, resources);
384+
const index = groupBy.indexOf(key);
385+
if (index !== -1) {
386+
labels[index] = resourceName;
387+
} else {
388+
labels.push(resourceName);
355389
}
390+
return;
391+
}
356392

357-
if (key === 'linode_id') {
358-
return (
359-
resources.find((resource) => resource.entities?.[value] !== undefined)
360-
?.entities?.[value] ?? value
361-
);
393+
if (key === 'linode_id') {
394+
const linodeLabel =
395+
resources.find((resource) => resource.entities?.[value] !== undefined)
396+
?.entities?.[value] ?? value;
397+
const index = groupBy.indexOf('linode_id');
398+
if (index !== -1) {
399+
labels[index] = linodeLabel;
400+
} else {
401+
labels.push(linodeLabel);
362402
}
403+
return;
404+
}
363405

364-
if (key === 'metric_name' && hideMetricName) {
365-
return '';
366-
}
406+
if (key === 'metric_name' && hideMetricName) {
407+
return;
408+
}
367409

368-
return (
369-
DIMENSION_TRANSFORM_CONFIG[serviceType]?.[key]?.(value) ?? value ?? ''
370-
);
371-
})
372-
.filter(Boolean)
373-
.join(' | ');
410+
const dimensionValue =
411+
DIMENSION_TRANSFORM_CONFIG[serviceType]?.[key]?.(value) ?? value ?? '';
412+
const index = groupBy.indexOf(key);
413+
if (index !== -1) {
414+
labels[index] = dimensionValue;
415+
} else {
416+
labels.push(dimensionValue);
417+
}
418+
});
419+
return labels.filter(Boolean).join(' | ');
374420
};
375421

376422
/**

packages/manager/src/features/CloudPulse/Widget/CloudPulseWidget.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,7 @@ export const CloudPulseWidget = (props: CloudPulseWidgetProperties) => {
277277
status,
278278
unit,
279279
serviceType,
280+
groupBy: widgetProp.group_by,
280281
});
281282

282283
data = generatedData.dimensions;

0 commit comments

Comments
 (0)