Skip to content

Commit 857a4e6

Browse files
author
OpenShift Bot
authored
Merge pull request #1160 from spadgett/metrics-loops
Merged by openshift-bot
2 parents 4690daf + 69adc9a commit 857a4e6

File tree

2 files changed

+101
-94
lines changed

2 files changed

+101
-94
lines changed

app/scripts/directives/podMetrics.js

+48-46
Original file line numberDiff line numberDiff line change
@@ -95,41 +95,6 @@ angular.module('openshiftConsole')
9595
});
9696
}
9797

98-
if (!window.OPENSHIFT_CONSTANTS.DISABLE_CUSTOM_METRICS) {
99-
// Load any custom metrics onto the page
100-
MetricsService.getCustomMetrics(scope.pod).then(
101-
function(response) {
102-
angular.forEach(response, function(metric) {
103-
104-
// set the label to the description if specified
105-
var label = metric.description || metric.name;
106-
107-
// get the unit value if specified
108-
var unit = metric.unit || "";
109-
110-
scope.metrics.push({
111-
label: label,
112-
units: unit,
113-
chartPrefix: "custom-" + _.uniqueId('custom-metric-'),
114-
chartType: "spline",
115-
116-
datasets: [
117-
{
118-
id: "custom/" + metric.name,
119-
label: label,
120-
type: metric.type,
121-
data: []
122-
},
123-
]
124-
});
125-
126-
});
127-
// update the page with the new charts.
128-
update();
129-
}
130-
);
131-
}
132-
13398
// Set to true when any data has been loaded (or failed to load).
13499
scope.loaded = false;
135100
scope.noData = true;
@@ -378,6 +343,40 @@ angular.module('openshiftConsole')
378343
};
379344
}
380345

346+
function getCustomMetrics() {
347+
if (window.OPENSHIFT_CONSTANTS.DISABLE_CUSTOM_METRICS) {
348+
return $q.when({});
349+
}
350+
351+
// Load any custom metrics onto the page
352+
return MetricsService.getCustomMetrics(scope.pod).then(function(response) {
353+
angular.forEach(response, function(metric) {
354+
355+
// set the label to the description if specified
356+
var label = metric.description || metric.name;
357+
358+
// get the unit value if specified
359+
var unit = metric.unit || "";
360+
361+
scope.metrics.push({
362+
label: label,
363+
units: unit,
364+
chartPrefix: "custom-" + _.uniqueId('custom-metric-'),
365+
chartType: "spline",
366+
367+
datasets: [
368+
{
369+
id: "custom/" + metric.name,
370+
label: label,
371+
type: metric.type,
372+
data: []
373+
},
374+
]
375+
});
376+
});
377+
});
378+
}
379+
381380
// Make sure there are no errors or missing data before updating.
382381
function canUpdate() {
383382
if (scope.metricsError || failureCount > 1) {
@@ -499,18 +498,21 @@ angular.module('openshiftConsole')
499498
});
500499
}
501500

502-
// Updates immediately and then on options changes.
503-
scope.$watch('options', function() {
504-
// Remove any existing data so that we request data for the new container or time range.
505-
_.each(scope.metrics, function(metric) {
506-
_.each(metric.datasets, function(dataset) {
507-
delete dataset.data;
501+
// Request custom metrics before calling update the first time.
502+
getCustomMetrics().finally(function() {
503+
// Updates immediately and then on options changes.
504+
scope.$watch('options', function() {
505+
// Remove any existing data so that we request data for the new container or time range.
506+
_.each(scope.metrics, function(metric) {
507+
_.each(metric.datasets, function(dataset) {
508+
delete dataset.data;
509+
});
508510
});
509-
});
510-
delete scope.metricsError;
511-
update();
512-
}, true);
513-
intervalPromise = $interval(update, MetricsCharts.getDefaultUpdateInterval(), false);
511+
delete scope.metricsError;
512+
update();
513+
}, true);
514+
intervalPromise = $interval(update, MetricsCharts.getDefaultUpdateInterval(), false);
515+
});
514516

515517
$rootScope.$on('metrics.charts.resize', function() {
516518
MetricsCharts.redraw(donutByMetric);

dist/scripts/scripts.js

+53-48
Original file line numberDiff line numberDiff line change
@@ -11483,12 +11483,12 @@ if (!m.pod) return null;
1148311483
var b = m.options.selectedContainer;
1148411484
switch (a) {
1148511485
case "memory/usage":
11486-
var c = D(b);
11486+
var c = E(b);
1148711487
if (c) return h.bytesToMiB(l(c));
1148811488
break;
1148911489

1149011490
case "cpu/usage_rate":
11491-
var d = E(b);
11491+
var d = F(b);
1149211492
if (d) return l(d);
1149311493
}
1149411494
return null;
@@ -11504,8 +11504,8 @@ Used:b.available > 0 ? "#0088ce" :"#ec7a08",
1150411504
Available:"#d1d1d1"
1150511505
}
1150611506
};
11507-
B[b.id] ? B[b.id].load(e) :(c = I(a), c.data = e, d(function() {
11508-
G || (B[b.id] = c3.generate(c));
11507+
C[b.id] ? C[b.id].load(e) :(c = J(a), c.data = e, d(function() {
11508+
H || (C[b.id] = c3.generate(c));
1150911509
}));
1151011510
}
1151111511
}
@@ -11519,10 +11519,10 @@ _.each(a.datasets, function(a) {
1151911519
c[a.id] = a.data;
1152011520
});
1152111521
var e, f = i.getSparklineData(c), g = a.chartPrefix + "sparkline";
11522-
C[g] ? C[g].load(f) :(e = J(a), e.data = f, a.chartDataColors && (e.color = {
11522+
D[g] ? D[g].load(f) :(e = K(a), e.data = f, a.chartDataColors && (e.color = {
1152311523
pattern:a.chartDataColors
1152411524
}), d(function() {
11525-
G || (C[g] = c3.generate(e));
11525+
H || (D[g] = c3.generate(e));
1152611526
}));
1152711527
}
1152811528
}
@@ -11533,7 +11533,7 @@ function r() {
1153311533
return 60 * m.options.timeRange.value * 1e3;
1153411534
}
1153511535
function s() {
11536-
return Math.floor(r() / F) + "ms";
11536+
return Math.floor(r() / G) + "ms";
1153711537
}
1153811538
function t(a, b, c) {
1153911539
var d, e = {
@@ -11548,17 +11548,17 @@ containerName:a.containerMetric ? m.options.selectedContainer.name :"pod"
1154811548
}) :null;
1154911549
}
1155011550
function u() {
11551-
K = 0, _.each(m.metrics, function(a) {
11551+
L = 0, _.each(m.metrics, function(a) {
1155211552
p(a), o(a);
1155311553
});
1155411554
}
1155511555
function v(a) {
11556-
if (!G) {
11557-
if (K++, m.noData) return void (m.metricsError = {
11556+
if (!H) {
11557+
if (L++, m.noData) return void (m.metricsError = {
1155811558
status:_.get(a, "status", 0),
1155911559
details:_.get(a, "data.errorMsg") || _.get(a, "statusText") || "Status code " + _.get(a, "status", 0)
1156011560
});
11561-
if (!(K < 2)) {
11561+
if (!(L < 2)) {
1156211562
var b = "metrics-failed-" + m.uniqueID;
1156311563
m.alerts[b] = {
1156411564
type:"error",
@@ -11567,28 +11567,47 @@ links:[ {
1156711567
href:"",
1156811568
label:"Retry",
1156911569
onClick:function() {
11570-
delete m.alerts[b], K = 1, z();
11570+
delete m.alerts[b], L = 1, A();
1157111571
}
1157211572
} ]
1157311573
};
1157411574
}
1157511575
}
1157611576
}
1157711577
function w() {
11578-
return !(m.metricsError || K > 1) && (m.pod && _.get(m, "options.selectedContainer"));
11578+
return window.OPENSHIFT_CONSTANTS.DISABLE_CUSTOM_METRICS ? e.when({}) :j.getCustomMetrics(m.pod).then(function(a) {
11579+
angular.forEach(a, function(a) {
11580+
var b = a.description || a.name, c = a.unit || "";
11581+
m.metrics.push({
11582+
label:b,
11583+
units:c,
11584+
chartPrefix:"custom-" + _.uniqueId("custom-metric-"),
11585+
chartType:"spline",
11586+
datasets:[ {
11587+
id:"custom/" + a.name,
11588+
label:b,
11589+
type:a.type,
11590+
data:[]
11591+
} ]
11592+
});
11593+
});
11594+
});
11595+
}
11596+
function x() {
11597+
return !(m.metricsError || L > 1) && (m.pod && _.get(m, "options.selectedContainer"));
1157911598
}
11580-
function x(a, b, c) {
11599+
function y(a, b, c) {
1158111600
b.total = n(b.id), b.total && (m.hasLimits = !0);
1158211601
var d = _.get(c, "usage.value");
1158311602
isNaN(d) && (d = 0), a.convert && (d = a.convert(d)), b.used = d3.round(d, a.usagePrecision), b.total && (b.available = d3.round(b.total - d, a.usagePrecision)), a.totalUsed += b.used;
1158411603
}
11585-
function y(a, b) {
11604+
function z(a, b) {
1158611605
m.noData = !1;
1158711606
var c = _.initial(b.data);
11588-
return a.data ? void (a.data = _.chain(a.data).takeRight(F).concat(c).value()) :void (a.data = c);
11607+
return a.data ? void (a.data = _.chain(a.data).takeRight(G).concat(c).value()) :void (a.data = c);
1158911608
}
11590-
function z() {
11591-
if (w()) {
11609+
function A() {
11610+
if (x()) {
1159211611
var a = q(), b = [];
1159311612
angular.forEach(m.metrics, function(c) {
1159411613
var d = [];
@@ -11599,16 +11618,16 @@ var g = j.get(f);
1159911618
d.push(g);
1160011619
var h = n(e.id);
1160111620
h && b.push(j.getCurrentUsage(f).then(function(a) {
11602-
x(c, e, a);
11621+
y(c, e, a);
1160311622
}));
1160411623
}
1160511624
}), b = b.concat(d), e.all(d).then(function(a) {
11606-
G || angular.forEach(a, function(a) {
11625+
H || angular.forEach(a, function(a) {
1160711626
if (a) {
1160811627
var b = _.find(c.datasets, {
1160911628
id:a.metricID
1161011629
});
11611-
y(b, a);
11630+
z(b, a);
1161211631
}
1161311632
});
1161411633
});
@@ -11618,7 +11637,7 @@ m.loaded = !0;
1161811637
}
1161911638
}
1162011639
m.includedMetrics = m.includedMetrics || [ "cpu", "memory", "network" ];
11621-
var A, B = {}, C = {}, D = c("resources.limits.memory"), E = c("resources.limits.cpu"), F = 30, G = !1;
11640+
var B, C = {}, D = {}, E = c("resources.limits.memory"), F = c("resources.limits.cpu"), G = 30, H = !1;
1162211641
m.uniqueID = i.uniqueID(), m.metrics = [], _.includes(m.includedMetrics, "memory") && m.metrics.push({
1162311642
label:"Memory",
1162411643
units:"MiB",
@@ -11657,35 +11676,19 @@ id:"network/rx_rate",
1165711676
label:"Received",
1165811677
data:[]
1165911678
} ]
11660-
}), window.OPENSHIFT_CONSTANTS.DISABLE_CUSTOM_METRICS || j.getCustomMetrics(m.pod).then(function(a) {
11661-
angular.forEach(a, function(a) {
11662-
var b = a.description || a.name, c = a.unit || "";
11663-
m.metrics.push({
11664-
label:b,
11665-
units:c,
11666-
chartPrefix:"custom-" + _.uniqueId("custom-metric-"),
11667-
chartType:"spline",
11668-
datasets:[ {
11669-
id:"custom/" + a.name,
11670-
label:b,
11671-
type:a.type,
11672-
data:[]
11673-
} ]
11674-
});
11675-
}), z();
1167611679
}), m.loaded = !1, m.noData = !0, m.showComputeUnitsHelp = function() {
1167711680
k.showComputeUnitsHelp();
1167811681
}, j.getMetricsURL().then(function(a) {
1167911682
m.metricsURL = a;
1168011683
}), m.options = {
1168111684
rangeOptions:i.getTimeRangeOptions()
1168211685
}, m.options.timeRange = _.head(m.options.rangeOptions);
11683-
var H = a("upperFirst"), I = function(a) {
11686+
var I = a("upperFirst"), J = function(a) {
1168411687
var b = "#" + a.chartPrefix + m.uniqueID + "-donut";
1168511688
return {
1168611689
bindto:b,
1168711690
onrendered:function() {
11688-
g.updateDonutCenterText(b, a.datasets[0].used, H(a.units) + " Used");
11691+
g.updateDonutCenterText(b, a.datasets[0].used, I(a.units) + " Used");
1168911692
},
1169011693
donut:{
1169111694
label:{
@@ -11701,24 +11704,26 @@ height:175,
1170111704
widht:175
1170211705
}
1170311706
};
11704-
}, J = function(a) {
11707+
}, K = function(a) {
1170511708
var b = a.chartPrefix + m.uniqueID + "-sparkline", c = i.getDefaultSparklineConfig(b, a.units);
1170611709
return 1 === a.datasets.length && _.set(c, "legend.show", !1), c;
11707-
}, K = 0;
11710+
}, L = 0;
11711+
w()["finally"](function() {
1170811712
m.$watch("options", function() {
1170911713
_.each(m.metrics, function(a) {
1171011714
_.each(a.datasets, function(a) {
1171111715
delete a.data;
1171211716
});
11713-
}), delete m.metricsError, z();
11714-
}, !0), A = b(z, i.getDefaultUpdateInterval(), !1), f.$on("metrics.charts.resize", function() {
11715-
i.redraw(B), i.redraw(C);
11717+
}), delete m.metricsError, A();
11718+
}, !0), B = b(A, i.getDefaultUpdateInterval(), !1);
11719+
}), f.$on("metrics.charts.resize", function() {
11720+
i.redraw(C), i.redraw(D);
1171611721
}), m.$on("$destroy", function() {
11717-
A && (b.cancel(A), A = null), angular.forEach(B, function(a) {
11722+
B && (b.cancel(B), B = null), angular.forEach(C, function(a) {
1171811723
a.destroy();
11719-
}), B = null, angular.forEach(C, function(a) {
11724+
}), C = null, angular.forEach(D, function(a) {
1172011725
a.destroy();
11721-
}), C = null, G = !0;
11726+
}), D = null, H = !0;
1172211727
});
1172311728
}
1172411729
};

0 commit comments

Comments
 (0)