Skip to content
This repository has been archived by the owner on Jun 4, 2024. It is now read-only.

Implement API to create a grid to store results of a scheduled query #444

Merged
merged 5 commits into from
May 28, 2018

Conversation

n-riesco
Copy link
Contributor

This API is needed for the upcoming UI for scheduling queries.

Closes #440


I've also taken the opportunity to move all the code invoking plotlyAPIRequest into file backend/persistent/plotly-api.js.


@tarzzz , could you review this PR, please?

@shannonlal, this is a good PR to get familiar with the API in the backend that connects to Plotly.

cc/ @jackparmer

n-riesco added 4 commits May 26, 2018 20:48
* Refactor code to retrieve a user's credentials into a function.
* Move all the code that invokes `plotlyAPIRequest` into
  `backend/persistent/plotly-api.js`.

* Ensure grids created for testing are deleted when the test completes.
* POST queries accepts field `filename` to create a grid for storing the
  results of the scheduled query.

Closes #440
Copy link
Contributor

@shannonlal shannonlal left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall really clean. Just some small comments on constants and names of variables. Was able to follow so not a show stopper.

Great job

//
// See API documentation at https://api.plot.ly/v2/
//
// TODO: Refactor as a class with a constructor that takes username as input,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this to be done as part of this PR? Or will you be creating a separate Issue

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, that's future TODO (a nice thing to have but it doesn't cause any issues at the moment).

}).then(res => {
Logger.log(`Request to Plotly for creating a grid took ${process.hrtime(startTime)[0]} seconds`, 2);

if (res.status !== 201) {
Copy link
Contributor

Choose a reason for hiding this comment

The 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

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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).

});
}

export function getGridMetadata(fid, requestor) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we use fid or filename? Above it is filename and now it is fid

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Plotly's API uses fid.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we can use getGridMeta as it is more consistent with Plotly nomenclature. Not a big deal though..

});
}

export function updateGrid(rows, fid, uids, requestor) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fid or filename?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Plotly's API uses fid.

accessToken,
method: 'GET'
}).then(function(res) {
if (res.status === 404) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should maybe flip the codes into constants with names?

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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 404 is an HTTP status code).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that using human readable constant names is generally a good approach. While it's clear that it's an HTTP status code, there are so many specific HTTP status codes, that it's not clear sometimes what an HTTP status code means exactly (e.g. 409 is not a very common status code).

https://philsturgeon.uk/http/2015/08/16/avoid-hardcoding-http-status-codes/

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@gzork One can always open a node session and run:

> http.STATUS_CODES[409]
'Conflict'

I can't think of any REST API that isn't defined in terms of numeric status codes. Let's say, I'm doing a review, and I come across an uncommon status code written like: if (res.status === CONFLICT) {...}. Then, I need to stop my review, and find where CONFLICT has been defined, to be able to confirm that the right status code has been used.

@@ -226,3 +228,8 @@ export function saveSetting(settingName, settingValue) {
*/
fs.writeFileSync(getSetting('SETTINGS_PATH'), YAML.stringify(settingsOnFile));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should put an issue on this. If you remember when we tried to pump out the Athena client on this line it would cause an error.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I sneaked a fix for this in the PR that implemented the CSV connector. See f3df19f .

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 Thanks

@n-riesco
Copy link
Contributor Author

@shannonlal I'm sorry about the quick replies (I'm rushing to wrap up a few things). If you want to discuss further the introduction of STATUS constants or Plotly API, let's open an issue for it.

@shannonlal
Copy link
Contributor

@n-riesco Nope. This is a small detail. Looks good and clean. It is a small detail. I would merge it.

@shannonlal
Copy link
Contributor

💃

* To be consistent with Plotly nomenclature:
  #444 (comment)
getQueries,
saveQuery,
deleteQuery
} from './Queries.js';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(Not necessarily in this PR) but we should small-case these filenames as well.. !! Queries, Datastore etc, and remove .js extensions to be consistent.. !!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I want to keep the extensions, because:

  • it's grep-friendly
  • we have imports that only differ in the extension (e.g. code-editor.css and code-editor.jsx)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The idea is to move to lower case, whenever we have a chance (instead of having a huge big PR). We don't really want to spend time on it.


return newGrid(uniqueFilename, names, rows, username);
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

let startTime;

// Check if the user even exists
if (!username || !(apiKey || accessToken)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can simplify it:

if !(username || apiKey || accessToken) {

Copy link
Contributor Author

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 both apiKey and accessToken are undefined.

});
});

it('can create a grid when it registers a query', function() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is better if you split this into two commits, one where you move the code into a different file, and anotherwhere you do updates/add new code.

Easier to review, and something to consider for next time.. !!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, will do next time.

Copy link
Contributor

@tarzzz tarzzz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Left some comments, but nothing blocking.. !!

💃

@n-riesco n-riesco merged commit 1b98662 into master May 28, 2018
@n-riesco n-riesco deleted the plotlyapi/create-grid branch June 5, 2018 15:09
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants