Skip to content

Commit 8f964a8

Browse files
author
OpenShift Bot
authored
Merge pull request #1018 from spadgett/refactor-metrics
Merged by openshift-bot
2 parents 3579a80 + e3ce330 commit 8f964a8

File tree

6 files changed

+429
-503
lines changed

6 files changed

+429
-503
lines changed

Diff for: app/index.html

+1
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,7 @@ <h1>JavaScript Required</h1>
190190
<script src="scripts/services/membership/roles.js"></script>
191191
<script src="scripts/services/membership/roleBindings.js"></script>
192192
<script src="scripts/services/metrics.js"></script>
193+
<script src="scripts/services/metricsCharts.js"></script>
193194
<script src="scripts/services/storage.js"></script>
194195
<script src="scripts/services/constants.js"></script>
195196
<script src="scripts/services/limits.js"></script>

Diff for: app/scripts/directives/deploymentMetrics.js

+11-87
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ angular.module('openshiftConsole')
88
$rootScope,
99
ChartsService,
1010
ConversionService,
11+
MetricsCharts,
1112
MetricsService) {
1213
return {
1314
restrict: 'E',
@@ -29,14 +30,13 @@ angular.module('openshiftConsole')
2930
link: function(scope) {
3031
var chartByMetric = {};
3132
var intervalPromise;
32-
var updateInterval = 60 * 1000; // 60 seconds
3333
var numDataPoints = 30;
3434
var compact = scope.profile === 'compact';
3535

3636
// Set to true when the route changes so we don't update charts that no longer exist.
3737
var destroyed = false;
3838

39-
scope.uniqueID = _.uniqueId('metrics-');
39+
scope.uniqueID = MetricsCharts.uniqueID();
4040

4141
// Map of metric.type -> podName -> metrics data
4242
var data = {};
@@ -104,95 +104,24 @@ angular.module('openshiftConsole')
104104

105105
// Relative time options.
106106
scope.options = {
107-
rangeOptions: [{
108-
label: "Last hour",
109-
value: 60
110-
}, {
111-
label: "Last 4 hours",
112-
value: 4 * 60
113-
}, {
114-
label: "Last day",
115-
value: 24 * 60
116-
}, {
117-
label: "Last 3 days",
118-
value: 3 * 24 * 60
119-
}, {
120-
label: "Last week",
121-
value: 7 * 24 * 60
122-
}]
107+
rangeOptions: MetricsCharts.getTimeRangeOptions()
123108
};
124109
// Show last hour by default.
125110
scope.options.timeRange = _.head(scope.options.rangeOptions);
126111
scope.options.selectedContainer = _.head(scope.containers);
127112

128113
var createSparklineConfig = function(metric) {
129-
return {
130-
bindto: '#' + metric.chartID,
131-
axis: {
132-
x: {
133-
show: !compact,
134-
type: 'timeseries',
135-
// With default padding you can have negative axis tick values.
136-
padding: {
137-
left: 0,
138-
bottom: 0
139-
},
140-
tick: {
141-
type: 'timeseries',
142-
format: '%a %H:%M'
143-
}
144-
},
145-
y: {
146-
show: !compact,
147-
label: metric.units,
148-
min: 0,
149-
// With default padding you can have negative axis tick values.
150-
padding: {
151-
left: 0,
152-
bottom: 0,
153-
top: 20
154-
},
155-
tick: {
156-
format: function(value) {
157-
return d3.round(value, 3);
158-
}
159-
}
160-
}
161-
},
162-
legend: {
163-
show: !compact && !scope.showAverage
164-
},
165-
point: {
166-
show: false
167-
},
168-
size: {
169-
height: compact ? 35 : 175
170-
},
171-
tooltip: {
172-
format: {
173-
value: function(value) {
174-
return d3.round(value, 2) + " " + metric.units;
175-
}
176-
}
177-
}
178-
};
114+
var config = MetricsCharts.getDefaultSparklineConfig(metric.chartID, metric.units, compact);
115+
_.set(config, 'legend.show', !compact && !scope.showAverage);
116+
117+
return config;
179118
};
180119

181120
function isNil(point) {
182121
return point.value === null || point.value === undefined;
183122
}
184123

185-
scope.formatUsage = function(usage) {
186-
if (usage < 0.01) {
187-
return '0';
188-
}
189-
190-
if (usage < 1) {
191-
return d3.format('.1r')(usage);
192-
}
193-
194-
return d3.format('.2r')(usage);
195-
};
124+
scope.formatUsage = MetricsCharts.formatUsage;
196125

197126
function averages(metric) {
198127
var label;
@@ -490,26 +419,21 @@ angular.module('openshiftConsole')
490419

491420
update();
492421
}, true);
493-
// Also update every 30 seconds.
494-
intervalPromise = $interval(update, updateInterval, false);
422+
intervalPromise = $interval(update, MetricsCharts.getDefaultUpdateInterval(), false);
495423

496424
// Pause or resume metrics updates when the element scrolls into and
497425
// out of view.
498426
scope.updateInView = function(inview) {
499427
paused = !inview;
500428

501429
// Update now if in view and it's been longer than updateInterval.
502-
if (inview && (!lastUpdated || Date.now() > (lastUpdated + updateInterval))) {
430+
if (inview && (!lastUpdated || Date.now() > (lastUpdated + MetricsCharts.getDefaultUpdateInterval()))) {
503431
update();
504432
}
505433
};
506434

507435
$rootScope.$on('metrics.charts.resize', function(){
508-
$timeout(function() {
509-
_.each(chartByMetric, function(chart) {
510-
chart.flush();
511-
});
512-
}, 0);
436+
MetricsCharts.redraw(chartByMetric);
513437
});
514438

515439
scope.$on('$destroy', function() {

Diff for: app/scripts/directives/podMetrics.js

+13-118
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,13 @@ angular.module('openshiftConsole')
99
$rootScope,
1010
ChartsService,
1111
ConversionService,
12+
MetricsCharts,
1213
MetricsService,
1314
usageValueFilter) {
1415
return {
1516
restrict: 'E',
1617
scope: {
1718
pod: '=',
18-
sparklineWidth: '=?',
19-
sparklineHeight: '=?',
2019
includedMetrics: '=?', // defaults to ["cpu", "memory", "network"]
2120
stackDonut: '=?', // Keep donut on top of sparkline (e.g. on the monitoring page)
2221
alerts: '=?'
@@ -29,15 +28,13 @@ angular.module('openshiftConsole')
2928
var getMemoryLimit = $parse('resources.limits.memory');
3029
var getCPULimit = $parse('resources.limits.cpu');
3130

32-
var updateInterval = 60 * 1000; // 60 seconds
33-
3431
// Number of data points to display on the chart.
3532
var numDataPoints = 30;
3633

3734
// Set to true when the route changes so we don't update charts that no longer exist.
3835
var destroyed = false;
3936

40-
scope.uniqueID = _.uniqueId('metrics-chart-');
37+
scope.uniqueID = MetricsCharts.uniqueID();
4138

4239
// Metrics to display.
4340
scope.metrics = [];
@@ -106,24 +103,8 @@ angular.module('openshiftConsole')
106103

107104
// Relative time options.
108105
scope.options = {
109-
rangeOptions: [{
110-
label: "Last hour",
111-
value: 60
112-
}, {
113-
label: "Last 4 hours",
114-
value: 4 * 60
115-
}, {
116-
label: "Last day",
117-
value: 24 * 60
118-
}, {
119-
label: "Last 3 days",
120-
value: 3 * 24 * 60
121-
}, {
122-
label: "Last week",
123-
value: 7 * 24 * 60
124-
}]
106+
rangeOptions: MetricsCharts.getTimeRangeOptions()
125107
};
126-
// Show last hour by default.
127108
scope.options.timeRange = _.head(scope.options.rangeOptions);
128109

129110
var upperFirst = $filter('upperFirst');
@@ -152,57 +133,8 @@ angular.module('openshiftConsole')
152133
};
153134

154135
var createSparklineConfig = function(metric) {
155-
return {
156-
bindto: '#' + metric.chartPrefix + scope.uniqueID + '-sparkline',
157-
axis: {
158-
x: {
159-
show: true,
160-
type: 'timeseries',
161-
// With default padding you can have negative axis tick values.
162-
padding: {
163-
left: 0,
164-
bottom: 0
165-
},
166-
tick: {
167-
type: 'timeseries',
168-
format: '%a %H:%M'
169-
}
170-
},
171-
y: {
172-
show: true,
173-
label: metric.units,
174-
min: 0,
175-
// With default padding you can have negative axis tick values.
176-
padding: {
177-
left: 0,
178-
top: 20,
179-
bottom: 0
180-
},
181-
tick: {
182-
format: function(value) {
183-
return d3.round(value, 3);
184-
}
185-
}
186-
}
187-
},
188-
legend: {
189-
show: metric.datasets.length > 1
190-
},
191-
point: {
192-
show: false
193-
},
194-
size: {
195-
height: scope.sparklineHeight || 175,
196-
width: scope.sparklineWidth,
197-
},
198-
tooltip: {
199-
format: {
200-
value: function(value) {
201-
return d3.round(value, 2) + " " + metric.units;
202-
}
203-
}
204-
}
205-
};
136+
var chartID = metric.chartPrefix + scope.uniqueID + '-sparkline';
137+
return MetricsCharts.getDefaultSparklineConfig(chartID, metric.units);
206138
};
207139

208140
function getLimit(metricID) {
@@ -267,49 +199,22 @@ angular.module('openshiftConsole')
267199
}
268200

269201
function updateSparkline(metric) {
270-
var dates, values = {};
271-
272202
var missingData = _.some(metric.datasets, function(dataset) {
273203
return !dataset.data;
274204
});
275205
if (missingData) {
276206
return;
277207
}
278208

279-
angular.forEach(metric.datasets, function(dataset) {
280-
var metricID = dataset.id, metricData = dataset.data;
281-
dates = ['dates'], values[metricID] = [dataset.label || metricID];
282-
angular.forEach(metricData, function(point) {
283-
dates.push(point.start);
284-
if (point.value === undefined || point.value === null) {
285-
// Don't attempt to round null values. These appear as gaps in the chart.
286-
values[metricID].push(point.value);
287-
} else {
288-
var value = metric.convert ? metric.convert(point.value) : point.value;
289-
switch (metricID) {
290-
case 'memory/usage':
291-
case 'network/rx':
292-
case 'network/tx':
293-
values[metricID].push(d3.round(value, 2));
294-
break;
295-
default:
296-
values[metricID].push(d3.round(value));
297-
}
298-
}
299-
});
300-
209+
var dataByID = {};
210+
_.each(metric.datasets, function(dataset) {
211+
dataByID[dataset.id] = dataset.data;
301212
});
302213

303-
var columns = [dates].concat(_.values(values));
304-
305-
var sparklineConfig, sparklineData = {
306-
type: metric.chartType || 'spline',
307-
x: 'dates',
308-
columns: columns
309-
};
310-
214+
var sparklineData = MetricsCharts.getSparklineData(dataByID);
311215
var chartId = metric.chartPrefix + "sparkline";
312216

217+
var sparklineConfig;
313218
if (!sparklineByMetric[chartId]) {
314219
sparklineConfig = createSparklineConfig(metric);
315220
sparklineConfig.data = sparklineData;
@@ -558,21 +463,11 @@ angular.module('openshiftConsole')
558463
delete scope.metricsError;
559464
update();
560465
}, true);
561-
// Also update every 30 seconds.
562-
intervalPromise = $interval(update, updateInterval, false);
466+
intervalPromise = $interval(update, MetricsCharts.getDefaultUpdateInterval(), false);
563467

564468
$rootScope.$on('metrics.charts.resize', function() {
565-
_.each(donutByMetric, function(chart) {
566-
setTimeout(function() {
567-
chart.flush();
568-
});
569-
});
570-
571-
_.each(sparklineByMetric, function(chart) {
572-
setTimeout(function() {
573-
chart.flush();
574-
});
575-
});
469+
MetricsCharts.redraw(donutByMetric);
470+
MetricsCharts.redraw(sparklineByMetric);
576471
});
577472

578473
scope.$on('$destroy', function() {

Diff for: app/scripts/services/metrics.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,6 @@ angular.module("openshiftConsole")
169169
});
170170
};
171171

172-
173172
return {
174173
// Check if the metrics service is available. The service is considered
175174
// available if a metrics URL is set. Returns a promise resolved with a
@@ -256,6 +255,6 @@ angular.module("openshiftConsole")
256255
// end: end time in millis (optional)
257256
//
258257
// Returns a promise resolved with the metrics data.
259-
getPodMetrics: getPodMetrics,
258+
getPodMetrics: getPodMetrics
260259
};
261260
});

0 commit comments

Comments
 (0)