From 025d6878ccf76badd93a2265bb4d6c45aa9c906f Mon Sep 17 00:00:00 2001 From: Beto Dealmeida Date: Fri, 7 Sep 2018 15:45:28 -0700 Subject: [PATCH] Fix cache for multiple time comparisons (#5828) * Fix cache for multiple time comparisons * Remove simple cache * Improve docstring (cherry picked from commit 299e20a23f743142fb151d6634ed35bfdcb0204d) --- superset/viz.py | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/superset/viz.py b/superset/viz.py index 911d8b8659edd..1f1f79c2c020f 100644 --- a/superset/viz.py +++ b/superset/viz.py @@ -331,15 +331,21 @@ def get_json(self): self.get_payload(), default=utils.json_int_dttm_ser, ignore_nan=True) - def cache_key(self, query_obj): + def cache_key(self, query_obj, **extra): """ - The cache key is made out of the key/values in `query_obj` + The cache key is made out of the key/values in `query_obj`, plus any + other key/values in `extra`. - We remove datetime bounds that are hard values, - and replace them with the use-provided inputs to bounds, which - may be time-relative (as in "5 days ago" or "now"). + We remove datetime bounds that are hard values, and replace them with + the use-provided inputs to bounds, which may be time-relative (as in + "5 days ago" or "now"). + + The `extra` arguments are currently used by time shift queries, since + different time shifts wil differ only in the `from_dttm` and `to_dttm` + values which are stripped. """ cache_dict = copy.copy(query_obj) + cache_dict.update(extra) for k in ['from_dttm', 'to_dttm']: del cache_dict[k] @@ -364,11 +370,11 @@ def get_payload(self, query_obj=None): del payload['df'] return payload - def get_df_payload(self, query_obj=None): + def get_df_payload(self, query_obj=None, **kwargs): """Handles caching around the df payload retrieval""" if not query_obj: query_obj = self.query_obj() - cache_key = self.cache_key(query_obj) if query_obj else None + cache_key = self.cache_key(query_obj, **kwargs) if query_obj else None logging.info('Cache key: {}'.format(cache_key)) is_loaded = False stacktrace = None @@ -1216,7 +1222,7 @@ def run_extra_queries(self): query_object['from_dttm'] -= delta query_object['to_dttm'] -= delta - df2 = self.get_df_payload(query_object).get('df') + df2 = self.get_df_payload(query_object, time_compare=option).get('df') if df2 is not None and DTTM_ALIAS in df2: label = '{} offset'. format(option) df2[DTTM_ALIAS] += delta