| 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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535 |
1
1
1
1
14
14
14
7
7
1
7
6
6
6
6
6
2
2
2
2
2
2
2
2
2
2
2
6
6
2
2
4
6
2
6
9
5
5
5
5
1
4
1
1
1
1
1
1
4
5
5
5
5
1
4
4
4
1
1
1
1
1
1
4
4
4
5
1
4
4
1
1
2
2
1
4
4
4
4
3
1
1
1
1
4
2
1
1
1
1
1
1
1
1
1
1
3
1
1
1
1
1
1
1
1
1
1
1
1
1
3
3
1
1
1
1
1
1
1
5
5
5
5
5
5
5
5
5
5
5
5
5
5
5
5
5
5
5
5
| 'use strict';
;require.register("controllers/main/host", 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 validator = require('utils/validator');
App.MainHostController = Em.ArrayController.extend(App.TableServerMixin, {
name: 'mainHostController',
clearFilters: null,
filteredCount: 0,
/**
* total number of installed hosts
* @type {number}
*/
totalCount: Em.computed.alias('App.allHostNames.length'),
/**
* @type {boolean}
* @default false
*/
resetStartIndex: false,
startIndex: 1,
/**
* true if any host page filter changed
*/
filterChangeHappened: false,
/**
* if true, do not clean stored filter before hosts page rendering.
*/
showFilterConditionsFirstLoad: false,
saveSelection: false,
content: App.Host.find(),
allHostStackVersions: App.HostStackVersion.find(),
/**
* filterProperties support follow types of filter:
* MATCH - match of RegExp
* EQUAL - equality "="
* LESS - "<"
* MORE - ">"
* MULTIPLE - multiple values to compare
* CUSTOM - substitute values with keys "{#}" in alias
*/
filterProperties: [{
name: 'hostName',
key: 'Hosts/host_name',
type: 'MATCH'
}, {
name: 'ip',
key: 'Hosts/ip',
type: 'MATCH'
}, {
name: 'cpu',
key: 'Hosts/cpu_count',
type: 'EQUAL'
}, {
name: 'memoryFormatted',
key: 'Hosts/total_mem',
type: 'EQUAL'
}, {
name: 'loadAvg',
key: 'metrics/load/load_one',
type: 'EQUAL'
}, {
name: 'rack',
key: 'Hosts/rack_info',
type: 'MATCH'
}, {
name: 'hostComponents',
key: 'host_components/HostRoles/component_name',
type: 'EQUAL',
isComponentRelatedFilter: true
}, {
name: 'services',
key: 'host_components/HostRoles/service_name',
type: 'MATCH',
isComponentRelatedFilter: true
}, {
name: 'state',
key: 'host_components/HostRoles/state',
type: 'MATCH',
isComponentRelatedFilter: true
}, {
name: 'healthClass',
key: 'Hosts/host_status',
type: 'EQUAL'
}, {
name: 'criticalWarningAlertsCount',
key: '(alerts_summary/CRITICAL{0}|alerts_summary/WARNING{1})',
type: 'CUSTOM'
}, {
name: 'componentsWithStaleConfigsCount',
key: 'host_components/HostRoles/stale_configs',
type: 'EQUAL',
isComponentRelatedFilter: true
}, {
name: 'componentsInPassiveStateCount',
key: 'host_components/HostRoles/maintenance_state',
type: 'MULTIPLE',
isComponentRelatedFilter: true
}, {
name: 'selected',
key: 'Hosts/host_name',
type: 'MULTIPLE'
}, {
name: 'version',
key: 'stack_versions/repository_versions/RepositoryVersions/display_name',
type: 'EQUAL'
}, {
name: 'versionState',
key: 'stack_versions/HostStackVersions/state',
type: 'EQUAL'
}, {
name: 'hostStackVersion',
key: 'stack_versions',
type: 'EQUAL'
}, {
name: 'componentState',
key: ['(host_components/HostRoles/component_name={0})', '(host_components/HostRoles/component_name={0}&host_components/HostRoles/state={1})', '(host_components/HostRoles/component_name={0}&host_components/HostRoles/desired_admin_state={1})', '(host_components/HostRoles/component_name={0}&host_components/HostRoles/maintenance_state={1})'],
type: 'COMBO',
isComponentRelatedFilter: true
}],
sortProps: [{
name: 'hostName',
key: 'Hosts/host_name'
}, {
name: 'ip',
key: 'Hosts/ip'
}, {
name: 'cpu',
key: 'Hosts/cpu_count'
}, {
name: 'memoryFormatted',
key: 'Hosts/total_mem'
}, {
name: 'diskUsage',
//TODO disk_usage is relative property and need support from API, metrics/disk/disk_free used temporarily
key: 'metrics/disk/disk_free'
}, {
name: 'rack',
key: 'Hosts/rack_info'
}, {
name: 'loadAvg',
key: 'metrics/load/load_one'
}],
/**
* Validate and convert input string to valid url parameter.
* Detect if user have passed string as regular expression or extend
* string to regexp.
*
* @param {String} value
* @return {String}
**/
getRegExp: function getRegExp(value) {
value = validator.isValidMatchesRegexp(value) ? value.replace(/(\.+\*?|(\.\*)+)$/, '') + '.*' : '^$';
value = /^\.\*/.test(value) || value == '^$' ? value : '.*' + value;
return value;
},
/**
* Sort by host_name by default
* @method getSortProps
* @returns {{value: 'asc|desc', name: string, type: 'SORT'}[]}
*/
getSortProps: function getSortProps() {
var controllerName = this.get('name'),
db = App.db.getSortingStatuses(controllerName);
if (db && db.everyProperty('status', 'sorting')) {
App.db.setSortingStatuses(controllerName, {
name: 'hostName',
status: 'sorting_asc'
});
}
return this._super();
},
/**
* get query parameters computed from filter properties, sort properties and custom properties of view
* @param {boolean} [skipNonFilterProperties]
* @return {Array}
* @method getQueryParameters
*/
getQueryParameters: function getQueryParameters(skipNonFilterProperties) {
skipNonFilterProperties = skipNonFilterProperties || false;
var queryParams = [],
savedFilterConditions = App.db.getFilterConditions(this.get('name')) || [],
colPropAssoc = this.get('colPropAssoc'),
filterProperties = this.get('filterProperties'),
sortProperties = this.get('sortProps'),
oldProperties = App.router.get('updateController.queryParams.Hosts');
this.set('resetStartIndex', false);
queryParams.pushObjects(this.getPaginationProps());
savedFilterConditions.forEach(function (filter) {
var property = filterProperties.findProperty('name', colPropAssoc[filter.iColumn]);
Eif (property && filter.value.length > 0 && !filter.skipFilter) {
var result = {
key: property.key,
value: filter.value,
type: property.type,
isFilter: true,
isComponentRelatedFilter: property.isComponentRelatedFilter
};
Eif (filter.type === 'string' && sortProperties.someProperty('name', colPropAssoc[filter.iColumn])) {
Iif (Em.isArray(filter.value)) {
for (var i = 0; i < filter.value.length; i++) {
filter.value[i] = this.getRegExp(filter.value[i]);
}
} else {
result.value = this.getRegExp(filter.value);
}
}
Iif (filter.type === 'number' || filter.type === 'ambari-bandwidth') {
result.type = this.getComparisonType(filter.value);
result.value = this.getProperValue(filter.value);
}
// enter an exact number for RAM filter, need to do a range number match for this
Iif (filter.type === 'ambari-bandwidth' && result.type == 'EQUAL' && result.value) {
var valuePair = this.convertMemoryToRange(filter.value);
queryParams.push({
key: result.key,
value: valuePair[0],
type: 'MORE'
});
queryParams.push({
key: result.key,
value: valuePair[1],
type: 'LESS'
});
} else Iif (filter.type === 'ambari-bandwidth' && result.type != 'EQUAL' && result.value) {
// enter a comparison type, eg > 1, just do regular match
result.value = this.convertMemory(filter.value);
queryParams.push(result);
} else Iif (filter.type === 'sub-resource') {
filter.value.forEach(function (item) {
queryParams.push({
key: result.key + "/" + item.property,
value: item.value,
type: 'EQUAL'
});
}, this);
} else {
queryParams.push(result);
}
}
}, this);
Eif (!oldProperties.findProperty('isHostDetails')) {
// shouldn't reset start index after coming back from Host Details page
if (queryParams.filterProperty('isFilter').length !== oldProperties.filterProperty('isFilter').length) {
queryParams.findProperty('key', 'from').value = 0;
this.set('resetStartIndex', true);
} else {
queryParams.filterProperty('isFilter').forEach(function (queryParam) {
var oldProperty = oldProperties.filterProperty('isFilter').findProperty('key', queryParam.key);
if (!oldProperty || JSON.stringify(oldProperty.value) !== JSON.stringify(queryParam.value)) {
queryParams.findProperty('key', 'from').value = 0;
this.set('resetStartIndex', true);
}
}, this);
}
}
if (!skipNonFilterProperties) {
queryParams.pushObjects(this.getSortProps());
}
return queryParams;
},
/**
* Return value without predicate
* @param {String} value
* @return {String}
*/
getProperValue: function getProperValue(value) {
return ['>', '<', '='].contains(value.charAt(0)) ? value.substr(1, value.length) : value;
},
/**
* Return value converted to kilobytes
* @param {String} value
* @return {number}
*/
convertMemory: function convertMemory(value) {
var scale = value.charAt(value.length - 1);
// first char may be predicate for comparison
value = this.getProperValue(value);
var parsedValue = parseFloat(value);
if (isNaN(parsedValue)) {
return value;
}
switch (scale) {
case 'g':
parsedValue *= 1048576;
break;
case 'm':
parsedValue *= 1024;
break;
case 'k':
break;
default:
//default value in GB
parsedValue *= 1048576;
}
return Math.round(parsedValue);
},
/**
* Return value converted to a range of kilobytes
* @param {String} value
* @return {Array}
*/
convertMemoryToRange: function convertMemoryToRange(value) {
var scale = value.charAt(value.length - 1);
// first char may be predicate for comparison
value = this.getProperValue(value);
var parsedValue = parseFloat(value);
if (isNaN(parsedValue)) {
return [0, 0];
}
var parsedValuePair = this.rangeConvertNumber(parsedValue, scale);
var multiplyingFactor = 1;
switch (scale) {
case 'g':
multiplyingFactor = 1048576;
break;
case 'm':
multiplyingFactor = 1024;
break;
case 'k':
break;
default:
//default value in GB
multiplyingFactor = 1048576;
}
parsedValuePair[0] = Math.round(parsedValuePair[0] * multiplyingFactor);
parsedValuePair[1] = Math.round(parsedValuePair[1] * multiplyingFactor);
return parsedValuePair;
},
/**
* Return value converted to a range of kilobytes
* eg, return value 1.83 g will target 1.82500 ~ 1.83499 g
* eg, return value 1.8 k will target 1.7500 ~ 1.8499 k
* eg, return value 1.8 m will target 1.7500 ~ 1.8499 m
* @param {number} value
* @param {String} scale
* @return {Array}
*/
rangeConvertNumber: function rangeConvertNumber(value, scale) {
if (isNaN(value)) {
return [0, 0];
}
var valuePair = [];
switch (scale) {
case 'g':
valuePair = [value - 0.005000, value + 0.004999999];
break;
case 'm':
case 'k':
valuePair = [value - 0.05000, value + 0.04999];
break;
default:
//default value in GB
valuePair = [value - 0.005000, value + 0.004999999];
}
return valuePair;
},
/**
* Return comparison type depending on populated predicate
* @param {string} value
* @return {string}
*/
getComparisonType: function getComparisonType(value) {
var comparisonChar = value.charAt(0);
var result = 'EQUAL';
if (isNaN(comparisonChar)) {
switch (comparisonChar) {
case '>':
result = 'MORE';
break;
case '<':
result = 'LESS';
break;
}
}
return result;
},
labelValueMap: {},
/**
* Filter hosts by componentName of <code>component</code>
* @param {App.HostComponent} component
*/
filterByComponent: function filterByComponent(component) {
if (!component) return;
var componentName = component.get('componentName');
var displayName = App.format.role(componentName, false);
var colPropAssoc = this.get('colPropAssoc');
var map = this.get('labelValueMap');
var filterForComponent = {
iColumn: 15,
value: componentName + ':ALL',
type: 'string'
};
map[displayName] = componentName;
map['All'] = 'ALL';
var filterStr = '"' + displayName + '"' + ': "All"';
App.db.setFilterConditions(this.get('name'), [filterForComponent]);
App.db.setComboSearchQuery(this.get('name'), filterStr);
},
/**
* Filter hosts by stack version and state
* @param {String} displayName
* @param {Array} states
*/
filterByStack: function filterByStack(displayName, states) {
if (Em.isNone(displayName) || Em.isNone(states) || !states.length) return;
var colPropAssoc = this.get('colPropAssoc');
var map = this.get('labelValueMap');
var stateFilterStrs = [];
var versionFilter = {
iColumn: 16,
value: displayName,
type: 'string'
};
var stateFilter = {
iColumn: 17,
value: states,
type: 'string'
};
map["Stack Version"] = colPropAssoc[versionFilter.iColumn];
map["Version State"] = colPropAssoc[stateFilter.iColumn];
stateFilter.value.forEach(function (state) {
map[App.HostStackVersion.formatStatus(state)] = state;
stateFilterStrs.push('"Version State": "' + App.HostStackVersion.formatStatus(state) + '"');
});
var versionFilterStr = '"Stack Version": "' + versionFilter.value + '"';
App.db.setFilterConditions(this.get('name'), [versionFilter, stateFilter]);
App.db.setComboSearchQuery(this.get('name'), [versionFilterStr, stateFilterStrs.join(' ')].join(' '));
},
goToHostAlerts: function goToHostAlerts(event) {
var host = event && event.context;
if (host) {
App.router.transitionTo('main.hosts.hostDetails.alerts', host);
}
},
/**
* remove selected hosts
*/
removeHosts: function removeHosts() {
var hosts = this.get('content');
var selectedHosts = hosts.filterProperty('isChecked');
this.get('fullContent').removeObjects(selectedHosts);
},
/**
* remove hosts with id equal host_id
* @param {String} host_id
*/
checkRemoved: function checkRemoved(host_id) {
var hosts = this.get('content');
var selectedHosts = hosts.filterProperty('id', host_id);
this.get('fullContent').removeObjects(selectedHosts);
},
/**
* associations between host property and column index
* @type {Array}
*/
colPropAssoc: function () {
var associations = [];
associations[0] = 'healthClass';
associations[1] = 'hostName';
associations[2] = 'ip';
associations[3] = 'cpu';
associations[4] = 'memoryFormatted';
associations[5] = 'loadAvg';
associations[6] = 'hostComponents';
associations[7] = 'criticalWarningAlertsCount';
associations[8] = 'componentsWithStaleConfigsCount';
associations[9] = 'componentsInPassiveStateCount';
associations[10] = 'selected';
associations[11] = 'hostStackVersion';
associations[12] = 'rack';
associations[13] = 'services';
associations[14] = 'state';
associations[15] = 'componentState';
associations[16] = 'version';
associations[17] = 'versionState';
return associations;
}.property()
});
}); |