-
Notifications
You must be signed in to change notification settings - Fork 2
/
graph-run.js
194 lines (174 loc) · 5.64 KB
/
graph-run.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
'use strict';
var Q = require('q');
var constants = require('./constants');
function qryqGraphRun(deferred, api, queries) {
try {
validateQueries(queries);
}
catch (ex) {
return deferred.reject(ex.message);
}
//queries are valid, so start running them
var queryPromisesHash = {};
var queryPromisesList = [];
queries.forEach(function promiseForQuery(query) {
var apiName = query.api;
var apiFunc = api[apiName];
if (typeof apiFunc !== 'function') {
return deferred.reject({
message: 'Failed to API function named: '+apiName,
});
}
var queryPromise = qryqGraphQueryRun(query, apiFunc, queryPromisesHash);
queryPromisesList.push(queryPromise);
queryPromisesHash[query.id] = queryPromise;
});
// console.log('queryPromisesList.length', queryPromisesList.length);
Q.allSettled(queryPromisesList).then(function onAllQueriesSettled(queryResults) {
var out = {};
// console.log('onAllQueriesSettled', 'queryResults', queryResults);
queryResults.forEach(function(queryResult, idx) {
// var filterOutput = queries[idx].filterOutput;
// filterOutput =
if (!queries[idx].filterOutput) {
var queryId = queries[idx].id;
if (queryResult.state === 'fulfilled') {
out[queryId] = queryResult.value;
}
else {
out[queryId] = queryResult;
}
}
});
return deferred.resolve(out);
},
function onAllQueriesError(err) {
// console.log('onAllQueriesError');
return deferred.reject(err);
});
}
module.exports = qryqGraphRun;
function qryqGraphQueryRun(query, apiFunc, queryPromisesHash) {
var queryDeferred = Q.defer();
var dependsPromises = [];
var depIds = query.depends;
depIds.forEach(function promiseForDependentQuery(depId) {
var dependPromise = queryPromisesHash[depId];
dependsPromises.push(dependPromise);
});
// console.log('dependsPromises.length', query.id, dependsPromises.length);
Q.allSettled(dependsPromises).then(function onAllDependentQueriesSettled(dependsResults) {
var dependsResultsHash = {};
dependsResults.forEach(function(depResult, idx) {
var depId = depIds[idx];
if (depResult.state === 'fulfilled') {
dependsResultsHash[depId] = depResult;
}
else {
dependsResultsHash[depId] = null;
}
});
processGraphQueryResults(query.input, dependsResultsHash);
apiFunc(queryDeferred, query.input);
});
return queryDeferred.promise;
}
function processGraphQueryResults(obj, dependsResults) {
if (typeof obj === 'object') {
eachArrayOrObject(obj, function(child, idx) {
if (typeof child === 'string') {
var found = child.match(constants.GRAPH_DEPENDENT_SUBSTITUTE_REGEX);
if (found && found.length > 1) {
var key = found[1]; //first regex match is always the entire string
if (key && key.length > 0) {
var dependResult = dependsResults[key];
var subKeys;
if (found.length > 1 && found[2].length > 0) {
var subKey = found[2];
subKeys = subKey.split('.');
}
else {
subKeys = [];
}
var numSubKeys = subKeys.length;
if (numSubKeys < 1) {
if (dependResult && dependResult.value) {
obj[idx] = dependResult.value;
}
}
else {
if (dependResult) {
var dependSubResult = dependResult.value;
subKeys.forEach(function(subKey, subKeyIdx) {
if (subKeyIdx > 0) {
//skip the first one
var useSubKey = parseInt(subKey, 10);
if (isNaN(useSubKey)) {
useSubKey = subKey;
}
dependSubResult = dependSubResult[useSubKey];
if (dependSubResult) {
if (subKeyIdx === (numSubKeys - 1)) {
obj[idx] = dependSubResult;
return;
}
}
else {
//leave as unsubstituted, and shortcut to safety
return;
}
}
});
}
}
}
}
}
else {
processGraphQueryResults(child, dependsResults);
}
});
}
}
/**
* Validates the set of queries as a whole,
* assuming that individual queries are themselves already valid
* @param queries {Array<Query>}
* @return {undefined}
*/
function validateQueries(queries) {
if (!queries || queries.length < 1) {
throw new Error('Must have at least one query');
}
var queryIds = {};
queries.forEach(function(query) {
queryIds[query.id] = true;
});
//validate that all depends do exist
queries.forEach(function(query) {
query.depends.forEach(function(dependId) {
if (!queryIds[dependId]) {
throw new Error('Query "'+query.id+
'" specifies a dependency on query "'+dependId+'", but that does not exist');
}
});
});
//TODO validate that there are no cycles in the dependency graph -
// qryq only works on directed acyclic graphs
}
function eachArrayOrObject(arrOrObj, callback) {
if (Array.isArray(arrOrObj)) {
return arrOrObj.forEach(callback);
}
else if (typeof arrOrObj === 'object') {
for (var key in arrOrObj) {
if (arrOrObj.hasOwnProperty(key)) {
// console.log(arrOrObj[key], key, arrOrObj);
callback(arrOrObj[key], key, arrOrObj);
}
}
}
else {
throw new Error('Can only iterate over arrays and objects');
}
}