Skip to content

Commit d264ce4

Browse files
author
OpenShift Bot
authored
Merge pull request #995 from spadgett/metrics-last-value
Merged by openshift-bot
2 parents e024781 + f30c6e2 commit d264ce4

File tree

4 files changed

+456
-369
lines changed

4 files changed

+456
-369
lines changed

app/scripts/directives/podMetrics.js

+74-59
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,42 @@ angular.module('openshiftConsole')
231231
return null;
232232
}
233233

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) {
235270
var dates, values = {};
236271

237272
var missingData = _.some(metric.datasets, function(dataset) {
@@ -241,31 +276,9 @@ angular.module('openshiftConsole')
241276
return;
242277
}
243278

244-
metric.totalUsed = 0;
245-
var largestValue = 0;
246279
angular.forEach(metric.datasets, function(dataset) {
247280
var metricID = dataset.id, metricData = dataset.data;
248281
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-
269282
angular.forEach(metricData, function(point) {
270283
dates.push(point.start);
271284
if (point.value === undefined || point.value === null) {
@@ -282,48 +295,13 @@ angular.module('openshiftConsole')
282295
default:
283296
values[metricID].push(d3.round(value));
284297
}
285-
largestValue = Math.max(value, largestValue);
286298
}
287299
});
288300

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-
}
319301
});
320302

321-
metric.totalUsed = _.round(metric.totalUsed, 1);
322-
323303
var columns = [dates].concat(_.values(values));
324304

325-
// Sparkline
326-
327305
var sparklineConfig, sparklineData = {
328306
type: metric.chartType || 'spline',
329307
x: 'dates',
@@ -397,6 +375,11 @@ angular.module('openshiftConsole')
397375
function metricsSucceeded() {
398376
// Reset the number of failures on a successful request.
399377
failureCount = 0;
378+
379+
_.each(scope.metrics, function(metric) {
380+
updateSparkline(metric);
381+
updateDonut(metric);
382+
});
400383
}
401384

402385
// If the first request for metrics fails, show an empty state error message.
@@ -452,6 +435,27 @@ angular.module('openshiftConsole')
452435
return scope.pod && _.get(scope, 'options.selectedContainer');
453436
}
454437

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+
455459
function updateData(dataset, response) {
456460
scope.noData = false;
457461

@@ -484,6 +488,8 @@ angular.module('openshiftConsole')
484488
angular.forEach(scope.metrics, function(metric) {
485489
var datasetPromises = [];
486490

491+
metric.totalUsed = 0;
492+
487493
// On metrics that require more than one set of data (e.g. network
488494
// incoming and outgoing traffic) we perform one request for each,
489495
// but collect and handle all requests in one single promise below.
@@ -498,6 +504,16 @@ angular.module('openshiftConsole')
498504
}
499505
var promise = MetricsService.get(config);
500506
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+
}
501517
});
502518

503519
allPromises = allPromises.concat(datasetPromises);
@@ -520,7 +536,6 @@ angular.module('openshiftConsole')
520536
});
521537
updateData(dataset, response);
522538
});
523-
updateChart(metric);
524539
});
525540
});
526541

app/scripts/services/metrics.js

+28
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,34 @@ angular.module("openshiftConsole")
219219
});
220220
},
221221

222+
getCurrentUsage: function(config) {
223+
return getRequestURL(config).then(function(url) {
224+
if (!url) {
225+
return null;
226+
}
227+
228+
// Request one data point for the last minute.
229+
var params = {
230+
bucketDuration: '1mn',
231+
start: '-1mn'
232+
};
233+
234+
return $http.get(url, {
235+
auth: {},
236+
headers: {
237+
Accept: 'application/json',
238+
'Hawkular-Tenant': config.namespace
239+
},
240+
params: params
241+
}).then(function(response) {
242+
return _.assign(response, {
243+
metricID: config.metric,
244+
usage: _.head(normalize(response.data))
245+
});
246+
});
247+
});
248+
},
249+
222250
// Get metrics data for a collection of pods (memory, CPU, network send and received).
223251
//
224252
// config keyword arguments

0 commit comments

Comments
 (0)