Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: show negative uptime for nodes when disconnected #1740

Merged
merged 2 commits into from
Dec 10, 2024
Merged
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
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import type {TTabletStateInfo} from '../../../types/api/tablet';
import {calcUptime} from '../../../utils/dataFormatters/dataFormatters';
import {getUptimeFromDateFormatted} from '../../../utils/dataFormatters/dataFormatters';
import {InfoViewer, createInfoFormatter, formatObject} from '../../InfoViewer';

const formatTablet = createInfoFormatter<TTabletStateInfo>({
values: {
ChangeTime: (value) => calcUptime(value),
ChangeTime: (value) => getUptimeFromDateFormatted(value),
},
labels: {
TabletId: 'Tablet',
Expand Down
2 changes: 1 addition & 1 deletion src/components/nodesColumns/columns.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ export function getUptimeColumn<T extends {StartTime?: string; Uptime?: string}>
sortAccessor: ({StartTime}) => (StartTime ? -StartTime : 0),
render: ({row}) => row.Uptime,
align: DataTable.RIGHT,
width: 110,
width: 120,
};
}

Expand Down
2 changes: 1 addition & 1 deletion src/components/nodesColumns/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ export const NODES_COLUMNS_TO_DATA_FIELDS: Record<NodesColumnId, NodesRequiredFi
DC: ['DC'],
Rack: ['Rack'],
Version: ['Version'],
Uptime: ['Uptime'],
Uptime: ['Uptime', 'DisconnectTime'],
Memory: ['Memory', 'MemoryDetailed'],
RAM: ['Memory'],
Pools: ['CPU'],
Expand Down
7 changes: 5 additions & 2 deletions src/containers/Tablet/components/TabletInfo/TabletInfo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {selectIsUserAllowedToMakeChanges} from '../../../../store/reducers/authe
import {ETabletState} from '../../../../types/api/tablet';
import type {TTabletStateInfo} from '../../../../types/api/tablet';
import {cn} from '../../../../utils/cn';
import {calcUptime} from '../../../../utils/dataFormatters/dataFormatters';
import {getUptimeFromDateFormatted} from '../../../../utils/dataFormatters/dataFormatters';
import {createTabletDeveloperUIHref} from '../../../../utils/developerUI/developerUI';
import {useTypedSelector} from '../../../../utils/hooks';
import {getDefaultNodePath} from '../../../Node/NodePages';
Expand Down Expand Up @@ -70,7 +70,10 @@ export const TabletInfo = ({tablet}: TabletInfoProps) => {
tabletInfo.push({label: tabletInfoKeyset('field_state'), value: <TabletState state={State} />});

if (hasUptime) {
tabletInfo.push({label: tabletInfoKeyset('field_uptime'), value: calcUptime(ChangeTime)});
tabletInfo.push({
label: tabletInfoKeyset('field_uptime'),
value: getUptimeFromDateFormatted(ChangeTime),
});
}

tabletInfo.push(
Expand Down
4 changes: 2 additions & 2 deletions src/containers/Tablet/components/TabletTable/TabletTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {InternalLink} from '../../../../components/InternalLink/InternalLink';
import {ResizeableDataTable} from '../../../../components/ResizeableDataTable/ResizeableDataTable';
import {TabletState} from '../../../../components/TabletState/TabletState';
import type {ITabletPreparedHistoryItem} from '../../../../types/store/tablet';
import {calcUptime} from '../../../../utils/dataFormatters/dataFormatters';
import {getUptimeFromDateFormatted} from '../../../../utils/dataFormatters/dataFormatters';
import {getDefaultNodePath} from '../../../Node/NodePages';

const TABLET_COLUMNS_WIDTH_LS_KEY = 'tabletTableColumnsWidth';
Expand All @@ -21,7 +21,7 @@ const columns: Column<ITabletPreparedHistoryItem>[] = [
name: 'Change time',
align: DataTable.RIGHT,
sortable: false,
render: ({row}) => calcUptime(row.changeTime),
render: ({row}) => getUptimeFromDateFormatted(row.changeTime),
},
{
name: 'State',
Expand Down
4 changes: 2 additions & 2 deletions src/containers/Tablets/TabletsTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {tabletApi} from '../../store/reducers/tablet';
import {ETabletState} from '../../types/api/tablet';
import type {TTabletStateInfo} from '../../types/api/tablet';
import {DEFAULT_TABLE_SETTINGS, EMPTY_DATA_PLACEHOLDER} from '../../utils/constants';
import {calcUptime} from '../../utils/dataFormatters/dataFormatters';
import {getUptimeFromDateFormatted} from '../../utils/dataFormatters/dataFormatters';
import {useTypedSelector} from '../../utils/hooks';
import {getDefaultNodePath} from '../Node/NodePages';

Expand Down Expand Up @@ -97,7 +97,7 @@ function getColumns({database}: {database?: string}) {
return i18n('Uptime');
},
render: ({row}) => {
return calcUptime(row.ChangeTime);
return getUptimeFromDateFormatted(row.ChangeTime);
},
sortAccessor: (row) => -Number(row.ChangeTime),
align: 'right',
Expand Down
4 changes: 2 additions & 2 deletions src/store/reducers/nodes/selectors.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {HOUR_IN_SECONDS} from '../../../utils/constants';
import {calcUptimeInSeconds} from '../../../utils/dataFormatters/dataFormatters';
import {calcTimeDiffInSec} from '../../../utils/dataFormatters/dataFormatters';
import {NodesUptimeFilterValues} from '../../../utils/nodes';

// ==== Filters ====
Expand All @@ -12,6 +12,6 @@ export const filterNodesByUptime = <T extends {StartTime?: string}>(
return nodesList;
}
return nodesList.filter(({StartTime}) => {
return !StartTime || calcUptimeInSeconds(StartTime) < HOUR_IN_SECONDS;
return !StartTime || calcTimeDiffInSec(StartTime) < HOUR_IN_SECONDS;
});
};
21 changes: 21 additions & 0 deletions src/utils/dataFormatters/__test__/formatUptime.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import {getDowntimeFromDateFormatted, getUptimeFromDateFormatted} from '../dataFormatters';

describe('getUptimeFromDateFormatted', () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's add test for downtime!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for comment, found a bug with downtime formatting

it('should calculate and format uptime', () => {
expect(getUptimeFromDateFormatted(3_600_000, 7_200_000)).toBe('1:00:00');
});
it('should return 0 if dateFrom after dateTo', () => {
expect(getUptimeFromDateFormatted(3_600_000, 3_599_000)).toBe('0:00:00');
});
});
describe('getDowntimeFromDateFormatted', () => {
it('should calculate and format downtime as -uptime', () => {
expect(getDowntimeFromDateFormatted(3_600_000, 7_200_000)).toBe('-1:00:00');
});
it('should not add sign if downtime is 0', () => {
expect(getDowntimeFromDateFormatted(3_600_000, 3_600_000)).toBe('0:00:00');
});
it('should return 0 if dateFrom after dateTo', () => {
expect(getDowntimeFromDateFormatted(3_600_000, 3_599_000)).toBe('0:00:00');
});
});
46 changes: 34 additions & 12 deletions src/utils/dataFormatters/dataFormatters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export const stringifyVdiskId = (id?: TVDiskID | TVSlotId) => {
return id ? Object.values(id).join('-') : '';
};

export const formatUptime = (seconds: number) => {
export const formatUptimeInSeconds = (seconds: number) => {
const days = Math.floor(seconds / DAY_IN_SECONDS);
const remain = seconds % DAY_IN_SECONDS;

Expand All @@ -52,9 +52,41 @@ export const formatUptime = (seconds: number) => {
};

export const formatMsToUptime = (ms?: number) => {
return ms && formatUptime(ms / 1000);
return ms && formatUptimeInSeconds(ms / 1000);
};

export function getUptimeFromDateFormatted(dateFrom?: number | string, dateTo?: number | string) {
let diff = calcTimeDiffInSec(dateFrom, dateTo);

// Our time and server time could differ a little
// Prevent wrong negative uptime values
diff = diff < 0 ? 0 : diff;

return formatUptimeInSeconds(diff);
}

export function getDowntimeFromDateFormatted(dateFrom?: number | string, dateTo?: number | string) {
let diff = calcTimeDiffInSec(dateFrom, dateTo);

// Our time and server time could differ a little
// Prevent wrong negative uptime values
diff = diff < 0 ? 0 : diff;

const formattedUptime = formatUptimeInSeconds(diff);

// Do not add sign to 0 values to prevent -0:00:00 uptime
return diff === 0 ? formattedUptime : '-' + formattedUptime;
}

export function calcTimeDiffInSec(
dateFrom?: number | string,
dateTo: number | string = new Date().getTime(),
) {
const diffMs = Number(dateTo) - Number(dateFrom);

return diffMs / 1000;
}

export function formatStorageValues(
value?: number,
total?: number,
Expand Down Expand Up @@ -175,16 +207,6 @@ export const formatTimestamp = (value?: string | number, defaultValue = '') => {
return formattedData ?? defaultValue;
};

export const calcUptimeInSeconds = (milliseconds: number | string) => {
const currentDate = new Date();
const diff = currentDate.getTime() - Number(milliseconds);
return diff <= 0 ? 0 : diff / 1000;
};

export const calcUptime = (milliseconds?: number | string) => {
return formatUptime(calcUptimeInSeconds(Number(milliseconds)));
};

export function getStringifiedData(value: unknown) {
if (value === undefined) {
return '';
Expand Down
19 changes: 14 additions & 5 deletions src/utils/nodes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ import type {TNodeInfo} from '../types/api/nodesList';
import type {NodeHostsMap} from '../types/store/nodesList';

import {HOUR_IN_SECONDS} from './constants';
import {calcUptime} from './dataFormatters/dataFormatters';
import {
getDowntimeFromDateFormatted,
getUptimeFromDateFormatted,
} from './dataFormatters/dataFormatters';

import {valueIsDefined} from '.';

Expand Down Expand Up @@ -63,15 +66,21 @@ export interface PreparedNodeSystemState extends TSystemStateInfo {
SharedCacheUsed?: number;
}

export const prepareNodeSystemState = (
export function prepareNodeSystemState(
systemState: TSystemStateInfo = {},
): PreparedNodeSystemState => {
): PreparedNodeSystemState {
// There is no Rack in Location field for din nodes
const Rack = systemState.Location?.Rack || systemState.Rack;
const DC = systemState.Location?.DataCenter || systemState.DataCenter;
const TenantName = systemState?.Tenants?.[0];

const Uptime = calcUptime(systemState.StartTime);
let Uptime: string;

if (systemState.DisconnectTime) {
Uptime = getDowntimeFromDateFormatted(systemState.DisconnectTime);
} else {
Uptime = getUptimeFromDateFormatted(systemState.StartTime);
}

const LoadAveragePercents = calculateLoadAveragePercents(systemState);

Expand All @@ -91,7 +100,7 @@ export const prepareNodeSystemState = (
SharedCacheLimit,
SharedCacheUsed,
};
};
}

export const getProblemParamValue = (problemFilter: ProblemFilterValue | undefined) => {
return problemFilter === ProblemFilterValues.PROBLEMS;
Expand Down
Loading