6
6
* Copyright Oxide Computer Company
7
7
*/
8
8
9
- import { useMemo , useState } from 'react'
9
+ import { useState } from 'react'
10
10
11
11
import { usePrefetchedApiQuery } from '@oxide/api'
12
12
@@ -16,6 +16,7 @@ import {
16
16
MetricRow ,
17
17
OxqlMetric ,
18
18
} from '~/components/oxql-metrics/OxqlMetric'
19
+ import type { OxqlVcpuState } from '~/components/oxql-metrics/util'
19
20
import { useInstanceSelector } from '~/hooks/use-params'
20
21
import { Listbox } from '~/ui/lib/Listbox'
21
22
@@ -29,35 +30,30 @@ export default function CpuMetricsTab() {
29
30
path : { instance } ,
30
31
query : { project } ,
31
32
} )
32
-
33
33
const { startTime, endTime, dateTimeRangePicker } = useMetricsContext ( )
34
34
35
- const queryBase = {
36
- metricName : 'virtual_machine:vcpu_usage' as const ,
37
- startTime,
38
- endTime,
39
- groupBy : { cols : [ 'vcpu_id' ] , op : 'sum' } as const ,
40
- }
41
-
42
- // all this memoization is ridiculous, but we need the filters referentially
43
- // table or everything will re-render too much
44
- const filterBase = useMemo ( ( ) => ( { instance_id : instanceData . id } ) , [ instanceData . id ] )
35
+ type CpuChartType = OxqlVcpuState | 'all'
45
36
46
- const stateItems = [
37
+ const stateItems : { label : string ; value : CpuChartType } [ ] = [
47
38
{ label : 'State: Running' , value : 'run' } ,
48
39
{ label : 'State: Emulating' , value : 'emulation' } ,
49
40
{ label : 'State: Idling' , value : 'idle' } ,
50
41
{ label : 'State: Waiting' , value : 'waiting' } ,
51
42
{ label : 'All states' , value : 'all' } ,
52
43
]
53
-
54
44
const [ selectedState , setSelectedState ] = useState ( stateItems [ 0 ] . value )
55
45
56
- const title = `CPU Utilization: ${ stateItems
57
- . find ( ( i ) => i . value === selectedState )
58
- ?. label . replace ( 'State: ' , '' )
59
- . replace ( 'All states' , 'Total' ) } `
60
- const state = selectedState === 'all' ? undefined : selectedState
46
+ const CpuStateMetric = ( { state } : { state : CpuChartType } ) => (
47
+ < OxqlMetric
48
+ title = { `CPU Utilization: ${ stateItems . find ( ( i ) => i . value === state ) ?. label . replace ( 'State: ' , '' ) } ` }
49
+ eqFilters = { { instance_id : instanceData . id , state } }
50
+ metricName = { 'virtual_machine:vcpu_usage' as const }
51
+ startTime = { startTime }
52
+ endTime = { endTime }
53
+ groupBy = { { cols : [ 'vcpu_id' ] , op : 'sum' } as const }
54
+ />
55
+ )
56
+
61
57
return (
62
58
< >
63
59
< MetricHeader >
@@ -74,41 +70,22 @@ export default function CpuMetricsTab() {
74
70
{ dateTimeRangePicker }
75
71
</ MetricHeader >
76
72
< MetricCollection >
77
- < MetricRow >
78
- < OxqlMetric
79
- title = { title }
80
- eqFilters = { useMemo ( ( ) => ( { ...filterBase , state } ) , [ filterBase , state ] ) }
81
- { ...queryBase }
82
- />
83
- </ MetricRow >
84
- { selectedState === 'all' && (
73
+ { selectedState === 'all' ? (
85
74
< >
86
75
< MetricRow >
87
- < OxqlMetric
88
- title = "CPU Utilization: Running"
89
- eqFilters = { { ...filterBase , state : 'run' } }
90
- { ...queryBase }
91
- />
92
- < OxqlMetric
93
- title = "CPU Utilization: Emulation"
94
- eqFilters = { { ...filterBase , state : 'emulation' } }
95
- { ...queryBase }
96
- />
76
+ < CpuStateMetric state = "run" />
77
+ < CpuStateMetric state = "emulation" />
97
78
</ MetricRow >
98
79
99
80
< MetricRow >
100
- < OxqlMetric
101
- title = "CPU Utilization: Idling"
102
- eqFilters = { { ...filterBase , state : 'idle' } }
103
- { ...queryBase }
104
- />
105
- < OxqlMetric
106
- title = "CPU Utilization: Waiting"
107
- eqFilters = { { ...filterBase , state : 'waiting' } }
108
- { ...queryBase }
109
- />
81
+ < CpuStateMetric state = "idle" />
82
+ < CpuStateMetric state = "waiting" />
110
83
</ MetricRow >
111
84
</ >
85
+ ) : (
86
+ < MetricRow >
87
+ < CpuStateMetric state = { selectedState } />
88
+ </ MetricRow >
112
89
) }
113
90
</ MetricCollection >
114
91
</ >
0 commit comments