Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: respect chart cache timeout setting #21637

Merged
merged 4 commits into from
Sep 29, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions superset/common/query_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
QueryContextProcessor,
)
from superset.common.query_object import QueryObject
from superset.models.slice import Slice

if TYPE_CHECKING:
from superset.connectors.base.models import BaseDatasource
Expand All @@ -46,6 +47,7 @@ class QueryContext:
enforce_numerical_metrics: ClassVar[bool] = True

datasource: BaseDatasource
slice_id: Optional[int] = None
mayurnewase marked this conversation as resolved.
Show resolved Hide resolved
queries: List[QueryObject]
form_data: Optional[Dict[str, Any]]
result_type: ChartDataResultType
Expand All @@ -64,6 +66,7 @@ def __init__(
*,
datasource: BaseDatasource,
queries: List[QueryObject],
slice: Optional[Slice],
form_data: Optional[Dict[str, Any]],
result_type: ChartDataResultType,
result_format: ChartDataResultFormat,
Expand All @@ -72,6 +75,7 @@ def __init__(
cache_values: Dict[str, Any],
) -> None:
self.datasource = datasource
self.slice = slice
self.result_type = result_type
self.result_format = result_format
self.queries = queries
Expand All @@ -98,6 +102,8 @@ def get_payload(
def get_cache_timeout(self) -> Optional[int]:
if self.custom_cache_timeout is not None:
return self.custom_cache_timeout
if self.slice and self.slice.cache_timeout:
return self.slice.cache_timeout
if self.datasource.cache_timeout is not None:
return self.datasource.cache_timeout
if hasattr(self.datasource, "database"):
Expand Down
11 changes: 11 additions & 0 deletions superset/common/query_context_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@
from typing import Any, Dict, List, Optional, TYPE_CHECKING

from superset import app, db
from superset.charts.dao import ChartDAO
from superset.common.chart_data import ChartDataResultFormat, ChartDataResultType
from superset.common.query_context import QueryContext
from superset.common.query_object_factory import QueryObjectFactory
from superset.datasource.dao import DatasourceDAO
from superset.models.slice import Slice
from superset.utils.core import DatasourceDict, DatasourceType

if TYPE_CHECKING:
Expand Down Expand Up @@ -55,6 +57,11 @@ def create(
datasource_model_instance = None
if datasource:
datasource_model_instance = self._convert_to_model(datasource)

slice = None
if form_data and form_data.get("slice_id") is not None:
slice = self._get_slice(form_data.get("slice_id"))

result_type = result_type or ChartDataResultType.FULL
result_format = result_format or ChartDataResultFormat.JSON
queries_ = [
Expand All @@ -72,6 +79,7 @@ def create(
return QueryContext(
datasource=datasource_model_instance,
queries=queries_,
slice=slice,
form_data=form_data,
result_type=result_type,
result_format=result_format,
Expand All @@ -88,3 +96,6 @@ def _convert_to_model(self, datasource: DatasourceDict) -> BaseDatasource:
datasource_type=DatasourceType(datasource["type"]),
datasource_id=int(datasource["id"]),
)

def _get_slice(self, slice_id: Any) -> Optional[Slice]:
return ChartDAO.find_by_id(slice_id)