Skip to content

Commit

Permalink
fix: min visible share
Browse files Browse the repository at this point in the history
  • Loading branch information
astandrik committed Nov 13, 2024
1 parent 45041ca commit 4595c07
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 8 deletions.
39 changes: 32 additions & 7 deletions src/components/MemoryViewer/MemoryViewer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,38 @@ import {DefinitionList, useTheme} from '@gravity-ui/uikit';
import type {TMemoryStats} from '../../types/api/nodes';
import {formatBytes} from '../../utils/bytesParsers';
import {cn} from '../../utils/cn';
import {GIGABYTE} from '../../utils/constants';
import {calculateProgressStatus} from '../../utils/progress';
import {isNumeric} from '../../utils/utils';
import {HoverPopup} from '../HoverPopup/HoverPopup';
import type {FormatProgressViewerValues} from '../ProgressViewer/ProgressViewer';
import {ProgressViewer} from '../ProgressViewer/ProgressViewer';

import {getMemorySegments} from './utils';

import './MemoryViewer.scss';

const MIN_VISIBLE_MEMORY_SHARE = 1;
const MIN_VISIBLE_MEMORY_VALUE = 0.01 * GIGABYTE;

const b = cn('memory-viewer');

type FormatProgressViewerValues = (
value?: number,
capacity?: number,
) => (string | number | undefined)[];
const formatDetailedValues: FormatProgressViewerValues = (value, total) => {
return [
formatBytes({
value,
size: 'gb',
withSizeLabel: false,
precision: 2,
}),
formatBytes({
value: total,
size: 'gb',
withSizeLabel: true,
precision: 1,
}),
];
};

export interface MemoryProgressViewerProps {
stats: TMemoryStats;
Expand Down Expand Up @@ -100,7 +117,7 @@ export function MemoryViewer({
<ProgressViewer
value={segmentSize}
capacity={segmentCapacity}
formatValues={formatValues}
formatValues={formatDetailedValues}
colorizeProgress
/>
) : (
Expand All @@ -122,15 +139,23 @@ export function MemoryViewer({
{memorySegments
.filter(({isInfo}) => !isInfo)
.map((segment) => {
if (segment.value < MIN_VISIBLE_MEMORY_VALUE) {
return null;
}

const currentMemoryShare = Math.max(
calculateMemoryShare(segment.value),
MIN_VISIBLE_MEMORY_SHARE,
);
const position = currentPosition;
currentPosition += calculateMemoryShare(segment.value);
currentPosition += currentMemoryShare;

return (
<div
key={segment.key}
className={b('segment', {type: segment.key})}
style={{
width: `${calculateMemoryShare(segment.value).toFixed(2)}%`,
width: `${currentMemoryShare}%`,
left: `${position}%`,
}}
/>
Expand Down
2 changes: 1 addition & 1 deletion src/components/ProgressViewer/ProgressViewer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const b = cn('progress-viewer');

type ProgressViewerSize = 'xs' | 's' | 'ns' | 'm' | 'n' | 'l' | 'head';

type FormatProgressViewerValues = (
export type FormatProgressViewerValues = (
value?: number,
capacity?: number,
) => (string | number | undefined)[];
Expand Down

0 comments on commit 4595c07

Please sign in to comment.