From a63121e3d4859b085c85c8a1ec98d11a037a7020 Mon Sep 17 00:00:00 2001 From: Maxime Beauchemin Date: Sat, 18 Feb 2017 22:34:21 -0800 Subject: [PATCH] Fixing the CACHING Caching wasn't working after deprecate_v1, this addresses it. Also surfacing whether the data is served from cache in explore view and a way to force run the query bypassing the cache. --- .../explorev2/actions/exploreActions.js | 4 +-- .../explorev2/components/ChartContainer.jsx | 30 +++++++++++++++---- .../components/ExploreViewContainer.jsx | 2 +- .../javascripts/explorev2/exploreUtils.js | 8 ++++- .../assets/javascripts/modules/superset.js | 2 +- superset/config.py | 2 +- superset/views.py | 3 +- superset/viz.py | 2 +- 8 files changed, 39 insertions(+), 14 deletions(-) diff --git a/superset/assets/javascripts/explorev2/actions/exploreActions.js b/superset/assets/javascripts/explorev2/actions/exploreActions.js index 7fba28a1e6851..dc3e36d6f6cfe 100644 --- a/superset/assets/javascripts/explorev2/actions/exploreActions.js +++ b/superset/assets/javascripts/explorev2/actions/exploreActions.js @@ -229,9 +229,9 @@ export function updateChartStatus(status) { } export const RUN_QUERY = 'RUN_QUERY'; -export function runQuery(formData, datasourceType) { +export function runQuery(formData, force = false) { return function (dispatch) { - const url = getExploreUrl(formData, datasourceType, 'json'); + const url = getExploreUrl(formData, 'json', force); const queryRequest = $.getJSON(url, function (queryResponse) { dispatch(chartUpdateSucceeded(queryResponse)); }).fail(function (err) { diff --git a/superset/assets/javascripts/explorev2/components/ChartContainer.jsx b/superset/assets/javascripts/explorev2/components/ChartContainer.jsx index 22c5b7552ac87..ca07a48b18a41 100644 --- a/superset/assets/javascripts/explorev2/components/ChartContainer.jsx +++ b/superset/assets/javascripts/explorev2/components/ChartContainer.jsx @@ -1,7 +1,7 @@ import $ from 'jquery'; import React, { PropTypes } from 'react'; import { connect } from 'react-redux'; -import { Panel, Alert, Collapse } from 'react-bootstrap'; +import { Alert, Collapse, Label, Panel } from 'react-bootstrap'; import visMap from '../../../visualizations/main'; import { d3format } from '../../modules/utils'; import ExploreActionButtons from './ExploreActionButtons'; @@ -125,10 +125,10 @@ class ChartContainer extends React.PureComponent { }, data: { - csv_endpoint: getExploreUrl(this.props.formData, this.props.datasource_type, 'csv'), - json_endpoint: getExploreUrl(this.props.formData, this.props.datasource_type, 'json'), + csv_endpoint: getExploreUrl(this.props.formData, 'csv'), + json_endpoint: getExploreUrl(this.props.formData, 'json'), standalone_endpoint: getExploreUrl( - this.props.formData, this.props.datasource_type, 'standalone'), + this.props.formData, 'standalone'), }, }; @@ -202,6 +202,9 @@ class ChartContainer extends React.PureComponent { ); } + runQuery() { + this.props.actions.runQuery(this.props.formData, true); + } render() { if (this.props.standalone) { @@ -241,6 +244,21 @@ class ChartContainer extends React.PureComponent { }
+ {this.props.chartStatus === 'success' && + this.props.queryResponse && + this.props.queryResponse.is_cached && + + + + }
@@ -286,6 +303,7 @@ function mapStateToProps(state) { table_name: formData.datasource_name, viz_type: formData.viz_type, triggerRender: state.triggerRender, + datasourceType: state.datasource ? state.datasource.type : null, }; } diff --git a/superset/assets/javascripts/explorev2/components/ExploreViewContainer.jsx b/superset/assets/javascripts/explorev2/components/ExploreViewContainer.jsx index 83117a6e36544..a8637ad1ed675 100644 --- a/superset/assets/javascripts/explorev2/components/ExploreViewContainer.jsx +++ b/superset/assets/javascripts/explorev2/components/ExploreViewContainer.jsx @@ -82,7 +82,7 @@ class ExploreViewContainer extends React.Component { runQuery() { - this.props.actions.runQuery(this.props.form_data, this.props.datasource_type); + this.props.actions.runQuery(this.props.form_data); } handleResize() { diff --git a/superset/assets/javascripts/explorev2/exploreUtils.js b/superset/assets/javascripts/explorev2/exploreUtils.js index 24b7b60eae466..59d83d2f14431 100644 --- a/superset/assets/javascripts/explorev2/exploreUtils.js +++ b/superset/assets/javascripts/explorev2/exploreUtils.js @@ -1,8 +1,14 @@ /* eslint camelcase: 0 */ -export function getExploreUrl(form_data, dummy, endpoint = 'base') { +export function getExploreUrl(form_data, endpoint = 'base', force = false) { + if (!form_data.datasource) { + return null; + } const [datasource_id, datasource_type] = form_data.datasource.split('__'); let params = `${datasource_type}/${datasource_id}/`; params += '?form_data=' + encodeURIComponent(JSON.stringify(form_data)); + if (force) { + params += '&force=true'; + } switch (endpoint) { case 'base': return `/superset/explore/${params}`; diff --git a/superset/assets/javascripts/modules/superset.js b/superset/assets/javascripts/modules/superset.js index 681af11a23b98..aae2191a326c7 100644 --- a/superset/assets/javascripts/modules/superset.js +++ b/superset/assets/javascripts/modules/superset.js @@ -63,7 +63,7 @@ const px = function () { const container = $(selector); const sliceId = data.slice_id; const formData = applyDefaultFormData(data.form_data); - const jsonEndpoint = getExploreUrl(formData, 'table', 'json'); + const jsonEndpoint = getExploreUrl(formData, 'json'); const origJsonEndpoint = jsonEndpoint; let dttm = 0; const stopwatch = function () { diff --git a/superset/config.py b/superset/config.py index 3a602bfeec70e..293fa485cdfb7 100644 --- a/superset/config.py +++ b/superset/config.py @@ -152,7 +152,7 @@ # Setup image size default is (300, 200, True) # IMG_SIZE = (300, 200, True) -CACHE_DEFAULT_TIMEOUT = None +CACHE_DEFAULT_TIMEOUT = 60 * 60 * 24 CACHE_CONFIG = {'CACHE_TYPE': 'null'} TABLE_NAMES_CACHE_CONFIG = {'CACHE_TYPE': 'null'} diff --git a/superset/views.py b/superset/views.py index c2803131aa2e3..a12b9892ffea7 100755 --- a/superset/views.py +++ b/superset/views.py @@ -1566,7 +1566,8 @@ def explore_json(self, datasource_type, datasource_id): payload = {} try: - payload = viz_obj.get_payload() + payload = viz_obj.get_payload( + force=request.args.get('force') == 'true') except Exception as e: logging.exception(e) return json_error_response(utils.error_msg_from_exception(e)) diff --git a/superset/viz.py b/superset/viz.py index bb9391ab5cbf8..288443aa2a5fd 100755 --- a/superset/viz.py +++ b/superset/viz.py @@ -223,7 +223,7 @@ def get_json(self, force=False): @property def cache_key(self): - s = str((k, self.form_data[k]) for k in sorted(self.form_data.keys())) + s = str([(k, self.form_data[k]) for k in sorted(self.form_data.keys())]) return hashlib.md5(s.encode('utf-8')).hexdigest() def get_payload(self, force=False):