| 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 |
1
1
1
1
1
7
7
7
7
7
7
7
7
4
4
4
4
7
7
7
7
7
7
7
1
1
1
1
1
12
12
8
4
1
3
2
2
1
12
7
7
4
4
4
4
2
2
2
1
4
5
5
5
5
5
5
4
4
1
3
1
2
2
4
2
5
4
4
2
2
1
1
1
4
1
3
2
1
1
| 'use strict';
;require.register("controllers/main/charts/heatmap_metrics/heatmap_metric", 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 date = require('utils/date/date');
var numberUtils = require('utils/number_utils');
/**
* Base class for any heatmap metric.
*
* This class basically provides the following for each heatmap metric.
* <ul>
* <li> Provides number of slots in which temperature can fall.
* <li> Maintains the maximum value so as to scale slot ranges.
* <li> Gets JSON data from server and maps response for all hosts into above
* slots.
* </ul>
*
*/
App.MainChartHeatmapMetric = Em.Object.extend({
/**
* Name of this metric
*/
name: null,
/**
* Number of slots this metric will be mapped into. When changing this value,
* the color count in 'slotColors' should also be changed.
*/
numberOfSlots: 5,
/**
* Colors for the each of the number of slots defined above. When changing the
* number of slots, the number of colors also should be updated.
*
* @type {Array}
*/
slotColors: ['#1EB475', '#1FB418', '#E9E23F', '#E9A840', '#EF6162'],
/**
* Minimum value of this metric. Default is 0.
*/
minimumValue: 0,
/**
* Maximum value of this metric. This has to be specified by extending classes
* so that the range from 'minimumValue' to 'maximumValue' can be split among
* 'numberOfSlots'. It is recommended that this value be a multiple of
* 'numberOfSlots'.
*/
maximumValue: 100,
/**
* Units of the maximum value which is shown in UI {String}
*/
units: '',
/**
* Provides following information about slots in an array of objects.
* <ul>
* <li> from: {number} Slot starts from this value
* <li> to: {number} Slot ends at this value (inclusive)
* <li> label: {String} Slot name to be shown
* <li> cssStyle: {String} style to be embedded on hosts which fall into this
* slot.
* </ul>
*
* Slot count will be the same as specified in 'numberOfSlots'. Slot
* definitions will be given in increasing temperature from 'minimumValue' to
* 'maximumValue'.
*
*/
slotDefinitions: function () {
var max = this.getMaximumValue();
var slotCount = this.get('numberOfSlots');
var units = this.get('units');
var delta = (max - this.get('minimumValue')) / slotCount;
var defs = [];
var slotColors = this.get('slotColors');
var slotColorIndex = 0;
for (var c = 0; c < slotCount - 1; c++) {
var slotColor = slotColors[slotColorIndex++];
var start = c * delta;
var end = (c + 1) * delta;
defs.push(this.generateSlot(start, end, units, slotColor));
}
slotColor = slotColors[slotColorIndex++];
start = (slotCount - 1) * delta;
defs.push(this.generateSlot(start, max, units, slotColor));
defs.push(Em.Object.create({
invalidData: true,
index: defs.length,
label: Em.I18n.t('charts.heatmap.label.invalidData'),
cssStyle: this.getHatchStyle()
}));
defs.push(Em.Object.create({
notAvailable: true,
index: defs.length,
label: Em.I18n.t('charts.heatmap.label.notAvailable'),
cssStyle: "background-color: #666"
}));
defs.push(Em.Object.create({
notApplicable: true,
index: defs.length,
label: Em.I18n.t('charts.heatmap.label.notApplicable'),
cssStyle: "background-color:#ccc"
}));
return defs;
}.property('minimumValue', 'maximumValue', 'numberOfSlots'),
/**
* genarate slot by given parameters
*
* @param min
* @param max
* @param units
* @param slotColor
* @return {object}
* @method generateSlot
*/
generateSlot: function generateSlot(min, max, units, slotColor) {
var fromLabel = this.formatLegendLabel(min, units);
var toLabel = this.formatLegendLabel(max, units);
var from = this.convertNumber(min, units);
var to = this.convertNumber(max, units);
return Em.Object.create({
hasBoundaries: true,
from: from,
to: to,
label: fromLabel + " - " + toLabel,
cssStyle: "background-color:" + slotColor
});
},
/**
* calculate hatch style of slot according to browser version used
* @return {String}
*/
getHatchStyle: function getHatchStyle() {
var hatchStyle = "background-color:#666";
if (jQuery.browser.webkit) {
hatchStyle = "background-image:-webkit-repeating-linear-gradient(-45deg, #666, #666 6px, #fff 6px, #fff 7px)";
} else if (jQuery.browser.mozilla) {
hatchStyle = "background-image:repeating-linear-gradient(-45deg, #666, #666 6px, #fff 6px, #fff 7px)";
} else if (jQuery.browser.msie && jQuery.browser.version) {
var majorVersion = parseInt(jQuery.browser.version.split('.')[0]);
if (majorVersion > 9) {
hatchStyle = "background-image:repeating-linear-gradient(-45deg, #666, #666 6px, #fff 6px, #fff 7px)";
}
}
return hatchStyle;
},
/**
* compatible with special input value, such as negative float number or string
* @return {float}
*/
getMaximumValue: function getMaximumValue() {
var max = this.get('maximumValue') ? parseFloat(this.get('maximumValue')) : 0;
return isNaN(max) ? 0 : Math.max(0, max);
},
/**
* In slot definitions this value is used to construct the label by appending
* it to slot min-max values. For example giving '%' here would result in slot
* definition label being '0% - 10%'.
*/
slotDefinitionLabelSuffix: '',
hostToValueMap: null,
hostToSlotMap: function () {
var hostToValueMap = this.get('hostToValueMap');
var hostNames = this.get('hostNames');
var hostToSlotMap = {};
if (hostToValueMap && hostNames) {
hostNames.forEach(function (hostName) {
var slot = this.calculateSlot(hostToValueMap, hostName);
if (slot > -1) {
hostToSlotMap[hostName] = slot;
}
}, this);
}
return hostToSlotMap;
}.property('hostToValueMap', 'slotDefinitions'),
/**
* calculate slot position in hostToSlotMap by hostname
* @param hostToValueMap
* @param hostName
* @return {Number}
*/
calculateSlot: function calculateSlot(hostToValueMap, hostName) {
var slotDefinitions = this.get('slotDefinitions');
var slotWithBoundaries = slotDefinitions.filterProperty('hasBoundaries');
var invalidDataSlot = slotDefinitions.findProperty('invalidData').get('index');
var notAvailableDataSlot = slotDefinitions.findProperty('notAvailable').get('index');
var slot = slotDefinitions.findProperty('notApplicable').get('index');
if (hostName in hostToValueMap) {
var value = hostToValueMap[hostName];
if (Em.isNone(value)) {
slot = notAvailableDataSlot;
} else if (isNaN(value) || !isFinite(value) || value < 0) {
slot = invalidDataSlot;
} else {
value = Number(value);
slotWithBoundaries.forEach(function (slotDef, slotIndex, array) {
if (value >= slotDef.from && value <= slotDef.to ||
// If value exceeds maximum then it pushed to the last/maximal slot
value > slotDef.to && slotIndex === array.length - 1) {
slot = slotIndex;
}
});
}
}
return slot;
},
/**
* Turns numbers into displayable values. For example 24.345432425 into 24.3
* etc.
*
* @private
*/
formatLegendLabel: function formatLegendLabel(num, units) {
var fraction = num % 1;
if (num >= 100) {
num = Math.round(num);
} else if (num >= 10 && fraction > 0) {
num = parseFloat(num.toFixed(1));
} else Eif (fraction > 0) {
num = parseFloat(num.toFixed(2));
}
if (units === 'ms') {
return date.timingFormat(num);
}
return num + units;
},
/**
* return converted value
*
* @param {number} number
* @param {string} units
*/
convertNumber: function convertNumber(number, units) {
if (units === 'MB') {
return Math.round(number * numberUtils.BYTES_IN_MB);
}
return number;
}
});
}); |