1
+ import React from 'react' ;
2
+ import { Typography } from '@material-ui/core' ;
3
+ import { ResponsiveBar } from '@nivo/bar' ;
4
+ import { ScaledObjectModel } from '../models/ScaledObjectModel' ;
5
+ import { V1Deployment } from '@kubernetes/client-node' ;
6
+
7
+ export default class ReplicaDisplay extends React . Component < { } , { currentReplicas : { [ key : string ] : number } } > {
8
+ replicaAutoscalingDataset : { [ key : string ] : number } [ ] = [ ] ;
9
+
10
+ constructor ( props : { } ) {
11
+ super ( props ) ;
12
+
13
+ this . state = {
14
+ currentReplicas : { } ,
15
+ }
16
+ }
17
+
18
+ async componentDidMount ( ) {
19
+ await this . getCurrentReplicaCount ( ) ;
20
+ await this . formatData ( ) ;
21
+
22
+ try {
23
+ setInterval ( async ( ) => {
24
+ await this . getCurrentReplicaCount ( ) ;
25
+ await this . formatData ( ) ;
26
+ } , 5000 ) ;
27
+ } catch ( e ) {
28
+ console . log ( e ) ;
29
+ }
30
+ }
31
+
32
+ async getCurrentReplicaCount ( ) {
33
+ await fetch ( '/api/scaledobjects' )
34
+ . then ( res => res . json ( ) )
35
+ . then ( ( { items } ) => {
36
+ let scaledObjects = items ;
37
+ let currentReplicas :{ [ key : string ] : number } = { } ;
38
+
39
+ scaledObjects . map ( async ( scaledObject :ScaledObjectModel ) => {
40
+ await fetch ( `/api/deployment/${ scaledObject . metadata . name } ` )
41
+ . then ( res => { return res . json ( ) } )
42
+ . then ( data => {
43
+ let deploy = new V1Deployment ( ) ;
44
+ deploy . metadata = data . metadata ;
45
+ deploy . spec = data . spec ;
46
+ deploy . status = data . status ;
47
+
48
+ if ( deploy . metadata && deploy . metadata ! . name ) {
49
+ currentReplicas [ deploy . metadata ! . name ] = ( deploy . spec ! . replicas === undefined ) ? 0 :deploy . spec ! . replicas ;
50
+ }
51
+
52
+ this . setState ( { currentReplicas : currentReplicas } ) ;
53
+ } )
54
+ } ) ;
55
+ } ) ;
56
+ }
57
+
58
+ formatData ( ) {
59
+ let dataAtTimestamp :{ [ key :string ] : any } = { } ;
60
+ let date = new Date ( ) ;
61
+ dataAtTimestamp [ "timestamp" ] = `${ date . getFullYear ( ) } -${ date . getMonth ( ) } -${ date . getDate ( ) } T${ date . getHours ( ) } -${ date . getMinutes ( ) } -${ date . getMilliseconds ( ) } ` ;
62
+
63
+ for ( let deploy in this . state . currentReplicas ) {
64
+ dataAtTimestamp [ deploy ] = this . state . currentReplicas [ deploy ] ;
65
+ }
66
+
67
+ this . replicaAutoscalingDataset . push ( dataAtTimestamp ) ;
68
+
69
+ console . log ( this . replicaAutoscalingDataset ) ;
70
+ }
71
+
72
+ render ( ) {
73
+ const data = [
74
+ {
75
+ "timestamp" : "AD" ,
76
+ "hot dog" : 155 ,
77
+ "hot dogColor" : "hsl(282, 70%, 50%)" ,
78
+ "burger" : 67 ,
79
+ "burgerColor" : "hsl(20, 70%, 50%)" ,
80
+ "sandwich" : 150 ,
81
+ "sandwichColor" : "hsl(10, 70%, 50%)" ,
82
+ "kebab" : 31 ,
83
+ "kebabColor" : "hsl(66, 70%, 50%)" ,
84
+ "fries" : 103 ,
85
+ "friesColor" : "hsl(147, 70%, 50%)" ,
86
+ "donut" : 99 ,
87
+ "donutColor" : "hsl(16, 70%, 50%)"
88
+ } ,
89
+ {
90
+ "timestamp" : "AE" ,
91
+ "hot dog" : 20 ,
92
+ "hot dogColor" : "hsl(209, 70%, 50%)" ,
93
+ "burger" : 104 ,
94
+ "burgerColor" : "hsl(193, 70%, 50%)" ,
95
+ "sandwich" : 138 ,
96
+ "sandwichColor" : "hsl(111, 70%, 50%)" ,
97
+ "kebab" : 30 ,
98
+ "kebabColor" : "hsl(95, 70%, 50%)" ,
99
+ "fries" : 14 ,
100
+ "friesColor" : "hsl(252, 70%, 50%)" ,
101
+ "donut" : 173 ,
102
+ "donutColor" : "hsl(354, 70%, 50%)"
103
+ } ,
104
+ {
105
+ "timestamp" : "AF" ,
106
+ "hot dog" : 12 ,
107
+ "hot dogColor" : "hsl(62, 70%, 50%)" ,
108
+ "burger" : 119 ,
109
+ "burgerColor" : "hsl(80, 70%, 50%)" ,
110
+ "sandwich" : 121 ,
111
+ "sandwichColor" : "hsl(353, 70%, 50%)" ,
112
+ "kebab" : 187 ,
113
+ "kebabColor" : "hsl(308, 70%, 50%)" ,
114
+ "fries" : 142 ,
115
+ "friesColor" : "hsl(353, 70%, 50%)" ,
116
+ "donut" : 96 ,
117
+ "donutColor" : "hsl(88, 70%, 50%)"
118
+ } ,
119
+ {
120
+ "timestamp" : "AG" ,
121
+ "hot dog" : 153 ,
122
+ "hot dogColor" : "hsl(153, 70%, 50%)" ,
123
+ "burger" : 196 ,
124
+ "burgerColor" : "hsl(125, 70%, 50%)" ,
125
+ "sandwich" : 49 ,
126
+ "sandwichColor" : "hsl(260, 70%, 50%)" ,
127
+ "kebab" : 135 ,
128
+ "kebabColor" : "hsl(81, 70%, 50%)" ,
129
+ "fries" : 22 ,
130
+ "friesColor" : "hsl(56, 70%, 50%)" ,
131
+ "donut" : 23 ,
132
+ "donutColor" : "hsl(6, 70%, 50%)"
133
+ } ,
134
+ {
135
+ "timestamp" : "AI" ,
136
+ "hot dog" : 39 ,
137
+ "hot dogColor" : "hsl(350, 70%, 50%)" ,
138
+ "burger" : 16 ,
139
+ "burgerColor" : "hsl(198, 70%, 50%)" ,
140
+ "sandwich" : 134 ,
141
+ "sandwichColor" : "hsl(129, 70%, 50%)" ,
142
+ "kebab" : 30 ,
143
+ "kebabColor" : "hsl(179, 70%, 50%)" ,
144
+ "fries" : 158 ,
145
+ "friesColor" : "hsl(334, 70%, 50%)" ,
146
+ "donut" : 73 ,
147
+ "donutColor" : "hsl(128, 70%, 50%)"
148
+ } ,
149
+ {
150
+ "timestamp" : "AL" ,
151
+ "hot dog" : 179 ,
152
+ "hot dogColor" : "hsl(226, 70%, 50%)" ,
153
+ "burger" : 39 ,
154
+ "burgerColor" : "hsl(137, 70%, 50%)" ,
155
+ "sandwich" : 63 ,
156
+ "sandwichColor" : "hsl(309, 70%, 50%)" ,
157
+ "kebab" : 118 ,
158
+ "kebabColor" : "hsl(96, 70%, 50%)" ,
159
+ "fries" : 100 ,
160
+ "friesColor" : "hsl(270, 70%, 50%)" ,
161
+ "donut" : 61 ,
162
+ "donutColor" : "hsl(179, 70%, 50%)"
163
+ } ,
164
+ {
165
+ "timestamp" : "AM" ,
166
+ "hot dog" : 136 ,
167
+ "hot dogColor" : "hsl(318, 70%, 50%)" ,
168
+ "burger" : 8 ,
169
+ "burgerColor" : "hsl(238, 70%, 50%)" ,
170
+ "sandwich" : 159 ,
171
+ "sandwichColor" : "hsl(157, 70%, 50%)" ,
172
+ "kebab" : 133 ,
173
+ "kebabColor" : "hsl(326, 70%, 50%)" ,
174
+ "fries" : 82 ,
175
+ "friesColor" : "hsl(357, 70%, 50%)" ,
176
+ "donut" : 88 ,
177
+ "donutColor" : "hsl(231, 70%, 50%)"
178
+ }
179
+ ]
180
+
181
+ return (
182
+ < div >
183
+ < Typography variant = "h6" id = "Replica Count" > Replicas</ Typography >
184
+ < div style = { { height : 400 } } >
185
+ < ResponsiveBar
186
+ data = { this . replicaAutoscalingDataset }
187
+ keys = { Object . keys ( this . state . currentReplicas ) }
188
+ indexBy = "timestamp"
189
+ margin = { { top : 50 , right : 130 , bottom : 50 , left : 60 } }
190
+ padding = { 0.3 }
191
+ colors = { { scheme : 'nivo' } }
192
+ axisTop = { null }
193
+ axisRight = { null }
194
+ axisBottom = { {
195
+ tickSize : 5 ,
196
+ tickPadding : 5 ,
197
+ tickRotation : 0 ,
198
+ legend : 'timestamp' ,
199
+ legendPosition : 'middle' ,
200
+ legendOffset : 32
201
+ } }
202
+ axisLeft = { {
203
+ tickSize : 5 ,
204
+ tickPadding : 5 ,
205
+ tickRotation : 0 ,
206
+ legend : 'replicas' ,
207
+ legendPosition : 'middle' ,
208
+ legendOffset : - 40
209
+ } }
210
+ labelSkipWidth = { 12 }
211
+ labelSkipHeight = { 12 }
212
+ labelTextColor = { { from : 'color' , modifiers : [ [ 'darker' , 1.6 ] ] } }
213
+ legends = { [
214
+ {
215
+ dataFrom : 'keys' ,
216
+ anchor : 'bottom-right' ,
217
+ direction : 'column' ,
218
+ justify : false ,
219
+ translateX : 120 ,
220
+ translateY : 0 ,
221
+ itemsSpacing : 2 ,
222
+ itemWidth : 100 ,
223
+ itemHeight : 20 ,
224
+ itemDirection : 'left-to-right' ,
225
+ itemOpacity : 0.85 ,
226
+ symbolSize : 20 ,
227
+ effects : [
228
+ {
229
+ on : 'hover' ,
230
+ style : {
231
+ itemOpacity : 1
232
+ }
233
+ }
234
+ ]
235
+ }
236
+ ] }
237
+ animate = { false }
238
+ motionStiffness = { 90 }
239
+ motionDamping = { 15 }
240
+ />
241
+ </ div >
242
+ </ div >
243
+ ) ;
244
+ }
245
+ }
0 commit comments