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 278
Implement API to create a grid to store results of a scheduled query #444
Merged
Merged
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
26ec584
PlotlyAPI: use lowercase filenames
n-riesco c26fa9d
settings: add getCredentials(username)
n-riesco b3504bf
PlotlyAPI: refactor
n-riesco 3aa1c34
routes: extend entrypoint POST queries
n-riesco 728888b
PlotlyAPI: rename getGridMetadat to getGridMeta
n-riesco File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,6 +16,7 @@ import { | |
import { | ||
getCurrentUser, | ||
getGridMetadata, | ||
newGrid, | ||
updateGrid | ||
} from './plotly-api.js'; | ||
|
||
|
@@ -25,6 +26,7 @@ class QueryScheduler { | |
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 | ||
|
@@ -119,7 +121,69 @@ class QueryScheduler { | |
Object.keys(this.queryJobs).forEach(this.clearQuery); | ||
} | ||
|
||
queryAndUpdateGrid (fid, uids, queryString, connectionId, requestor) { | ||
queryAndCreateGrid(filename, query, connectionId, requestor) { | ||
const {username, apiKey, accessToken} = getCredentials(requestor); | ||
let startTime; | ||
|
||
// 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); | ||
} | ||
|
||
|
||
startTime = process.hrtime(); | ||
|
||
Logger.log(`Querying "${query}" with connection ${connectionId} to create a new grid`, 2); | ||
return Connections.query(query, getConnectionById(connectionId)); | ||
|
||
}).then(({rows, columnnames}) => { | ||
Logger.log(`Query "${query}" took ${process.hrtime(startTime)[0]} seconds`, 2); | ||
Logger.log('Create a new grid with new data', 2); | ||
Logger.log(`First row: ${JSON.stringify(rows.slice(0, 1))}`, 2); | ||
|
||
startTime = process.hrtime(); | ||
|
||
return newGrid( | ||
filename, | ||
columnnames, | ||
rows, | ||
requestor | ||
); | ||
|
||
}).then(res => { | ||
Logger.log(`Request to Plotly for creating a grid took ${process.hrtime(startTime)[0]} seconds`, 2); | ||
|
||
if (res.status !== 201) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should be putting this into a constants file with actual codes? I noticed below we have 200, 401, etc. If we used names would make code easier to read There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No, it'd make the code less readable (in this context it's clear 201 is an HTTP status code). |
||
Logger.log(`Error ${res.status} while creating a grid`, 2); | ||
} | ||
|
||
return res.json().then((json) => { | ||
Logger.log(`Grid ${json.file.fid} has been updated.`, 2); | ||
return json; | ||
}); | ||
}); | ||
|
||
} | ||
|
||
queryAndUpdateGrid(fid, uids, query, connectionId, requestor) { | ||
const requestedDBConnections = getConnectionById(connectionId); | ||
let startTime = process.hrtime(); | ||
|
||
|
@@ -156,21 +220,22 @@ class QueryScheduler { | |
throw new Error(errorMessage); | ||
} | ||
|
||
Logger.log(`Querying "${queryString}" with connection ${connectionId} to update grid ${fid}`, 2); | ||
return Connections.query(queryString, requestedDBConnections); | ||
Logger.log(`Querying "${query}" with connection ${connectionId} to update grid ${fid}`, 2); | ||
return Connections.query(query, requestedDBConnections); | ||
|
||
}).then(rowsAndColumns => { | ||
}).then(({rows}) => { | ||
|
||
Logger.log(`Query "${queryString}" took ${process.hrtime(startTime)[0]} seconds`, 2); | ||
Logger.log(`Query "${query}" took ${process.hrtime(startTime)[0]} seconds`, 2); | ||
Logger.log(`Updating grid ${fid} with new data`, 2); | ||
Logger.log( | ||
'First row: ' + | ||
JSON.stringify(rowsAndColumns.rows.slice(0, 1)), | ||
JSON.stringify(rows.slice(0, 1)), | ||
2); | ||
|
||
startTime = process.hrtime(); | ||
|
||
return updateGrid( | ||
rowsAndColumns.rows, | ||
rows, | ||
fid, | ||
uids, | ||
requestor | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can simplify it:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
tricky, but not equivalent: the latter doesn't catch the case when
username
is defined but bothapiKey
andaccessToken
are undefined.