1- import React from 'react' ;
2-
31import { DefinitionList , useTheme } from '@gravity-ui/uikit' ;
42
53import type { TMemoryStats } from '../../types/api/nodes' ;
64import { cn } from '../../utils/cn' ;
7- import { formatNumber , roundToPrecision } from '../../utils/dataFormatters/dataFormatters' ;
85import { calculateProgressStatus } from '../../utils/progress' ;
9- import { UNBREAKABLE_GAP , isNumeric } from '../../utils/utils' ;
6+ import { isNumeric } from '../../utils/utils' ;
107import { HoverPopup } from '../HoverPopup/HoverPopup' ;
118import { ProgressViewer } from '../ProgressViewer/ProgressViewer' ;
129
13- import i18n from './i18n ' ;
10+ import { getMemorySegments } from './utils ' ;
1411
1512import './MemoryViewer.scss' ;
1613
@@ -21,25 +18,13 @@ type FormatProgressViewerValues = (
2118 capacity ?: number ,
2219) => ( string | number | undefined ) [ ] ;
2320
24- const formatValue2 = ( value ?: number ) => {
25- return formatNumber ( roundToPrecision ( Number ( value ) , 2 ) ) ;
26- } ;
27-
28- const defaultFormatValues : FormatProgressViewerValues = ( value , total ) => {
29- return [ formatValue2 ( value ) , formatValue2 ( total ) ] ;
30- } ;
31-
32- type ProgressViewerSize = 'xs' | 's' | 'ns' | 'm' | 'n' | 'l' | 'head' ;
33-
3421export interface MemoryProgressViewerProps {
35- stats ?: TMemoryStats ;
36- totalCapacity : number ;
22+ stats : TMemoryStats ;
3723 className ?: string ;
38- size ?: ProgressViewerSize ;
3924 warningThreshold ?: number ;
4025 value ?: number | string ;
4126 capacity ?: number | string ;
42- formatValues ? : FormatProgressViewerValues ;
27+ formatValues : FormatProgressViewerValues ;
4328 percents ?: boolean ;
4429 dangerThreshold ?: number ;
4530}
@@ -49,10 +34,8 @@ export function MemoryViewer({
4934 value,
5035 capacity,
5136 percents,
52- formatValues = defaultFormatValues ,
53- totalCapacity,
37+ formatValues,
5438 className,
55- size = 'xs' ,
5639 warningThreshold = 60 ,
5740 dangerThreshold = 80 ,
5841} : MemoryProgressViewerProps ) {
@@ -80,49 +63,20 @@ export function MemoryViewer({
8063 return valueText ;
8164 } ;
8265
83- const calculateMemoryPercentage = ( value : number ) => {
66+ const calculateMemoryShare = ( segmentSize : number ) => {
8467 if ( ! value ) {
8568 return 0 ;
8669 }
87- return parseFloat ( ( ( value / parseFloat ( String ( capacity ) ) ) * 100 ) . toFixed ( 2 ) ) ;
70+ return ( segmentSize / parseFloat ( String ( capacity ) ) ) * 100 ;
8871 } ;
8972
90- const memorySegments = [
91- {
92- label : i18n ( 'text_external-consumption' ) ,
93- key : 'ExternalConsumption' ,
94- value : parseFloat ( stats ?. ExternalConsumption || '0' ) ,
95- } ,
96- {
97- label : i18n ( 'text_allocator-caches' ) ,
98- key : 'AllocatorCachesMemory' ,
99- value : parseFloat ( stats ?. AllocatorCachesMemory || '0' ) ,
100- } ,
101- {
102- label : i18n ( 'text_shared-cache' ) ,
103- key : 'SharedCacheConsumption' ,
104- value : parseFloat ( stats ?. SharedCacheConsumption || '0' ) ,
105- capacity : stats ?. SharedCacheLimit ,
106- } ,
107- {
108- label : i18n ( 'text_memtable' ) ,
109- key : 'MemTableConsumption' ,
110- value : parseFloat ( stats ?. MemTableConsumption || '0' ) ,
111- capacity : stats ?. MemTableLimit ,
112- } ,
113- {
114- label : i18n ( 'text_query-execution' ) ,
115- key : 'QueryExecutionConsumption' ,
116- value : parseFloat ( stats ?. QueryExecutionConsumption || '0' ) ,
117- capacity : stats ?. QueryExecutionLimit ,
118- } ,
119- ] ;
120-
121- console . log ( memorySegments ) ;
73+ const memorySegments = getMemorySegments ( stats ) ;
12274
12375 const totalUsedMemory =
124- memorySegments . reduce ( ( acc , segment ) => acc + calculateMemoryPercentage ( segment . value ) , 0 ) /
125- totalCapacity ;
76+ memorySegments
77+ . filter ( ( { isInfo} ) => ! isInfo )
78+ . reduce ( ( acc , segment ) => acc + calculateMemoryShare ( segment . value ) , 0 ) /
79+ parseFloat ( String ( capacity ) ) ;
12680
12781 const status = calculateProgressStatus ( {
12882 fillWidth : totalUsedMemory ,
@@ -137,9 +91,8 @@ export function MemoryViewer({
13791 < HoverPopup
13892 popupContent = {
13993 < DefinitionList responsive >
140- { memorySegments
141- . filter ( ( { value} ) => value )
142- . map ( ( { label, value, capacity, key} ) => (
94+ { memorySegments . map (
95+ ( { label, value : segmentSize , capacity : segmentCapacity , key} ) => (
14396 < DefinitionList . Item
14497 key = { label }
14598 name = {
@@ -149,67 +102,41 @@ export function MemoryViewer({
149102 </ div >
150103 }
151104 >
152- < div className = { b ( 'memory-segment' ) } >
153- { capacity ? (
154- < React . Fragment >
155- < ProgressViewer
156- value = { value }
157- capacity = { capacity }
158- formatValues = { formatValues }
159- colorizeProgress
160- percents
161- />
162- { UNBREAKABLE_GAP }
163- </ React . Fragment >
164- ) : null }
165- < div className = { b ( 'memory-segment-percentage' ) } >
166- { formatValues ( value , value ) [ 1 ] }
167- </ div >
168- { UNBREAKABLE_GAP + '/' + UNBREAKABLE_GAP }
169- < div className = { b ( 'memory-segment-percentage' ) } >
170- { calculateMemoryPercentage ( value ) } % total
171- </ div >
172- </ div >
105+ { segmentCapacity ? (
106+ < ProgressViewer
107+ value = { segmentSize }
108+ capacity = { segmentCapacity }
109+ formatValues = { formatValues }
110+ colorizeProgress
111+ />
112+ ) : (
113+ formatValues ( segmentSize ) [ 0 ]
114+ ) }
173115 </ DefinitionList . Item >
174- ) ) }
116+ ) ,
117+ ) }
175118 </ DefinitionList >
176119 }
177120 >
178- < div className = { b ( { size , theme, status} , className ) } >
121+ < div className = { b ( { theme, status} , className ) } >
179122 < div className = { b ( 'progress-container' ) } >
180- { memorySegments . map ( ( segment ) => {
181- const position = currentPosition ;
182- currentPosition += calculateMemoryPercentage ( segment . value ) ;
183-
184- return (
185- < div
186- key = { segment . key }
187- className = { b ( 'segment' , { type : segment . key } ) }
188- style = { {
189- width : `${ calculateMemoryPercentage ( segment . value ) } %` ,
190- left : `${ position } %` ,
191- } }
192- />
193- ) ;
194- } ) }
195- { stats ?. SoftLimit ? (
196- < div
197- className = { b ( 'soft-limit' ) }
198- style = { {
199- width : '2px' ,
200- left : `${ calculateMemoryPercentage ( parseFloat ( stats ?. SoftLimit ) ) } %` ,
201- } }
202- />
203- ) : null }
204- { stats ?. HardLimit ? (
205- < div
206- className = { b ( 'hard-limit' ) }
207- style = { {
208- width : '2px' ,
209- left : `${ calculateMemoryPercentage ( parseFloat ( stats ?. HardLimit ) ) } %` ,
210- } }
211- />
212- ) : null }
123+ { memorySegments
124+ . filter ( ( { isInfo} ) => ! isInfo )
125+ . map ( ( segment ) => {
126+ const position = currentPosition ;
127+ currentPosition += calculateMemoryShare ( segment . value ) ;
128+
129+ return (
130+ < div
131+ key = { segment . key }
132+ className = { b ( 'segment' , { type : segment . key } ) }
133+ style = { {
134+ width : `${ calculateMemoryShare ( segment . value ) . toFixed ( 2 ) } %` ,
135+ left : `${ position } %` ,
136+ } }
137+ />
138+ ) ;
139+ } ) }
213140 < div className = { b ( 'text' ) } > { renderContent ( ) } </ div >
214141 </ div >
215142 </ div >
0 commit comments