Skip to content

Commit 1ffd9e7

Browse files
committed
fix: better code
1 parent 26760eb commit 1ffd9e7

File tree

8 files changed

+57
-48
lines changed

8 files changed

+57
-48
lines changed

src/containers/Cluster/ClusterOverview/components/ClusterMetricsCores.tsx

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import {DoughnutMetrics} from '../../../../components/DoughnutMetrics/DoughnutMetrics';
2-
import {formatNumber, formatNumericValues} from '../../../../utils/dataFormatters/dataFormatters';
2+
import {formatCoresLegend} from '../../../../utils/metrics/formatMetricLegend';
33
import i18n from '../../i18n';
44
import type {ClusterMetricsCommonProps} from '../shared';
55
import {getDiagramValues} from '../utils';
@@ -8,16 +8,6 @@ import {ClusterMetricsCardContent} from './ClusterMetricsCard';
88

99
interface ClusterMetricsCoresProps extends ClusterMetricsCommonProps {}
1010

11-
function formatCoresLegend({value, capacity}: {value: number; capacity: number}) {
12-
let formatted = [];
13-
if (capacity < 10_000) {
14-
formatted = [formatNumber(Math.round(value)), formatNumber(Math.round(capacity))];
15-
} else {
16-
formatted = formatNumericValues(value, capacity, undefined, '', true);
17-
}
18-
return `${formatted[0]} ${i18n('context_of')} ${formatted[1]} ${i18n('context_cores')}`;
19-
}
20-
2111
export function ClusterMetricsCores({
2212
collapsed,
2313
value,

src/containers/Cluster/ClusterOverview/components/ClusterMetricsMemory.tsx

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import {DoughnutMetrics} from '../../../../components/DoughnutMetrics/DoughnutMetrics';
2-
import {formatStorageValues} from '../../../../utils/dataFormatters/dataFormatters';
2+
import {formatStorageLegend} from '../../../../utils/metrics/formatMetricLegend';
33
import i18n from '../../i18n';
44
import type {ClusterMetricsCommonProps} from '../shared';
55
import {getDiagramValues} from '../utils';
@@ -8,11 +8,6 @@ import {ClusterMetricsCardContent} from './ClusterMetricsCard';
88

99
interface ClusterMetricsMemoryProps extends ClusterMetricsCommonProps {}
1010

11-
function formatStorageLegend({value, capacity}: {value: number; capacity: number}) {
12-
const formatted = formatStorageValues(value, capacity, undefined, '\n');
13-
return `${formatted[0]} ${i18n('context_of')} ${formatted[1]}`;
14-
}
15-
1611
export function ClusterMetricsMemory({
1712
value,
1813
capacity,

src/containers/Cluster/ClusterOverview/components/ClusterMetricsStorage.tsx

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import {DoughnutMetrics} from '../../../../components/DoughnutMetrics/DoughnutMetrics';
2-
import {formatStorageValues} from '../../../../utils/dataFormatters/dataFormatters';
2+
import {formatStorageLegend} from '../../../../utils/metrics/formatMetricLegend';
33
import i18n from '../../i18n';
44
import type {ClusterMetricsCommonProps} from '../shared';
55
import {getDiagramValues} from '../utils';
@@ -10,11 +10,6 @@ interface ClusterMetricsStorageProps extends ClusterMetricsCommonProps {
1010
groups: number;
1111
}
1212

13-
function formatStorageLegend({value, capacity}: {value: number; capacity: number}) {
14-
const formatted = formatStorageValues(value, capacity, undefined, '\n');
15-
return `${formatted[0]} ${i18n('context_of')} ${formatted[1]}`;
16-
}
17-
1813
export function ClusterMetricsStorage({
1914
value,
2015
capacity,

src/containers/Tenant/Diagnostics/TenantOverview/MetricsCards/MetricsCards.tsx

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,6 @@ export function MetricsCards({
8888
limit={cpuMetrics.totalLimit}
8989
unit="cores"
9090
active={metricsTab === TENANT_METRICS_TABS_IDS.cpu}
91-
status={cpuMetrics.status}
9291
helpText={i18n('context_cpu-description')}
9392
/>
9493
</Link>
@@ -106,7 +105,6 @@ export function MetricsCards({
106105
limit={storageMetrics.totalLimit}
107106
unit="bytes"
108107
active={metricsTab === TENANT_METRICS_TABS_IDS.storage}
109-
status={storageMetrics.status}
110108
helpText={i18n('context_storage-description')}
111109
/>
112110
</Link>
@@ -124,7 +122,6 @@ export function MetricsCards({
124122
limit={memoryMetrics.totalLimit}
125123
unit="bytes"
126124
active={metricsTab === TENANT_METRICS_TABS_IDS.memory}
127-
status={memoryMetrics.status}
128125
helpText={i18n('context_memory-description')}
129126
/>
130127
</Link>
@@ -139,7 +136,6 @@ export function MetricsCards({
139136
limit={networkMetrics.totalLimit}
140137
unit="bytes"
141138
active={false}
142-
status={networkMetrics.status}
143139
clickable={false}
144140
helpText={i18n('context_network-description')}
145141
/>

src/containers/Tenant/Diagnostics/TenantOverview/TabCard/TabCard.tsx

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
import {Card, Flex} from '@gravity-ui/uikit';
22

33
import {DoughnutMetrics} from '../../../../../components/DoughnutMetrics/DoughnutMetrics';
4-
import {formatBytes} from '../../../../../utils/bytesParsers';
4+
import {getDiagramValues} from '../../../../../containers/Cluster/ClusterOverview/utils';
55
import {cn} from '../../../../../utils/cn';
6-
import {formatPercent} from '../../../../../utils/dataFormatters/dataFormatters';
7-
import type {ProgressStatus} from '../../../../../utils/progress';
6+
import {
7+
formatCoresLegend,
8+
formatStorageLegend,
9+
} from '../../../../../utils/metrics/formatMetricLegend';
810

911
import './TabCard.scss';
1012

@@ -15,9 +17,8 @@ interface TabCardProps {
1517
sublabel?: string;
1618
value: number;
1719
limit: number;
18-
unit?: string;
20+
unit: 'bytes' | 'cores';
1921
active?: boolean;
20-
status?: ProgressStatus;
2122
helpText?: string;
2223
clickable?: boolean;
2324
}
@@ -27,24 +28,18 @@ export function TabCard({
2728
sublabel,
2829
value,
2930
limit,
30-
unit = '',
31+
unit,
3132
active,
32-
status = 'good',
3333
helpText,
3434
clickable = true,
3535
}: TabCardProps) {
36-
const percentage = limit > 0 ? (value / limit) * 100 : 0;
37-
const formattedPercentage = formatPercent(percentage / 100);
36+
const legendFormatter = unit === 'bytes' ? formatStorageLegend : formatCoresLegend;
3837

39-
// Format values based on unit type
40-
let formattedValue: string;
41-
if (unit === 'bytes') {
42-
const formattedUsed = formatBytes({value});
43-
const formattedLimit = formatBytes({value: limit});
44-
formattedValue = `${formattedUsed} of ${formattedLimit}`;
45-
} else {
46-
formattedValue = `${value} of ${limit}${unit ? ' ' + unit : ''}`;
47-
}
38+
const {status, percents, legend, fill} = getDiagramValues({
39+
value,
40+
capacity: limit,
41+
legendFormatter,
42+
});
4843

4944
return (
5045
<div className={b({active})}>
@@ -57,16 +52,16 @@ export function TabCard({
5752
<DoughnutMetrics
5853
size="small"
5954
status={status}
60-
fillWidth={percentage}
55+
fillWidth={fill}
6156
className={b('doughnut')}
6257
>
6358
<DoughnutMetrics.Value variant="subheader-1">
64-
{formattedPercentage}
59+
{percents}
6560
</DoughnutMetrics.Value>
6661
</DoughnutMetrics>
6762
<div className={b('legend-wrapper')}>
6863
<DoughnutMetrics.Legend variant="subheader-2">
69-
{formattedValue}
64+
{legend}
7065
</DoughnutMetrics.Legend>
7166
<DoughnutMetrics.Legend
7267
variant="body-1"
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import {
2+
formatNumber,
3+
formatNumericValues,
4+
formatStorageValues,
5+
} from '../dataFormatters/dataFormatters';
6+
7+
import i18n from './i18n';
8+
9+
export interface MetricFormatParams {
10+
value: number;
11+
capacity: number;
12+
}
13+
14+
export function formatStorageLegend({value, capacity}: MetricFormatParams): string {
15+
const formatted = formatStorageValues(value, capacity, undefined, '\n');
16+
return `${formatted[0]} ${i18n('context_of')} ${formatted[1]}`;
17+
}
18+
19+
export function formatCoresLegend({value, capacity}: MetricFormatParams): string {
20+
let formatted = [];
21+
if (capacity < 10_000) {
22+
formatted = [formatNumber(Math.round(value)), formatNumber(Math.round(capacity))];
23+
} else {
24+
formatted = formatNumericValues(value, capacity, undefined, '', true);
25+
}
26+
return `${formatted[0]} ${i18n('context_of')} ${formatted[1]} ${i18n('context_cores')}`;
27+
}

src/utils/metrics/i18n/en.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"context_of": "of",
3+
"context_cores": "cores"
4+
}

src/utils/metrics/i18n/index.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import {registerKeysets} from '../../i18n';
2+
3+
import en from './en.json';
4+
5+
const COMPONENT = 'ydb-metrics';
6+
7+
export default registerKeysets(COMPONENT, {en});

0 commit comments

Comments
 (0)