Skip to content

Commit e22147f

Browse files
committed
feat: use same duration functions
1 parent 23e4856 commit e22147f

File tree

8 files changed

+46
-49
lines changed

8 files changed

+46
-49
lines changed

src/components/VDiskInfo/VDiskInfo.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ import {getPDiskPagePath, useVDiskPagePath} from '../../routes';
77
import {EVDiskState} from '../../types/api/vdisk';
88
import {cn} from '../../utils/cn';
99
import {
10+
formatDurationSeconds,
1011
formatStorageValuesToGb,
11-
formatUptimeInSeconds,
1212
} from '../../utils/dataFormatters/dataFormatters';
1313
import {createVDiskDeveloperUILink} from '../../utils/developerUI/developerUI';
1414
import {getSeverityColor} from '../../utils/disks/helpers';
@@ -165,7 +165,7 @@ export function VDiskInfo<T extends PreparedVDisk>({
165165
});
166166
}
167167
if (!isNil(ReplicationSecondsRemaining)) {
168-
const timeRemaining = formatUptimeInSeconds(ReplicationSecondsRemaining);
168+
const timeRemaining = formatDurationSeconds(ReplicationSecondsRemaining);
169169
if (timeRemaining) {
170170
rightColumn.push({
171171
label: vDiskInfoKeyset('replication-time-remaining'),

src/components/VDiskPopup/VDiskPopup.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import {EFlag} from '../../types/api/enums';
99
import {EVDiskState} from '../../types/api/vdisk';
1010
import {cn} from '../../utils/cn';
1111
import {EMPTY_DATA_PLACEHOLDER} from '../../utils/constants';
12-
import {formatUptimeInSeconds} from '../../utils/dataFormatters/dataFormatters';
12+
import {formatDurationSeconds} from '../../utils/dataFormatters/dataFormatters';
1313
import {createVDiskDeveloperUILink} from '../../utils/developerUI/developerUI';
1414
import {getStateSeverity} from '../../utils/disks/calculateVDiskSeverity';
1515
import {
@@ -236,7 +236,7 @@ const prepareVDiskData = (
236236
}
237237

238238
if (!isNil(ReplicationSecondsRemaining)) {
239-
const timeRemaining = formatUptimeInSeconds(ReplicationSecondsRemaining);
239+
const timeRemaining = formatDurationSeconds(ReplicationSecondsRemaining);
240240
if (timeRemaining) {
241241
vdiskData.push({
242242
name: vDiskPopupKeyset('label_remaining'),

src/containers/Operations/columns.tsx

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import {duration} from '@gravity-ui/date-utils';
21
import {Ban, CircleStop} from '@gravity-ui/icons';
32
import type {Column as DataTableColumn} from '@gravity-ui/react-data-table';
43
import {ActionTooltip, Flex, Icon, Text} from '@gravity-ui/uikit';
@@ -8,9 +7,9 @@ import {CellWithPopover} from '../../components/CellWithPopover/CellWithPopover'
87
import {operationsApi} from '../../store/reducers/operations';
98
import type {IndexBuildMetadata, OperationKind, TOperation} from '../../types/api/operations';
109
import {EStatusCode} from '../../types/api/operations';
11-
import {EMPTY_DATA_PLACEHOLDER, HOUR_IN_SECONDS, SECOND_IN_MS} from '../../utils/constants';
10+
import {EMPTY_DATA_PLACEHOLDER} from '../../utils/constants';
1211
import createToast from '../../utils/createToast';
13-
import {formatDateTime} from '../../utils/dataFormatters/dataFormatters';
12+
import {formatDateTime, formatDurationMs} from '../../utils/dataFormatters/dataFormatters';
1413
import {parseProtobufTimestampToMs} from '../../utils/timeParsers';
1514

1615
import {COLUMNS_NAMES, COLUMNS_TITLES} from './constants';
@@ -158,10 +157,8 @@ export function getColumns({
158157
durationValue = Date.now() - createTime;
159158
}
160159

161-
const durationFormatted =
162-
durationValue > HOUR_IN_SECONDS * SECOND_IN_MS
163-
? duration(durationValue).format('hh:mm:ss')
164-
: duration(durationValue).format('mm:ss');
160+
const safeDurationMs = durationValue < 0 ? 0 : durationValue;
161+
const durationFormatted = formatDurationMs(safeDurationMs);
165162

166163
return row.end_time
167164
? durationFormatted

src/containers/Tenant/Diagnostics/Consumers/TopicStats/ConsumersTopicStats.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import {SpeedMultiMeter} from '../../../../../components/SpeedMultiMeter';
22
import type {IPreparedTopicStats} from '../../../../../types/store/topic';
33
import {cn} from '../../../../../utils/cn';
4-
import {formatMsToUptime} from '../../../../../utils/dataFormatters/dataFormatters';
4+
import {formatDurationMs} from '../../../../../utils/dataFormatters/dataFormatters';
55

66
import './ConsumersTopicStats.scss';
77

@@ -21,11 +21,11 @@ export const ConsumersTopicStats = ({data}: ConsumersTopicStatsProps) => {
2121
},
2222
{
2323
label: 'Write lag',
24-
value: formatMsToUptime(partitionsWriteLag || 0),
24+
value: formatDurationMs(partitionsWriteLag || 0),
2525
},
2626
{
2727
label: 'Write idle time',
28-
value: formatMsToUptime(partitionsIdleTime || 0),
28+
value: formatDurationMs(partitionsIdleTime || 0),
2929
},
3030
];
3131

src/containers/Tenant/Diagnostics/Consumers/columns/columns.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import {getTenantPath} from '../../../../../routes';
99
import {TENANT_DIAGNOSTICS_TABS_IDS} from '../../../../../store/reducers/tenant/constants';
1010
import type {IPreparedConsumerData} from '../../../../../types/store/topic';
1111
import {cn} from '../../../../../utils/cn';
12-
import {formatMsToUptime} from '../../../../../utils/dataFormatters/dataFormatters';
12+
import {formatDurationMs} from '../../../../../utils/dataFormatters/dataFormatters';
1313
import {TenantTabsGroups} from '../../../TenantPages';
1414
import {ReadLagsHeader} from '../Headers';
1515
import {
@@ -57,23 +57,23 @@ export const columns: Column<IPreparedConsumerData>[] = [
5757
CONSUMERS_READ_LAGS_SUB_COLUMNS_IDS.WRITE_LAG
5858
],
5959
align: DataTable.RIGHT,
60-
render: ({row}) => formatMsToUptime(row.writeLag),
60+
render: ({row}) => formatDurationMs(row.writeLag),
6161
},
6262
{
6363
name: CONSUMERS_READ_LAGS_SUB_COLUMNS_IDS.READ_LAG,
6464
header: CONSUMERS_READ_LAGS_SUB_COLUMNS_TITLES[
6565
CONSUMERS_READ_LAGS_SUB_COLUMNS_IDS.READ_LAG
6666
],
6767
align: DataTable.RIGHT,
68-
render: ({row}) => formatMsToUptime(row.readLag),
68+
render: ({row}) => formatDurationMs(row.readLag),
6969
},
7070
{
7171
name: CONSUMERS_READ_LAGS_SUB_COLUMNS_IDS.READ_IDLE_TIME,
7272
header: CONSUMERS_READ_LAGS_SUB_COLUMNS_TITLES[
7373
CONSUMERS_READ_LAGS_SUB_COLUMNS_IDS.READ_IDLE_TIME
7474
],
7575
align: DataTable.RIGHT,
76-
render: ({row}) => formatMsToUptime(row.readIdleTime),
76+
render: ({row}) => formatDurationMs(row.readIdleTime),
7777
},
7878
],
7979
},

src/containers/Tenant/Diagnostics/Partitions/columns/columns.tsx

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import {useTopicDataAvailable} from '../../../../../store/reducers/capabilities/
99
import {TENANT_DIAGNOSTICS_TABS_IDS} from '../../../../../store/reducers/tenant/constants';
1010
import {cn} from '../../../../../utils/cn';
1111
import {EMPTY_DATA_PLACEHOLDER} from '../../../../../utils/constants';
12-
import {formatBytes, formatMsToUptime} from '../../../../../utils/dataFormatters/dataFormatters';
12+
import {formatBytes, formatDurationMs} from '../../../../../utils/dataFormatters/dataFormatters';
1313
import {isNumeric} from '../../../../../utils/utils';
1414
import {useDiagnosticsPageLinkGetter} from '../../DiagnosticsPages';
1515
import {
@@ -86,15 +86,15 @@ export const allColumns: Column<PreparedPartitionDataWithHosts>[] = [
8686
PARTITIONS_WRITE_LAGS_SUB_COLUMNS_IDS.PARTITION_WRITE_LAG
8787
],
8888
align: DataTable.RIGHT,
89-
render: ({row}) => formatMsToUptime(row.partitionWriteLag),
89+
render: ({row}) => formatDurationMs(row.partitionWriteLag),
9090
},
9191
{
9292
name: PARTITIONS_WRITE_LAGS_SUB_COLUMNS_IDS.PARTITION_WRITE_IDLE_TIME,
9393
header: PARTITIONS_WRITE_LAGS_SUB_COLUMNS_TITLES[
9494
PARTITIONS_WRITE_LAGS_SUB_COLUMNS_IDS.PARTITION_WRITE_IDLE_TIME
9595
],
9696
align: DataTable.RIGHT,
97-
render: ({row}) => formatMsToUptime(row.partitionWriteIdleTime),
97+
render: ({row}) => formatDurationMs(row.partitionWriteIdleTime),
9898
},
9999
],
100100
},
@@ -109,23 +109,23 @@ export const allColumns: Column<PreparedPartitionDataWithHosts>[] = [
109109
PARTITIONS_READ_LAGS_SUB_COLUMNS_IDS.CONSUMER_WRITE_LAG
110110
],
111111
align: DataTable.RIGHT,
112-
render: ({row}) => formatMsToUptime(row.consumerWriteLag),
112+
render: ({row}) => formatDurationMs(row.consumerWriteLag),
113113
},
114114
{
115115
name: PARTITIONS_READ_LAGS_SUB_COLUMNS_IDS.CONSUMER_READ_LAG,
116116
header: PARTITIONS_READ_LAGS_SUB_COLUMNS_TITLES[
117117
PARTITIONS_READ_LAGS_SUB_COLUMNS_IDS.CONSUMER_READ_LAG
118118
],
119119
align: DataTable.RIGHT,
120-
render: ({row}) => formatMsToUptime(row.consumerReadLag),
120+
render: ({row}) => formatDurationMs(row.consumerReadLag),
121121
},
122122
{
123123
name: PARTITIONS_READ_LAGS_SUB_COLUMNS_IDS.CONSUMER_READ_IDLE_TIME,
124124
header: PARTITIONS_READ_LAGS_SUB_COLUMNS_TITLES[
125125
PARTITIONS_READ_LAGS_SUB_COLUMNS_IDS.CONSUMER_READ_IDLE_TIME
126126
],
127127
align: DataTable.RIGHT,
128-
render: ({row}) => formatMsToUptime(row.consumerReadIdleTime),
128+
render: ({row}) => formatDurationMs(row.consumerReadIdleTime),
129129
},
130130
],
131131
},
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import {UNBREAKABLE_GAP} from '../../constants';
22
import {
3-
formatUptimeInSeconds,
3+
formatDurationSeconds,
44
getDowntimeFromDateFormatted,
55
getUptimeFromDateFormatted,
66
} from '../dataFormatters';
@@ -24,46 +24,46 @@ describe('getDowntimeFromDateFormatted', () => {
2424
expect(getDowntimeFromDateFormatted(3_600_000, 3_599_000)).toBe('0s');
2525
});
2626
});
27-
describe('formatUptimeInSeconds', () => {
27+
describe('formatDurationSeconds', () => {
2828
const M = 60;
2929
const H = 60 * M;
3030
const D = 24 * H;
3131

3232
test('should return days if value is more than 24h', () => {
33-
expect(formatUptimeInSeconds(24 * H)).toBe('1d' + UNBREAKABLE_GAP + '00:00:00');
34-
expect(formatUptimeInSeconds(D + H + M + 12)).toBe('1d' + UNBREAKABLE_GAP + '01:01:12');
33+
expect(formatDurationSeconds(24 * H)).toBe('1d' + UNBREAKABLE_GAP + '00:00:00');
34+
expect(formatDurationSeconds(D + H + M + 12)).toBe('1d' + UNBREAKABLE_GAP + '01:01:12');
3535
// 1 week
36-
expect(formatUptimeInSeconds(7 * D + H + M + 12)).toBe('7d' + UNBREAKABLE_GAP + '01:01:12');
36+
expect(formatDurationSeconds(7 * D + H + M + 12)).toBe('7d' + UNBREAKABLE_GAP + '01:01:12');
3737
// 1 month
38-
expect(formatUptimeInSeconds(30 * D + 11 * H + 30 * M + 12)).toBe(
38+
expect(formatDurationSeconds(30 * D + 11 * H + 30 * M + 12)).toBe(
3939
'30d' + UNBREAKABLE_GAP + '11:30:12',
4040
);
41-
expect(formatUptimeInSeconds(1234 * D + 12 * H + 12 * M + 12)).toBe(
41+
expect(formatDurationSeconds(1234 * D + 12 * H + 12 * M + 12)).toBe(
4242
'1234d' + UNBREAKABLE_GAP + '12:12:12',
4343
);
4444
});
4545
test('should return hours if value is less than 24h', () => {
46-
expect(formatUptimeInSeconds(H + M + 12)).toBe('1:01:12');
47-
expect(formatUptimeInSeconds(12 * H + 12 * M + 12)).toBe('12:12:12');
46+
expect(formatDurationSeconds(H + M + 12)).toBe('1:01:12');
47+
expect(formatDurationSeconds(12 * H + 12 * M + 12)).toBe('12:12:12');
4848
});
4949
test('should return minutes if value is less than hour', () => {
50-
expect(formatUptimeInSeconds(M + 12)).toBe('1:12');
51-
expect(formatUptimeInSeconds(12 * M + 2)).toBe('12:02');
50+
expect(formatDurationSeconds(M + 12)).toBe('1:12');
51+
expect(formatDurationSeconds(12 * M + 2)).toBe('12:02');
5252
});
5353
test('should return second if value is less than hour', () => {
54-
expect(formatUptimeInSeconds(12)).toBe('12s');
55-
expect(formatUptimeInSeconds(2)).toBe('2s');
54+
expect(formatDurationSeconds(12)).toBe('12s');
55+
expect(formatDurationSeconds(2)).toBe('2s');
5656
});
5757
test('should correctly process negative values', () => {
58-
expect(formatUptimeInSeconds(-0)).toBe('0s');
59-
expect(formatUptimeInSeconds(-12)).toBe('-12s');
60-
expect(formatUptimeInSeconds(-1 * (12 * M + 2))).toBe('-12:02');
61-
expect(formatUptimeInSeconds(-1 * (12 * H + 12 * M + 12))).toBe('-12:12:12');
62-
expect(formatUptimeInSeconds(-1 * (12 * D + 12 * H + 12 * M + 12))).toBe(
58+
expect(formatDurationSeconds(-0)).toBe('0s');
59+
expect(formatDurationSeconds(-12)).toBe('-12s');
60+
expect(formatDurationSeconds(-1 * (12 * M + 2))).toBe('-12:02');
61+
expect(formatDurationSeconds(-1 * (12 * H + 12 * M + 12))).toBe('-12:12:12');
62+
expect(formatDurationSeconds(-1 * (12 * D + 12 * H + 12 * M + 12))).toBe(
6363
'-12d' + UNBREAKABLE_GAP + '12:12:12',
6464
);
6565
});
6666
test('should return empty placeholder on NaN', () => {
67-
expect(formatUptimeInSeconds(Number.NaN)).toBe(undefined);
67+
expect(formatDurationSeconds(Number.NaN)).toBe(undefined);
6868
});
6969
});

src/utils/dataFormatters/dataFormatters.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,9 @@ export const stringifyVdiskId = (id?: TVDiskID | TVSlotId) => {
3939

4040
/**
4141
* It works well only with positive values,
42-
* if you want to get negative formatted uptime, use some wrapper like getDowntimeFromDateFormatted
42+
* if you want to get negative formatted duration, use some wrapper like getDowntimeFromDateFormatted
4343
*/
44-
export function formatUptimeInSeconds(seconds: number) {
44+
export function formatDurationSeconds(seconds: number) {
4545
if (!isNumeric(seconds)) {
4646
return undefined;
4747
}
@@ -69,8 +69,8 @@ export function formatUptimeInSeconds(seconds: number) {
6969
return sign + value;
7070
}
7171

72-
export const formatMsToUptime = (ms?: number) => {
73-
return formatUptimeInSeconds(Number(ms) / 1000);
72+
export const formatDurationMs = (ms?: number) => {
73+
return formatDurationSeconds(Number(ms) / 1000);
7474
};
7575

7676
export function getUptimeFromDateFormatted(dateFrom?: number | string, dateTo?: number | string) {
@@ -80,7 +80,7 @@ export function getUptimeFromDateFormatted(dateFrom?: number | string, dateTo?:
8080
// Prevent wrong negative uptime values
8181
diff = diff < 0 ? 0 : diff;
8282

83-
return formatUptimeInSeconds(diff);
83+
return formatDurationSeconds(diff);
8484
}
8585

8686
export function getDowntimeFromDateFormatted(dateFrom?: number | string, dateTo?: number | string) {
@@ -90,7 +90,7 @@ export function getDowntimeFromDateFormatted(dateFrom?: number | string, dateTo?
9090
// Prevent wrong negative uptime values
9191
diff = diff < 0 ? 0 : diff;
9292

93-
return formatUptimeInSeconds(-diff);
93+
return formatDurationSeconds(-diff);
9494
}
9595

9696
function calcTimeDiffInSec(

0 commit comments

Comments
 (0)