| 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 |
1
1
1
1
2
1
2
2
2
2
1
1
7
6
6
6
6
2
6
6
6
6
3
9
9
9
9
6
6
9
6
6
6
7
7
5
5
3
2
2
2
6
4
2
2
9
1
1
1
| 'use strict';
;require.register("views/common/widget/heatmap_widget_view", function (exports, require, module) {
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
var App = require('app');
var numberUtils = require('utils/number_utils');
App.HeatmapWidgetView = Em.View.extend(App.WidgetMixin, {
templateName: require('templates/common/widget/heatmap_widget'),
/**
* common metrics container
* @type {Array}
*/
metrics: [],
/**
* racks container bound in the template
* @type {Array}
*/
racks: [],
/**
* @type {$.ajax|null}
* @default null
*/
activeRequest: null,
onMetricsLoaded: function onMetricsLoaded() {
if (!this.get('isLoaded')) {
this.set('controller.inputMaximum', this.get('content.properties.max_limit'));
}
this._super();
},
willDestroyElement: function willDestroyElement() {
Eif ($.isPlainObject(this.get('activeRequest'))) {
this.get('activeRequest').abort();
this.set('activeRequest', null);
}
},
getHostComponentsMetrics: function getHostComponentsMetrics(request) {
var ajax = this._super(request);
this.set('activeRequest', ajax);
return ajax;
},
getHostsMetrics: function getHostsMetrics(request) {
var ajax = this._super(request);
this.set('activeRequest', ajax);
return ajax;
},
/**
* skip metrics loading if AMBARI METRICS service is not started
*/
loadMetrics: function loadMetrics() {
Iif (App.Service.find('AMBARI_METRICS').get('isStarted')) {
this._super();
} else {
this.onMetricsLoaded();
}
},
/**
* draw widget
*/
drawWidget: function () {
if (this.get('isLoaded')) {
var hostToValueMap = this.calculateValues();
var hostNames = [];
Eif (this.get('racks').everyProperty('isLoaded', true)) {
this.get('racks').forEach(function (rack) {
hostNames = hostNames.concat(rack.hosts.mapProperty('hostName'));
});
}
var metricObject = App.MainChartHeatmapMetric.create({
name: this.get('content.widgetName'),
units: this.get('content.properties.display_unit'),
maximumValue: this.get('controller.inputMaximum'),
hostNames: hostNames,
hostToValueMap: hostToValueMap
});
this.set('controller.selectedMetric', metricObject);
App.loadTimer.finish('Heatmaps Page');
App.loadTimer.finish('Service Heatmaps Page');
}
}.observes('racks.@each.isLoaded'),
/**
* calculate value for heatmap widgets
* @returns {Object}
*/
calculateValues: function calculateValues() {
return this.computeExpression(this.extractExpressions(this.get('content.values')[0]), this.get('metrics'));
},
/**
* compute expression
* @param {Array} expressions
* @param {Array} metrics
* @returns {object}
*/
computeExpression: function computeExpression(expressions, metrics) {
var hostToValueMap = {};
var hostNames = metrics.mapProperty('hostName');
var metricsMap = {};
metrics.forEach(function (_metric) {
this.convertDataWhenMB(_metric);
metricsMap[_metric.name + "_" + _metric.hostName] = _metric;
}, this);
hostNames.forEach(function (_hostName) {
expressions.forEach(function (_expression) {
var validExpression = true;
//replace values with metrics data
var beforeCompute = _expression.replace(this.get('VALUE_NAME_REGEX'), function (match) {
var _metric;
if (window.isNaN(match)) {
_metric = metricsMap[match + "_" + _hostName];
if (_metric) {
return _metric.data;
} else {
validExpression = false;
console.warn('Metrics with name "' + match + '" not found to compute expression');
}
} else {
return match;
}
});
if (validExpression && this.get('MATH_EXPRESSION_REGEX').test(beforeCompute)) {
hostToValueMap[_hostName] = Number(window.eval(beforeCompute)).toString();
} else Eif (beforeCompute.contains('undefined')) {
// Data not available
hostToValueMap[_hostName] = undefined;
} else {
console.error('Value for metric is not correct mathematical expression: ' + beforeCompute);
}
}, this);
}, this);
return hostToValueMap;
},
/**
* If metric has value in MB convert it to bytes
* @param _metric
*/
convertDataWhenMB: function convertDataWhenMB(_metric) {
Eif (_metric.metric_path.endsWith('M') && !Em.isNone(_metric.data) && isFinite(_metric.data)) {
_metric.originalData = _metric.data;
_metric.data = Math.round(_metric.data * numberUtils.BYTES_IN_MB);
}
}
});
}); |