-
-
Notifications
You must be signed in to change notification settings - Fork 275
Rewrite grid updating functionality #511
Changes from all commits
c8c0b28
50ce572
df31eec
81381f2
9c014ce
5d3b558
dbdd654
51ea8cc
61749cb
a3037b6
02ab47c
1a2bfd5
dc40912
9049db3
b4f4f0a
9351cc9
a4cf0d4
400b5c2
75fd5fc
005702a
7a74837
ee413c4
c526883
8f1d1dd
c39a0fd
File filter
Filter by extension
Conversations
Jump to
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); | ||
|
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. This function orders Please, ignore this comment. I've checked and this is already the behaviour in master. |
||
|
|
||
| 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; | ||
|
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.
|
||
| } | ||
|
|
||
| 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.
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.