@@ -231,7 +231,42 @@ angular.module('openshiftConsole')
231
231
return null ;
232
232
}
233
233
234
- function updateChart ( metric ) {
234
+ function updateDonut ( metric ) {
235
+ var dataset = _ . head ( metric . datasets ) ;
236
+ if ( ! dataset . total ) {
237
+ return ;
238
+ }
239
+
240
+ var donutData = {
241
+ type : 'donut' ,
242
+ columns : [
243
+ [ 'Used' , dataset . used ] ,
244
+ [ 'Available' , Math . max ( dataset . available , 0 ) ]
245
+ ] ,
246
+ colors : {
247
+ // Blue if not at limit, orange if at or over limit
248
+ Used : ( dataset . available > 0 ) ? "#0088ce" : "#ec7a08" ,
249
+ Available : "#d1d1d1"
250
+ }
251
+ } ;
252
+
253
+ var donutConfig ;
254
+ if ( ! donutByMetric [ dataset . id ] ) {
255
+ donutConfig = createDonutConfig ( metric ) ;
256
+ donutConfig . data = donutData ;
257
+ $timeout ( function ( ) {
258
+ if ( destroyed ) {
259
+ return ;
260
+ }
261
+
262
+ donutByMetric [ dataset . id ] = c3 . generate ( donutConfig ) ;
263
+ } ) ;
264
+ } else {
265
+ donutByMetric [ dataset . id ] . load ( donutData ) ;
266
+ }
267
+ }
268
+
269
+ function updateSparkline ( metric ) {
235
270
var dates , values = { } ;
236
271
237
272
var missingData = _ . some ( metric . datasets , function ( dataset ) {
@@ -241,31 +276,9 @@ angular.module('openshiftConsole')
241
276
return ;
242
277
}
243
278
244
- metric . totalUsed = 0 ;
245
- var largestValue = 0 ;
246
279
angular . forEach ( metric . datasets , function ( dataset ) {
247
280
var metricID = dataset . id , metricData = dataset . data ;
248
281
dates = [ 'dates' ] , values [ metricID ] = [ dataset . label || metricID ] ;
249
-
250
- dataset . total = getLimit ( metricID ) ;
251
- if ( dataset . total ) {
252
- scope . hasLimits = true ;
253
- }
254
-
255
- var lastValue = _ . last ( metricData ) . value ;
256
- if ( isNaN ( lastValue ) ) {
257
- lastValue = 0 ;
258
- }
259
- if ( metric . convert ) {
260
- lastValue = metric . convert ( lastValue ) ;
261
- }
262
-
263
- dataset . used = lastValue ;
264
- if ( dataset . total ) {
265
- dataset . available = dataset . total - dataset . used ;
266
- }
267
- metric . totalUsed += dataset . used ;
268
-
269
282
angular . forEach ( metricData , function ( point ) {
270
283
dates . push ( point . start ) ;
271
284
if ( point . value === undefined || point . value === null ) {
@@ -282,48 +295,13 @@ angular.module('openshiftConsole')
282
295
default :
283
296
values [ metricID ] . push ( d3 . round ( value ) ) ;
284
297
}
285
- largestValue = Math . max ( value , largestValue ) ;
286
298
}
287
299
} ) ;
288
300
289
- dataset . used = _ . round ( dataset . used ) ;
290
- dataset . total = _ . round ( dataset . total ) ;
291
- dataset . available = _ . round ( dataset . available ) ;
292
-
293
- // Donut
294
- var donutConfig , donutData ;
295
- if ( dataset . total ) {
296
- donutData = {
297
- type : 'donut' ,
298
- columns : [
299
- [ 'Used' , dataset . used ] ,
300
- [ 'Available' , Math . max ( dataset . available , 0 ) ]
301
- ] ,
302
- colors : {
303
- // Blue if not at limit, orange if at or over limit
304
- Used : ( dataset . available > 0 ) ? "#0088ce" : "#ec7a08" ,
305
- Available : "#d1d1d1"
306
- }
307
- } ;
308
-
309
- if ( ! donutByMetric [ metricID ] ) {
310
- donutConfig = createDonutConfig ( metric ) ;
311
- donutConfig . data = donutData ;
312
- $timeout ( function ( ) {
313
- donutByMetric [ metricID ] = c3 . generate ( donutConfig ) ;
314
- } ) ;
315
- } else {
316
- donutByMetric [ metricID ] . load ( donutData ) ;
317
- }
318
- }
319
301
} ) ;
320
302
321
- metric . totalUsed = _ . round ( metric . totalUsed , 1 ) ;
322
-
323
303
var columns = [ dates ] . concat ( _ . values ( values ) ) ;
324
304
325
- // Sparkline
326
-
327
305
var sparklineConfig , sparklineData = {
328
306
type : metric . chartType || 'spline' ,
329
307
x : 'dates' ,
@@ -397,6 +375,11 @@ angular.module('openshiftConsole')
397
375
function metricsSucceeded ( ) {
398
376
// Reset the number of failures on a successful request.
399
377
failureCount = 0 ;
378
+
379
+ _ . each ( scope . metrics , function ( metric ) {
380
+ updateSparkline ( metric ) ;
381
+ updateDonut ( metric ) ;
382
+ } ) ;
400
383
}
401
384
402
385
// If the first request for metrics fails, show an empty state error message.
@@ -452,6 +435,27 @@ angular.module('openshiftConsole')
452
435
return scope . pod && _ . get ( scope , 'options.selectedContainer' ) ;
453
436
}
454
437
438
+ function updateCurrentUsage ( metric , dataset , response ) {
439
+ dataset . total = getLimit ( dataset . id ) ;
440
+ if ( dataset . total ) {
441
+ scope . hasLimits = true ;
442
+ }
443
+
444
+ var currentUsage = _ . get ( response , 'usage.value' ) ;
445
+ if ( isNaN ( currentUsage ) ) {
446
+ currentUsage = 0 ;
447
+ }
448
+ if ( metric . convert ) {
449
+ currentUsage = metric . convert ( currentUsage ) ;
450
+ }
451
+
452
+ dataset . used = _ . round ( currentUsage ) ;
453
+ if ( dataset . total ) {
454
+ dataset . available = _ . round ( dataset . total - currentUsage ) ;
455
+ }
456
+ metric . totalUsed += dataset . used ;
457
+ }
458
+
455
459
function updateData ( dataset , response ) {
456
460
scope . noData = false ;
457
461
@@ -484,6 +488,8 @@ angular.module('openshiftConsole')
484
488
angular . forEach ( scope . metrics , function ( metric ) {
485
489
var datasetPromises = [ ] ;
486
490
491
+ metric . totalUsed = 0 ;
492
+
487
493
// On metrics that require more than one set of data (e.g. network
488
494
// incoming and outgoing traffic) we perform one request for each,
489
495
// but collect and handle all requests in one single promise below.
@@ -498,6 +504,16 @@ angular.module('openshiftConsole')
498
504
}
499
505
var promise = MetricsService . get ( config ) ;
500
506
datasetPromises . push ( promise ) ;
507
+
508
+ // Only request current usage if we have a limit. This lets us
509
+ // show consistent values inside the donut chart no matter what
510
+ // time range is selected.
511
+ var limit = getLimit ( dataset . id ) ;
512
+ if ( limit ) {
513
+ allPromises . push ( MetricsService . getCurrentUsage ( config ) . then ( function ( response ) {
514
+ updateCurrentUsage ( metric , dataset , response ) ;
515
+ } ) ) ;
516
+ }
501
517
} ) ;
502
518
503
519
allPromises = allPromises . concat ( datasetPromises ) ;
@@ -520,7 +536,6 @@ angular.module('openshiftConsole')
520
536
} ) ;
521
537
updateData ( dataset , response ) ;
522
538
} ) ;
523
- updateChart ( metric ) ;
524
539
} ) ;
525
540
} ) ;
526
541
0 commit comments