-
Notifications
You must be signed in to change notification settings - Fork 3
/
queryEngine.js
90 lines (74 loc) · 2.44 KB
/
queryEngine.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
'use strict';
let runQuery = require('./sql').runQuery;
let runQueryCached = require('./sqlCached').runQuery;
let mapAsync = require('map-async');
let glob = require('glob');
let queryProviders = glob.sync('queryProviders/*.js').map(f => require('./' + f));
let targetProviderMap = new Map();
queryProviders.forEach((qp) => {
qp.targets.forEach((t) => {
targetProviderMap.set(t, qp);
});
});
let allTargets = Array.from(targetProviderMap.keys());
allTargets.sort();
module.exports.targets = allTargets;
function loadDataProvider(dataProvider, cached) {
return function loadData(argList, callback) {
let dataset = [];
let datasetIndex = {};
dataProvider.targets.forEach((target) => {
datasetIndex[target] = dataset.push({ target: target, datapoints: [] }) - 1;
});
let runQueryFn = cached ? runQueryCached : runQuery;
runQueryFn(dataProvider.query, argList, (err, results) => {
if (err) {
return callback(err);
}
results.forEach((r) => {
let target = dataProvider.getTarget(r);
let datasetEntryIndex = datasetIndex[target];
if (datasetEntryIndex > -1) {
let datapoint = dataProvider.convertToDatapoint(r);
dataset[datasetEntryIndex].datapoints.push(datapoint);
}
});
callback(null, dataset);
});
};
}
module.exports.run = (targets, params, cached, callback) => {
if (typeof targets === 'string') {
targets = queryProviders.filter((qp) => qp.name === targets).pop().targets;
} else if (!Array.isArray(targets)) {
callback = cached;
cached = params;
params = targets;
targets = allTargets;
}
let dataProviderNames = new Set();
for (var t of targets) {
if (!targetProviderMap.has(t)) {
return callback(new Error('No data provider found for target: ' + t));
}
dataProviderNames.add(targetProviderMap.get(t).name);
}
if (!dataProviderNames.size) {
return callback(new Error('No data providers for target(s): ' + targets.join(', ')));
}
let dataProviders = [];
dataProviderNames.forEach((providerName) => {
dataProviders.push(queryProviders.filter((qp) => qp.name === providerName).pop());
});
mapAsync(dataProviders, (provider, next) => {
provider.run(loadDataProvider(provider, cached), params, next);
}, (err, results) => {
if (err) {
return callback(err);
}
callback(null, results.reduce((o, r) => {
o.push(...r);
return o;
}, []));
});
};