Skip to content

feat: render per component memory consumption #1574

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

Merged
merged 16 commits into from
Nov 13, 2024
104 changes: 104 additions & 0 deletions src/components/MemoryViewer/MemoryViewer.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
@import '../../styles/mixins.scss';

$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-neutral-light-hover),
);

@mixin memory-type-color($type) {
background-color: map-get($memory-type-colors, $type);
}

.memory-viewer {
$block: &;

position: relative;
z-index: 0;

min-width: 150px;
padding: 0 var(--g-spacing-1);

&__progress-container {
position: relative;

overflow: hidden;

height: 20px;

border-radius: 2px;
background: var(--g-color-base-generic);
}

&__container {
display: flex;

padding: 2px 0;
}

&__legend {
position: absolute;
bottom: 2px;

width: 20px;
height: 20px;

border-radius: 2px;

@each $type, $color in $memory-type-colors {
&_type_#{$type} {
@include memory-type-color($type);
}
}
}

&__segment {
position: absolute;

height: 100%;

@each $type, $color in $memory-type-colors {
&_type_#{$type} {
@include memory-type-color($type);
}
}
}

&__name {
padding-left: 28px;
}

&_theme_dark {
color: var(--g-color-text-light-primary);

#{$block}__segment {
opacity: 0.75;
}
}

&_status {
&_good {
#{$block}__progress-container {
background-color: var(--g-color-base-positive-light);
}
}
&_warning {
#{$block}__progress-container {
background-color: var(--g-color-base-yellow-light);
}
}
&_danger {
#{$block}__progress-container {
background-color: var(--g-color-base-danger-light);
}
}
}

&__text {
display: flex;
justify-content: center;
align-items: center;
}
}
169 changes: 169 additions & 0 deletions src/components/MemoryViewer/MemoryViewer.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
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');

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;
className?: string;
warningThreshold?: number;
value?: number | string;
capacity?: number | string;
formatValues: FormatProgressViewerValues;
percents?: boolean;
dangerThreshold?: number;
}

export function MemoryViewer({
stats,
value,
capacity,
percents,
formatValues,
className,
warningThreshold = 60,
dangerThreshold = 80,
}: MemoryProgressViewerProps) {
const theme = useTheme();
let fillWidth =
Math.round((parseFloat(String(value)) / parseFloat(String(capacity))) * 100) || 0;
fillWidth = fillWidth > 100 ? 100 : fillWidth;
let valueText: number | string | undefined = value,
capacityText: number | string | undefined = capacity,
divider = '/';
if (percents) {
valueText = fillWidth + '%';
capacityText = '';
divider = '';
} else if (formatValues) {
[valueText, capacityText] = formatValues(Number(value), Number(capacity));
}

const renderContent = () => {
if (isNumeric(capacity)) {
return `${valueText} ${divider} ${capacityText}`;
}

return valueText;
};

const calculateMemoryShare = (segmentSize: number) => {
if (!value) {
return 0;
}
return (segmentSize / parseFloat(String(capacity))) * 100;
};

const memorySegments = getMemorySegments(stats);

const status = calculateProgressStatus({
fillWidth,
warningThreshold,
dangerThreshold,
colorizeProgress: true,
});

let currentPosition = 0;

return (
<HoverPopup
popupContent={
<DefinitionList responsive>
{memorySegments.map(
({label, value: segmentSize, capacity: segmentCapacity, key}) => (
<DefinitionList.Item
key={label}
name={
<div className={b('container')}>
<div className={b('legend', {type: key})}></div>
<div className={b('name')}>{label}</div>
</div>
}
>
{segmentCapacity ? (
<ProgressViewer
value={segmentSize}
capacity={segmentCapacity}
formatValues={formatDetailedValues}
colorizeProgress
/>
) : (
formatBytes({
value: segmentSize,
size: 'gb',
withSizeLabel: true,
precision: 2,
})
)}
</DefinitionList.Item>
),
)}
</DefinitionList>
}
>
<div className={b({theme, status}, className)}>
<div className={b('progress-container')}>
{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 += currentMemoryShare;

return (
<div
key={segment.key}
className={b('segment', {type: segment.key})}
style={{
width: `${currentMemoryShare}%`,
left: `${position}%`,
}}
/>
);
})}
<div className={b('text')}>{renderContent()}</div>
</div>
</div>
</HoverPopup>
);
}
11 changes: 11 additions & 0 deletions src/components/MemoryViewer/i18n/en.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"text_external-consumption": "External Consumption",
"text_allocator-caches": "Allocator Caches",
"text_shared-cache": "Shared Cache",
"text_memtable": "MemTable",
"text_query-execution": "Query Execution",
"text_usage": "Usage",
"text_soft-limit": "Soft Limit",
"text_hard-limit": "Hard Limit",
"text_other": "Other"
}
7 changes: 7 additions & 0 deletions src/components/MemoryViewer/i18n/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import {registerKeysets} from '../../../utils/i18n';

import en from './en.json';

const COMPONENT = 'ydb-memory-viewer';

export default registerKeysets(COMPONENT, {en});
95 changes: 95 additions & 0 deletions src/components/MemoryViewer/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
import type {TMemoryStats} from '../../types/api/nodes';
import {isNumeric} from '../../utils/utils';

import i18n from './i18n';

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

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

export function getMemorySegments(stats: TMemoryStats): MemorySegment[] {
const segments = [
{
label: i18n('text_shared-cache'),
key: 'SharedCacheConsumption',
value: getMaybeNumber(stats.SharedCacheConsumption),
capacity: getMaybeNumber(stats.SharedCacheLimit),
isInfo: false,
},
{
label: i18n('text_query-execution'),
key: 'QueryExecutionConsumption',
value: getMaybeNumber(stats.QueryExecutionConsumption),
capacity: getMaybeNumber(stats.QueryExecutionLimit),
isInfo: false,
},
{
label: i18n('text_memtable'),
key: 'MemTableConsumption',
value: getMaybeNumber(stats.MemTableConsumption),
capacity: getMaybeNumber(stats.MemTableLimit),
isInfo: false,
},
{
label: i18n('text_allocator-caches'),
key: 'AllocatorCachesMemory',
value: getMaybeNumber(stats.AllocatorCachesMemory),
isInfo: false,
},
];

const nonInfoSegments = segments.filter(
(segment) => segment.value !== undefined,
) as MemorySegment[];
const sumNonInfoSegments = nonInfoSegments.reduce((acc, segment) => acc + segment.value, 0);

const totalMemory = getMaybeNumber(stats.AnonRss);

if (totalMemory) {
const otherMemory = Math.max(0, totalMemory - sumNonInfoSegments);

segments.push({
label: i18n('text_other'),
key: 'Other',
value: otherMemory,
isInfo: false,
});
}

segments.push(
{
label: i18n('text_external-consumption'),
key: 'ExternalConsumption',
value: getMaybeNumber(stats.ExternalConsumption),
isInfo: true,
},
{
label: i18n('text_usage'),
key: 'Usage',
value: getMaybeNumber(stats.AnonRss),
isInfo: true,
},
{
label: i18n('text_soft-limit'),
key: 'SoftLimit',
value: getMaybeNumber(stats.SoftLimit),
isInfo: true,
},
{
label: i18n('text_hard-limit'),
key: 'HardLimit',
value: getMaybeNumber(stats.HardLimit),
isInfo: true,
},
);

return segments.filter((segment) => segment.value !== undefined) as MemorySegment[];
}
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
Loading
Loading