| 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 |
1
1
1
1
3
2
2
2
2
2
2
2
1
1
1
1
1
26
26
26
68
24
44
26
25
18
18
11
1
26
26
26
26
8
1
3
1
1
1
11
11
11
3
1
6
6
6
6
5
2
2
2
2
3
3
1
6
6
6
4
1
1
1
3
1
2
1
1
1
1
1
1
1
1
| "use strict";
;require.register("views/main/service/widgets/create/expression_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 misc = require('utils/misc');
var number_utils = require("utils/number_utils");
App.WidgetWizardExpressionView = Em.View.extend({
templateName: require('templates/main/service/widgets/create/expression'),
/**
* @type {Array}
*/
classNames: ['metric-container'],
/**
* @type {Array}
*/
classNameBindings: ['isInvalid'],
/**
* list of operators that can be used in expression
* @type {Array}
* @constant
*/
OPERATORS: ["+", "-", "*", "/", "(", ")"],
/**
* @type {Array}
* @const
*/
AGGREGATE_FUNCTIONS: ['avg', 'sum', 'min', 'max', 'rate'],
/**
* @type {RegExp}
* @const
*/
VALID_EXPRESSION_REGEX: /^((\(\s)*[\d]+)[\(\)\+\-\*\/\.\d\s]*[\d\)]*$/,
/**
* contains expression data before editing in order to restore previous state
*/
dataBefore: [],
/**
* @type {Ember.Object}
*/
expression: null,
/**
* @type {boolean}
*/
isInvalid: false,
/**
* @type {boolean}
*/
isInvalidTextfield: false,
/**
* contains value of number added to expression
* @type {string}
*/
numberValue: "",
/**
* @type {boolean}
*/
isNumberValueInvalid: function () {
return this.get('numberValue').trim() === "" || !number_utils.isPositiveNumber(this.get('numberValue').trim());
}.property('numberValue'),
/**
* add operator to expression data
* @param event
*/
addOperator: function addOperator(event) {
var data = this.get('expression.data');
var lastId = data.length > 0 ? Math.max.apply(this, data.mapProperty('id')) : 0;
data.pushObject(Em.Object.create({
id: ++lastId,
name: event.context,
isOperator: true
}));
},
/**
* add operator to expression data
*/
addNumber: function addNumber() {
var data = this.get('expression.data');
var lastId = data.length > 0 ? Math.max.apply(this, data.mapProperty('id')) : 0;
data.pushObject(Em.Object.create({
id: ++lastId,
name: this.get('numberValue'),
isNumber: true
}));
this.set('numberValue', "");
},
/**
* redraw expression
* NOTE: needed in order to avoid collision between scrollable lib and metric action event
*/
redrawField: function redrawField() {
this.set('expression.data', misc.sortByOrder($(this.get('element')).find('.metric-instance').map(function () {
return this.id;
}), this.get('expression.data')));
},
/**
* enable metric edit area
*/
didInsertElement: function didInsertElement() {
var self = this;
this.propertyDidChange('expression');
Em.run.next(function () {
$(self.get('element')).find('.metric-field').sortable({
items: "> .metric-instance",
tolerance: "pointer",
scroll: false,
update: function update() {
self.redrawField();
}
}).disableSelection();
});
},
/**
* remove metric or operator from expression
* @param {object} event
*/
removeElement: function removeElement(event) {
this.get('expression.data').removeObject(event.context);
},
validate: function () {
//number 1 used as substitute to test expression to be mathematically correct
var testNumber = 1;
var isInvalid = true;
var expression = this.get('expression.data').map(function (element) {
if (element.isMetric) {
return testNumber;
} else {
return element.name;
}
}, this).join(" ");
if (expression.length > 0) {
if (this.get('VALID_EXPRESSION_REGEX').test(expression)) {
try {
isInvalid = !isFinite(window.eval(expression));
} catch (e) {
isInvalid = true;
}
}
} else {
isInvalid = false;
}
this.set('isInvalid', isInvalid);
this.set('expression.isInvalid', isInvalid);
this.get('controller').propertyDidChange('isSubmitDisabled');
if (!isInvalid) {
this.get('controller').updateExpressions();
}
}.observes('expression.data.length')
});
/**
* input used to add number to expression
* @type {Em.TextField}
* @class
*/
App.AddNumberExpressionView = Em.TextField.extend({
classNameBindings: ['isInvalid'],
/**
* @type {boolean}
*/
isInvalid: function () {
return this.get('value').trim().length > 0 && !number_utils.isPositiveNumber(this.get('value').trim());
}.property('value')
});
/**
* show menu view that provide ability to add metric
*/
App.AddMetricExpressionView = Em.View.extend({
templateName: require('templates/main/service/widgets/create/step2_add_metric'),
controller: Em.computed.alias('parentView.controller'),
elementId: Em.computed.format('add-metric-menu_{0}', 'parentView.expression.id'),
didInsertElement: function didInsertElement() {
//prevent dropdown closing on click select
$('html').on('click.dropdown', '.dropdown-menu li', function (e) {
$(this).hasClass('keep-open') && e.stopPropagation();
});
},
metricsSelectionObj: function () {
var self = this;
return Em.Object.create({
placeholder_text: Em.I18n.t('dashboard.widgets.wizard.step2.selectMetric'),
no_results_text: Em.I18n.t('widget.create.wizard.step2.noMetricFound'),
onChangeCallback: function onChangeCallback(event, obj) {
var filteredComponentMetrics = self.get('controller.filteredMetrics').filterProperty('component_name', self.get('currentSelectedComponent.componentName')).filterProperty('level', self.get('currentSelectedComponent.level'));
var filteredMetric = filteredComponentMetrics.findProperty('name', obj.selected);
var selectedMetric = Em.Object.create({
name: obj.selected,
componentName: self.get('currentSelectedComponent.componentName'),
serviceName: self.get('currentSelectedComponent.serviceName'),
metricPath: filteredMetric.widget_id,
isMetric: true
});
if (self.get('currentSelectedComponent.hostComponentCriteria')) {
selectedMetric.hostComponentCriteria = self.get('currentSelectedComponent.hostComponentCriteria');
}
if (self.get('currentSelectedComponent.tag')) {
selectedMetric.tag = self.get('currentSelectedComponent.tag');
}
self.set('currentSelectedComponent.selectedMetric', selectedMetric);
if (self.get('currentSelectedComponent.selectedAggregation') == Em.I18n.t('dashboard.widgets.wizard.step2.aggregateFunction.scanOps')) {
var defaultAggregator = self.get('parentView.AGGREGATE_FUNCTIONS')[0];
self.set('currentSelectedComponent.selectedAggregation', defaultAggregator);
}
}
});
}.property(),
aggregateFnSelectionObj: function () {
var self = this;
return Em.Object.create({
placeholder_text: Em.I18n.t('dashboard.widgets.wizard.step2.aggregateFunction.scanOps'),
no_results_text: Em.I18n.t('dashboard.widgets.wizard.step2.aggregateFunction.notFound'),
onChangeCallback: function onChangeCallback(event, obj) {
self.set('currentSelectedComponent.selectedAggregation', obj.selected);
}
});
}.property(),
/**
* @type {Ember.Object}
* @default null
*/
currentSelectedComponent: null,
/**
* select component
* @param {object} event
*/
selectComponents: function selectComponents(event) {
this.set('currentSelectedComponent', event.context);
event.stopPropagation();
},
/**
* add current metrics and aggregation to expression
* @param event
*/
addMetric: function addMetric(event) {
var selectedMetric = event.context.get('selectedMetric'),
aggregateFunction = event.context.get('selectedAggregation'),
result = Em.Object.create(selectedMetric);
if (event.context.get('isAddEnabled')) {
var data = this.get('parentView').get('expression.data'),
id = data.length > 0 ? Math.max.apply(this.get('parentView'), data.mapProperty('id')) + 1 : 1;
result.set('id', id);
if (event.context.get('showAggregateSelect')) {
result.set('metricPath', result.get('metricPath') + '._' + aggregateFunction);
result.set('name', result.get('name') + '._' + aggregateFunction);
}
data.pushObject(result);
this.cancel();
}
},
/**
* cancel adding metric, close add metric menu
*/
cancel: function cancel() {
$(".service-level-dropdown").parent().removeClass('open');
var id = "#" + this.get('currentSelectedComponent.id');
var aggregatorId = "#" + this.get('currentSelectedComponent.aggregatorId');
$(id).val('').trigger("chosen:updated");
$(aggregatorId).val('').trigger("chosen:updated");
this.set('currentSelectedComponent.selectedAggregation', Em.I18n.t('dashboard.widgets.wizard.step2.aggregateFunction.scanOps'));
this.set('currentSelectedComponent.selectedMetric', null);
},
/**
* map of components
* has following hierarchy: service -> component -> metrics
*/
componentMap: function () {
var hasNameNodeFederation = App.get('hasNameNodeFederation');
var servicesMap = {};
var result = [];
var nameServiceGroups = [];
var masterNames = App.StackServiceComponent.find().filterProperty('isMaster').mapProperty('componentName');
var parentView = this.get('parentView');
var expressionId = "_" + parentView.get('expression.id');
if (this.get('controller.filteredMetrics')) {
this.get('controller.filteredMetrics').forEach(function (metric) {
// ignore NameNode component level metrics on federated cluster
if (hasNameNodeFederation && metric.component_name === 'NAMENODE' && metric.level === 'COMPONENT') return false;
var service = servicesMap[metric.service_name];
if (!service) {
service = {
count: 0,
components: {}
};
servicesMap[metric.service_name] = service;
}
var componentId = masterNames.contains(metric.component_name) ? metric.component_name + '_' + metric.level : metric.component_name;
service.count++;
if (service.components[componentId]) {
service.components[componentId].count++;
service.components[componentId].metrics.push(metric.name);
} else {
service.components[componentId] = {
component_name: metric.component_name,
level: metric.level,
count: 1,
hostComponentCriteria: metric.host_component_criteria,
metrics: [metric.name]
};
}
}, this);
}
if (hasNameNodeFederation) {
App.HDFSService.find().objectAt(0).get('masterComponentGroups').forEach(function (group) {
nameServiceGroups.push({
tag: group.name,
displayName: Em.I18n.t('dashboard.widgets.wizard.step2.nameSpaceDropDownItem').format(group.name),
component: null
});
});
}
for (var serviceName in servicesMap) {
var components = [];
for (var componentId in servicesMap[serviceName].components) {
// Hide the option if none of the hostComponent is created in the cluster yet
var componentName = servicesMap[serviceName].components[componentId].component_name;
if (App.HostComponent.getCount(componentName, 'totalCount') === 0) continue;
if (hasNameNodeFederation && componentName === 'NAMENODE') {
nameServiceGroups.forEach(function (group) {
group.component = this.createComponentItem(servicesMap[serviceName], serviceName, componentId, expressionId, group.tag);
}, this);
} else {
components.push(this.createComponentItem(servicesMap[serviceName], serviceName, componentId, expressionId));
}
}
if (hasNameNodeFederation && serviceName === 'HDFS') {
components.push(Em.Object.create({
displayName: 'NameNodes',
isGroup: true,
components: nameServiceGroups
}));
}
result.push(Em.Object.create({
serviceName: serviceName,
//in order to support panel lists
href: '#' + serviceName,
displayName: App.StackService.find(serviceName).get('displayName'),
count: servicesMap[serviceName].count,
components: components
}));
}
return this.putContextServiceOnTop(result);
}.property('controller.filteredMetrics', 'App.router.clusterController.isComponentsStateLoaded'),
createComponentItem: function createComponentItem(service, serviceName, componentId, expressionId, tag) {
var stackComponent = App.StackServiceComponent.find(service.components[componentId].component_name);
var component = service.components[componentId];
tag = tag || '';
return Em.Object.create({
componentName: component.component_name,
level: component.level,
displayName: stackComponent.get('isMaster') && component.level === 'HOSTCOMPONENT' ? Em.I18n.t('widget.create.wizard.step2.activeComponents').format(stackComponent.get('displayName')) : Em.I18n.t('widget.create.wizard.step2.allComponents').format(pluralize(stackComponent.get('displayName'))),
tag: tag,
count: component.count,
metrics: component.metrics.uniq().sort(),
selected: false,
id: componentId + expressionId + tag,
aggregatorId: componentId + expressionId + '_aggregator',
serviceName: serviceName,
showAggregateSelect: Em.computed.equal('level', 'COMPONENT'),
selectedMetric: null,
selectedAggregation: Em.I18n.t('dashboard.widgets.wizard.step2.aggregateFunction.scanOps'),
hostComponentCriteria: component.level === 'HOSTCOMPONENT' ? component.hostComponentCriteria : null,
isAddEnabled: function () {
var selectedMetric = this.get('selectedMetric'),
aggregateFunction = this.get('selectedAggregation');
if (this.get('showAggregateSelect')) {
return !!selectedMetric && !!aggregateFunction && aggregateFunction != Em.I18n.t('dashboard.widgets.wizard.step2.aggregateFunction.scanOps');
} else {
return !!selectedMetric;
}
}.property('selectedMetric', 'selectedAggregation')
});
},
/**
* returns the input array with the context service (service from which widget browser is launched) as the first element of the array
* @param serviceComponentMap {Array}
* @return {Array}
*/
putContextServiceOnTop: function putContextServiceOnTop(serviceComponentMap) {
var contextService = this.get('controller.content.widgetService');
var serviceIndex = serviceComponentMap.indexOf(serviceComponentMap.findProperty('serviceName', contextService));
return serviceComponentMap.slice(serviceIndex, serviceComponentMap.length).concat(serviceComponentMap.slice(0, serviceIndex));
}
});
App.InputCursorTextfieldView = Ember.TextField.extend({
placeholder: "",
classNameBindings: ['isInvalid'],
isInvalid: false,
didInsertElement: function didInsertElement() {
this.focusCursor();
},
focusCursor: function () {
var self = this;
Em.run.next(function () {
if (self.$()) {
self.$().focus();
}
});
}.observes('parentView.expression.data.length'),
focusOut: function focusOut() {
this.saveNumber();
},
validateInput: function () {
var value = this.get('value');
var parentView = this.get('parentView');
var isInvalid = false,
isInvalidTextfield = false;
if (!number_utils.isPositiveNumber(value)) {
if (value && parentView.get('OPERATORS').contains(value)) {
// add operator
var data = parentView.get('expression.data');
var lastId = data.length > 0 ? Math.max.apply(parentView, data.mapProperty('id')) : 0;
data.pushObject(Em.Object.create({
id: ++lastId,
name: value,
isOperator: true
}));
this.set('value', '');
} else Iif (value && value === 'm') {
// open add metric menu
var expressionId = "_" + parentView.get('expression.id');
$('#add-metric-menu' + expressionId + '> div > a').click();
this.set('value', '');
} else if (value) {
// invalid operator
isInvalid = isInvalidTextfield = true;
}
}
this.set('isInvalid', isInvalid);
this.set('parentView.isInvalid', isInvalid);
this.set('parentView.isInvalidTextfield', isInvalidTextfield);
}.observes('value'),
keyDown: function keyDown(event) {
if ((event.keyCode === 8 || event.which === 8) && !this.get('value')) {
// backspace
var data = this.get('parentView.expression.data');
Eif (data.length >= 1) {
data.removeObject(data[data.length - 1]);
}
} else if (event.keyCode === 13) {
//Enter
this.saveNumber();
}
},
saveNumber: function saveNumber() {
if (number_utils.isPositiveNumber(this.get('value'))) {
var data = this.get('parentView.expression.data');
var lastId = data.length > 0 ? Math.max.apply(this, data.mapProperty('id')) : 0;
data.pushObject(Em.Object.create({
id: ++lastId,
name: this.get('value'),
isNumber: true
}));
this.set('numberValue', "");
this.set('isInvalid', false);
this.set('parentView.isInvalid', false);
this.set('parentView.isInvalidTextfield', false);
this.set('value', '');
}
}
});
}); |