From 72d815c0f950ad655cb076ef8f8f8d01b65fe9aa Mon Sep 17 00:00:00 2001 From: John Bodley <4567245+john-bodley@users.noreply.github.com> Date: Mon, 2 Jul 2018 15:32:21 -0700 Subject: [PATCH] [cache] Allowing zero cache-timeout (#5315) --- superset/connectors/druid/views.py | 8 ++++++++ superset/connectors/sqla/views.py | 3 +++ superset/views/core.py | 6 +++++- superset/viz.py | 6 +++--- tests/viz_tests.py | 9 +++++++++ 5 files changed, 28 insertions(+), 4 deletions(-) diff --git a/superset/connectors/druid/views.py b/superset/connectors/druid/views.py index 9d2539a46..9a33c8de4 100644 --- a/superset/connectors/druid/views.py +++ b/superset/connectors/druid/views.py @@ -176,6 +176,11 @@ class DruidClusterModelView(SupersetModelView, DeleteMixin, YamlExportMixin): # 'broker_port': _('Broker Port'), 'broker_endpoint': _('Broker Endpoint'), } + description_columns = { + 'cache_timeout': _( + 'Duration (in seconds) of the caching timeout for this cluster. ' + 'Note this defaults to the global timeout if undefined.'), + } def pre_add(self, cluster): security_manager.merge_perm('database_access', cluster.perm) @@ -249,6 +254,9 @@ class DruidDatasourceModelView(DatasourceModelView, DeleteMixin, YamlExportMixin 'default_endpoint': _( 'Redirects to this endpoint when clicking on the datasource ' 'from the datasource list'), + 'cache_timeout': _( + 'Duration (in seconds) of the caching timeout for this datasource. ' + 'Note this defaults to the cluster timeout if undefined.'), } base_filters = [['id', DatasourceFilter, lambda: []]] label_columns = { diff --git a/superset/connectors/sqla/views.py b/superset/connectors/sqla/views.py index b46af1015..b8349e57f 100644 --- a/superset/connectors/sqla/views.py +++ b/superset/connectors/sqla/views.py @@ -219,6 +219,9 @@ class TableModelView(DatasourceModelView, DeleteMixin, YamlExportMixin): # noqa 'template_params': _( 'A set of parameters that become available in the query using ' 'Jinja templating syntax'), + 'cache_timeout': _( + 'Duration (in seconds) of the caching timeout for this table. ' + 'Note this defaults to the database timeout if undefined.'), } label_columns = { 'slices': _('Associated Charts'), diff --git a/superset/views/core.py b/superset/views/core.py index 9756b5b4a..bddc54437 100755 --- a/superset/views/core.py +++ b/superset/views/core.py @@ -266,6 +266,9 @@ class DatabaseView(SupersetModelView, DeleteMixin, YamlExportMixin): # noqa 'Allow SQL Lab to fetch a list of all tables and all views across ' 'all database schemas. For large data warehouse with thousands of ' 'tables, this can be expensive and put strain on the system.'), + 'cache_timeout': _( + 'Duration (in seconds) of the caching timeout for this database. ' + 'Note this defaults to the global timeout if undefined.'), } label_columns = { 'expose_in_sqllab': _('Expose in SQL Lab'), @@ -450,7 +453,8 @@ class SliceModelView(SupersetModelView, DeleteMixin): # noqa 'want to alter specific parameters.', ), 'cache_timeout': _( - 'Duration (in seconds) of the caching timeout for this chart.'), + 'Duration (in seconds) of the caching timeout for this chart. ' + 'Note this defaults to the datasource/table timeout if undefined.'), } base_filters = [['id', SliceFilter, lambda: []]] label_columns = { diff --git a/superset/viz.py b/superset/viz.py index a460b9f41..6b3c6d843 100644 --- a/superset/viz.py +++ b/superset/viz.py @@ -311,13 +311,13 @@ class BaseViz(object): @property def cache_timeout(self): - if self.form_data.get('cache_timeout'): + if self.form_data.get('cache_timeout') is not None: return int(self.form_data.get('cache_timeout')) - if self.datasource.cache_timeout: + if self.datasource.cache_timeout is not None: return self.datasource.cache_timeout if ( hasattr(self.datasource, 'database') and - self.datasource.database.cache_timeout): + self.datasource.database.cache_timeout) is not None: return self.datasource.database.cache_timeout return config.get('CACHE_DEFAULT_TIMEOUT') diff --git a/tests/viz_tests.py b/tests/viz_tests.py index 1bc6e62a9..474c5500a 100644 --- a/tests/viz_tests.py +++ b/tests/viz_tests.py @@ -10,6 +10,7 @@ import unittest from mock import Mock, patch import pandas as pd +from superset import app from superset.utils import DTTM_ALIAS import superset.viz as viz from .utils import load_fixture @@ -101,13 +102,21 @@ class BaseVizTestCase(unittest.TestCase): def test_cache_timeout(self): datasource = Mock() + datasource.cache_timeout = 0 + test_viz = viz.BaseViz(datasource, form_data={}) + self.assertEqual(0, test_viz.cache_timeout) datasource.cache_timeout = 156 test_viz = viz.BaseViz(datasource, form_data={}) self.assertEqual(156, test_viz.cache_timeout) datasource.cache_timeout = None datasource.database = Mock() + datasource.database.cache_timeout = 0 + self.assertEqual(0, test_viz.cache_timeout) datasource.database.cache_timeout = 1666 self.assertEqual(1666, test_viz.cache_timeout) + datasource.database.cache_timeout = None + test_viz = viz.BaseViz(datasource, form_data={}) + self.assertEqual(app.config['CACHE_DEFAULT_TIMEOUT'], test_viz.cache_timeout) class TableVizTestCase(unittest.TestCase):