Skip to content

Commit e7fa8fe

Browse files
authored
refactor: migrate ProgressViewer to ts (#533)
1 parent 721883c commit e7fa8fe

File tree

47 files changed

+284
-264
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+284
-264
lines changed

src/components/FullNodeViewer/FullNodeViewer.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ import cn from 'bem-cn-lite';
33
import type {TSystemStateInfo} from '../../types/api/nodes';
44

55
import {LOAD_AVERAGE_TIME_INTERVALS} from '../../utils/constants';
6-
import {calcUptime} from '../../utils';
6+
import {calcUptime} from '../../utils/dataFormatters/dataFormatters';
77

88
import InfoViewer from '../InfoViewer/InfoViewer';
9-
import ProgressViewer from '../ProgressViewer/ProgressViewer';
9+
import {ProgressViewer} from '../ProgressViewer/ProgressViewer';
1010
import {PoolUsage} from '../PoolUsage/PoolUsage';
1111

1212
import './FullNodeViewer.scss';

src/components/InfoViewer/formatters/common.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type {TDirEntry} from '../../../types/api/schema';
2-
import {formatDateTime} from '../../../utils';
2+
import {formatDateTime} from '../../../utils/dataFormatters/dataFormatters';
33

44
import {createInfoFormatter} from '../utils';
55

src/components/InfoViewer/formatters/pqGroup.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import {
44
TPQPartitionConfig,
55
TPQTabletConfig,
66
} from '../../../types/api/schema';
7-
import {formatBps, formatBytes, formatNumber} from '../../../utils';
7+
import {formatBps, formatBytes, formatNumber} from '../../../utils/dataFormatters/dataFormatters';
88
import {HOUR_IN_SECONDS} from '../../../utils/constants';
99

1010
import {createInfoFormatter} from '../utils';

src/components/InfoViewer/formatters/table.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
import type {TFollowerGroup, TPartitionConfig, TTableStats} from '../../../types/api/schema';
22
import type {TMetrics} from '../../../types/api/tenant';
3-
import {formatCPU, formatNumber, formatBps, formatDateTime} from '../../../utils';
3+
import {
4+
formatBps,
5+
formatCPU,
6+
formatDateTime,
7+
formatNumber,
8+
} from '../../../utils/dataFormatters/dataFormatters';
49
import {toFormattedSize} from '../../FormattedBytes/utils';
510

611
import {createInfoFormatter} from '../utils';

src/components/ProgressViewer/ProgressViewer.js

Lines changed: 0 additions & 92 deletions
This file was deleted.
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
import cn from 'bem-cn-lite';
2+
3+
import type {ValueOf} from '../../types/common';
4+
5+
import './ProgressViewer.scss';
6+
7+
const b = cn('progress-viewer');
8+
9+
export const PROGRESS_VIEWER_SIZE_IDS = {
10+
xs: 'xs',
11+
s: 's',
12+
ns: 'ns',
13+
m: 'm',
14+
n: 'n',
15+
l: 'l',
16+
head: 'head',
17+
} as const;
18+
19+
type ProgressViewerSize = ValueOf<typeof PROGRESS_VIEWER_SIZE_IDS>;
20+
21+
/*
22+
23+
Props description:
24+
1) value - the amount of progress
25+
2) capacity - maximum possible progress value
26+
3) formatValues - function for formatting the value and capacity
27+
4) percents - display progress in percents
28+
5) colorizeProgress - change the color of the progress bar depending on its value
29+
6) inverseColorize - invert the colors of the progress bar
30+
*/
31+
32+
interface ProgressViewerProps {
33+
value?: number | string;
34+
capacity?: number | string;
35+
formatValues?: (value?: number, capacity?: number) => (string | undefined)[];
36+
percents?: boolean;
37+
className?: string;
38+
size?: ProgressViewerSize;
39+
colorizeProgress?: boolean;
40+
inverseColorize?: boolean;
41+
}
42+
43+
export function ProgressViewer({
44+
value,
45+
capacity = 100,
46+
formatValues,
47+
percents,
48+
className,
49+
size = PROGRESS_VIEWER_SIZE_IDS.xs,
50+
colorizeProgress,
51+
inverseColorize,
52+
}: ProgressViewerProps) {
53+
let fillWidth = Math.round((parseFloat(String(value)) / parseFloat(String(capacity))) * 100);
54+
fillWidth = fillWidth > 100 ? 100 : fillWidth;
55+
56+
let valueText: number | string | undefined = Math.round(Number(value)),
57+
capacityText: number | string | undefined = capacity,
58+
divider = '/';
59+
if (formatValues) {
60+
[valueText, capacityText] = formatValues(Number(value), Number(capacity));
61+
} else if (percents) {
62+
valueText = fillWidth + '%';
63+
capacityText = '';
64+
divider = '';
65+
}
66+
67+
let bg = inverseColorize ? 'scarlet' : 'apple';
68+
if (colorizeProgress) {
69+
if (fillWidth > 60 && fillWidth <= 80) {
70+
bg = 'saffron';
71+
} else if (fillWidth > 80) {
72+
bg = inverseColorize ? 'apple' : 'scarlet';
73+
}
74+
}
75+
76+
const lineStyle = {
77+
width: fillWidth + '%',
78+
};
79+
80+
const text = fillWidth > 60 ? 'contrast0' : 'contrast70';
81+
82+
if (!isNaN(fillWidth)) {
83+
return (
84+
<div className={b({size}, className)}>
85+
<div className={b('line', {bg})} style={lineStyle}></div>
86+
<span
87+
className={b('text', {text})}
88+
>{`${valueText} ${divider} ${capacityText}`}</span>
89+
</div>
90+
);
91+
}
92+
93+
return <div className={`${b({size})} ${className} error`}>no data</div>;
94+
}

src/components/TooltipsContent/TabletTooltipContent/TabletTooltipContent.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type {TTabletStateInfo} from '../../../types/api/tablet';
22

3-
import {calcUptime} from '../../../utils';
3+
import {calcUptime} from '../../../utils/dataFormatters/dataFormatters';
44
import {InfoViewer, createInfoFormatter, formatObject} from '../../InfoViewer';
55

66
const formatTablet = createInfoFormatter<TTabletStateInfo>({

src/containers/Cluster/ClusterInfo/ClusterInfo.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import block from 'bem-cn-lite';
33
import {Skeleton} from '@gravity-ui/uikit';
44

55
import EntityStatus from '../../../components/EntityStatus/EntityStatus';
6-
import ProgressViewer from '../../../components/ProgressViewer/ProgressViewer';
6+
import {ProgressViewer} from '../../../components/ProgressViewer/ProgressViewer';
77
import InfoViewer, {InfoViewerItem} from '../../../components/InfoViewer/InfoViewer';
88
import {Tags} from '../../../components/Tags';
99
import {Tablet} from '../../../components/Tablet';
@@ -16,7 +16,7 @@ import type {AdditionalClusterProps, ClusterLink} from '../../../types/additiona
1616
import type {VersionValue} from '../../../types/versions';
1717
import type {TClusterInfo} from '../../../types/api/cluster';
1818
import {backend, customBackend} from '../../../store';
19-
import {formatStorageValues} from '../../../utils';
19+
import {formatStorageValues} from '../../../utils/dataFormatters/dataFormatters';
2020
import {useSetting, useTypedSelector} from '../../../utils/hooks';
2121
import {
2222
CLUSTER_DEFAULT_TITLE,

src/containers/Heatmap/Heatmap.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {Checkbox, Select} from '@gravity-ui/uikit';
77
import type {IHeatmapMetricValue} from '../../types/store/heatmap';
88
import {getTabletsInfo, setHeatmapOptions} from '../../store/reducers/heatmap';
99
import {showTooltip, hideTooltip} from '../../store/reducers/tooltip';
10-
import {formatNumber} from '../../utils';
10+
import {formatNumber} from '../../utils/dataFormatters/dataFormatters';
1111
import {useAutofetcher, useTypedSelector} from '../../utils/hooks';
1212

1313
import {Loader} from '../../components/Loader';

src/containers/Heatmap/Histogram/Histogram.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import PropTypes from 'prop-types';
33
import cn from 'bem-cn-lite';
44

55
import {getColorRange, getCurrentMetricLimits} from '../util';
6-
import {formatNumber} from '../../../utils';
6+
import {formatNumber} from '../../../utils/dataFormatters/dataFormatters';
77

88
import './Histogram.scss';
99

0 commit comments

Comments
 (0)