Skip to content

Commit

Permalink
Fixing the CACHING
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
mistercrunch committed Feb 19, 2017
1 parent b9e7f29 commit a63121e
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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'),
},

};
Expand Down Expand Up @@ -202,6 +202,9 @@ class ChartContainer extends React.PureComponent {
</div>
);
}
runQuery() {
this.props.actions.runQuery(this.props.formData, true);
}

render() {
if (this.props.standalone) {
Expand Down Expand Up @@ -241,6 +244,21 @@ class ChartContainer extends React.PureComponent {
}

<div className="pull-right">
{this.props.chartStatus === 'success' &&
this.props.queryResponse &&
this.props.queryResponse.is_cached &&
<TooltipWrapper
tooltip="Loaded from cache. Click to force refresh"
label="cache-desc"
>
<Label
style={{ fontSize: '10px', marginRight: '5px', cursor: 'pointer' }}
onClick={this.runQuery.bind(this)}
>
cached
</Label>
</TooltipWrapper>
}
<Timer
startTime={this.props.chartUpdateStartTime}
endTime={this.props.chartUpdateEndTime}
Expand All @@ -251,8 +269,7 @@ class ChartContainer extends React.PureComponent {
<ExploreActionButtons
slice={this.state.mockSlice}
canDownload={this.props.can_download}
queryEndpoint={getExploreUrl(
this.props.latestQueryFormData, this.props.datasource_type, 'query')}
queryEndpoint={getExploreUrl(this.props.latestQueryFormData, 'query')}
/>
</div>
</div>
Expand Down Expand Up @@ -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,
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
8 changes: 7 additions & 1 deletion superset/assets/javascripts/explorev2/exploreUtils.js
Original file line number Diff line number Diff line change
@@ -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}`;
Expand Down
2 changes: 1 addition & 1 deletion superset/assets/javascripts/modules/superset.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand Down
2 changes: 1 addition & 1 deletion superset/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'}

Expand Down
3 changes: 2 additions & 1 deletion superset/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
2 changes: 1 addition & 1 deletion superset/viz.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down

0 comments on commit a63121e

Please sign in to comment.