feat: Improve state key generation for dashboards and charts (#18576)

* feat: Improve state key generation for dashboards and charts
This commit is contained in:
Michael S. Molina
2022-02-14 17:09:06 -03:00
committed by GitHub
parent 801091be03
commit 48a80950de
29 changed files with 694 additions and 234 deletions

View File

@@ -16,7 +16,9 @@
# under the License.
import logging
from abc import ABC
from typing import Optional
from flask import session
from sqlalchemy.exc import SQLAlchemyError
from superset.commands.base import BaseCommand
@@ -28,6 +30,7 @@ from superset.key_value.commands.exceptions import (
KeyValueAccessDeniedError,
KeyValueUpdateFailedError,
)
from superset.key_value.utils import cache_key, random_key
logger = logging.getLogger(__name__)
@@ -38,7 +41,7 @@ class UpdateFormDataCommand(BaseCommand, ABC):
):
self._cmd_params = cmd_params
def run(self) -> bool:
def run(self) -> Optional[str]:
try:
dataset_id = self._cmd_params.dataset_id
chart_id = self._cmd_params.chart_id
@@ -53,14 +56,25 @@ class UpdateFormDataCommand(BaseCommand, ABC):
user_id = actor.get_user_id()
if state["owner"] != user_id:
raise KeyValueAccessDeniedError()
# Generate a new key if tab_id changes or equals 0
tab_id = self._cmd_params.tab_id
contextual_key = cache_key(
session.get("_id"), tab_id, dataset_id, chart_id
)
key = cache_manager.explore_form_data_cache.get(contextual_key)
if not key or not tab_id:
key = random_key()
cache_manager.explore_form_data_cache.set(contextual_key, key)
new_state: TemporaryExploreState = {
"owner": actor.get_user_id(),
"dataset_id": dataset_id,
"chart_id": chart_id,
"form_data": form_data,
}
return cache_manager.explore_form_data_cache.set(key, new_state)
return False
cache_manager.explore_form_data_cache.set(key, new_state)
return key
except SQLAlchemyError as ex:
logger.exception("Error running update command")
raise KeyValueUpdateFailedError() from ex