This repository has been archived by the owner on Jun 4, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 281
/
QueryScheduler.js
504 lines (437 loc) · 19.6 KB
/
QueryScheduler.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
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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
import {has} from 'ramda';
import * as scheduler from 'node-schedule';
import {getConnectionById} from './Connections.js';
import * as Connections from './datastores/Datastores.js';
import {mapRefreshToCron, mapCronToRefresh} from '../utils/cronUtils.js';
import {extractOrderedUids} from '../utils/gridUtils.js';
import Logger from '../logger';
import {getQuery, getQueries, saveQuery, updateQuery, deleteQuery} from './Queries.js';
import {getTags} from './Tags.js';
import {getCredentials, getSetting} from '../settings.js';
import {getCurrentUser, getGridMeta, newGrid, patchGrid, updateGrid, getGridColumn} from './plotly-api.js';
import {EXE_STATUS} from '../../shared/constants.js';
const SUCCESS_CODES = [200, 201, 204];
class QueryScheduler {
constructor() {
this.scheduleQuery = this.scheduleQuery.bind(this);
this.loadQueries = this.loadQueries.bind(this);
this.clearQuery = this.clearQuery.bind(this);
this.clearQueries = this.clearQueries.bind(this);
this.queryAndCreateGrid = this.queryAndCreateGrid.bind(this);
this.queryAndUpdateGrid = this.queryAndUpdateGrid.bind(this);
// this.job wraps this.queryAndUpdateGrid to avoid concurrent runs of the same job
this.runningJobs = {};
this.job = (fid, query, connectionId, requestor, cronInterval, refreshInterval) => {
const startedAt = Date.now();
try {
if (this.runningJobs[fid]) {
return;
}
this.runningJobs[fid] = true;
updateQuery(fid, {
lastExecution: {
status: EXE_STATUS.running,
startedAt
}
});
return this.queryAndUpdateGrid(fid, query, connectionId, requestor, cronInterval, refreshInterval)
.then(exeRes => {
const {completedAt, duration, rowCount} = exeRes.queryResults;
updateQuery(fid, {
lastExecution: {
status: EXE_STATUS.ok,
startedAt,
completedAt,
duration,
rowCount
}
});
})
.catch(error => {
Logger.log(error, 0);
updateQuery(fid, {
lastExecution: {
status: EXE_STATUS.failed,
startedAt,
completedAt: Date.now(),
errorMessage: error.toString()
}
});
})
.then(() => {
delete this.runningJobs[fid];
});
} catch (e) {
Logger.log(e, 0);
updateQuery(fid, {
lastExecution: {
status: EXE_STATUS.failed,
startedAt,
completedAt: Date.now(),
errorMessage: e.toString()
}
});
}
};
// Expose this.minimumRefreshInterval so that tests can overwrite it
this.minimumRefreshInterval = 60;
this.queryJobs = {};
}
scheduleQuery({
requestor,
fid,
uids,
refreshInterval = null,
cronInterval = null,
name,
tags,
lastExecution,
nextScheduledAt,
query,
connectionId
}) {
if (!cronInterval && !refreshInterval) {
throw new Error('A scheduling interval was not supplied');
} else if (refreshInterval && refreshInterval < this.minimumRefreshInterval) {
throw new Error(
[
`Refresh interval must be at least ${this.minimumRefreshInterval} seconds`,
`(supplied ${refreshInterval})`
].join(' ')
);
}
if (name && name.length > 150) {
throw new Error('Invalid query name. Must be less than 150 characters.');
}
Logger.log(`Scheduling "${query}" with connection ${connectionId} updating grid ${fid}`);
// Delete query if it is already saved
if (getQuery(fid)) {
deleteQuery(fid);
}
// Remove the query from the in-memory timers
if (has(fid, this.queryJobs)) {
this.clearQuery(fid);
}
// Save query to a file
const queryParams = {
requestor,
fid,
uids,
refreshInterval: refreshInterval || mapCronToRefresh(cronInterval),
cronInterval,
query,
connectionId,
lastExecution,
nextScheduledAt,
name,
tags
};
saveQuery(queryParams);
// Schedule
const job = () => this.job(fid, query, connectionId, requestor, cronInterval, refreshInterval);
let jobInterval = cronInterval;
if (!jobInterval) {
// convert refresh interval to cron representation
jobInterval = mapRefreshToCron(refreshInterval);
}
this.queryJobs[fid] = scheduler.scheduleJob(jobInterval, job);
this.queryJobs[fid].on('scheduled', nextInvocation => {
updateQuery(fid, {nextScheduledAt: nextInvocation.getTime()});
});
// we'll miss the first schedule event, so initialize manually the first time
updateQuery(fid, {nextScheduledAt: this.queryJobs[fid].nextInvocation().getTime()});
}
// Load and schedule queries - To be run on app start.
loadQueries() {
// read persisted data from disk
const tags = getTags();
const queries = getQueries();
// sanitize queries before scheduling
queries.forEach((query) => {
const cleanQuery = this.sanitizeQuery({ query, tags });
updateQuery(query.fid, cleanQuery);
});
const cleanedQueries = getQueries();
cleanedQueries.forEach(this.scheduleQuery);
}
// Remove query from memory
clearQuery(fid) {
if (this.queryJobs[fid]) {
this.queryJobs[fid].cancel();
}
delete this.queryJobs[fid];
}
// Clear out setInterval queries from memory - used to clean up tests
clearQueries() {
Object.keys(this.queryJobs).forEach(this.clearQuery);
}
sanitizeQuery({ query, tags }) {
// prune invalid "running" executions from last shutdown
if (query.lastExecution && query.lastExecution.status === EXE_STATUS.running) {
query.lastExecution = null;
}
// prune invalid tags
if (query.tags) {
const allTagIds = tags.map(tag => tag.id);
if (!Array.isArray(query.tags)) {
query.tags = [];
}
const validTags = query.tags.filter(tagId => allTagIds.includes(tagId));
query.tags = validTags;
}
return query;
}
queryAndCreateGrid(filename, query, connectionId, requestor, cronInterval, refreshInterval) {
const {username, apiKey, accessToken} = getCredentials(requestor);
const formattedRefresh = refreshInterval || mapCronToRefresh(cronInterval);
let currStartTime;
let fid;
const BEGIN_STAMP = Date.now();
const queryResults = {};
// Check if the user even exists
if (!username || !(apiKey || accessToken)) {
/*
* Warning: The front end looks for "Unauthenticated" in this error message. Don't change it!
*/
const errorMessage =
'Unauthenticated: Attempting to create a grid but the ' +
`authentication credentials for the user "${username}" do not exist.`;
Logger.log(errorMessage, 0);
throw new Error(errorMessage);
}
// Check if the credentials are valid
return getCurrentUser(username)
.then(res => {
if (res.status !== 200) {
const errorMessage = `Unauthenticated: ${getSetting(
'PLOTLY_API_URL'
)} failed to identify ${username}.`;
Logger.log(errorMessage, 0);
throw new Error(errorMessage);
}
currStartTime = process.hrtime();
Logger.log(`Querying "${query}" with connection ${connectionId} to create a new grid`, 2);
return Connections.query(query, getConnectionById(connectionId)).catch(e => {
/*
* Warning: The front end looks for "QueryExecutionError" in this error message. Don't change it!
*/
throw new Error(`QueryExecutionError: ${e.message}`);
});
})
.then(({rows, columnnames}) => {
Logger.log(`Query "${query}" took ${process.hrtime(currStartTime)[0]} seconds`, 2);
Logger.log('Create a new grid with new data', 2);
Logger.log(`First row: ${JSON.stringify(rows.slice(0, 1))}`, 2);
queryResults.rowCount = rows && rows.length ? rows.length : 0;
currStartTime = process.hrtime();
return newGrid(filename, columnnames, rows, requestor).catch(e => {
/*
* Warning: The front end looks for "PlotlyApiError" in this error message. Don't change it!
*/
throw new Error(`PlotlyApiError: ${e.message}`);
});
})
.then(res => {
Logger.log(`Request to Plotly for creating a grid took ${process.hrtime(currStartTime)[0]} seconds`, 2);
if (res.status !== 201) {
Logger.log(`Error ${res.status} while creating a grid`, 2);
}
return res.json();
})
.then(json => {
fid = json.file.fid;
queryResults.uids = json.file.cols.map(col => col.uid);
currStartTime = process.hrtime();
return patchGrid(fid, requestor, {
metadata: {
query,
connectionId,
connectorUrl: getSetting('BASE_URL')
},
refresh_interval: formattedRefresh
}).catch(e => {
/*
* Warning: The front end looks for "MetadataError" in this error message. Don't change it!
*/
throw new Error(`MetadataError: ${e.message}`);
});
})
.then(() => {
Logger.log(`Request to Plotly for creating a grid took ${process.hrtime(currStartTime)[0]} seconds`, 2);
Logger.log(`Grid ${fid} has been updated.`, 2);
queryResults.duration = Math.floor((Date.now() - BEGIN_STAMP) / 1000);
queryResults.completedAt = Date.now();
return {
fid,
queryResults
};
});
}
queryAndUpdateGrid(fid, query, connectionId, requestor, cronInterval, refreshInterval) {
const requestedDBConnections = getConnectionById(connectionId);
const formattedRefresh = refreshInterval || mapCronToRefresh(cronInterval);
let currStartTime = process.hrtime();
const BEGIN_STAMP = Date.now();
const queryResults = {};
/*
* Do a pre-query check that the user has the correct credentials
* and that the connector can connect to plotly
*/
/*
* If the user is the owner, then requestor === fid.split(':')[0]
* If the user is a collaborator, then requestor is different
*/
const {username, apiKey, accessToken} = getCredentials(requestor);
// Check if the user even exists
if (!username || !(apiKey || accessToken)) {
/*
* Warning: The front end looks for "Unauthenticated" in this error message. Don't change it!
*/
const errorMessage =
`Unauthenticated: Attempting to update grid ${fid} but the ` +
`authentication credentials for the user "${username}" do not exist.`;
Logger.log(errorMessage, 0);
throw new Error(errorMessage);
}
// Check if the credentials are valid
return getCurrentUser(username)
.then(res => {
if (res.status !== 200) {
const errorMessage = `Unauthenticated: ${getSetting(
'PLOTLY_API_URL'
)} failed to identify ${username}.`;
Logger.log(errorMessage, 0);
throw new Error(errorMessage);
}
Logger.log(`Querying "${query}" with connection ${connectionId} to update grid ${fid}`, 2);
return Connections.query(query, requestedDBConnections).catch(e => {
/*
* Warning: The front end looks for "QueryExecutionError" in this error message. Don't change it!
*/
throw new Error(`QueryExecutionError: ${e.message}`);
});
})
.then(({rows, columnnames}) => {
Logger.log(`Query "${query}" took ${process.hrtime(currStartTime)[0]} seconds`, 2);
Logger.log(`Updating grid ${fid} with new data`, 2);
Logger.log('First row: ' + JSON.stringify(rows.slice(0, 1)), 2);
queryResults.rowCount = rows && rows.length ? rows.length : 0;
currStartTime = process.hrtime();
return updateGrid(rows, columnnames, fid, requestor).catch(e => {
/*
* Warning: The front end looks for "PlotlyApiError" in this error message. Don't change it!
*/
throw new Error(`PlotlyApiError: ${e.message}`);
});
})
.then(res => {
Logger.log(`Request to Plotly for grid ${fid} took ${process.hrtime(currStartTime)[0]} seconds`, 2);
if (!SUCCESS_CODES.includes(res.status)) {
Logger.log(`Error ${res.status} while updating grid ${fid}.`, 2);
/*
* If it was a 404 error and the requestor was the owner, then
* it might've been because the owner has deleted their grid.
* If it was a 404 and the requestor wasn't the owner,
* then either the owner has deleted the grid or the
* requestor no longer has permission to view the graph.
* In any case, delete the query.
* Note that when a user is requesting to update or save
* a query via `post /queries`, we first check that they have
* permission to edit the file. Otherwise, this code block
* could delete an owner's grid when given any request by
* a non-collaborator.
* In Plotly, deletes can either be permenant or non-permentant.
* Delete the query in either case.
*/
/*
* Plotly's API returns a 500 instead of a 404 in these
* PUTs. To check if the file is really there or not,
* make an additional API call to GET it
*/
return getGridMeta(fid, username).then(resFromGET => {
if (resFromGET.status === 404) {
Logger.log(`Grid ID ${fid} doesn't exist on Plotly anymore, removing persistent query.`, 2);
this.clearQuery(fid);
return deleteQuery(fid);
}
return resFromGET.text().then(text => {
let filemeta;
try {
filemeta = JSON.parse(text);
} catch (e) {
Logger.log(`Failed to parse the JSON of request ${fid}`, 0);
Logger.log(e);
Logger.log('Text response: ' + text, 0);
throw new Error(e);
}
if (filemeta.deleted) {
Logger.log(
`
Grid ID ${fid} was deleted,
removing persistent query.`,
2
);
this.clearQuery(fid);
return deleteQuery(fid);
}
/*
* Warning: The front end looks for "PlotlyApiError" in this error message. Don't change it!
*/
throw new Error(`PlotlyApiError: non 200 grid update response code (got: ${res.status})`);
});
});
}
currStartTime = process.hrtime();
return patchGrid(fid, requestor, {
metadata: {
query,
connectionId,
connectorUrl: getSetting('BASE_URL')
},
refresh_interval: formattedRefresh
}).catch(e => {
/*
* Warning: The front end looks for "MetadataError" in this error message. Don't change it!
*/
throw new Error(`MetadataError: ${e.message}`);
});
})
.then(res => {
Logger.log(`Request to Plotly for creating a grid took ${process.hrtime(currStartTime)[0]} seconds`, 2);
Logger.log(`Grid ${fid} has been updated.`, 2);
// res is undefined if the `deleteQuery` is returned above
if (res && res.status && res.status !== 200) {
throw new Error(`MetadataError: error updating grid metadata (status: ${res.status})`);
}
currStartTime = process.hrtime();
// fetch updated grid column for returning
return getGridColumn(fid, requestor);
})
.then(res => {
Logger.log(
`Request to Plotly for fetching updated grid took ${process.hrtime(currStartTime)[0]} seconds`,
2
);
if (res.status !== 200) {
throw new Error(`PlotlyApiError: error fetching updated columns (status: ${res.status})`);
}
return res.json();
})
.then(grid => {
queryResults.uids = extractOrderedUids(grid);
queryResults.duration = Math.floor((Date.now() - BEGIN_STAMP) / 1000);
queryResults.completedAt = Date.now();
return {
fid,
queryResults
};
});
}
}
export default QueryScheduler;
// TODO - do we allow the user to change their connection
// and all of their saved queries? if we save
// serializedConfiguration in plotly, then that'll be hard to
// update.
// or wait, no it won't, we can just make an API call to the
// grid and update it.