|
| 1 | +import {DefinitionList, useTheme} from '@gravity-ui/uikit'; |
| 2 | + |
| 3 | +import type {TMemoryStats} from '../../types/api/nodes'; |
| 4 | +import {cn} from '../../utils/cn'; |
| 5 | +import {calculateProgressStatus} from '../../utils/progress'; |
| 6 | +import {isNumeric} from '../../utils/utils'; |
| 7 | +import {HoverPopup} from '../HoverPopup/HoverPopup'; |
| 8 | +import {ProgressViewer} from '../ProgressViewer/ProgressViewer'; |
| 9 | + |
| 10 | +import {getMemorySegments} from './utils'; |
| 11 | + |
| 12 | +import './MemoryViewer.scss'; |
| 13 | + |
| 14 | +const b = cn('memory-viewer'); |
| 15 | + |
| 16 | +type FormatProgressViewerValues = ( |
| 17 | + value?: number, |
| 18 | + capacity?: number, |
| 19 | + precision?: number, |
| 20 | +) => (string | number | undefined)[]; |
| 21 | + |
| 22 | +export interface MemoryProgressViewerProps { |
| 23 | + stats: TMemoryStats; |
| 24 | + className?: string; |
| 25 | + warningThreshold?: number; |
| 26 | + value?: number | string; |
| 27 | + capacity?: number | string; |
| 28 | + formatValues: FormatProgressViewerValues; |
| 29 | + percents?: boolean; |
| 30 | + dangerThreshold?: number; |
| 31 | +} |
| 32 | + |
| 33 | +const MEMORY_PRECISION = 2; |
| 34 | + |
| 35 | +export function MemoryViewer({ |
| 36 | + stats, |
| 37 | + value, |
| 38 | + capacity, |
| 39 | + percents, |
| 40 | + formatValues, |
| 41 | + className, |
| 42 | + warningThreshold = 60, |
| 43 | + dangerThreshold = 80, |
| 44 | +}: MemoryProgressViewerProps) { |
| 45 | + const theme = useTheme(); |
| 46 | + let fillWidth = |
| 47 | + Math.round((parseFloat(String(value)) / parseFloat(String(capacity))) * 100) || 0; |
| 48 | + fillWidth = fillWidth > 100 ? 100 : fillWidth; |
| 49 | + let valueText: number | string | undefined = value, |
| 50 | + capacityText: number | string | undefined = capacity, |
| 51 | + divider = '/'; |
| 52 | + if (percents) { |
| 53 | + valueText = fillWidth + '%'; |
| 54 | + capacityText = ''; |
| 55 | + divider = ''; |
| 56 | + } else if (formatValues) { |
| 57 | + [valueText, capacityText] = formatValues(Number(value), Number(capacity), MEMORY_PRECISION); |
| 58 | + } |
| 59 | + |
| 60 | + const renderContent = () => { |
| 61 | + if (isNumeric(capacity)) { |
| 62 | + return `${valueText} ${divider} ${capacityText}`; |
| 63 | + } |
| 64 | + |
| 65 | + return valueText; |
| 66 | + }; |
| 67 | + |
| 68 | + const calculateMemoryShare = (segmentSize: number) => { |
| 69 | + if (!value) { |
| 70 | + return 0; |
| 71 | + } |
| 72 | + return (segmentSize / parseFloat(String(capacity))) * 100; |
| 73 | + }; |
| 74 | + |
| 75 | + const memorySegments = getMemorySegments(stats); |
| 76 | + |
| 77 | + const totalUsedMemory = |
| 78 | + memorySegments |
| 79 | + .filter(({isInfo}) => !isInfo) |
| 80 | + .reduce((acc, segment) => acc + calculateMemoryShare(segment.value), 0) / |
| 81 | + parseFloat(String(capacity)); |
| 82 | + |
| 83 | + const status = calculateProgressStatus({ |
| 84 | + fillWidth: totalUsedMemory, |
| 85 | + warningThreshold, |
| 86 | + dangerThreshold, |
| 87 | + colorizeProgress: true, |
| 88 | + }); |
| 89 | + |
| 90 | + let currentPosition = 0; |
| 91 | + |
| 92 | + return ( |
| 93 | + <HoverPopup |
| 94 | + popupContent={ |
| 95 | + <DefinitionList responsive> |
| 96 | + {memorySegments.map( |
| 97 | + ({label, value: segmentSize, capacity: segmentCapacity, key}) => ( |
| 98 | + <DefinitionList.Item |
| 99 | + key={label} |
| 100 | + name={ |
| 101 | + <div className={b('container')}> |
| 102 | + <div className={b('legend', {type: key})}></div> |
| 103 | + <div className={b('name')}>{label}</div> |
| 104 | + </div> |
| 105 | + } |
| 106 | + > |
| 107 | + {segmentCapacity ? ( |
| 108 | + <ProgressViewer |
| 109 | + value={segmentSize} |
| 110 | + capacity={segmentCapacity} |
| 111 | + formatValues={(val, size) => |
| 112 | + formatValues(val, size, MEMORY_PRECISION) |
| 113 | + } |
| 114 | + colorizeProgress |
| 115 | + /> |
| 116 | + ) : ( |
| 117 | + formatValues(segmentSize, undefined, MEMORY_PRECISION)[0] |
| 118 | + )} |
| 119 | + </DefinitionList.Item> |
| 120 | + ), |
| 121 | + )} |
| 122 | + </DefinitionList> |
| 123 | + } |
| 124 | + > |
| 125 | + <div className={b({theme, status}, className)}> |
| 126 | + <div className={b('progress-container')}> |
| 127 | + {memorySegments |
| 128 | + .filter(({isInfo}) => !isInfo) |
| 129 | + .map((segment) => { |
| 130 | + const position = currentPosition; |
| 131 | + currentPosition += calculateMemoryShare(segment.value); |
| 132 | + |
| 133 | + return ( |
| 134 | + <div |
| 135 | + key={segment.key} |
| 136 | + className={b('segment', {type: segment.key})} |
| 137 | + style={{ |
| 138 | + width: `${calculateMemoryShare(segment.value).toFixed(2)}%`, |
| 139 | + left: `${position}%`, |
| 140 | + }} |
| 141 | + /> |
| 142 | + ); |
| 143 | + })} |
| 144 | + <div className={b('text')}>{renderContent()}</div> |
| 145 | + </div> |
| 146 | + </div> |
| 147 | + </HoverPopup> |
| 148 | + ); |
| 149 | +} |
0 commit comments