• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

eswdd / aardvark / 4031096410

pending completion
4031096410

push

github

GitHub
Add rel="noopener noreferrer" to external link (#247)

1622 of 2218 branches covered (73.13%)

3379 of 4186 relevant lines covered (80.72%)

83.13 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

74.53
/static-content/QueryEditorDialogCtrl.js
1
aardvark.controller('QueryEditorDialogCtrl', function QueryEditorDialogCtrl($uibModalInstance, idGenerator, tsdbUtils, deepUtils, graphs, queries) {
1✔
2
    var $ctrl = this;
21✔
3

4
    $ctrl.queries = deepUtils.deepClone(queries);
21✔
5
    $ctrl.gexpSubQueriesById = {};
21✔
6
    
7
    $ctrl.deletedQueries = [];
21✔
8
    
9
    $ctrl.currentQueryAdding = false;
21✔
10
    $ctrl.currentQuery = null;
21✔
11
    $ctrl.currentQueryId = null;
21✔
12
    
13
    // gexp stuff
14
    $ctrl.gexpFunctions = tsdbUtils.gexpFunctions;
21✔
15
    // single query arg:
16
    $ctrl.selectedSubQueryId = null;
21✔
17
    // multi query args:
18
    $ctrl.selectedAvailableSubQueries = [];
21✔
19
    $ctrl.selectedIncludedSubQueries = [];
21✔
20
    $ctrl.availableSubQueries = [];
21✔
21
    $ctrl.includedSubQueries = [];
21✔
22

23
    $ctrl.init = function() {
21✔
24
        for (var q=0; q<$ctrl.queries.length; q++) {
21✔
25
            if ($ctrl.queries[q].type == "metric" || $ctrl.queries[q].type == "gexp") {
86✔
26
                $ctrl.gexpSubQueriesById[queries[q].id + ""] = $ctrl.queries[q];
83✔
27
            }
28
        }
29
        $ctrl.initialiseAvailableSubQueries();
21✔
30
    }
31
    
32
    $ctrl.initialiseAvailableSubQueries = function() {
21✔
33
        $ctrl.availableSubQueries = [];
52✔
34
        
35
        function isDAG(query, seenIds) {
1✔
36
            if (seenIds == null) {
158!
37
                seenIds = {};
×
38
            }
39

40
            var idStr = query.id.toString();
158✔
41
            if (seenIds.hasOwnProperty(idStr)) {
158✔
42
                return false;
35✔
43
            }
44
            // add to set
45
            seenIds[idStr] = idStr;
123✔
46
            if (query.type == "gexp") {
123✔
47
                for (var i=0; i<query.subQueries.length; i++) {
21✔
48
                    var subQueryId = query.subQueries[i];
23✔
49
                    var subQuery = $ctrl.gexpSubQueriesById[subQueryId];
23✔
50
                    if (subQuery != null) {
23!
51
                        if (!isDAG(subQuery, seenIds)) {
23✔
52
                            return false;
7✔
53
                        }
54
                    }
55
                }
56
            }
57
            delete seenIds[idStr];
116✔
58
            return true;
116✔
59
        }
60
        
61
        for (var q=0; q<$ctrl.queries.length; q++) {
52✔
62
            if ($ctrl.queries[q].type == "metric" || $ctrl.queries[q].type == "gexp") {
223✔
63
                // todo: incomplete, doesn't handle nesting
64
                var seenIds = {};
218✔
65
                if ($ctrl.currentQuery != null) {
218✔
66
                    seenIds[$ctrl.currentQuery.id] = $ctrl.currentQuery.id;
135✔
67
                }
68
                if ($ctrl.currentQuery == null || isDAG($ctrl.queries[q], seenIds)) {
218✔
69
                    $ctrl.availableSubQueries.push($ctrl.queries[q]);
183✔
70
                }
71
            }
72
        }
73
        
74
    }
75
    
76
    $ctrl.availableGexpFunctionNames = function() {
21✔
77
        return Object.keys($ctrl.gexpFunctions);
×
78
    }
79

80
    $ctrl.querySelectionChanged = function() {
21✔
81
        // le grand reset
82
        $ctrl.currentQuery = null;
5✔
83
        $ctrl.selectedSubQueryId = null;
5✔
84
        $ctrl.selectedAvailableSubQueries = [];
5✔
85
        $ctrl.selectedIncludedSubQueries = [];
5✔
86
        $ctrl.availableSubQueries = [];
5✔
87
        $ctrl.includedSubQueries = [];
5✔
88
        // find the query
89
        for (var q=0; q<$ctrl.queries.length; q++) {
5✔
90
            if ($ctrl.queries[q].id == $ctrl.currentQueryId) {
14✔
91
                $ctrl.currentQuery = $ctrl.queries[q];
5✔
92
                break;
5✔
93
            }
94
        }
95
        // now init ui for this query
96
        if ($ctrl.currentQuery != null) {
5!
97
            $ctrl.initialiseAvailableSubQueries();
5✔
98
            if ($ctrl.currentQuery.type == "gexp" && $ctrl.currentQuery.function != null) {
5!
99
                var fn = $ctrl.gexpFunctions[$ctrl.currentQuery.function];
5✔
100
                if (fn != null) {
5!
101
                    if (fn.maxSubQueries == 1) {
5✔
102
                        $ctrl.selectedSubQueryId = $ctrl.currentQuery.subQueries.length > 0 ? $ctrl.currentQuery.subQueries[0] : null;
1!
103
                    }
104
                    if (fn.maxSubQueries > 1) {
5✔
105
                        $ctrl.includedSubQueries = $ctrl.currentQuery.subQueries.map(function (queryId) {
4✔
106
                            var query = $ctrl.gexpSubQueriesById[queryId + ""];
5✔
107
                            var wrapper = {
5✔
108
                                id: idGenerator.nextId(),
109
                                query: query
110
                            };
111
                            return wrapper;
5✔
112
                        });
113
                    }
114
                }
115
            }
116
        }
117
    }
118
    
119
    $ctrl.moveMetricUp = function() {
21✔
120
        // selectedIncludedSubQueries -> includedSubQueries
121
        var wrappersToMove = $ctrl.selectedIncludedSubQueries.map(function (id) {return id;});
19✔
122
        // for each item, whether it can be moved
123
        var indexesToMove = $ctrl.includedSubQueries.map(function (item) {
11✔
124
            var ind = wrappersToMove.indexOf(item.id);
49✔
125
            return (ind >= 0);
49✔
126
        });
127
       
128
        var startIndex = 0;
11✔
129
        // skip over items to move which are already at top
130
        while (indexesToMove[startIndex]) {
11✔
131
            startIndex++;
6✔
132
        }
133
        for (var i=Math.max(startIndex,1); i<indexesToMove.length; i++) {
11✔
134
            if (indexesToMove[i]) {
36✔
135
                var removed = $ctrl.includedSubQueries[i];
13✔
136
                $ctrl.includedSubQueries.splice(i, 1);
13✔
137
                $ctrl.includedSubQueries.splice(i-1, 0, removed);
13✔
138
            }
139
        }
140
    }
141
    
142
    $ctrl.moveMetricDown = function() {
21✔
143
        // selectedIncludedSubQueries -> includedSubQueries
144
        var wrappersToMove = $ctrl.selectedIncludedSubQueries.map(function (id) {return id;});
19✔
145
        // for each item, whether it can be moved
146
        var indexesToMove = $ctrl.includedSubQueries.map(function (item) {
11✔
147
            var ind = wrappersToMove.indexOf(item.id);
49✔
148
            return (ind >= 0);
49✔
149
        });
150

151
        var startIndex = indexesToMove.length-1;
11✔
152
        // skip over items to move which are already at top
153
        while (indexesToMove[startIndex]) {
11✔
154
            startIndex--;
6✔
155
        }
156
        for (var i=Math.min(startIndex,indexesToMove.length-2); i>=0; i--) {
11✔
157
            if (indexesToMove[i]) {
36✔
158
                var removed = $ctrl.includedSubQueries[i];
13✔
159
                $ctrl.includedSubQueries.splice(i, 1);
13✔
160
                $ctrl.includedSubQueries.splice(i+1, 0, removed);
13✔
161
            }
162
        }
163
    }
164
    
165
    $ctrl.moveMetricIn = function() {
21✔
166
        // selectedAvailableSubQueries -> includedSubQueries
167
        var includedMetricCount = $ctrl.includedSubQueries.length;
7✔
168
        var maxMetricCount = $ctrl.gexpFunctions[$ctrl.currentQuery.function].maxSubQueries;
7✔
169
        var newWrapperIds = [];
7✔
170
        for (var q=0; q<$ctrl.selectedAvailableSubQueries.length && includedMetricCount<maxMetricCount; q++) {
7✔
171
            var wrapper = {
33✔
172
                id: idGenerator.nextId(),
173
                query: $ctrl.gexpSubQueriesById[$ctrl.selectedAvailableSubQueries[q]]
174
            }
175
            $ctrl.includedSubQueries.push(wrapper);
33✔
176
            newWrapperIds.push(wrapper.id);
33✔
177
            includedMetricCount++;
33✔
178
        }
179
        $ctrl.selectedAvailableSubQueries = [];
7✔
180
        $ctrl.selectedIncludedSubQueries = newWrapperIds.map(function (id) {return id;});
33✔
181
    }
182
    
183
    $ctrl.moveMetricOut = function() {
21✔
184
        var wrappersToRemove = $ctrl.selectedIncludedSubQueries.map(function (id) {return id;});
3✔
185
        // selectedIncludedSubQueries -> out
186
        for (var q=$ctrl.includedSubQueries.length-1; q>=0; q--) {
2✔
187
            var ind = wrappersToRemove.indexOf($ctrl.includedSubQueries[q].id); 
6✔
188
            if (ind >= 0) {
6✔
189
                $ctrl.includedSubQueries.splice(q, 1);
3✔
190
            }
191
        }
192
    }
193

194
    $ctrl.queryTypeChanged = function() {
21✔
195
        // todo
196
    }
197
    
198
    $ctrl.queryString = function(query) {
21✔
199
        if (query.type == "metric") {
6!
200
            return tsdbUtils.metricQuery(query, false, null/*globalDownsampleTo*/, null/*downsampleOverrideFn*/, function(s){});
×
201
        }
202
        if (query.type == "gexp") {
6!
203
            return tsdbUtils.gexpQuery(query, $ctrl.gexpSubQueriesById, false, null/*globalDownsampleTo*/, null/*downsampleOverrideFn*/, function(s){});
6✔
204
        }
205
        return "Unsupported query type: "+query.type;
×
206
    }
207

208
    $ctrl.allocatedGraph = function() {
21✔
209
        if ($ctrl.currentQuery == null || $ctrl.currentQuery.graphOptions == null) {
×
210
            return null;
×
211
        }
212
        var graph = null;
×
213
        if (graphs != null) {
×
214
            for (var g=0; g<graphs.length; g++) {
×
215
                if (graphs[g].id == $ctrl.currentQuery.graphOptions.graphId) {
×
216
                    graph = graphs[g];
×
217
                }
218
            }
219
        }
220
        return graph;
×
221
    }
222
    
223
    $ctrl.graphType = function() {
21✔
224
        var graph = $ctrl.allocatedGraph();
×
225
        if (graph != null) {
×
226
            return graph.type;
×
227
        }
228
        return "";
×
229
    }
230
    
231
    $ctrl.newQuery = function() {
21✔
232
        $ctrl.currentQuery = {
17✔
233
            id: idGenerator.nextId(),
234
            type: "gexp",
235
            name: "Query "+($ctrl.queries.length+1),
236
            subQueries: [null],
237
            // defaults
238
            graphOptions: {
239
                graphId: "0",
240
                rightAxis: false,
241
                dygraph: {
242
                    drawLines: true,
243
                    drawPoints: false
244
                }
245
            }
246
        }
247
        $ctrl.queries.push($ctrl.currentQuery);
17✔
248
        $ctrl.currentQueryId = $ctrl.currentQuery.id;
17✔
249
        $ctrl.currentQueryAdding = true;
17✔
250
        $ctrl.initialiseAvailableSubQueries();
17✔
251
        $ctrl.selectedSubQueryId = null;
17✔
252
        $ctrl.includedSubQueries = [];
17✔
253
        $ctrl.selectedAvailableSubQueries = [];
17✔
254
        $ctrl.selectedIncludedSubQueries = [];
17✔
255
    }
256
    
257
    $ctrl.deleteCurrent = function() {
21✔
258
        var ind = -1;
×
259
        for (var m=0; m<$ctrl.queries.length; m++) {
×
260
            if ($ctrl.queries[m].id == $ctrl.currentQueryId) {
×
261
                ind = m;
×
262
            }
263
        }
264
        if (ind != -1) {
×
265
            var query = $ctrl.queries[ind];
×
266
            $ctrl.deletedQueries.push(query);
×
267
            $ctrl.queries.splice(ind, 1);
×
268
            if ($ctrl.gexpSubQueriesById.hasOwnProperty(query.id)) {
×
269
                delete $ctrl.gexpSubQueriesById[query.id];
×
270
            }
271
            $ctrl.currentQueryId = null;
×
272
            if (query.type == "metric") {
×
273
                // todo: delete references to this?
274
            }
275
        }
276
    }
277
    
278
    $ctrl.saveCurrent = function() {
21✔
279
        switch ($ctrl.currentQuery.type) {
9!
280
            case "metric":
281
                // todo
282
                break;
×
283
            case "gexp":
284
                if ($ctrl.currentQuery.function == null) {
9!
285
                    $ctrl.currentQuery.subQueries = [];
×
286
                }
287
                else {
288
                    var fn = $ctrl.gexpFunctions[$ctrl.currentQuery.function];
9✔
289
                    if (fn == null || fn.maxSubQueries > 1) {
9✔
290
                        $ctrl.currentQuery.subQueries = $ctrl.includedSubQueries.map(function (wrapper){return wrapper.query.id;});
10✔
291
                    }
292
                    else if (fn.maxSubQueries == 1) {
3!
293
                        $ctrl.currentQuery.subQueries = [$ctrl.selectedSubQueryId];
3✔
294
                    }
295
                }
296
                break;
9✔
297
            case "exp":
298
                // todo
299
                break;
×
300
            default:
301
                throw "Unrecognised query type: "+$ctrl.currentQuery.type;
×
302
        }
303
        if ($ctrl.currentQueryAdding) {
9!
304
            if ($ctrl.currentQuery.type == "gexp" || $ctrl.currentQuery.type == "metric") {
9!
305
                $ctrl.gexpSubQueriesById[$ctrl.currentQuery.id] = $ctrl.currentQuery;
9✔
306
                $ctrl.initialiseAvailableSubQueries();
9✔
307
            }
308
            $ctrl.currentQueryAdding = false;
9✔
309
        }
310
        
311
    }
312

313
    $ctrl.save = function () {
21✔
314
        $uibModalInstance.close({changed:$ctrl.queries, deleted:$ctrl.deletedQueries});
×
315
    };
316

317
    $ctrl.cancel = function () {
21✔
318
        $uibModalInstance.dismiss('cancel');
×
319
    };
320
    $ctrl.init();
21✔
321

322
    
323
});
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc