@@ -22,6 +22,16 @@ import { getInstanceSelector, useInstanceSelector } from 'app/hooks'
2222
2323const TimeSeriesChart = React . lazy ( ( ) => import ( 'app/components/TimeSeriesChart' ) )
2424
25+ export function getCycleCount ( num : number , base : number ) {
26+ let cycleCount = 0
27+ let transformedValue = num
28+ while ( transformedValue > base ) {
29+ transformedValue = transformedValue / base
30+ cycleCount ++
31+ }
32+ return cycleCount
33+ }
34+
2535type DiskMetricParams = {
2636 title : string
2737 unit : 'Bytes' | 'Count'
@@ -56,36 +66,28 @@ function DiskMetric({
5666
5767 const isBytesChart = unit === 'Bytes'
5868
59- let unitForSet = ''
60- let cycleCount = 0
61- let label
62- const divisorBase = isBytesChart ? 1024 : 1000
63- if ( metrics ?. items ?. length ) {
64- // Because we're using cumulative metrics, we can use the last (and therefore largest)
65- // value in the set to determine the unit and the divisor for the set.
66- const largestValue =
67- ( metrics ?. items ?. [ metrics . items . length - 1 ] . datum . datum as Cumulativeint64 ) . value || 0
68-
69- // We'll need to divide each number in the set by a consistent multiple of 1024 (for Bytes) or 1000 (for Counts)
70- // Figure out what that multiple is:
71- let transformedValue = largestValue
72- while ( transformedValue > divisorBase ) {
73- transformedValue = transformedValue / divisorBase
74- cycleCount ++
75- }
69+ const largestValue = useMemo ( ( ) => {
70+ if ( ! metrics || metrics . items . length === 0 ) return 0
71+ return Math . max ( ...metrics . items . map ( ( m ) => ( m . datum . datum as Cumulativeint64 ) . value ) )
72+ } , [ metrics ] )
7673
77- // Now that we know how many cycles of "divide by 1024 || 1000" to run through
78- // (via cylceCount), we can determine the proper unit for the set
79- if ( isBytesChart ) {
80- const byteUnits = [ 'Bytes' , 'KiB' , 'MiB' , 'GiB' , 'TiB' ]
81- unitForSet = byteUnits [ cycleCount ]
82- label = `(${ unitForSet } )`
83- } else {
84- label = '(COUNT)'
85- }
74+ // We'll need to divide each number in the set by a consistent exponent
75+ // of 1024 (for Bytes) or 1000 (for Counts)
76+ const base = isBytesChart ? 1024 : 1000
77+ // Figure out what that exponent is:
78+ const cycleCount = getCycleCount ( largestValue , base )
79+
80+ // Now that we know how many cycles of "divide by 1024 || 1000" to run through
81+ // (via cycleCount), we can determine the proper unit for the set
82+ let unitForSet = ''
83+ let label = '(COUNT)'
84+ if ( isBytesChart ) {
85+ const byteUnits = [ 'Bytes' , 'KiB' , 'MiB' , 'GiB' , 'TiB' ]
86+ unitForSet = byteUnits [ cycleCount ]
87+ label = `(${ unitForSet } )`
8688 }
8789
88- const divisor = divisorBase ** cycleCount
90+ const divisor = base ** cycleCount
8991
9092 const data = useMemo (
9193 ( ) =>
0 commit comments