Skip to content
Merged
Show file tree
Hide file tree
Changes from 10 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
18 changes: 5 additions & 13 deletions src/components/MemoryViewer/MemoryViewer.scss
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
$memory-type-colors: (
'AllocatorCachesMemory': var(--g-color-base-utility-medium-hover),
'SharedCacheConsumption': var(--g-color-base-info-medium-hover),
'MemTableConsumption': var(--g-color-base-warning-medium-hover),
'QueryExecutionConsumption': var(--g-color-base-positive-medium-hover),
'Other': var(--g-color-base-generic-medium-hover),
'AllocatorCachesMemory': var(--g-color-base-danger-medium),
'SharedCacheConsumption': var(--g-color-base-info-medium),
'MemTableConsumption': var(--g-color-base-warning-medium),
'QueryExecutionConsumption': var(--g-color-base-positive-medium),
'Other': var(--g-color-base-neutral-medium),
);

@mixin memory-type-color($type) {
Expand Down Expand Up @@ -31,8 +31,6 @@ $memory-type-colors: (
}

&__container {
display: flex;

padding: 2px 0;
}

Expand Down Expand Up @@ -93,10 +91,4 @@ $memory-type-colors: (
}
}
}

&__text {
display: flex;
justify-content: center;
align-items: center;
}
}
10 changes: 6 additions & 4 deletions src/components/MemoryViewer/MemoryViewer.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {DefinitionList, useTheme} from '@gravity-ui/uikit';
import {DefinitionList, Flex, useTheme} from '@gravity-ui/uikit';

import type {TMemoryStats} from '../../types/api/nodes';
import {formatBytes} from '../../utils/bytesParsers';
Expand Down Expand Up @@ -107,10 +107,10 @@ export function MemoryViewer({
<DefinitionList.Item
key={label}
name={
<div className={b('container')}>
<Flex alignItems="center" gap="1" className={b('container')}>
<div className={b('legend', {type: key})}></div>
<div className={b('name')}>{label}</div>
</div>
</Flex>
}
>
{segmentCapacity ? (
Expand Down Expand Up @@ -161,7 +161,9 @@ export function MemoryViewer({
/>
);
})}
<div className={b('text')}>{renderContent()}</div>
<Flex justifyContent="center" alignItems="center" className={b('text')}>
{renderContent()}
</Flex>
</div>
</div>
</HoverPopup>
Expand Down
3 changes: 2 additions & 1 deletion src/components/MemoryViewer/i18n/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@
"text_usage": "Usage",
"text_soft-limit": "Soft Limit",
"text_hard-limit": "Hard Limit",
"text_other": "Other"
"text_other": "Other",
"text_memory-details": "Memory Details"
}
17 changes: 15 additions & 2 deletions src/components/MemoryViewer/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,31 @@ export function calculateAllocatedMemory(stats: TMemoryStats) {
return String(allocatedMemory + allocatorCaches);
}

export function getMaybeNumber(value: string | number | undefined): number | undefined {
function getMaybeNumber(value: string | number | undefined): number | undefined {
return isNumeric(value) ? parseFloat(String(value)) : undefined;
}

interface MemorySegment {
export interface MemorySegment {
label: string;
key: string;
value: number;
capacity?: number;
isInfo?: boolean;
}

// Memory segment colors using CSS variables for theme support
export const MEMORY_SEGMENT_COLORS: Record<string, string> = {
AllocatorCachesMemory: 'var(--g-color-base-danger-medium)',
SharedCacheConsumption: 'var(--g-color-base-info-medium)',
MemTableConsumption: 'var(--g-color-base-warning-medium)',
QueryExecutionConsumption: 'var(--g-color-base-positive-medium)',
Other: 'var(--g-color-base-neutral-medium)',
};

export function getMemorySegmentColor(key: string): string {
return MEMORY_SEGMENT_COLORS[key] || MEMORY_SEGMENT_COLORS['Other'];
}

export function getMemorySegments(stats: TMemoryStats, memoryUsage: number): MemorySegment[] {
const segments = [
{
Expand Down
25 changes: 25 additions & 0 deletions src/components/ProgressWrapper/ProgressContainer.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import {Flex, Text} from '@gravity-ui/uikit';

import {getProgressStyle} from './progressUtils';
import type {ProgressContainerProps} from './types';

export function ProgressContainer({
children,
displayText,
withCapacityUsage = false,
className,
width,
}: ProgressContainerProps) {
const progressStyle = getProgressStyle(width);

return (
<Flex alignItems="center" gap="2" className={className}>
<div style={progressStyle}>{children}</div>
{withCapacityUsage && displayText && (
<Text variant="body-1" color="secondary">
{displayText}
</Text>
)}
</Flex>
);
}
10 changes: 10 additions & 0 deletions src/components/ProgressWrapper/ProgressWrapper.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import {SingleProgress} from './SingleProgress';
import {StackProgress} from './StackProgress';
import type {ProgressWrapperProps} from './types';

export function ProgressWrapper(props: ProgressWrapperProps) {
if ('stack' in props && props.stack) {
return <StackProgress {...props} />;
}
return <SingleProgress {...props} />;
}
54 changes: 54 additions & 0 deletions src/components/ProgressWrapper/SingleProgress.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import React from 'react';

import {Progress} from '@gravity-ui/uikit';

import {defaultFormatProgressValues} from '../../utils/progress';
import {safeParseNumber} from '../../utils/utils';

import {ProgressContainer} from './ProgressContainer';
import i18n from './i18n';
import {
PROGRESS_SIZE,
calculateProgressWidth,
formatDisplayValues,
formatProgressText,
isValidValue,
} from './progressUtils';
import type {ProgressWrapperSingleProps} from './types';

export function SingleProgress({
value,
capacity,
formatValues = defaultFormatProgressValues,
className,
width,
size = PROGRESS_SIZE,
withCapacityUsage = false,
}: ProgressWrapperSingleProps) {
if (!isValidValue(value)) {
return <div className={className}>{i18n('alert_no-data')}</div>;
}

const numericValue = safeParseNumber(value);
const numericCapacity = safeParseNumber(capacity);
const clampedFillWidth = calculateProgressWidth(numericValue, numericCapacity);

const [valueText, capacityText] = React.useMemo(() => {
return formatDisplayValues(value, capacity, formatValues);
}, [formatValues, value, capacity]);

const displayText = React.useMemo(() => {
return formatProgressText(valueText, capacityText, numericCapacity);
}, [valueText, capacityText, numericCapacity]);

return (
<ProgressContainer
displayText={displayText}
withCapacityUsage={withCapacityUsage}
className={className}
width={width}
>
<Progress value={clampedFillWidth} theme="success" size={size} />
</ProgressContainer>
);
}
72 changes: 72 additions & 0 deletions src/components/ProgressWrapper/StackProgress.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import React from 'react';

import {Progress} from '@gravity-ui/uikit';

import {defaultFormatProgressValues} from '../../utils/progress';
import {safeParseNumber} from '../../utils/utils';
import {getMemorySegmentColor} from '../MemoryViewer/utils';

import {ProgressContainer} from './ProgressContainer';
import i18n from './i18n';
import {
MAX_PERCENTAGE,
PROGRESS_SIZE,
formatDisplayValues,
formatProgressText,
} from './progressUtils';
import type {ProgressWrapperStackProps} from './types';

export function StackProgress({
stack,
totalCapacity,
formatValues = defaultFormatProgressValues,
className,
width,
size = PROGRESS_SIZE,
withCapacityUsage = false,
}: ProgressWrapperStackProps) {
const displaySegments = React.useMemo(() => {
return stack.filter((segment) => !segment.isInfo && segment.value > 0);
}, [stack]);

if (displaySegments.length === 0) {
return <div className={className}>{i18n('alert_no-data')}</div>;
}

const totalValue = React.useMemo(() => {
return displaySegments.reduce((sum, segment) => sum + segment.value, 0);
}, [displaySegments]);

const numericTotalCapacity = React.useMemo(() => {
return safeParseNumber(totalCapacity);
}, [totalCapacity]);

const maxValue = numericTotalCapacity || totalValue;

const stackElements = React.useMemo(() => {
return displaySegments.map((segment) => ({
value: maxValue > 0 ? (segment.value / maxValue) * MAX_PERCENTAGE : 0,
color: getMemorySegmentColor(segment.key),
title: segment.label,
}));
}, [displaySegments, maxValue]);

const [totalValueText, totalCapacityText] = React.useMemo(() => {
return formatDisplayValues(totalValue, numericTotalCapacity || totalValue, formatValues);
}, [formatValues, totalValue, numericTotalCapacity]);

const displayText = React.useMemo(() => {
return formatProgressText(totalValueText, totalCapacityText, numericTotalCapacity || 0);
}, [totalValueText, totalCapacityText, numericTotalCapacity]);

return (
<ProgressContainer
displayText={displayText}
withCapacityUsage={withCapacityUsage}
className={className}
width={width}
>
<Progress value={MAX_PERCENTAGE} stack={stackElements} size={size} />
</ProgressContainer>
);
}
11 changes: 11 additions & 0 deletions src/components/ProgressWrapper/i18n/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import {registerKeysets} from '../../../utils/i18n';

import en from './en.json';

const COMPONENT = 'progress-wrapper';

const keysets = {
en,
};

export default registerKeysets(COMPONENT, keysets);
18 changes: 18 additions & 0 deletions src/components/ProgressWrapper/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Main component - public API
export {ProgressWrapper} from './ProgressWrapper';

// Individual components - for direct usage if needed
export {SingleProgress} from './SingleProgress';
export {StackProgress} from './StackProgress';
export {ProgressContainer} from './ProgressContainer';

// Types - for consumers
export type {
ProgressWrapperProps,
ProgressWrapperSingleProps,
ProgressWrapperStackProps,
ProgressContainerProps,
} from './types';

// Utils - for advanced usage
export * from './progressUtils';
53 changes: 53 additions & 0 deletions src/components/ProgressWrapper/progressUtils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import type {FormatProgressViewerValues} from '../../utils/progress';
import {isNumeric, safeParseNumber} from '../../utils/utils';

import i18n from './i18n';

// Constants that were previously in TenantStorage/constants
export const DEFAULT_PROGRESS_WIDTH = 400;
export const MAX_PERCENTAGE = 100;
export const MIN_PERCENTAGE = 0;
export const PROGRESS_SIZE = 's';

export const isValidValue = (val?: number | string): boolean =>
isNumeric(val) && safeParseNumber(val) >= 0;
Copy link
Contributor

Choose a reason for hiding this comment

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

safeParseNumber(val) always return value >=0, seems to be redundant check

Copy link
Collaborator Author

@astandrik astandrik Jul 28, 2025

Choose a reason for hiding this comment

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

value may be negative


export function calculateProgressWidth(value: number, capacity: number): number {
const rawPercentage =
capacity > 0 ? Math.floor((value / capacity) * MAX_PERCENTAGE) : MAX_PERCENTAGE;
const fillWidth = Math.max(MIN_PERCENTAGE, rawPercentage);
return Math.min(fillWidth, MAX_PERCENTAGE);
}

export function getProgressStyle(width?: number | 'full') {
const isFullWidth = width === 'full';
const validatedWidth = isFullWidth ? 0 : Math.max(0, width || DEFAULT_PROGRESS_WIDTH);

return {
width: isFullWidth ? '100%' : `${validatedWidth}px`,
flex: isFullWidth ? '1' : 'none',
};
}

export function formatProgressText(
valueText: string | number | undefined,
capacityText: string | number | undefined,
numericCapacity: number,
): string {
if (numericCapacity <= 0) {
return String(valueText);
}
return i18n('context_capacity-usage', {value: valueText, capacity: capacityText});
}

export function formatDisplayValues(
value: number | string | undefined,
capacity: number | string | undefined,
formatValues?: FormatProgressViewerValues,
): [string | number | undefined, string | number | undefined] {
if (formatValues) {
const result = formatValues(Number(value), Number(capacity));
return [result[0], result[1]] as [string | number | undefined, string | number | undefined];
}
return [value, capacity];
}
35 changes: 35 additions & 0 deletions src/components/ProgressWrapper/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import type {ProgressSize} from '@gravity-ui/uikit';

import type {FormatProgressViewerValues} from '../../utils/progress';
import type {MemorySegment} from '../MemoryViewer/utils';

export interface ProgressWrapperBaseProps {
formatValues?: FormatProgressViewerValues;
className?: string;
width?: number | 'full';
size?: ProgressSize;
withCapacityUsage?: boolean;
}

export interface ProgressWrapperSingleProps extends ProgressWrapperBaseProps {
value?: number | string;
capacity?: number | string;
stack?: never;
}

export interface ProgressWrapperStackProps extends ProgressWrapperBaseProps {
stack: MemorySegment[];
totalCapacity?: number | string;
value?: never;
capacity?: never;
}

export type ProgressWrapperProps = ProgressWrapperSingleProps | ProgressWrapperStackProps;

export interface ProgressContainerProps {
children: React.ReactNode;
displayText?: string;
withCapacityUsage?: boolean;
className?: string;
width?: number | 'full';
}
Loading
Loading