| 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 |
1
1
1
1
1
6
6
5
6
1
5
2
1
4
4
4
4
3
3
3
3
2
2
2
1
4
8
8
11
9
8
8
8
8
1
1
8
7
1
7
6
6
10
9
9
9
1
6
6
1
6
8
8
7
7
7
6
6
6
7
14
30
11
6
6
5
1
1
4
4
5
4
5
2
2
1
6
5
1
4
1
1
2
2
2
1
2
3
3
3
20
20
14
20
8
8
12
| 'use strict';
;require.register("views/common/widget/graph_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 fileUtils = require('utils/file_utils');
var CUSTOM_TIME_INDEX = 8;
App.GraphWidgetView = Em.View.extend(App.WidgetMixin, App.ExportMetricsMixin, {
templateName: require('templates/common/widget/graph_widget'),
/**
* type of metric query from which the widget is comprised
*/
metricType: 'TEMPORAL',
/**
* common metrics container
* @type {Array}
*/
metrics: [],
/**
* 3600 sec in 1 hour
* @const
*/
TIME_FACTOR: 3600,
/**
* custom time range, set when graph opened in popup
* @type {number|null}
*/
customTimeRange: null,
/**
* value in seconds
* @type {number}
*/
timeRange: function () {
var timeRange = parseInt(this.get('content.properties.time_range'));
if (isNaN(timeRange)) {
//1h - default time range
timeRange = 1;
}
// Custom start and end time is specified by user
if (this.get('exportTargetView.currentTimeIndex') === CUSTOM_TIME_INDEX) {
return 0;
}
return this.get('customTimeRange') || timeRange * this.get('TIME_FACTOR');
}.property('content.properties.time_range', 'customTimeRange'),
/**
* value in ms
* @type {number}
*/
timeStep: 15,
/**
* @type {Array}
*/
data: [],
/**
* time range index for graph
* @type {number}
*/
timeIndex: 0,
/**
* custom start time for graph
* @type {number|null}
*/
startTime: null,
/**
* custom end time for graph
* @type {number|null}
*/
endTime: null,
/**
* graph time range duration in seconds
* @type {number|null}
*/
graphSeconds: null,
/**
* time range duration as string
* @type {string|null}
*/
durationFormatted: null,
exportTargetView: Em.computed.alias('childViews.lastObject'),
drawWidget: function drawWidget() {
if (this.get('isLoaded')) {
this.set('data', this.calculateValues());
}
},
/**
* calculate series datasets for graph widgets
*/
calculateValues: function calculateValues() {
var metrics = this.get('metrics');
var seriesData = [];
Eif (this.get('content.values')) {
this.get('content.values').forEach(function (value) {
var expression = this.extractExpressions(value)[0];
var computedData;
var datasetKey;
if (expression) {
datasetKey = value.value.match(this.get('EXPRESSION_REGEX'))[0];
computedData = this.computeExpression(expression, metrics)[datasetKey];
//exclude empty datasets
if (computedData.length > 0) {
seriesData.push({
name: value.name,
data: computedData
});
}
}
}, this);
}
return seriesData;
},
/**
* compute expression
*
* @param {string} expression
* @param {object} metrics
* @returns {object}
*/
computeExpression: function computeExpression(expression, metrics) {
var validExpression = true,
value = [],
dataLinks = {},
dataLength = -1,
beforeCompute,
result = {},
isDataCorrupted = false,
isPointNull = false;
//replace values with metrics data
expression.match(this.get('VALUE_NAME_REGEX')).forEach(function (match) {
if (isNaN(match)) {
if (metrics.someProperty('name', match)) {
dataLinks[match] = metrics.findProperty('name', match).data;
Eif (!isDataCorrupted) {
isDataCorrupted = dataLength !== -1 && dataLength !== dataLinks[match].length;
}
dataLength = dataLinks[match].length > dataLength ? dataLinks[match].length : dataLength;
} else {
validExpression = false;
console.warn('Metrics with name "' + match + '" not found to compute expression');
}
}
});
if (validExpression) {
if (isDataCorrupted) {
this.adjustData(dataLinks, dataLength);
}
for (var i = 0, timestamp; i < dataLength; i++) {
isPointNull = false;
beforeCompute = expression.replace(this.get('VALUE_NAME_REGEX'), function (match) {
if (isNaN(match)) {
timestamp = dataLinks[match][i][1];
isPointNull = isPointNull ? true : Em.isNone(dataLinks[match][i][0]);
return dataLinks[match][i][0];
} else {
return match;
}
});
var dataLinkPointValue = isPointNull ? null : Number(window.eval(beforeCompute));
// expression resulting into `0/0` will produce NaN Object which is not a valid series data value for RickShaw graphs
if (isNaN(dataLinkPointValue)) {
dataLinkPointValue = 0;
}
value.push([dataLinkPointValue, timestamp]);
}
}
result['${' + expression + '}'] = value;
return result;
},
/**
* add missing points, with zero value, to series
*
* @param {object} dataLinks
* @param {number} length
*/
adjustData: function adjustData(dataLinks, length) {
//series with full data taken as original
var original = [];
var substituteValue = null;
for (var i in dataLinks) {
Eif (dataLinks[i].length === length) {
original = dataLinks[i];
break;
}
}
original.forEach(function (point, index) {
for (var i in dataLinks) {
if (!dataLinks[i][index] || dataLinks[i][index][1] !== point[1]) {
dataLinks[i].splice(index, 0, [substituteValue, point[1]]);
}
}
}, this);
},
/**
* add time properties
* @param {Array} metricPaths
* @returns {Array} result
*/
addTimeProperties: function addTimeProperties(metricPaths) {
var toSeconds,
fromSeconds,
step = this.get('timeStep'),
timeRange = this.get('timeRange'),
result = [],
targetView = this.get('exportTargetView.isPopup') ? this.get('exportTargetView') : this.get('parentView');
//if view destroyed then no metrics should be asked
if (Em.isNone(targetView)) return result;
if (timeRange === 0 && !Em.isNone(targetView.get('customStartTime')) && !Em.isNone(targetView.get('customEndTime'))) {
// Custom start/end time is specified by user
toSeconds = targetView.get('customEndTime') / 1000;
fromSeconds = targetView.get('customStartTime') / 1000;
} else {
// Preset time range is specified by user
toSeconds = Math.round(App.dateTime() / 1000);
fromSeconds = toSeconds - timeRange;
}
metricPaths.forEach(function (metricPath) {
result.push(metricPath + '[' + fromSeconds + ',' + toSeconds + ',' + step + ']');
}, this);
return result;
},
/**
* @type {Em.View}
* @class
*/
graphView: App.ChartLinearTimeView.extend({
noTitleUnderGraph: true,
inWidget: true,
description: Em.computed.alias('parentView.content.description'),
isPreview: Em.computed.alias('parentView.isPreview'),
displayUnit: Em.computed.alias('parentView.content.properties.display_unit'),
setYAxisFormatter: function () {
var displayUnit = this.get('displayUnit');
if (displayUnit) {
this.set('yAxisFormatter', function (value) {
return App.ChartLinearTimeView.DisplayUnitFormatter(value, displayUnit);
});
}
}.observes('displayUnit'),
/**
* set custom time range for graph widget
*/
setTimeRange: function () {
if (this.get('isPopup')) {
if (this.get('currentTimeIndex') === CUSTOM_TIME_INDEX) {
// Custom start and end time is specified by user
this.get('parentView').propertyDidChange('customTimeRange');
} else {
// Preset time range is specified by user
this.set('parentView.customTimeRange', this.get('timeUnitSeconds'));
}
} else {
this.set('parentView.customTimeRange', null);
}
}.observes('isPopup', 'timeUnitSeconds'),
/**
* graph height
* @type {number}
*/
height: 95,
/**
* @type {string}
*/
id: function () {
return 'widget_' + this.get('parentView.content.id') + '_graph';
}.property('parentView.content.id'),
/**
* @type {string}
*/
renderer: function () {
return this.get('parentView.content.properties.graph_type') === 'STACK' ? 'area' : 'line';
}.property('parentView.content.properties.graph_type'),
title: Em.computed.alias('parentView.content.widgetName'),
transformToSeries: function transformToSeries(seriesData) {
var seriesArray = [];
seriesData.forEach(function (_series) {
seriesArray.push(this.transformData(_series.data, _series.name));
}, this);
return seriesArray;
},
loadData: function loadData() {
var self = this;
Em.run.next(function () {
self._refreshGraph(self.get('parentView.data'), self.get('parentView'));
});
},
didInsertElement: function () {
var self = this;
this.$().closest('.graph-widget').on('mouseleave', function () {
self.set('parentView.isExportMenuHidden', true);
});
this.setYAxisFormatter();
if (!arguments.length || this.get('parentView.data.length')) {
this.loadData();
}
Em.run.next(function () {
if (self.get('isPreview')) {
App.tooltip(self.$("[rel='ZoomInTooltip']"), 'disable');
} else {
App.tooltip(self.$("[rel='ZoomInTooltip']"), {
placement: 'left',
template: '<div class="tooltip"><div class="tooltip-arrow"></div><div class="tooltip-inner graph-tooltip"></div></div>'
});
}
});
}.observes('parentView.data')
}),
getCustomFileName: function getCustomFileName() {
// get current service name if it exists.
var currentServiceName = Em.isEmpty(this.get('controller.content.serviceName')) ? "" : this.get('controller.content.serviceName') + '_';
// serviceName_widgetName_metricName
return (currentServiceName + this.get('content.widgetName').replace(/\s+/g, '_')).toLowerCase();
},
exportGraphData: function exportGraphData(event) {
this.set('isExportMenuHidden', true);
var data,
isCSV = !!event.context,
fileType = isCSV ? 'csv' : 'json',
fileName = (Em.isEmpty(this.get('content.widgetName')) ? 'data' : this.getCustomFileName()) + '.' + fileType,
metrics = this.get('data'),
hasData = Em.isArray(metrics) && metrics.some(function (item) {
return Em.isArray(item.data);
});
if (hasData) {
data = isCSV ? this.prepareCSV(metrics) : JSON.stringify(metrics, this.jsonReplacer(), 4);
fileUtils.downloadTextFile(data, fileType, fileName);
} else {
App.showAlertPopup(Em.I18n.t('graphs.noData.title'), Em.I18n.t('graphs.noData.tooltip.title'));
}
}
});
}); |