feat(dashboard): make permalink deterministic (#20632)

This commit is contained in:
Jesse Yang
2022-07-12 16:33:18 -07:00
committed by GitHub
parent 5317462b49
commit c3ac61271a
5 changed files with 37 additions and 16 deletions

View File

@@ -22,13 +22,19 @@ from superset.dashboards.dao import DashboardDAO
from superset.dashboards.permalink.commands.base import BaseDashboardPermalinkCommand
from superset.dashboards.permalink.exceptions import DashboardPermalinkCreateFailedError
from superset.dashboards.permalink.types import DashboardPermalinkState
from superset.key_value.commands.create import CreateKeyValueCommand
from superset.key_value.utils import encode_permalink_key
from superset.key_value.commands.upsert import UpsertKeyValueCommand
from superset.key_value.utils import encode_permalink_key, get_deterministic_uuid
from superset.utils.core import get_user_id
logger = logging.getLogger(__name__)
class CreateDashboardPermalinkCommand(BaseDashboardPermalinkCommand):
"""
Get or create a permalink key for the given dashboard in certain state.
Will reuse the key for the same user and dashboard state.
"""
def __init__(
self,
dashboard_id: str,
@@ -45,12 +51,13 @@ class CreateDashboardPermalinkCommand(BaseDashboardPermalinkCommand):
"dashboardId": self.dashboard_id,
"state": self.state,
}
key = CreateKeyValueCommand(
user_id = get_user_id()
key = UpsertKeyValueCommand(
resource=self.resource,
key=get_deterministic_uuid(self.salt, (user_id, value)),
value=value,
).run()
if key.id is None:
raise DashboardPermalinkCreateFailedError("Unexpected missing key id")
assert key.id # for type checks
return encode_permalink_key(key=key.id, salt=self.salt)
except SQLAlchemyError as ex:
logger.exception("Error running create command")