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