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

perf: Memoize the common_bootstrap_payload and include user param (#21018) #21439

Merged
merged 2 commits into from
Sep 13, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 2 additions & 2 deletions superset/embedded/view.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import json
from typing import Callable

from flask import abort, request
from flask import abort, g, request
from flask_appbuilder import expose
from flask_login import AnonymousUserMixin, LoginManager
from flask_wtf.csrf import same_origin
Expand Down Expand Up @@ -77,7 +77,7 @@ def embedded(
)

bootstrap_data = {
"common": common_bootstrap_payload(),
"common": common_bootstrap_payload(g.user),
"embedded": {
"dashboard_id": embedded.dashboard_id,
},
Expand Down
27 changes: 17 additions & 10 deletions superset/views/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
from flask_appbuilder.actions import action
from flask_appbuilder.forms import DynamicForm
from flask_appbuilder.models.sqla.filters import BaseFilter
from flask_appbuilder.security.sqla.models import User
from flask_appbuilder.widgets import ListWidget
from flask_babel import get_locale, gettext as __, lazy_gettext as _
from flask_jwt_extended.exceptions import NoAuthorizationError
Expand Down Expand Up @@ -71,6 +72,7 @@
SupersetException,
SupersetSecurityException,
)
from superset.extensions import cache_manager
from superset.models.helpers import ImportExportMixin
from superset.reports.models import ReportRecipientType
from superset.superset_typing import FlaskResponse
Expand Down Expand Up @@ -284,7 +286,7 @@ def json_response(obj: Any, status: int = 200) -> FlaskResponse:
def render_app_template(self) -> FlaskResponse:
payload = {
"user": bootstrap_user_data(g.user, include_perms=True),
"common": common_bootstrap_payload(),
"common": common_bootstrap_payload(g.user),
}
return self.render_template(
"superset/spa.html",
Expand All @@ -295,7 +297,7 @@ def render_app_template(self) -> FlaskResponse:
)


def menu_data() -> Dict[str, Any]:
def menu_data(user: User) -> Dict[str, Any]:
menu = appbuilder.menu.get_data()

languages = {}
Expand Down Expand Up @@ -340,22 +342,27 @@ def menu_data() -> Dict[str, Any]:
"build_number": build_number,
"languages": languages,
"show_language_picker": len(languages.keys()) > 1,
"user_is_anonymous": g.user.is_anonymous,
"user_is_anonymous": user.is_anonymous,
"user_info_url": None
if appbuilder.app.config["MENU_HIDE_USER_INFO"]
else appbuilder.get_url_for_userinfo,
"user_logout_url": appbuilder.get_url_for_logout,
"user_login_url": appbuilder.get_url_for_login,
"user_profile_url": None
if g.user.is_anonymous or appbuilder.app.config["MENU_HIDE_USER_INFO"]
else f"/superset/profile/{g.user.username}",
if user.is_anonymous or appbuilder.app.config["MENU_HIDE_USER_INFO"]
else f"/superset/profile/{user.username}",
"locale": session.get("locale", "en"),
},
}


def common_bootstrap_payload() -> Dict[str, Any]:
"""Common data always sent to the client"""
@cache_manager.cache.memoize(timeout=60)
def common_bootstrap_payload(user: User) -> Dict[str, Any]:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I checked the docs, and while they appear to recommend not using mutable objects as the key, it should work in this case as __repr__() will always return the same value for a single user.

Memoization docs: https://flask-caching.readthedocs.io/en/latest/#memoization

"""Common data always sent to the client

The function is memoized as the return value only changes based
on configuration and feature flag values.
"""
messages = get_flashed_messages(with_categories=True)
locale = str(get_locale())

Expand Down Expand Up @@ -388,7 +395,7 @@ def common_bootstrap_payload() -> Dict[str, Any]:
"extra_sequential_color_schemes": conf["EXTRA_SEQUENTIAL_COLOR_SCHEMES"],
"extra_categorical_color_schemes": conf["EXTRA_CATEGORICAL_COLOR_SCHEMES"],
"theme_overrides": conf["THEME_OVERRIDES"],
"menu_data": menu_data(),
"menu_data": menu_data(user),
}
bootstrap_data.update(conf["COMMON_BOOTSTRAP_OVERRIDES_FUNC"](bootstrap_data))
return bootstrap_data
Expand Down Expand Up @@ -499,7 +506,7 @@ def show_unexpected_exception(ex: Exception) -> FlaskResponse:
def get_common_bootstrap_data() -> Dict[str, Any]:
def serialize_bootstrap_data() -> str:
return json.dumps(
{"common": common_bootstrap_payload()},
{"common": common_bootstrap_payload(g.user)},
default=utils.pessimistic_json_iso_dttm_ser,
)

Expand All @@ -517,7 +524,7 @@ class SupersetModelView(ModelView):
def render_app_template(self) -> FlaskResponse:
payload = {
"user": bootstrap_user_data(g.user, include_perms=True),
"common": common_bootstrap_payload(),
"common": common_bootstrap_payload(g.user),
}
return self.render_template(
"superset/spa.html",
Expand Down
12 changes: 6 additions & 6 deletions superset/views/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -929,7 +929,7 @@ def explore(
"force": force,
"user": bootstrap_user_data(g.user, include_perms=True),
"forced_height": request.args.get("height"),
"common": common_bootstrap_payload(),
"common": common_bootstrap_payload(g.user),
}
if slc:
title = slc.slice_name
Expand Down Expand Up @@ -1564,7 +1564,7 @@ def recent_activity( # pylint: disable=too-many-locals
@has_access_api
@event_logger.log_this
@expose("/available_domains/", methods=["GET"])
def available_domains(self) -> FlaskResponse: # pylint: disable=no-self-use
def available_domains(self) -> FlaskResponse:
"""
Returns the list of available Superset Webserver domains (if any)
defined in config. This enables charts embedded in other apps to
Expand Down Expand Up @@ -1989,7 +1989,7 @@ def dashboard(

bootstrap_data = {
"user": bootstrap_user_data(g.user, include_perms=True),
"common": common_bootstrap_payload(),
"common": common_bootstrap_payload(g.user),
}

return self.render_template(
Expand Down Expand Up @@ -2730,7 +2730,7 @@ def welcome(self) -> FlaskResponse:

payload = {
"user": bootstrap_user_data(g.user, include_perms=True),
"common": common_bootstrap_payload(),
"common": common_bootstrap_payload(g.user),
}

return self.render_template(
Expand Down Expand Up @@ -2759,7 +2759,7 @@ def profile(self, username: str) -> FlaskResponse:

payload = {
"user": bootstrap_user_data(user, include_perms=True),
"common": common_bootstrap_payload(),
"common": common_bootstrap_payload(g.user),
}

return self.render_template(
Expand Down Expand Up @@ -2823,7 +2823,7 @@ def sqllab(self) -> FlaskResponse:
"""SQL Editor"""
payload = {
"defaultDbId": config["SQLLAB_DEFAULT_DBID"],
"common": common_bootstrap_payload(),
"common": common_bootstrap_payload(g.user),
**self._get_sqllab_tabs(get_user_id()),
}

Expand Down
2 changes: 1 addition & 1 deletion superset/views/dashboard/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ def embedded(
)

bootstrap_data = {
"common": common_bootstrap_payload(),
"common": common_bootstrap_payload(g.user),
"embedded": {"dashboard_id": dashboard_id_or_slug},
}

Expand Down
4 changes: 3 additions & 1 deletion tests/integration_tests/core_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
from superset.db_engine_specs.base import BaseEngineSpec
from superset.db_engine_specs.mssql import MssqlEngineSpec
from superset.exceptions import SupersetException
from superset.extensions import async_query_manager
from superset.extensions import async_query_manager, cache_manager
from superset.models import core as models
from superset.models.annotations import Annotation, AnnotationLayer
from superset.models.dashboard import Dashboard
Expand Down Expand Up @@ -1455,6 +1455,8 @@ def test_feature_flag_serialization(self):
"""
Functions in feature flags don't break bootstrap data serialization.
"""
# feature flags are cached
cache_manager.cache.clear()
self.login()

encoded = json.dumps(
Expand Down