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
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 22 additions & 14 deletions backend/persistent/QueryScheduler.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,23 @@
import * as Connections from './datastores/Datastores';
import {getConnectionById} from './Connections';
import {getQuery, getQueries, saveQuery, deleteQuery} from './Queries';
import {getSetting} from '../settings';
import Logger from '../logger';
import {PlotlyAPIRequest, updateGrid} from './plotly-api.js';
import {has} from 'ramda';

import {getConnectionById} from './Connections.js';
import * as Connections from './datastores/Datastores.js';
import Logger from '../logger';
import {
getQuery,
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.

import {
getCredentials,
getSetting
} from '../settings.js';
import {
PlotlyAPIRequest,
updateGrid
} from './plotly-api.js';

class QueryScheduler {
constructor() {
this.scheduleQuery = this.scheduleQuery.bind(this);
Expand Down Expand Up @@ -118,16 +130,12 @@ class QueryScheduler {
* If the user is the owner, then requestor === fid.split(':')[0]
* If the user is a collaborator, then requestor is different
*/
const username = requestor;
const user = getSetting('USERS').find(
u => u.username === username
);
const {username, apiKey, accessToken} = getCredentials(requestor);

// Check if the user even exists
if (!user || !(user.apiKey || user.accessToken)) {
if (!username || !(apiKey || accessToken)) {
/*
* Heads up - the front end looks for "Unauthenticated" in this
* error message. So don't change it!
* 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 ` +
Expand All @@ -136,7 +144,7 @@ class QueryScheduler {
Logger.log(errorMessage, 0);
throw new Error(errorMessage);
}
const {apiKey, accessToken} = user;

// Check if the credentials are valid
return PlotlyAPIRequest('users/current', {
method: 'GET', username, apiKey, accessToken
Expand Down
81 changes: 35 additions & 46 deletions backend/persistent/plotly-api.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
import fetch from 'node-fetch';
import {getSetting} from '../settings.js';
import Logger from '../logger';
import FormData from 'form-data';

import Logger from '../logger.js';
import {
getCredentials,
getSetting
} from '../settings.js';


// Module to access Plot.ly REST API
//
// See API documentation at https://api.plot.ly/v2/
Expand Down Expand Up @@ -31,19 +36,15 @@ export function PlotlyAPIRequest(relativeUrl, {body, username, apiKey, accessTok
}

export function newDatacache(payloadJSON, type, requestor) {
const form = new FormData();
form.append('type', type);
form.append('origin', 'Falcon');
form.append('payload', payloadJSON);
const body = form;

const users = getSetting('USERS');
const user = users.find(
u => u.username === requestor
);

if (user) {
form.append('username', user.username);
const {username, apiKey, accessToken} = getCredentials(requestor);

const body = new FormData();
body.append('type', type);
body.append('origin', 'Falcon');
body.append('payload', payloadJSON);

if (username) {
body.append('username', username);
}

/*
Expand All @@ -52,17 +53,12 @@ export function newDatacache(payloadJSON, type, requestor) {
* to proceed with blank `Authorization` header.
*/
let authorization = '';
if (user) {
const apiKey = user.apiKey;
const accessToken = user.accessToken;

if (apiKey) {
authorization = 'Basic ' + new Buffer(
requestor + ':' + apiKey
).toString('base64');
} else if (accessToken) {
authorization = `Bearer ${accessToken}`;
}
if (apiKey) {
authorization = 'Basic ' + new Buffer(
requestor + ':' + apiKey
).toString('base64');
} else if (accessToken) {
authorization = `Bearer ${accessToken}`;
}

return fetch(`${getSetting('PLOTLY_URL')}/datacache`, {
Expand All @@ -85,13 +81,7 @@ export function newDatacache(payloadJSON, type, requestor) {
}

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.

const username = requestor;
const users = getSetting('USERS');
const user = users.find(
u => u.username === username
);
const apiKey = user.apiKey;
const accessToken = user.accessToken;
const {username, apiKey, accessToken} = getCredentials(requestor);

// TODO - Test case where no rows are returned.
if (uids.length !== rows[0].length) {
Expand Down Expand Up @@ -129,16 +119,12 @@ export function updateGrid(rows, fid, uids, requestor) {

// Resolve if the requestor has permission to update fid, reject otherwise
export function checkWritePermissions(fid, requestor) {
const owner = fid.split(':')[0];
const user = getSetting('USERS').find(
u => u.username === requestor
);
const {username, apiKey, accessToken} = getCredentials(requestor);

// Check if the user even exists
if (!user || !(user.apiKey || user.accessToken)) {
if (!username || !(apiKey || accessToken)) {
/*
* Heads up - the front end looks for "Unauthenticated" in this
* error message. So don't change it!
* 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 ` +
Expand All @@ -147,7 +133,7 @@ export function checkWritePermissions(fid, requestor) {
Logger.log(errorMessage, 0);
throw new Error(errorMessage);
}
const {apiKey, accessToken} = user;

return PlotlyAPIRequest(`grids/${fid}`, {
username: requestor,
apiKey,
Expand All @@ -166,14 +152,17 @@ export function checkWritePermissions(fid, requestor) {
return res.json();
}
}).then(function(filemeta) {
if (filemeta.collaborators &&
const owner = fid.split(':')[0];

if (owner === requestor) {
return Promise.resolve();
} else if (filemeta.collaborators &&
filemeta.collaborators.results &&
Boolean(filemeta.collaborators.results.find(collab => requestor === collab.username))
filemeta.collaborators.results.find(collab => requestor === collab.username)
) {
return new Promise(function(resolve) {resolve();});
} else if (owner === requestor) {
return new Promise(function(resolve) {resolve();});
return Promise.resolve();
}

throw new Error('Permission denied');
});
}
11 changes: 9 additions & 2 deletions backend/settings.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import fs from 'fs';
import os from 'os';
import path from 'path';

import {concat, contains, has} from 'ramda';
import YAML from 'yamljs';

import {createStoragePath} from './utils/homeFiles';
import path from 'path';
import os from 'os';

const DEFAULT_SETTINGS = {
HEADLESS: false,
Expand Down Expand Up @@ -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

}

// Get user credentials
export function getCredentials(username) {
return getSetting('USERS').find(u => u.username === username) || {};
}