Skip to content

Commit

Permalink
Fix cache for multiple time comparisons (apache#5828)
Browse files Browse the repository at this point in the history
* Fix cache for multiple time comparisons

* Remove simple cache

* Improve docstring

(cherry picked from commit 299e20a)
  • Loading branch information
betodealmeida authored and youngyjd committed Oct 17, 2018
1 parent b0c3dac commit e1d0d3f
Showing 1 changed file with 14 additions and 8 deletions.
22 changes: 14 additions & 8 deletions superset/viz.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit e1d0d3f

Please sign in to comment.