forked from sirkjohannsen/grafana-datasource-datadog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquery_ctrl.js
264 lines (234 loc) · 9.01 KB
/
query_ctrl.js
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
'use strict';
System.register(['lodash', './dfunc', 'app/plugins/sdk', './func_editor', './add_datadog_func', './query_builder'], function (_export, _context) {
"use strict";
var _, dfunc, QueryCtrl, queryBuilder, _createClass, DataDogQueryCtrl;
function _classCallCheck(instance, Constructor) {
if (!(instance instanceof Constructor)) {
throw new TypeError("Cannot call a class as a function");
}
}
function _possibleConstructorReturn(self, call) {
if (!self) {
throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
}
return call && (typeof call === "object" || typeof call === "function") ? call : self;
}
function _inherits(subClass, superClass) {
if (typeof superClass !== "function" && superClass !== null) {
throw new TypeError("Super expression must either be null or a function, not " + typeof superClass);
}
subClass.prototype = Object.create(superClass && superClass.prototype, {
constructor: {
value: subClass,
enumerable: false,
writable: true,
configurable: true
}
});
if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass;
}
return {
setters: [function (_lodash) {
_ = _lodash.default;
}, function (_dfunc) {
dfunc = _dfunc.default;
}, function (_appPluginsSdk) {
QueryCtrl = _appPluginsSdk.QueryCtrl;
}, function (_func_editor) {}, function (_add_datadog_func) {}, function (_query_builder) {
queryBuilder = _query_builder;
}],
execute: function () {
_createClass = function () {
function defineProperties(target, props) {
for (var i = 0; i < props.length; i++) {
var descriptor = props[i];
descriptor.enumerable = descriptor.enumerable || false;
descriptor.configurable = true;
if ("value" in descriptor) descriptor.writable = true;
Object.defineProperty(target, descriptor.key, descriptor);
}
}
return function (Constructor, protoProps, staticProps) {
if (protoProps) defineProperties(Constructor.prototype, protoProps);
if (staticProps) defineProperties(Constructor, staticProps);
return Constructor;
};
}();
_export('DataDogQueryCtrl', DataDogQueryCtrl = function (_QueryCtrl) {
_inherits(DataDogQueryCtrl, _QueryCtrl);
function DataDogQueryCtrl($scope, $injector, uiSegmentSrv, templateSrv) {
_classCallCheck(this, DataDogQueryCtrl);
var _this = _possibleConstructorReturn(this, (DataDogQueryCtrl.__proto__ || Object.getPrototypeOf(DataDogQueryCtrl)).call(this, $scope, $injector));
_this.removeText = '-- remove tag --';
_this.uiSegmentSrv = uiSegmentSrv;
_this.templateSrv = templateSrv;
if (_this.target.aggregation) {
_this.aggregationSegment = new uiSegmentSrv.newSegment(_this.target.aggregation);
} else {
_this.aggregationSegment = new uiSegmentSrv.newSegment({
value: 'Select Aggregation',
fake: true,
custom: false
});
}
if (_this.target.metric) {
_this.metricSegment = new uiSegmentSrv.newSegment(_this.target.metric);
} else {
_this.metricSegment = new uiSegmentSrv.newSegment({
value: 'Select Metric',
fake: true,
custom: false
});
}
_this.target.tags = _this.target.tags || [];
_this.tagSegments = _this.target.tags.map(uiSegmentSrv.newSegment);
_this.fixTagSegments();
_this.functions = [];
_this.target.functions = _this.target.functions || [];
_this.functions = _.map(_this.target.functions, function (func) {
var f = dfunc.createFuncInstance(func.funcDef, { withDefaultParams: false });
f.params = func.params.slice();
return f;
});
if (_this.target.as) {
_this.asSegment = uiSegmentSrv.newSegment(_this.target.as);
} else {
_this.asSegment = uiSegmentSrv.newSegment({
value: 'Select As',
fake: true,
custom: false
});
}
return _this;
}
_createClass(DataDogQueryCtrl, [{
key: 'toggleEditorMode',
value: function toggleEditorMode() {
this.target.rawQuery = !this.target.rawQuery;
}
}, {
key: 'getMetrics',
value: function getMetrics() {
return this.datasource.metricFindQuery().then(this.uiSegmentSrv.transformToSegments(true));
}
}, {
key: 'getAggregations',
value: function getAggregations() {
return Promise.resolve([{ text: 'avg by', value: 'avg' }, { text: 'max by', value: 'max' }, { text: 'min by', value: 'min' }, { text: 'sub by', value: 'sum' }]);
}
}, {
key: 'getAs',
value: function getAs() {
return Promise.resolve([{ text: 'None', value: 'None' }, { text: 'as_count', value: 'as_count' }, { text: 'as_rate', value: 'as_rate' }]);
}
}, {
key: 'getTags',
value: function getTags(segment) {
var _this2 = this;
return this.datasource.tagFindQuery().then(this.uiSegmentSrv.transformToSegments(true)).then(function (results) {
if (segment.type !== 'plus-button') {
var removeSegment = _this2.uiSegmentSrv.newFake(_this2.removeText);
results.unshift(removeSegment);
}
return results;
});
}
}, {
key: 'aggregationChanged',
value: function aggregationChanged() {
this.target.aggregation = this.aggregationSegment.value;
this.panelCtrl.refresh();
}
}, {
key: 'metricChanged',
value: function metricChanged() {
this.target.metric = this.metricSegment.value;
this.panelCtrl.refresh();
}
}, {
key: 'asChanged',
value: function asChanged() {
if (this.asSegment.value === 'None') {
this.target.as = null;
} else {
this.target.as = this.asSegment.value;
}
this.panelCtrl.refresh();
}
}, {
key: 'fixTagSegments',
value: function fixTagSegments() {
var count = this.tagSegments.length;
var lastSegment = this.tagSegments[Math.max(count - 1, 0)];
if (!lastSegment || lastSegment.type !== 'plus-button') {
this.tagSegments.push(this.uiSegmentSrv.newPlusButton());
}
}
}, {
key: 'targetChanged',
value: function targetChanged() {
if (this.error) {
return;
}
this.panelCtrl.refresh();
}
}, {
key: 'persistFunctions',
value: function persistFunctions() {
this.target.functions = _.map(this.functions, function (func) {
return {
funcDef: func.def.name,
params: func.params.slice()
};
});
}
}, {
key: 'removeFunction',
value: function removeFunction(func) {
this.functions = _.without(this.functions, func);
this.persistFunctions();
this.targetChanged();
}
}, {
key: 'addFunction',
value: function addFunction(funcDef) {
var func = dfunc.createFuncInstance(funcDef, { withDefaultParams: true });
func.added = true;
this.functions.push(func);
this.persistFunctions();
this.targetChanged();
}
}, {
key: 'tagSegmentUpdated',
value: function tagSegmentUpdated(segment, index) {
if (segment.value === this.removeText) {
this.tagSegments.splice(index, 1);
}
var realSegments = _.filter(this.tagSegments, function (segment) {
return segment.value;
});
this.target.tags = realSegments.map(function (segment) {
return segment.value;
});
this.tagSegments = _.map(this.target.tags, this.uiSegmentSrv.newSegment);
this.fixTagSegments();
this.panelCtrl.refresh();
}
}, {
key: 'getCollapsedText',
value: function getCollapsedText() {
if (this.target.rawQuery) {
return this.target.query;
} else {
return queryBuilder.buildQuery(this.target);
}
}
}]);
return DataDogQueryCtrl;
}(QueryCtrl));
_export('DataDogQueryCtrl', DataDogQueryCtrl);
DataDogQueryCtrl.templateUrl = 'partials/query.editor.html';
}
};
});
//# sourceMappingURL=query_ctrl.js.map