| 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
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599 |
1
1
1
1
4
4
4
2
2
10
1
9
4
4
2
2
1
4
5
6
6
5
3
3
6
5
5
5
5
5
5
3
3
1
1
5
9
9
9
1
9
9
9
1
10
10
10
10
5
10
5
66
66
66
30
10
10
10
5
5
5
5
66
66
2
2
2
1
1
1
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
1
1
1
1
2
2
2
2
1
1
3
1
1
1
1
3
3
1
2
42
42
9
3
42
2
2
2
2
1
1
1
1
2
2
2
1
2
2
2
2
2
5
1
1
1
1
4
2
1
2
2
2
1
1
1
1
| 'use strict';
;require.register("controllers/main/service/widgets/create/step2_controller", 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.WidgetWizardStep2Controller = Em.Controller.extend({
name: "widgetWizardStep2Controller",
EXPRESSION_PREFIX: 'Expression',
/**
* @type {RegExp}
* @const
*/
EXPRESSION_REGEX: /\$\{([\w\s\.\,\+\-\*\/\(\)\:\=\[\]]*)\}/g,
/**
* list of operators that can be used in expression
* @type {Array}
* @constant
*/
OPERATORS: ["+", "-", "*", "/", "(", ")"],
/**
* actual values of properties in API format
* @type {object}
*/
widgetProperties: {},
/**
* @type {Array}
*/
widgetValues: [],
/**
* @type {Array}
*/
widgetMetrics: [],
/**
* @type {Array}
*/
expressions: [],
/**
* used only for GRAPH widget
* @type {Array}
*/
dataSets: [],
/**
* content of template of Template widget
* @type {string}
*/
templateValue: '',
/**
* views of properties
* @type {Array}
*/
widgetPropertiesViews: [],
/**
* @type {boolean}
*/
isEditWidget: Em.computed.equal('content.controllerName', 'widgetEditController'),
/**
* metrics filtered by type
* @type {Array}
*/
filteredMetrics: function () {
var type = this.get('content.widgetType');
return this.get('content.allMetrics').filter(function (metric) {
if (type === 'GRAPH') {
return metric.temporal;
} else {
return metric.point_in_time;
}
}, this);
}.property('content.allMetrics'),
/**
* @type {boolean}
*/
isSubmitDisabled: function () {
if (this.get('widgetPropertiesViews').someProperty('isValid', false)) {
return true;
}
switch (this.get('content.widgetType')) {
case "NUMBER":
case "GAUGE":
var expression = this.get('expressions')[0];
return !(this.isExpressionComplete(expression) && this.isExpressionWithMetrics(expression));
case "GRAPH":
return !this.isGraphDataComplete(this.get('dataSets'));
case "TEMPLATE":
return this.get('isTemplateInvalid') || !this.isTemplateDataComplete(this.get('expressions'), this.get('templateValue'));
}
return false;
}.property('widgetPropertiesViews.@each.isValid', 'dataSets.@each.label', 'templateValue', 'isTemplateInvalid'),
/**
* check whether data of expression is complete
* @param {Em.Object} expression
* @returns {boolean}
*/
isExpressionComplete: function isExpressionComplete(expression) {
return Boolean(expression && !expression.get('isInvalid') && !expression.get('isEmpty'));
},
/**
* check whether data of expression contains metrics
* @param {Em.Object} expression
* @returns {boolean}
*/
isExpressionWithMetrics: function isExpressionWithMetrics(expression) {
return Boolean(expression && expression.get('data') && expression.get('data').someProperty('isMetric'));
},
/**
* check whether any of the expressions is incomplete or invalid
* @returns {boolean}
*/
isAnyExpressionInvalid: function () {
var isAnyExpressionInvalid = false;
switch (this.get('content.widgetType')) {
case "NUMBER":
case "GAUGE":
case "TEMPLATE":
isAnyExpressionInvalid = this.get('isSubmitDisabled') && this.get('expressions').someProperty('isEmpty', false);
break;
case "GRAPH":
var dataSets = this.get('dataSets'),
isNotEmpty = false;
for (var i = 0; i < dataSets.length; i++) {
if (dataSets[i].get('expression.data').length > 0) {
isNotEmpty = true;
break;
}
}
isAnyExpressionInvalid = this.get('isSubmitDisabled') && isNotEmpty;
}
return isAnyExpressionInvalid;
}.property('isSubmitDisabled'),
/**
* check whether data of graph widget is complete
* @param dataSets
* @returns {boolean} isComplete
*/
isGraphDataComplete: function isGraphDataComplete(dataSets) {
var isComplete = Boolean(dataSets.length),
expressions = dataSets.mapProperty('expression'),
isMetricsIncluded = expressions.some(this.isExpressionWithMetrics);
for (var i = 0; i < dataSets.length; i++) {
if (!this.checkIfIsLabelValid(dataSets[i]) || !this.isExpressionComplete(dataSets[i].get('expression'))) {
isComplete = false;
break;
}
}
return isComplete && isMetricsIncluded;
},
/**
* if label is valid
* @param dataset
* @returns {boolean} isValid
*/
checkIfIsLabelValid: function checkIfIsLabelValid(dataset) {
var label = dataset.get('label');
var isValid = label.trim() !== '' && validator.isValidChartWidgetDatasetLabel(label);
dataset.set('isInvalidLabel', !isValid);
return isValid;
},
/**
* check whether data of template widget is complete
* @param {Array} expressions
* @param {string} templateValue
* @returns {boolean} isComplete
*/
isTemplateDataComplete: function isTemplateDataComplete(expressions, templateValue) {
var isComplete = Boolean(expressions.length > 0 && templateValue.trim() !== ''),
isMetricsIncluded = expressions.some(this.isExpressionWithMetrics);
if (isComplete) {
for (var i = 0; i < expressions.length; i++) {
if (!this.isExpressionComplete(expressions[i])) {
isComplete = false;
break;
}
}
}
return isComplete && isMetricsIncluded;
},
/**
* Add data set
* @param {object|null} event
* @param {boolean} isDefault
* @returns {number} id
*/
addDataSet: function addDataSet(event, isDefault) {
var id = isDefault ? 1 : Math.max.apply(this, this.get('dataSets').mapProperty('id')) + 1;
this.get('dataSets').pushObject(Em.Object.create({
id: id,
label: Em.I18n.t('dashboard.widgets.wizard.step2.dataSeries').format(id),
isRemovable: !isDefault,
expression: Em.Object.create({
id: id,
data: [],
isInvalid: false,
isEmpty: Em.computed.equal('data.length', 0)
})
}));
return id;
},
/**
* Remove data set
* @param {object} event
*/
removeDataSet: function removeDataSet(event) {
this.get('dataSets').removeObject(event.context);
},
/**
* Add expression
* @param {object|null} event
* @param {boolean} isDefault
* @returns {number} id
*/
addExpression: function addExpression(event, isDefault) {
var id = isDefault ? 1 : Math.max.apply(this, this.get('expressions').mapProperty('id')) + 1;
this.get('expressions').pushObject(Em.Object.create({
id: id,
isRemovable: !isDefault,
data: [],
alias: '{{' + this.get('EXPRESSION_PREFIX') + id + '}}',
isInvalid: false,
isEmpty: Em.computed.equal('data.length', 0)
}));
return id;
},
/**
* Remove expression
* @param {object} event
*/
removeExpression: function removeExpression(event) {
this.get('expressions').removeObject(event.context);
},
/**
* initialize data
* widget should have at least one expression or dataSet
*/
initWidgetData: function initWidgetData() {
this.set('widgetProperties', this.get('content.widgetProperties'));
this.set('widgetValues', this.get('content.widgetValues'));
this.set('widgetMetrics', this.get('content.widgetMetrics'));
if (this.get('expressions.length') === 0) {
this.addExpression(null, true);
}
if (this.get('dataSets.length') === 0) {
this.addDataSet(null, true);
}
},
/**
* update preview widget with latest expression data
* Note: in order to draw widget it should be converted to API format of widget
*/
updateExpressions: function () {
var widgetType = this.get('content.widgetType');
var expressionData = {
values: [],
metrics: []
};
if (this.get('expressions').length > 0 && this.get('dataSets').length > 0) {
switch (widgetType) {
case 'GAUGE':
case 'NUMBER':
expressionData = this.parseExpression(this.get('expressions')[0]);
expressionData.values = [{
name: "",
value: expressionData.value
}];
break;
case 'TEMPLATE':
expressionData = this.parseTemplateExpression(this.get('templateValue'), this.get('expressions'));
break;
case 'GRAPH':
expressionData = this.parseGraphDataset(this.get('dataSets'));
break;
}
}
this.set('widgetValues', expressionData.values);
this.set('widgetMetrics', expressionData.metrics);
}.observes('templateValue', 'dataSets.@each.label'),
/**
* parse Graph data set
* @param {Array} dataSets
* @returns {{metrics: Array, values: Array}}
*/
parseGraphDataset: function parseGraphDataset(dataSets) {
var metrics = [];
var values = [];
dataSets.forEach(function (dataSet) {
var result = this.parseExpression(dataSet.get('expression'));
metrics.pushObjects(result.metrics);
values.push({
name: dataSet.get('label'),
value: result.value
});
}, this);
return {
metrics: metrics,
values: values
};
},
/**
* parse expression from template
* @param {string} templateValue
* @param {Array} expressions
* @returns {{metrics: Array, values: {value: *}[]}}
*/
parseTemplateExpression: function parseTemplateExpression(templateValue, expressions) {
var metrics = [];
var self = this;
// check if there is invalid expression name eg. {{myExpre}}
var isTemplateInvalid = false;
var validExpressionName = /\{\{(Expression[\d])\}\}/g;
var expressionName = /\{\{((?!}}).)*\}\}/g;
Eif (templateValue) {
var expressionNames = templateValue.match(expressionName);
Eif (expressionNames) {
expressionNames.forEach(function (name) {
Iif (!name.match(validExpressionName)) {
isTemplateInvalid = true;
}
});
}
}
this.set('isTemplateInvalid', isTemplateInvalid);
var expression = templateValue.replace(/\{\{Expression[\d]\}\}/g, function (exp) {
var result;
if (expressions.someProperty('alias', exp)) {
result = self.parseExpression(expressions.findProperty('alias', exp));
metrics.pushObjects(result.metrics);
return result.value;
}
return exp;
});
return {
metrics: metrics,
values: [{
value: expression
}]
};
},
/**
*
* @param {object} expression
* @returns {{metrics: Array, value: string}}
*/
parseExpression: function parseExpression(expression) {
var value = '';
var metrics = [];
if (expression.data.length > 0) {
value = '${';
expression.data.forEach(function (element) {
if (element.isMetric) {
var metricObj = {
"name": element.name,
"service_name": element.serviceName,
"component_name": element.componentName,
"metric_path": element.metricPath,
"tag": element.tag
};
Eif (element.hostComponentCriteria) {
metricObj.host_component_criteria = element.hostComponentCriteria;
}
metrics.push(metricObj);
}
Iif (element.isOperator) {
// operators should have spaces around in order to differentiate them when symbol is a part of metric name
// e.g "metric-a" and "metric1 - metric2"
value += " " + element.name + " ";
} else {
value += element.name;
}
}, this);
value += '}';
}
return {
metrics: metrics,
value: value
};
},
/**
* update properties of preview widget
*/
updateProperties: function () {
var result = {};
this.get('widgetPropertiesViews').forEach(function (property) {
for (var key in property.valueMap) {
result[property.valueMap[key]] = property.get(key);
}
});
this.set('widgetProperties', result);
}.observes('widgetPropertiesViews.@each.value', 'widgetPropertiesViews.@each.bigValue', 'widgetPropertiesViews.@each.smallValue'),
/*
* Generate the thresholds, unit, time range.etc object based on the widget type selected in previous step.
*/
renderProperties: function renderProperties() {
var widgetProperties = App.WidgetType.find(this.get('content.widgetType')).get('properties');
var propertiesData = this.get('widgetProperties');
var result = [];
widgetProperties.forEach(function (property) {
var definition = App.WidgetPropertyTypes.findProperty('name', property.name);
property = $.extend({}, property);
//restore previous values
for (var key in definition.valueMap) {
if (propertiesData[definition.valueMap[key]]) {
property[key] = propertiesData[definition.valueMap[key]];
}
}
result.pushObject(App.WidgetProperty.create($.extend(definition, property)));
});
this.set('widgetPropertiesViews', result);
},
/**
* convert data with model format to editable format
* Note: in order to edit widget expression it should be converted to editable format
*/
convertData: function convertData() {
var self = this;
var expressionId = 0;
var widgetValues = this.get('content.widgetValues');
var widgetMetrics = this.get('content.widgetMetrics');
this.get('expressions').clear();
this.get('dataSets').clear();
if (widgetValues.length > 0) {
switch (this.get('content.widgetType')) {
case 'NUMBER':
case 'GAUGE':
var id = this.addExpression(null, true);
this.get('expressions').findProperty('id', id).set('data', this.parseValue(widgetValues[0].value, widgetMetrics)[0]);
break;
case 'TEMPLATE':
this.parseValue(widgetValues[0].value, widgetMetrics).forEach(function (item, index) {
var id = this.addExpression(null, index === 0);
this.get('expressions').findProperty('id', id).set('data', item);
}, this);
this.set('templateValue', widgetValues[0].value.replace(this.get('EXPRESSION_REGEX'), function () {
return '{{' + self.get('EXPRESSION_PREFIX') + ++expressionId + '}}';
}));
break;
case 'GRAPH':
widgetValues.forEach(function (value, index) {
var id = this.addDataSet(null, index === 0);
var dataSet = this.get('dataSets').findProperty('id', id);
dataSet.set('label', value.name);
dataSet.set('expression.data', this.parseValue(value.value, widgetMetrics)[0]);
}, this);
break;
}
}
},
/**
* parse value
* @param value
* @param metrics
* @returns {Array}
*/
parseValue: function parseValue(value, metrics) {
var pattern = this.get('EXPRESSION_REGEX'),
expressions = [],
match;
while (match = pattern.exec(value)) {
expressions.push(this.getExpressionData(match[1], metrics));
}
return expressions;
},
/**
* format values into expression data objects
* @param {string} expression
* @param {Array} metrics
* @returns {Array}
*/
getExpressionData: function getExpressionData(expression, metrics) {
var str = '';
var data = [];
var id = 0;
for (var i = 0, l = expression.length; i < l; i++) {
if (this.get('OPERATORS').contains(expression[i])) {
Eif (str.trim().length > 0) {
data.pushObject(this.getExpressionVariable(str.trim(), ++id, metrics));
str = '';
}
data.pushObject(Em.Object.create({
id: ++id,
name: expression[i],
isOperator: true
}));
} else {
str += expression[i];
}
}
if (str.trim().length > 0) {
data.pushObject(this.getExpressionVariable(str.trim(), ++id, metrics));
}
return data;
},
/**
* get variable of expression
* could be name of metric "m1" or constant number "1"
* @param {string} name
* @param {Array} metrics
* @param {number} id
* @returns {Em.Object}
*/
getExpressionVariable: function getExpressionVariable(name, id, metrics) {
var metric;
if (isNaN(Number(name))) {
metric = metrics.findProperty('name', name);
return Em.Object.create({
id: id,
name: metric.name,
isMetric: true,
componentName: metric.component_name,
serviceName: metric.service_name,
metricPath: metric.metric_path,
hostComponentCriteria: metric.host_component_criteria
});
} else {
return Em.Object.create({
id: id,
name: name,
isNumber: true
});
}
},
next: function next() {
App.router.send('next');
}
});
}); |