Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/components/VDiskInfo/VDiskInfo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import {getPDiskPagePath, useVDiskPagePath} from '../../routes';
import {EVDiskState} from '../../types/api/vdisk';
import {cn} from '../../utils/cn';
import {
formatDurationSeconds,
formatStorageValuesToGb,
formatUptimeInSeconds,
} from '../../utils/dataFormatters/dataFormatters';
import {createVDiskDeveloperUILink} from '../../utils/developerUI/developerUI';
import {getSeverityColor} from '../../utils/disks/helpers';
Expand Down Expand Up @@ -165,7 +165,7 @@ export function VDiskInfo<T extends PreparedVDisk>({
});
}
if (!isNil(ReplicationSecondsRemaining)) {
const timeRemaining = formatUptimeInSeconds(ReplicationSecondsRemaining);
const timeRemaining = formatDurationSeconds(ReplicationSecondsRemaining);
if (timeRemaining) {
rightColumn.push({
label: vDiskInfoKeyset('replication-time-remaining'),
Expand Down
4 changes: 2 additions & 2 deletions src/components/VDiskPopup/VDiskPopup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {EFlag} from '../../types/api/enums';
import {EVDiskState} from '../../types/api/vdisk';
import {cn} from '../../utils/cn';
import {EMPTY_DATA_PLACEHOLDER} from '../../utils/constants';
import {formatUptimeInSeconds} from '../../utils/dataFormatters/dataFormatters';
import {formatDurationSeconds} from '../../utils/dataFormatters/dataFormatters';
import {createVDiskDeveloperUILink} from '../../utils/developerUI/developerUI';
import {getStateSeverity} from '../../utils/disks/calculateVDiskSeverity';
import {
Expand Down Expand Up @@ -236,7 +236,7 @@ const prepareVDiskData = (
}

if (!isNil(ReplicationSecondsRemaining)) {
const timeRemaining = formatUptimeInSeconds(ReplicationSecondsRemaining);
const timeRemaining = formatDurationSeconds(ReplicationSecondsRemaining);
if (timeRemaining) {
vdiskData.push({
name: vDiskPopupKeyset('label_remaining'),
Expand Down
11 changes: 4 additions & 7 deletions src/containers/Operations/columns.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import {duration} from '@gravity-ui/date-utils';
import {Ban, CircleStop} from '@gravity-ui/icons';
import type {Column as DataTableColumn} from '@gravity-ui/react-data-table';
import {ActionTooltip, Flex, Icon, Text} from '@gravity-ui/uikit';
Expand All @@ -8,9 +7,9 @@ import {CellWithPopover} from '../../components/CellWithPopover/CellWithPopover'
import {operationsApi} from '../../store/reducers/operations';
import type {IndexBuildMetadata, OperationKind, TOperation} from '../../types/api/operations';
import {EStatusCode} from '../../types/api/operations';
import {EMPTY_DATA_PLACEHOLDER, HOUR_IN_SECONDS, SECOND_IN_MS} from '../../utils/constants';
import {EMPTY_DATA_PLACEHOLDER} from '../../utils/constants';
import createToast from '../../utils/createToast';
import {formatDateTime} from '../../utils/dataFormatters/dataFormatters';
import {formatDateTime, formatDurationMs} from '../../utils/dataFormatters/dataFormatters';
import {parseProtobufTimestampToMs} from '../../utils/timeParsers';

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

const durationFormatted =
durationValue > HOUR_IN_SECONDS * SECOND_IN_MS
? duration(durationValue).format('hh:mm:ss')
: duration(durationValue).format('mm:ss');
const safeDurationMs = durationValue < 0 ? 0 : durationValue;
const durationFormatted = formatDurationMs(safeDurationMs);

return row.end_time
? durationFormatted
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {SpeedMultiMeter} from '../../../../../components/SpeedMultiMeter';
import type {IPreparedTopicStats} from '../../../../../types/store/topic';
import {cn} from '../../../../../utils/cn';
import {formatMsToUptime} from '../../../../../utils/dataFormatters/dataFormatters';
import {formatDurationMs} from '../../../../../utils/dataFormatters/dataFormatters';

import './ConsumersTopicStats.scss';

Expand All @@ -21,11 +21,11 @@ export const ConsumersTopicStats = ({data}: ConsumersTopicStatsProps) => {
},
{
label: 'Write lag',
value: formatMsToUptime(partitionsWriteLag || 0),
value: formatDurationMs(partitionsWriteLag || 0),
},
{
label: 'Write idle time',
value: formatMsToUptime(partitionsIdleTime || 0),
value: formatDurationMs(partitionsIdleTime || 0),
},
];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {getTenantPath} from '../../../../../routes';
import {TENANT_DIAGNOSTICS_TABS_IDS} from '../../../../../store/reducers/tenant/constants';
import type {IPreparedConsumerData} from '../../../../../types/store/topic';
import {cn} from '../../../../../utils/cn';
import {formatMsToUptime} from '../../../../../utils/dataFormatters/dataFormatters';
import {formatDurationMs} from '../../../../../utils/dataFormatters/dataFormatters';
import {TenantTabsGroups} from '../../../TenantPages';
import {ReadLagsHeader} from '../Headers';
import {
Expand Down Expand Up @@ -57,23 +57,23 @@ export const columns: Column<IPreparedConsumerData>[] = [
CONSUMERS_READ_LAGS_SUB_COLUMNS_IDS.WRITE_LAG
],
align: DataTable.RIGHT,
render: ({row}) => formatMsToUptime(row.writeLag),
render: ({row}) => formatDurationMs(row.writeLag),
},
{
name: CONSUMERS_READ_LAGS_SUB_COLUMNS_IDS.READ_LAG,
header: CONSUMERS_READ_LAGS_SUB_COLUMNS_TITLES[
CONSUMERS_READ_LAGS_SUB_COLUMNS_IDS.READ_LAG
],
align: DataTable.RIGHT,
render: ({row}) => formatMsToUptime(row.readLag),
render: ({row}) => formatDurationMs(row.readLag),
},
{
name: CONSUMERS_READ_LAGS_SUB_COLUMNS_IDS.READ_IDLE_TIME,
header: CONSUMERS_READ_LAGS_SUB_COLUMNS_TITLES[
CONSUMERS_READ_LAGS_SUB_COLUMNS_IDS.READ_IDLE_TIME
],
align: DataTable.RIGHT,
render: ({row}) => formatMsToUptime(row.readIdleTime),
render: ({row}) => formatDurationMs(row.readIdleTime),
},
],
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {useTopicDataAvailable} from '../../../../../store/reducers/capabilities/
import {TENANT_DIAGNOSTICS_TABS_IDS} from '../../../../../store/reducers/tenant/constants';
import {cn} from '../../../../../utils/cn';
import {EMPTY_DATA_PLACEHOLDER} from '../../../../../utils/constants';
import {formatBytes, formatMsToUptime} from '../../../../../utils/dataFormatters/dataFormatters';
import {formatBytes, formatDurationMs} from '../../../../../utils/dataFormatters/dataFormatters';
import {isNumeric} from '../../../../../utils/utils';
import {useDiagnosticsPageLinkGetter} from '../../DiagnosticsPages';
import {
Expand Down Expand Up @@ -86,15 +86,15 @@ export const allColumns: Column<PreparedPartitionDataWithHosts>[] = [
PARTITIONS_WRITE_LAGS_SUB_COLUMNS_IDS.PARTITION_WRITE_LAG
],
align: DataTable.RIGHT,
render: ({row}) => formatMsToUptime(row.partitionWriteLag),
render: ({row}) => formatDurationMs(row.partitionWriteLag),
},
{
name: PARTITIONS_WRITE_LAGS_SUB_COLUMNS_IDS.PARTITION_WRITE_IDLE_TIME,
header: PARTITIONS_WRITE_LAGS_SUB_COLUMNS_TITLES[
PARTITIONS_WRITE_LAGS_SUB_COLUMNS_IDS.PARTITION_WRITE_IDLE_TIME
],
align: DataTable.RIGHT,
render: ({row}) => formatMsToUptime(row.partitionWriteIdleTime),
render: ({row}) => formatDurationMs(row.partitionWriteIdleTime),
},
],
},
Expand All @@ -109,23 +109,23 @@ export const allColumns: Column<PreparedPartitionDataWithHosts>[] = [
PARTITIONS_READ_LAGS_SUB_COLUMNS_IDS.CONSUMER_WRITE_LAG
],
align: DataTable.RIGHT,
render: ({row}) => formatMsToUptime(row.consumerWriteLag),
render: ({row}) => formatDurationMs(row.consumerWriteLag),
},
{
name: PARTITIONS_READ_LAGS_SUB_COLUMNS_IDS.CONSUMER_READ_LAG,
header: PARTITIONS_READ_LAGS_SUB_COLUMNS_TITLES[
PARTITIONS_READ_LAGS_SUB_COLUMNS_IDS.CONSUMER_READ_LAG
],
align: DataTable.RIGHT,
render: ({row}) => formatMsToUptime(row.consumerReadLag),
render: ({row}) => formatDurationMs(row.consumerReadLag),
},
{
name: PARTITIONS_READ_LAGS_SUB_COLUMNS_IDS.CONSUMER_READ_IDLE_TIME,
header: PARTITIONS_READ_LAGS_SUB_COLUMNS_TITLES[
PARTITIONS_READ_LAGS_SUB_COLUMNS_IDS.CONSUMER_READ_IDLE_TIME
],
align: DataTable.RIGHT,
render: ({row}) => formatMsToUptime(row.consumerReadIdleTime),
render: ({row}) => formatDurationMs(row.consumerReadIdleTime),
},
],
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {UNBREAKABLE_GAP} from '../../utils';
import {
formatUptimeInSeconds,
formatDurationSeconds,
getDowntimeFromDateFormatted,
getUptimeFromDateFormatted,
} from '../dataFormatters';
Expand All @@ -24,46 +24,46 @@ describe('getDowntimeFromDateFormatted', () => {
expect(getDowntimeFromDateFormatted(3_600_000, 3_599_000)).toBe('0s');
});
});
describe('formatUptimeInSeconds', () => {
describe('formatDurationSeconds', () => {
const M = 60;
const H = 60 * M;
const D = 24 * H;

test('should return days if value is more than 24h', () => {
expect(formatUptimeInSeconds(24 * H)).toBe('1d' + UNBREAKABLE_GAP + '00:00:00');
expect(formatUptimeInSeconds(D + H + M + 12)).toBe('1d' + UNBREAKABLE_GAP + '01:01:12');
expect(formatDurationSeconds(24 * H)).toBe('1d' + UNBREAKABLE_GAP + '00:00:00');
expect(formatDurationSeconds(D + H + M + 12)).toBe('1d' + UNBREAKABLE_GAP + '01:01:12');
// 1 week
expect(formatUptimeInSeconds(7 * D + H + M + 12)).toBe('7d' + UNBREAKABLE_GAP + '01:01:12');
expect(formatDurationSeconds(7 * D + H + M + 12)).toBe('7d' + UNBREAKABLE_GAP + '01:01:12');
// 1 month
expect(formatUptimeInSeconds(30 * D + 11 * H + 30 * M + 12)).toBe(
expect(formatDurationSeconds(30 * D + 11 * H + 30 * M + 12)).toBe(
'30d' + UNBREAKABLE_GAP + '11:30:12',
);
expect(formatUptimeInSeconds(1234 * D + 12 * H + 12 * M + 12)).toBe(
expect(formatDurationSeconds(1234 * D + 12 * H + 12 * M + 12)).toBe(
'1234d' + UNBREAKABLE_GAP + '12:12:12',
);
});
test('should return hours if value is less than 24h', () => {
expect(formatUptimeInSeconds(H + M + 12)).toBe('1:01:12');
expect(formatUptimeInSeconds(12 * H + 12 * M + 12)).toBe('12:12:12');
expect(formatDurationSeconds(H + M + 12)).toBe('1:01:12');
expect(formatDurationSeconds(12 * H + 12 * M + 12)).toBe('12:12:12');
});
test('should return minutes if value is less than hour', () => {
expect(formatUptimeInSeconds(M + 12)).toBe('1:12');
expect(formatUptimeInSeconds(12 * M + 2)).toBe('12:02');
expect(formatDurationSeconds(M + 12)).toBe('1:12');
expect(formatDurationSeconds(12 * M + 2)).toBe('12:02');
});
test('should return second if value is less than hour', () => {
expect(formatUptimeInSeconds(12)).toBe('12s');
expect(formatUptimeInSeconds(2)).toBe('2s');
expect(formatDurationSeconds(12)).toBe('12s');
expect(formatDurationSeconds(2)).toBe('2s');
});
test('should correctly process negative values', () => {
expect(formatUptimeInSeconds(-0)).toBe('0s');
expect(formatUptimeInSeconds(-12)).toBe('-12s');
expect(formatUptimeInSeconds(-1 * (12 * M + 2))).toBe('-12:02');
expect(formatUptimeInSeconds(-1 * (12 * H + 12 * M + 12))).toBe('-12:12:12');
expect(formatUptimeInSeconds(-1 * (12 * D + 12 * H + 12 * M + 12))).toBe(
expect(formatDurationSeconds(-0)).toBe('0s');
expect(formatDurationSeconds(-12)).toBe('-12s');
expect(formatDurationSeconds(-1 * (12 * M + 2))).toBe('-12:02');
expect(formatDurationSeconds(-1 * (12 * H + 12 * M + 12))).toBe('-12:12:12');
expect(formatDurationSeconds(-1 * (12 * D + 12 * H + 12 * M + 12))).toBe(
'-12d' + UNBREAKABLE_GAP + '12:12:12',
);
});
test('should return empty placeholder on NaN', () => {
expect(formatUptimeInSeconds(Number.NaN)).toBe(undefined);
expect(formatDurationSeconds(Number.NaN)).toBe(undefined);
});
});
12 changes: 6 additions & 6 deletions src/utils/dataFormatters/dataFormatters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ export const stringifyVdiskId = (id?: TVDiskID | TVSlotId) => {

/**
* It works well only with positive values,
* if you want to get negative formatted uptime, use some wrapper like getDowntimeFromDateFormatted
* if you want to get negative formatted duration, use some wrapper like getDowntimeFromDateFormatted
*/
export function formatUptimeInSeconds(seconds: number) {
export function formatDurationSeconds(seconds: number) {
if (!isNumeric(seconds)) {
return undefined;
}
Expand Down Expand Up @@ -69,8 +69,8 @@ export function formatUptimeInSeconds(seconds: number) {
return sign + value;
}

export const formatMsToUptime = (ms?: number) => {
return formatUptimeInSeconds(Number(ms) / 1000);
export const formatDurationMs = (ms?: number) => {
return formatDurationSeconds(Number(ms) / 1000);
};

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

return formatUptimeInSeconds(diff);
return formatDurationSeconds(diff);
}

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

return formatUptimeInSeconds(-diff);
return formatDurationSeconds(-diff);
}

function calcTimeDiffInSec(
Expand Down
Loading