forked from openshift/origin-web-console
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmetrics.js
393 lines (357 loc) · 12.5 KB
/
metrics.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
'use strict';
angular.module('openshiftConsole')
.directive('podMetrics', function($interval,
$parse,
$timeout,
$q,
ChartsService,
MetricsService,
usageValueFilter) {
return {
restrict: 'E',
scope: {
pod: '='
},
templateUrl: 'views/directives/_pod-metrics.html',
link: function(scope) {
var donutByMetric = {}, sparklineByMetric = {};
var intervalPromise;
var getMemoryLimit = $parse('resources.limits.memory');
var getCPULimit = $parse('resources.limits.cpu');
function bytesToMiB(value) {
if (!value) {
return value;
}
return value / (1024 * 1024);
}
scope.uniqueID = _.uniqueId('metrics-chart-');
// Metrics to display.
scope.metrics = [
{
label: "Memory",
units: "MiB",
chartPrefix: "memory-",
convert: bytesToMiB,
containerMetric: true,
datasets: [
{
id: "memory/usage",
data: []
}
]
},
{
label: "CPU",
units: "millicores",
chartPrefix: "cpu-",
containerMetric: true,
datasets: [
{
id: "cpu/usage",
data: []
}
]
},
{
label: "Network",
units: "MiB",
chartPrefix: "network-",
chartType: "line",
convert: bytesToMiB,
datasets: [
{
id: "network/tx",
label: "Sent",
data: []
},
{
id: "network/rx",
label: "Received",
data: []
}
]
}
];
// Set to true when any data has been loaded (or failed to load).
scope.loaded = false;
// Get the URL to show in error messages.
MetricsService.getMetricsURL().then(function(url) {
scope.metricsURL = url;
});
// Relative time options.
scope.options = {
rangeOptions: [{
label: "Last hour",
value: 60
}, {
label: "Last 4 hours",
value: 4 * 60
}, {
label: "Last day",
value: 24 * 60
}, {
label: "Last 3 days",
value: 3 * 24 * 60
}, {
label: "Last week",
value: 7 * 24 * 60
}]
};
// Show last hour by default.
scope.options.timeRange = scope.options.rangeOptions[0];
scope.usageByMetric = {};
scope.anyUsageByMetric = function(metric) {
return _.some(_.map(metric.datasets, 'id'), function(metricID) { return scope.usageByMetric[metricID] !== undefined; });
};
var createDonutConfig = function(metric) {
var chartID = '#' + metric.chartPrefix + scope.uniqueID + '-donut';
return {
bindto: chartID,
onrendered: function() {
var used = scope.usageByMetric[metric.datasets[0].id].used;
ChartsService.updateDonutCenterText(chartID, used, metric.units);
},
donut: {
label: {
show: false
},
width: 10
},
legend: {
show: false
},
size: {
height: 175,
widht: 175
}
};
};
var createSparklineConfig = function(metric) {
return {
bindto: '#' + metric.chartPrefix + scope.uniqueID + '-sparkline',
axis: {
x: {
show: true,
type: 'timeseries',
// With default padding you can have negative axis tick values.
padding: {
left: 0,
bottom: 0
},
tick: {
type: 'timeseries',
format: '%a %H:%M'
}
},
y: {
label: metric.units,
min: 0,
// With default padding you can have negative axis tick values.
padding: {
left: 0,
top: 0,
bottom: 0
},
show: true,
tick: {
format: function(value) {
return d3.round(value, 2);
}
}
}
},
legend: {
show: metric.datasets.length > 1
},
point: {
show: false
},
size: {
height: 160
}
};
};
function getLimit(metricID) {
var container = scope.options.selectedContainer;
switch (metricID) {
case 'memory/usage':
var memLimit = getMemoryLimit(container);
if (memLimit) {
// Convert to MiB. usageValueFilter returns bytes.
return bytesToMiB(usageValueFilter(memLimit));
}
break;
case 'cpu/usage':
var cpuLimit = getCPULimit(container);
if (cpuLimit) {
// Convert cores to millicores.
return usageValueFilter(cpuLimit) * 1000;
}
break;
}
return null;
}
function updateChart(metric) {
var dates, values = {};
angular.forEach(metric.datasets, function(dataset) {
var metricID = dataset.id, metricData = dataset.data;
dates = ['dates'], values[metricID] = [dataset.label || metricID];
var usage = scope.usageByMetric[metricID] = {
total: getLimit(metricID)
};
var mostRecentValue = _.last(metricData).value;
if (isNaN(mostRecentValue)) {
mostRecentValue = 0;
}
if (metric.convert) {
mostRecentValue = metric.convert(mostRecentValue);
}
// Round to the closest whole number for the utilization chart.
usage.used = d3.round(mostRecentValue);
if (usage.total) {
usage.available = Math.max(usage.total - usage.used, 0);
}
angular.forEach(metricData, function(point) {
dates.push(point.timestamp);
if (point.value === undefined || point.value === null) {
// Don't attempt to round null values. These appear as gaps in the chart.
values[metricID].push(point.value);
} else {
var value = metric.convert ? metric.convert(point.value) : point.value;
switch (metricID) {
case 'memory/usage':
case 'network/rx':
case 'network/tx':
values[metricID].push(d3.round(value, 2));
break;
default:
values[metricID].push(d3.round(value));
}
}
});
// Donut
var donutConfig, donutData;
if (usage.total) {
donutData = {
type: 'donut',
columns: [
['Used', usage.used],
['Available', usage.available]
],
colors: {
Used: "#0088ce", // Blue
Available: "#d1d1d1" // Gray
}
};
if (!donutByMetric[metricID]) {
donutConfig = createDonutConfig(metric);
donutConfig.data = donutData;
$timeout(function() {
donutByMetric[metricID] = c3.generate(donutConfig);
});
} else {
donutByMetric[metricID].load(donutData);
}
}
});
var columns = [dates].concat(_.values(values));
// Sparkline
var sparklineConfig, sparklineData = {
type: metric.chartType || 'area',
x: 'dates',
columns: columns
};
var chartId = metric.chartPrefix + "sparkline";
if (!sparklineByMetric[chartId]) {
sparklineConfig = createSparklineConfig(metric);
sparklineConfig.data = sparklineData;
if (metric.chartDataColors) {
sparklineConfig.color = { pattern: metric.chartDataColors };
}
$timeout(function() {
sparklineByMetric[chartId] = c3.generate(sparklineConfig);
});
} else {
sparklineByMetric[chartId].load(sparklineData);
}
}
function update() {
var pod = scope.pod,
container = scope.options.selectedContainer,
start = Date.now() - scope.options.timeRange.value * 60 * 1000;
if (!pod || !container || scope.metricsError) {
return;
}
// Leave the end time off to use the server's current time as the end
// time. This prevents an issue where the donut chart shows 0 for
// current usage if the client clock is ahead of the server clock.
angular.forEach(scope.metrics, function(metric) {
var promises = [];
// On metrics that require more than one set of data (e.g. network
// incoming and outgoing traffic) we perform one request for each,
// but collect and handle all requests in one single promise below.
// It's important that every metric uses the same 'start' timestamp
// and number of buckets, so that the returned data for every metric
// fit in the same collection of 'dates' and can be displayed in
// exactly the same point in time in the graph.
angular.forEach(_.map(metric.datasets, 'id'), function(metricID) {
promises.push(MetricsService.get({
pod: pod,
// some metrics (network, disk) are not available at container
// level (only at pod and node level)
containerName: metric.containerMetric ? container.name : "pod",
metric: metricID,
start: start
}));
});
// Collect all promises from every metric requested into one, so we
// have all data the chart wants at the time of the chart creation
// (or timeout updates, etc).
$q.all(promises).then(
// success
function(responses) {
angular.forEach(responses, function(response) {
_.find(metric.datasets, {'id': response.metricID}).data = response.data;
});
updateChart(metric);
},
// failure
function(responses) {
angular.forEach(responses, function(response) {
scope.metricsError = {
status: response.status,
details: _.get(response, 'data.errorMsg') || response.statusText || "Status code " + response.status
};
});
}
).finally(function() {
// Even on errors mark metrics as loaded to replace the
// "Loading..." message with "No metrics to display."
scope.loaded = true;
});
});
}
// Updates immediately and then on options changes.
scope.$watch('options', function() {
delete scope.metricsError;
update();
}, true);
// Also update every 15 seconds.
intervalPromise = $interval(update, 15 * 1000, false);
scope.$on('$destroy', function() {
if (intervalPromise) {
$interval.cancel(intervalPromise);
intervalPromise = null;
}
angular.forEach(donutByMetric, function(chart) {
chart.destroy();
});
donutByMetric = null;
angular.forEach(sparklineByMetric, function(chart) {
chart.destroy();
});
sparklineByMetric = null;
});
}
};
});