Skip to content

Commit 8ce3539

Browse files
committed
feat: better code
1 parent 37ee90d commit 8ce3539

File tree

8 files changed

+137
-219
lines changed

8 files changed

+137
-219
lines changed

src/components/MemoryViewer/MemoryViewer.scss

Lines changed: 15 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -5,27 +5,31 @@
55

66
position: relative;
77
z-index: 0;
8+
89
min-width: 150px;
910
padding: 0 4px;
1011

1112
&__progress-container {
1213
position: relative;
14+
15+
overflow: hidden;
16+
1317
height: 23px;
18+
1419
border-radius: 2px;
1520
background: var(--g-color-base-generic);
16-
overflow: hidden;
1721
}
1822

1923
&__container {
2024
display: flex;
2125
align-items: end;
26+
2227
text-align: end;
2328
}
2429

2530
&__legend {
2631
width: 20px;
2732
height: 20px;
28-
2933
margin-right: 4px;
3034

3135
&_type_ExternalConsumption {
@@ -51,6 +55,7 @@
5155

5256
&__segment {
5357
position: absolute;
58+
5459
height: 100%;
5560

5661
&_type_ExternalConsumption {
@@ -78,22 +83,27 @@
7883
display: flex;
7984
flex-wrap: wrap;
8085
gap: 12px;
86+
8187
margin-top: 8px;
88+
8289
color: var(--g-color-text-complementary);
8390
}
8491

8592
&__legend-item {
8693
display: flex;
8794
align-items: center;
8895
gap: 4px;
96+
8997
white-space: nowrap;
9098
}
9199

92100
&__legend-color {
101+
flex-shrink: 0;
102+
93103
width: 12px;
94104
height: 12px;
105+
95106
border-radius: 2px;
96-
flex-shrink: 0;
97107
}
98108

99109
&_theme_dark {
@@ -123,45 +133,8 @@
123133
}
124134

125135
&_size {
126-
&_xs {
127-
height: 20px;
128-
@include body-2-typography();
129-
}
130-
131-
&_s {
132-
height: 28px;
133-
font-size: var(--g-text-body-1-font-size);
134-
line-height: 28px;
135-
}
136-
137-
&_m {
138-
height: 32px;
139-
font-size: var(--g-text-body-2-font-size);
140-
line-height: 32px;
141-
}
142-
143-
&_ns {
144-
height: 24px;
145-
font-size: 13px;
146-
line-height: var(--g-text-subheader-3-line-height);
147-
}
148-
149-
&_n {
150-
height: 36px;
151-
font-size: var(--g-text-body-1-font-size);
152-
line-height: 36px;
153-
}
154-
155-
&_l {
156-
height: 38px;
157-
font-size: var(--g-text-subheader-3-font-size);
158-
line-height: 38px;
159-
}
160-
161-
&_head {
162-
font-size: var(--g-text-body-1-font-size);
163-
line-height: 36px;
164-
}
136+
height: 20px;
137+
@include body-2-typography();
165138

166139
#{$block}__progress-container {
167140
height: inherit;
@@ -170,26 +143,7 @@
170143

171144
&__text {
172145
display: flex;
173-
align-items: center;
174146
justify-content: center;
175-
}
176-
177-
&__memory-segment {
178-
display: flex;
179147
align-items: center;
180-
181-
width: 280px;
182-
}
183-
184-
&__hard-limit {
185-
position: absolute;
186-
height: 100%;
187-
background-color: var(--g-color-base-danger-medium);
188-
}
189-
190-
&__soft-limit {
191-
position: absolute;
192-
height: 100%;
193-
background-color: var(--g-color-base-warning-medium);
194148
}
195149
}

src/components/MemoryViewer/MemoryViewer.tsx

Lines changed: 44 additions & 117 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
1-
import React from 'react';
2-
31
import {DefinitionList, useTheme} from '@gravity-ui/uikit';
42

53
import type {TMemoryStats} from '../../types/api/nodes';
64
import {cn} from '../../utils/cn';
7-
import {formatNumber, roundToPrecision} from '../../utils/dataFormatters/dataFormatters';
85
import {calculateProgressStatus} from '../../utils/progress';
9-
import {UNBREAKABLE_GAP, isNumeric} from '../../utils/utils';
6+
import {isNumeric} from '../../utils/utils';
107
import {HoverPopup} from '../HoverPopup/HoverPopup';
118
import {ProgressViewer} from '../ProgressViewer/ProgressViewer';
129

13-
import i18n from './i18n';
10+
import {getMemorySegments} from './utils';
1411

1512
import './MemoryViewer.scss';
1613

@@ -21,25 +18,13 @@ type FormatProgressViewerValues = (
2118
capacity?: number,
2219
) => (string | number | undefined)[];
2320

24-
const formatValue2 = (value?: number) => {
25-
return formatNumber(roundToPrecision(Number(value), 2));
26-
};
27-
28-
const defaultFormatValues: FormatProgressViewerValues = (value, total) => {
29-
return [formatValue2(value), formatValue2(total)];
30-
};
31-
32-
type ProgressViewerSize = 'xs' | 's' | 'ns' | 'm' | 'n' | 'l' | 'head';
33-
3421
export interface MemoryProgressViewerProps {
35-
stats?: TMemoryStats;
36-
totalCapacity: number;
22+
stats: TMemoryStats;
3723
className?: string;
38-
size?: ProgressViewerSize;
3924
warningThreshold?: number;
4025
value?: number | string;
4126
capacity?: number | string;
42-
formatValues?: FormatProgressViewerValues;
27+
formatValues: FormatProgressViewerValues;
4328
percents?: boolean;
4429
dangerThreshold?: number;
4530
}
@@ -49,10 +34,8 @@ export function MemoryViewer({
4934
value,
5035
capacity,
5136
percents,
52-
formatValues = defaultFormatValues,
53-
totalCapacity,
37+
formatValues,
5438
className,
55-
size = 'xs',
5639
warningThreshold = 60,
5740
dangerThreshold = 80,
5841
}: MemoryProgressViewerProps) {
@@ -80,49 +63,20 @@ export function MemoryViewer({
8063
return valueText;
8164
};
8265

83-
const calculateMemoryPercentage = (value: number) => {
66+
const calculateMemoryShare = (segmentSize: number) => {
8467
if (!value) {
8568
return 0;
8669
}
87-
return parseFloat(((value / parseFloat(String(capacity))) * 100).toFixed(2));
70+
return (segmentSize / parseFloat(String(capacity))) * 100;
8871
};
8972

90-
const memorySegments = [
91-
{
92-
label: i18n('text_external-consumption'),
93-
key: 'ExternalConsumption',
94-
value: parseFloat(stats?.ExternalConsumption || '0'),
95-
},
96-
{
97-
label: i18n('text_allocator-caches'),
98-
key: 'AllocatorCachesMemory',
99-
value: parseFloat(stats?.AllocatorCachesMemory || '0'),
100-
},
101-
{
102-
label: i18n('text_shared-cache'),
103-
key: 'SharedCacheConsumption',
104-
value: parseFloat(stats?.SharedCacheConsumption || '0'),
105-
capacity: stats?.SharedCacheLimit,
106-
},
107-
{
108-
label: i18n('text_memtable'),
109-
key: 'MemTableConsumption',
110-
value: parseFloat(stats?.MemTableConsumption || '0'),
111-
capacity: stats?.MemTableLimit,
112-
},
113-
{
114-
label: i18n('text_query-execution'),
115-
key: 'QueryExecutionConsumption',
116-
value: parseFloat(stats?.QueryExecutionConsumption || '0'),
117-
capacity: stats?.QueryExecutionLimit,
118-
},
119-
];
120-
121-
console.log(memorySegments);
73+
const memorySegments = getMemorySegments(stats);
12274

12375
const totalUsedMemory =
124-
memorySegments.reduce((acc, segment) => acc + calculateMemoryPercentage(segment.value), 0) /
125-
totalCapacity;
76+
memorySegments
77+
.filter(({isInfo}) => !isInfo)
78+
.reduce((acc, segment) => acc + calculateMemoryShare(segment.value), 0) /
79+
parseFloat(String(capacity));
12680

12781
const status = calculateProgressStatus({
12882
fillWidth: totalUsedMemory,
@@ -137,9 +91,8 @@ export function MemoryViewer({
13791
<HoverPopup
13892
popupContent={
13993
<DefinitionList responsive>
140-
{memorySegments
141-
.filter(({value}) => value)
142-
.map(({label, value, capacity, key}) => (
94+
{memorySegments.map(
95+
({label, value: segmentSize, capacity: segmentCapacity, key}) => (
14396
<DefinitionList.Item
14497
key={label}
14598
name={
@@ -149,67 +102,41 @@ export function MemoryViewer({
149102
</div>
150103
}
151104
>
152-
<div className={b('memory-segment')}>
153-
{capacity ? (
154-
<React.Fragment>
155-
<ProgressViewer
156-
value={value}
157-
capacity={capacity}
158-
formatValues={formatValues}
159-
colorizeProgress
160-
percents
161-
/>
162-
{UNBREAKABLE_GAP}
163-
</React.Fragment>
164-
) : null}
165-
<div className={b('memory-segment-percentage')}>
166-
{formatValues(value, value)[1]}
167-
</div>
168-
{UNBREAKABLE_GAP + '/' + UNBREAKABLE_GAP}
169-
<div className={b('memory-segment-percentage')}>
170-
{calculateMemoryPercentage(value)}% total
171-
</div>
172-
</div>
105+
{segmentCapacity ? (
106+
<ProgressViewer
107+
value={segmentSize}
108+
capacity={segmentCapacity}
109+
formatValues={formatValues}
110+
colorizeProgress
111+
/>
112+
) : (
113+
formatValues(segmentSize)[0]
114+
)}
173115
</DefinitionList.Item>
174-
))}
116+
),
117+
)}
175118
</DefinitionList>
176119
}
177120
>
178-
<div className={b({size, theme, status}, className)}>
121+
<div className={b({theme, status}, className)}>
179122
<div className={b('progress-container')}>
180-
{memorySegments.map((segment) => {
181-
const position = currentPosition;
182-
currentPosition += calculateMemoryPercentage(segment.value);
183-
184-
return (
185-
<div
186-
key={segment.key}
187-
className={b('segment', {type: segment.key})}
188-
style={{
189-
width: `${calculateMemoryPercentage(segment.value)}%`,
190-
left: `${position}%`,
191-
}}
192-
/>
193-
);
194-
})}
195-
{stats?.SoftLimit ? (
196-
<div
197-
className={b('soft-limit')}
198-
style={{
199-
width: '2px',
200-
left: `${calculateMemoryPercentage(parseFloat(stats?.SoftLimit))}%`,
201-
}}
202-
/>
203-
) : null}
204-
{stats?.HardLimit ? (
205-
<div
206-
className={b('hard-limit')}
207-
style={{
208-
width: '2px',
209-
left: `${calculateMemoryPercentage(parseFloat(stats?.HardLimit))}%`,
210-
}}
211-
/>
212-
) : null}
123+
{memorySegments
124+
.filter(({isInfo}) => !isInfo)
125+
.map((segment) => {
126+
const position = currentPosition;
127+
currentPosition += calculateMemoryShare(segment.value);
128+
129+
return (
130+
<div
131+
key={segment.key}
132+
className={b('segment', {type: segment.key})}
133+
style={{
134+
width: `${calculateMemoryShare(segment.value).toFixed(2)}%`,
135+
left: `${position}%`,
136+
}}
137+
/>
138+
);
139+
})}
213140
<div className={b('text')}>{renderContent()}</div>
214141
</div>
215142
</div>

src/components/MemoryViewer/i18n/en.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,7 @@
55
"text_shared-cache": "Shared Cache",
66
"text_memtable": "MemTable",
77
"text_query-execution": "Query Execution",
8+
"text_soft-limit": "Soft Limit",
9+
"text_hard-limit": "Hard Limit",
810
"memory-detailed": "Memory Detailed"
911
}

0 commit comments

Comments
 (0)