Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 272
Rewrite grid updating functionality#511
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
c8c0b2850ce572df31eec81381f29c014ce5d3b558dbdd65451ea8cc61749cba3037b602ab47c1a2bfd5dc409129049db3b4f4f0a9351cc9a4cf0d4400b5c275fd5fc005702a7a74837ee413c4c5268838f1d1ddc39a0fdFile filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,15 +1,17 @@ | ||
| import React from 'react'; | ||
| import PropTypes from 'prop-types'; | ||
| const style = {marginBottom: 16, fontSize: 16, color: '#00cc96'}; | ||
| const style = {fontSize: 16}; | ||
| const SuccessMessage = (props) => ( | ||
| <em style={style}> | ||
| <div style={{ padding: '8px 16px', borderLeft: '4px solid #00cc96', background: 'white'}}> | ||
| <div style={style}>{props.message}</div> | ||
| {props.children} | ||
| </em> | ||
| </div> | ||
| ); | ||
| SuccessMessage.propTypes = { | ||
| children: PropTypes.node | ||
| children: PropTypes.node, | ||
| message: PropTypes.string | ||
| }; | ||
| export default SuccessMessage; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -6,6 +6,7 @@ import { | ||
| getCredentials, | ||
| getSetting | ||
| } from '../settings.js'; | ||
| import {extractOrderedUids} from '../utils/gridUtils.js'; | ||
| // Module to access Plot.ly REST API | ||
| @@ -164,36 +165,103 @@ export function patchGrid(fid, requestor, body) { | ||
| }); | ||
| } | ||
| export function updateGrid(rows, fid, uids, requestor) { | ||
| export function getGridColumn(fid, requestor) { | ||
| const {username, apiKey, accessToken} = getCredentials(requestor); | ||
| // TODO - Test case where no rows are returned. | ||
| if (uids.length !== (rows[0] || []).length) { | ||
| Logger.log(` | ||
| A different number of columns was returned in the | ||
| query than what was initially saved in the grid. | ||
| ${rows[0].length} columns were queried, | ||
| ${uids.length} columns were originally saved. | ||
| The connector does not create columns (yet), | ||
| and so we will only update the first ${uids.length} | ||
| columns. | ||
| `); | ||
| } | ||
| return plotlyAPIRequest(`grids/${fid}/col`, { | ||
| method: 'GET', | ||
| username, | ||
| apiKey, | ||
| accessToken | ||
| }); | ||
| } | ||
| /* | ||
| * For now, only update up to the number of columns that are | ||
| * already saved. In the future, we should just create more | ||
| * columns. See error message above. | ||
| */ | ||
| const columns = getColumns(rows, uids.length); | ||
| const url = `grids/${fid}/col?uid=${uids.join(',')}`; | ||
| const body = { | ||
| cols: JSON.stringify(columns.map(column => ({ | ||
| data: column | ||
| }))) | ||
| }; | ||
| return plotlyAPIRequest(url, {body, username, apiKey, accessToken, method: 'PUT'}); | ||
| export function updateGrid(rows, columnnames, fid, requestor) { | ||
| const numColumns = columnnames.length; | ||
| const columns = getColumns(rows, numColumns); | ||
| const columnEntries = columns.map((column, columnIndex) => { | ||
| return { data: column, name: columnnames[columnIndex], order: columnIndex }; | ||
| }); | ||
| const {username, apiKey, accessToken} = getCredentials(requestor); | ||
| const baseUrl = `grids/${fid}/col`; | ||
| const baseParams = { username, apiKey, accessToken }; | ||
| // fetch latest grid to get the source of truth | ||
| return getGridColumn(fid, requestor) | ||
| .then(res => { | ||
| if (res.status !== 200) { | ||
| return res; | ||
| } | ||
| return res.json(); | ||
| }).then(data => { | ||
| if (data.status) { | ||
| // bad res was passed along, return it | ||
| return data; | ||
| } | ||
| const uids = extractOrderedUids(data); | ||
| ||
| if (numColumns > uids.length) { | ||
| // repopulate existing columns | ||
| const putUrl = `${baseUrl}?uid=${uids.join(',')}`; | ||
Contributor 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. do we need to slice Contributor 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, UIDs are shorter in length than numColumns. Not sure what we would slice it to. | ||
| const putBody = { cols: columnEntries.slice(0, uids.length) }; | ||
| // append new columns | ||
| const postUrl = baseUrl; | ||
| const postBody = { cols: columnEntries.slice(uids.length) }; | ||
| return plotlyAPIRequest(postUrl, { | ||
| ...baseParams, | ||
| body: postBody, | ||
| method: 'POST' | ||
| }).then((res) => { | ||
| if (res.status !== 200) { | ||
| return res; | ||
| ||
| } | ||
| return plotlyAPIRequest(putUrl, { | ||
| ...baseParams, | ||
| body: putBody, | ||
| method: 'PUT' | ||
| }); | ||
| }); | ||
| } else if (numColumns < uids.length) { | ||
| // delete unused existing columns | ||
| const deleteUrl = `${baseUrl}?uid=${uids.slice(numColumns)}`; | ||
| // repopulate used existing columns | ||
| const putUrl = `${baseUrl}?uid=${uids.slice(0, numColumns).join(',')}`; | ||
| const putBody = { cols: columnEntries }; | ||
| return plotlyAPIRequest(deleteUrl, { | ||
| ...baseParams, | ||
| method: 'DELETE' | ||
| }).then((res) => { | ||
| if (res.status !== 204) { | ||
| return res; | ||
| } | ||
| return plotlyAPIRequest(putUrl, { | ||
| ...baseParams, | ||
| body: putBody, | ||
| method: 'PUT' | ||
| }); | ||
| }); | ||
| } | ||
| // repopulate existing columns | ||
| const putUrl = `${baseUrl}?uid=${uids.join(',')}`; | ||
| const putBody = { cols: columnEntries }; | ||
| return plotlyAPIRequest(putUrl, { | ||
| ...baseParams, | ||
| body: putBody, | ||
| method: 'PUT' | ||
| }); | ||
| }); | ||
| } | ||
| function getColumns(rows, maxColumns) { | ||
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
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.
Elsewhere we throw an error, why not here?This is done so that it behaves like
plotlyAPIRequest.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.
The status was being used by the tests so we just returned (https://github.com/plotly/falcon-sql-client/blob/dc4091249e48a10db12068abe1838900f36437a5/test/backend/QueryScheduler.spec.js#L629). Is it worth updating?
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.
@mfix22 Judging by how we use
updateGridhere, we shouldn't throw.