Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Support multiple queries per request #11880

Merged
merged 21 commits into from
Dec 18, 2020
Merged
Show file tree
Hide file tree
Changes from 2 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
16 changes: 5 additions & 11 deletions superset-frontend/src/chart/Chart.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ const propTypes = {
chartAlert: PropTypes.string,
chartStatus: PropTypes.string,
chartStackTrace: PropTypes.string,
queryResponse: PropTypes.object,
queriesResponse: PropTypes.arrayOf(PropTypes.object),
triggerQuery: PropTypes.bool,
refreshOverlayVisible: PropTypes.bool,
errorMessage: PropTypes.node,
Expand Down Expand Up @@ -148,14 +148,8 @@ class Chart extends React.PureComponent {
});
}

renderErrorMessage() {
const {
chartAlert,
chartStackTrace,
dashboardId,
owners,
queryResponse,
} = this.props;
renderErrorMessage(queryResponse) {
const { chartAlert, chartStackTrace, dashboardId, owners } = this.props;

const error = queryResponse?.errors?.[0];
if (error) {
Expand Down Expand Up @@ -185,14 +179,14 @@ class Chart extends React.PureComponent {
errorMessage,
onQuery,
refreshOverlayVisible,
queriesResponse,
} = this.props;

const isLoading = chartStatus === 'loading';

const isFaded = refreshOverlayVisible && !errorMessage;
this.renderContainerStartTime = Logger.getTimestamp();
if (chartStatus === 'failed') {
return this.renderErrorMessage();
return queriesResponse.map(item => this.renderErrorMessage(item));
Copy link
Member

Choose a reason for hiding this comment

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

Do you have a screenshot of multiple errors rendered in a chart?

Copy link
Contributor Author

@simcha90 simcha90 Dec 3, 2020

Choose a reason for hiding this comment

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

@robdiciuccio Hi good catch, I didn't check it properly for all kinds of charts (new/old). I pushed now some small fix for it. Basically for now for new api there is no case that it returns more than 1 error per request even for multi queries, but if it will return we can support it, but for now it will be only one error.

P.S. If you have some more places where I can check this functionality, update me please, because I'm new in Superset so I don't know all places and cases of its usages :)

Attaching screenshots after my fix for charts:

  1. Old charts:
    Screen Shot 2020-12-03 at 9 02 11

  2. New charts:
    Screen Shot 2020-12-03 at 9 10 54

  3. New charts with multiqueries (looks the same):
    Screen Shot 2020-12-03 at 9 00 19

}
if (errorMessage) {
return (
Expand Down
14 changes: 9 additions & 5 deletions superset-frontend/src/chart/ChartRenderer.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ const propTypes = {
chartAlert: PropTypes.string,
chartStatus: PropTypes.string,
queryResponse: PropTypes.object,
queriesResponse: PropTypes.arrayOf(PropTypes.object),
triggerQuery: PropTypes.bool,
refreshOverlayVisible: PropTypes.bool,
// dashboard callbacks
Expand Down Expand Up @@ -85,7 +86,8 @@ class ChartRenderer extends React.Component {

if (resultsReady) {
this.hasQueryResponseChange =
nextProps.queryResponse !== this.props.queryResponse;
nextProps.queryResponse !== this.props.queryResponse ||
nextProps.queriesResponse !== this.props.queriesResponse;
return (
this.hasQueryResponseChange ||
nextProps.annotationData !== this.props.annotationData ||
Expand Down Expand Up @@ -180,6 +182,7 @@ class ChartRenderer extends React.Component {
initialValues,
formData,
queryResponse,
queriesResponse,
} = this.props;

// It's bad practice to use unprefixed `vizType` as classnames for chart
Expand All @@ -197,10 +200,10 @@ class ChartRenderer extends React.Component {
? `-${
// eslint-disable-next-line camelcase
typeof __webpack_require__ !== 'undefined' &&
// eslint-disable-next-line camelcase, no-undef
typeof __webpack_require__.h === 'function' &&
// eslint-disable-next-line no-undef
__webpack_require__.h()
// eslint-disable-next-line camelcase, no-undef
typeof __webpack_require__.h === 'function' &&
// eslint-disable-next-line no-undef
__webpack_require__.h()
}`
: '';

Expand All @@ -219,6 +222,7 @@ class ChartRenderer extends React.Component {
formData={formData}
hooks={this.hooks}
queryData={queryResponse}
queriesData={queriesResponse}
onRenderSuccess={this.handleRenderSuccess}
onRenderFailure={this.handleRenderFailure}
/>
Expand Down
51 changes: 27 additions & 24 deletions superset-frontend/src/chart/chartAction.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ export function chartUpdateStarted(queryController, latestQueryFormData, key) {
}

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

export const CHART_UPDATE_STOPPED = 'CHART_UPDATE_STOPPED';
Expand Down Expand Up @@ -132,7 +132,7 @@ const legacyChartDataRequest = async (
// Make the legacy endpoint return a payload that corresponds to the
// V1 chart data endpoint response signature.
return {
result: [json],
result: [json],
};
});
};
Expand Down Expand Up @@ -226,7 +226,7 @@ export function runAnnotationQuery(
key,
isDashboardRequest = false,
) {
return function (dispatch, getState) {
return function(dispatch, getState) {
const sliceKey = key || Object.keys(getState().charts)[0];
// make a copy of formData, not modifying original formData
const fd = {
Expand Down Expand Up @@ -360,28 +360,31 @@ export function exploreJSON(
// problem: response holds a list of results, when before we were just getting one result.
// How to make the entire app compatible with multiple results?
// For now just use the first result.
const result = response.result[0];
const queryResponse = response.result[0]; // deprecated
const queriesResponse = response.result;

dispatch(
logEvent(LOG_ACTIONS_LOAD_CHART, {
slice_id: key,
applied_filters: result.applied_filters,
is_cached: result.is_cached,
force_refresh: force,
row_count: result.rowcount,
datasource: formData.datasource,
start_offset: logStart,
ts: new Date().getTime(),
duration: Logger.getTimestamp() - logStart,
has_extra_filters:
formData.extra_filters && formData.extra_filters.length > 0,
viz_type: formData.viz_type,
data_age: result.is_cached
? moment(new Date()).diff(moment.utc(result.cached_dttm))
: null,
}),
queriesResponse.forEach(resultItem =>
dispatch(
logEvent(LOG_ACTIONS_LOAD_CHART, {
slice_id: key,
applied_filters: resultItem.applied_filters,
is_cached: resultItem.is_cached,
force_refresh: force,
row_count: resultItem.rowcount,
datasource: formData.datasource,
start_offset: logStart,
ts: new Date().getTime(),
duration: Logger.getTimestamp() - logStart,
has_extra_filters:
formData.extra_filters && formData.extra_filters.length > 0,
viz_type: formData.viz_type,
data_age: resultItem.is_cached
? moment(new Date()).diff(moment.utc(resultItem.cached_dttm))
: null,
}),
),
);
return dispatch(chartUpdateSucceeded(result, key));
return dispatch(chartUpdateSucceeded(queryResponse, queriesResponse, key));
})
.catch(response => {
const appendErrorLog = (errorDetails, isCached) => {
Expand Down
4 changes: 3 additions & 1 deletion superset-frontend/src/chart/chartReducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export const chart = {
latestQueryFormData: {},
queryController: null,
queryResponse: null,
queriesResponse: null,
triggerQuery: true,
lastRendered: 0,
};
Expand All @@ -47,8 +48,9 @@ export default function chartReducer(charts = {}, action) {
return {
...state,
chartStatus: 'success',
queryResponse: action.queryResponse,
chartAlert: null,
queryResponse: action.queryResponse,
queriesResponse: action.queriesResponse,
chartUpdateEndTime: now(),
};
},
Expand Down
8 changes: 4 additions & 4 deletions superset-frontend/src/dashboard/components/SliceHeader.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ const propTypes = {
innerRef: PropTypes.func,
slice: PropTypes.object.isRequired,
isExpanded: PropTypes.bool,
isCached: PropTypes.bool,
cachedDttm: PropTypes.string,
isCached: PropTypes.arrayOf(PropTypes.bool),
cachedDttm: PropTypes.arrayOf(PropTypes.string),
updatedDttm: PropTypes.number,
updateSliceName: PropTypes.func,
toggleExpandSlice: PropTypes.func,
Expand Down Expand Up @@ -64,8 +64,8 @@ const defaultProps = {
annotationError: {},
cachedDttm: null,
updatedDttm: null,
isCached: false,
isExpanded: false,
isCached: [],
isExpanded: [],
sliceName: '',
supersetCanExplore: false,
supersetCanCSV: false,
Expand Down
22 changes: 14 additions & 8 deletions superset-frontend/src/dashboard/components/SliceHeaderControls.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ const propTypes = {
componentId: PropTypes.string.isRequired,
dashboardId: PropTypes.number.isRequired,
addDangerToast: PropTypes.func.isRequired,
isCached: PropTypes.bool,
isCached: PropTypes.arrayOf(PropTypes.bool),
cachedDttm: PropTypes.arrayOf(PropTypes.string),
isExpanded: PropTypes.bool,
cachedDttm: PropTypes.string,
updatedDttm: PropTypes.number,
supersetCanExplore: PropTypes.bool,
supersetCanCSV: PropTypes.bool,
Expand All @@ -49,9 +49,9 @@ const defaultProps = {
toggleExpandSlice: () => ({}),
exploreChart: () => ({}),
exportCSV: () => ({}),
cachedDttm: null,
cachedDttm: [],
updatedDttm: null,
isCached: false,
isCached: [],
isExpanded: false,
supersetCanExplore: false,
supersetCanCSV: false,
Expand Down Expand Up @@ -171,11 +171,17 @@ class SliceHeaderControls extends React.PureComponent {
addDangerToast,
isFullSize,
} = this.props;
const cachedWhen = moment.utc(cachedDttm).fromNow();
const cachedWhen = cachedDttm.map(itemCachedDttm =>
moment.utc(itemCachedDttm).fromNow(),
);
const updatedWhen = updatedDttm ? moment.utc(updatedDttm).fromNow() : '';
const refreshTooltip = isCached
? t('Cached %s', cachedWhen)
: (updatedWhen && t('Fetched %s', updatedWhen)) || '';
let refreshTooltip = isCached.map(itemCached =>
(itemCached
? t('Cached %s', cachedWhen)
: (updatedWhen && t('Fetched %s', updatedWhen)) || ''),
);
// If all queries have same cache time we can unit them to one
refreshTooltip = [...new Set(refreshTooltip)].join(', ');
const resizeLabel = isFullSize ? t('Minimize') : t('Maximize');

const menu = (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -257,17 +257,19 @@ export default class Chart extends React.Component {
return <MissingChart height={this.getChartHeight()} />;
}

const { queryResponse, chartUpdateEndTime } = chart;
const isCached = queryResponse && queryResponse.is_cached;
const cachedDttm = queryResponse && queryResponse.cached_dttm;
const { chartUpdateEndTime, queriesResponse } = chart;
// eslint-disable-next-line camelcase
const isCached = queriesResponse?.map(({ is_cached }) => is_cached) || [];
const cachedDttm =
// eslint-disable-next-line camelcase
queriesResponse?.map(({ cached_dttm }) => cached_dttm) || [];
const isOverflowable = OVERFLOWABLE_VIZ_TYPES.has(slice.viz_type);
const initialValues = isFilterBox(id)
? getFilterValuesByFilterId({
activeFilters: filters,
filterId: id,
})
: {};

return (
<div>
<SliceHeader
Expand Down Expand Up @@ -333,6 +335,7 @@ export default class Chart extends React.Component {
dashboardId={dashboardId}
initialValues={initialValues}
formData={formData}
queriesResponse={chart.queriesResponse}
queryResponse={chart.queryResponse}
timeout={timeout}
triggerQuery={chart.triggerQuery}
Expand Down
1 change: 1 addition & 0 deletions superset-frontend/src/dashboard/util/propShapes.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ export const chartPropShape = PropTypes.shape({
latestQueryFormData: PropTypes.object,
queryController: PropTypes.shape({ abort: PropTypes.func }),
queryResponse: PropTypes.object,
queriesResponse: PropTypes.arrayOf(PropTypes.object),
triggerQuery: PropTypes.bool,
lastRendered: PropTypes.number,
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ class ExploreChartPanel extends React.PureComponent {
onQuery={this.props.onQuery}
owners={this.props?.slice?.owners}
queryResponse={chart.queryResponse}
queriesResponse={chart.queriesResponse}
refreshOverlayVisible={this.props.refreshOverlayVisible}
setControlValue={this.props.actions.setControlValue}
timeout={this.props.timeout}
Expand Down