Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
20 changes: 15 additions & 5 deletions superset/assets/javascripts/explorev2/actions/exploreActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,15 +131,23 @@ export function setFieldValue(fieldName, value, validationErrors) {
}

export const CHART_UPDATE_STARTED = 'CHART_UPDATE_STARTED';
export function chartUpdateStarted() {
return { type: CHART_UPDATE_STARTED };
export function chartUpdateStarted(queryRequest) {
return { type: CHART_UPDATE_STARTED, queryRequest };
}

export const CHART_UPDATE_SUCCEEDED = 'CHART_UPDATE_SUCCEEDED';
export function chartUpdateSucceeded(queryResponse) {
return { type: CHART_UPDATE_SUCCEEDED, queryResponse };
}

export const CHART_UPDATE_STOPPED = 'CHART_UPDATE_STOPPED';
export function chartUpdateStopped(queryRequest) {
if (queryRequest) {
queryRequest.abort();
}
return { type: CHART_UPDATE_STOPPED };
}

export const CHART_UPDATE_FAILED = 'CHART_UPDATE_FAILED';
export function chartUpdateFailed(queryResponse) {
return { type: CHART_UPDATE_FAILED, queryResponse };
Expand Down Expand Up @@ -223,13 +231,15 @@ export function updateChartStatus(status) {
export const RUN_QUERY = 'RUN_QUERY';
export function runQuery(formData, datasourceType) {
return function (dispatch) {
dispatch(chartUpdateStarted('loading'));
const url = getExploreUrl(formData, datasourceType, 'json');
$.getJSON(url, function (queryResponse) {
const queryRequest = $.getJSON(url, function (queryResponse) {
dispatch(chartUpdateSucceeded(queryResponse));
}).fail(function (err) {
dispatch(chartUpdateFailed(err.responseJSON));
if (err.statusText !== 'abort') {
dispatch(chartUpdateFailed(err.responseJSON));
}
});
dispatch(chartUpdateStarted(queryRequest));
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,9 @@ class ChartContainer extends React.PureComponent {
prevProps.queryResponse !== this.props.queryResponse ||
prevProps.height !== this.props.height ||
this.props.triggerRender
) &&
!this.props.queryResponse.error
) && !this.props.queryResponse.error
&& this.props.chartStatus !== 'failed'
&& this.props.chartStatus !== 'stopped'
) {
this.renderViz();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const propTypes = {
form_data: PropTypes.object.isRequired,
standalone: PropTypes.bool.isRequired,
triggerQuery: PropTypes.bool.isRequired,
queryRequest: PropTypes.object,
};

class ExploreViewContainer extends React.Component {
Expand Down Expand Up @@ -67,6 +68,10 @@ class ExploreViewContainer extends React.Component {
getExploreUrl(this.props.form_data));
}

onStop() {
this.props.actions.chartUpdateStopped(this.props.queryRequest);
}

getHeight() {
if (this.props.forcedHeight) {
return this.props.forcedHeight + 'px';
Expand Down Expand Up @@ -146,7 +151,8 @@ class ExploreViewContainer extends React.Component {
canAdd="True"
onQuery={this.onQuery.bind(this)}
onSave={this.toggleModal.bind(this)}
disabled={this.props.chartStatus === 'loading'}
onStop={this.onStop.bind(this)}
loading={this.props.chartStatus === 'loading'}
errorMessage={this.renderErrorMessage()}
/>
<br />
Expand Down Expand Up @@ -177,6 +183,7 @@ function mapStateToProps(state) {
standalone: state.standalone,
triggerQuery: state.triggerQuery,
forcedHeight: state.forced_height,
queryRequest: state.queryRequest,
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,38 +7,49 @@ const propTypes = {
canAdd: PropTypes.string.isRequired,
onQuery: PropTypes.func.isRequired,
onSave: PropTypes.func,
disabled: PropTypes.bool,
onStop: PropTypes.func,
loading: PropTypes.bool,
errorMessage: PropTypes.string,
};

const defaultProps = {
onStop: () => {},
onSave: () => {},
disabled: false,
};

export default function QueryAndSaveBtns({ canAdd, onQuery, onSave, disabled, errorMessage }) {
export default function QueryAndSaveBtns(
{ canAdd, onQuery, onSave, onStop, loading, errorMessage }) {
const saveClasses = classnames({
'disabled disabledButton': canAdd !== 'True',
});
const qryButtonStyle = errorMessage ? 'danger' : 'primary';
const qryButtonDisabled = errorMessage ? true : disabled;
const saveButtonDisabled = errorMessage ? true : loading;
const qryOrStopButton = loading ? (
<Button
onClick={onStop}
bsStyle="warning"
>
<i className="fa-stop-circle-o" /> Stop
</Button>
) : (
<Button
onClick={onQuery}
bsStyle={qryButtonStyle}
>
<i className="fa fa-bolt" /> Query
</Button>
);

return (
<div>
<ButtonGroup className="query-and-save">
<Button
id="query_button"
onClick={onQuery}
disabled={qryButtonDisabled}
bsStyle={qryButtonStyle}
>
<i className="fa fa-bolt" /> Query
</Button>
{qryOrStopButton}
<Button
className={saveClasses}
data-target="#save_modal"
data-toggle="modal"
disabled={qryButtonDisabled}
disabled={saveButtonDisabled}
onClick={onSave}
>
<i className="fa fa-plus-circle"></i> Save as
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,14 @@ export const exploreReducer = function (state, action) {
chartUpdateEndTime: null,
chartUpdateStartTime: now(),
triggerQuery: false,
queryRequest: action.queryRequest,
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

@ascott is it ok to put objects with methods in the Redux state?

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

i'm don't think this is problematic. i'm doing a little research to see how others are handling this, but i don't know of a reason why this wouldn't be ok at this point.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

ok so it sounds like it is not ideal to store functions or promises in the state... http://redux.js.org/docs/faq/OrganizingState.html#can-i-put-functions-promises-or-other-non-serializable-items-in-my-store-state, however, if this is working as expected, i'm not against it. i'm trying to think of another way to handle this.

Copy link
Copy Markdown

@ascott ascott Feb 7, 2017

Choose a reason for hiding this comment

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

i think @mistercrunch's suggestion of keeping track of latestQueryId in state is a good solution here. so we wouldn't keep track of the xhr request and would not call xhr.abort() but we would keep track of latestQueryId and only handle the results when the request returns if the latestQueryId and id in the response match. this way we won't have to put a function into our redux store, keeping with redux best practices.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

The problem with keeping latestQueryId in the state is that: when ajax returns before reducer updates the state with new latestQueryId, old queryResponse is dispatched, sort of like a race condition

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

then we have to move the ajax call in to component instead of action creators, is it ok?

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

my thinking is that i would prefer to put an object with methods on it into the state rather than move the async calls to the component. both are not ideal best practices, but putting an object with methods into the state seems cleaner to me. @vera-liu what do you think at this point?

Copy link
Copy Markdown
Contributor Author

@vera-liu vera-liu Feb 9, 2017

Choose a reason for hiding this comment

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

I personally prefer putting the request into state, I don't think it's too much anti-pattern, since it's just a request object, on the other hand, we have mapStateToFields methods attached with all our fields in state as well.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

yeah, that's a good point. let's stick with your current implementation!

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

@vera-liu I cleaned up mapStateToFields from the fields' state a few commits ago for that reason, but I guess we can remove the request object from the state later if it creates an issue. For instance if we were to persist the state to local storage it might not work, but we're not planning on doing that so it's ok.

});
},
[actions.CHART_UPDATE_STOPPED]() {
return Object.assign({}, state,
{
chartStatus: 'stopped',
chartAlert: 'Updating chart was stopped',
});
},
[actions.CHART_RENDERING_FAILED]() {
Expand Down
2 changes: 1 addition & 1 deletion superset/assets/javascripts/explorev2/stores/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export function getFieldsState(state, form_data) {
if (field.multi && formData[k].length > 0 && choiceValues.indexOf(formData[k][0]) < 0) {
delete formData[k];
} else if (!field.multi && choiceValues.indexOf(formData[k]) < 0) {
//delete form_data[k];
// delete form_data[k];
// TODO what's not working here?
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import React from 'react';
import { expect } from 'chai';
import { describe, it } from 'mocha';
import { shallow } from 'enzyme';

import ExploreActionButtons from '../../../../javascripts/explorev2/components/ExploreActionButtons';
import ExploreActionButtons from
'../../../../javascripts/explorev2/components/ExploreActionButtons';

describe('ExploreActionButtons', () => {
const defaultProps = {
Expand Down