Skip to content

Commit

Permalink
move tasks back and fix digest func
Browse files Browse the repository at this point in the history
  • Loading branch information
villebro committed Dec 9, 2022
1 parent 5d2ce4e commit fdc2698
Show file tree
Hide file tree
Showing 13 changed files with 94 additions and 124 deletions.
2 changes: 1 addition & 1 deletion docs/docs/installation/alerts-reports.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ REDIS_PORT = "6379"

class CeleryConfig:
broker_url = 'redis://%s:%s/0' % (REDIS_HOST, REDIS_PORT)
imports = ('superset.sql_lab', "superset.tasks", "superset.thumbnails.tasks", )
imports = ('superset.sql_lab', "superset.tasks", "superset.tasks.thumbnails", )
result_backend = 'redis://%s:%s/0' % (REDIS_HOST, REDIS_PORT)
worker_prefetch_multiplier = 10
task_acks_late = True
Expand Down
2 changes: 1 addition & 1 deletion docs/docs/installation/cache.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ from s3cache.s3cache import S3Cache

class CeleryConfig(object):
broker_url = "redis://localhost:6379/0"
imports = ("superset.sql_lab", "superset.tasks", "superset.thumbnails.tasks")
imports = ("superset.sql_lab", "superset.tasks", "superset.tasks.thumbnails")
result_backend = "redis://localhost:6379/0"
worker_prefetch_multiplier = 10
task_acks_late = True
Expand Down
2 changes: 1 addition & 1 deletion docs/docs/installation/running-on-kubernetes.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ configOverrides:
class CeleryConfig(object):
broker_url = f"redis://{env('REDIS_HOST')}:{env('REDIS_PORT')}/0"
imports = ('superset.sql_lab', "superset.tasks", "superset.thumbnails.tasks", )
imports = ('superset.sql_lab', "superset.tasks", "superset.tasks.thumbnails", )
result_backend = f"redis://{env('REDIS_HOST')}:{env('REDIS_PORT')}/0"
task_annotations = {
'sql_lab.get_sql_results': {
Expand Down
2 changes: 1 addition & 1 deletion superset/charts/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@
from superset.constants import MODEL_API_RW_METHOD_PERMISSION_MAP, RouteMethod
from superset.extensions import event_logger
from superset.models.slice import Slice
from superset.tasks.thumbnails import cache_chart_thumbnail
from superset.tasks.utils import get_current_user
from superset.thumbnails.tasks import cache_chart_thumbnail
from superset.utils.screenshots import ChartScreenshot
from superset.utils.urls import get_url_path
from superset.views.base_api import (
Expand Down
2 changes: 1 addition & 1 deletion superset/cli/thumbnails.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ def compute_thumbnails(
# pylint: disable=import-outside-toplevel
from superset.models.dashboard import Dashboard
from superset.models.slice import Slice
from superset.thumbnails.tasks import (
from superset.tasks.thumbnails import (
cache_chart_thumbnail,
cache_dashboard_thumbnail,
)
Expand Down
2 changes: 1 addition & 1 deletion superset/dashboards/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,8 @@
from superset.extensions import event_logger
from superset.models.dashboard import Dashboard
from superset.models.embedded_dashboard import EmbeddedDashboard
from superset.tasks.thumbnails import cache_dashboard_thumbnail
from superset.tasks.utils import get_current_user
from superset.thumbnails.tasks import cache_dashboard_thumbnail
from superset.utils.cache import etag_cache
from superset.utils.screenshots import DashboardScreenshot
from superset.utils.urls import get_url_path
Expand Down
2 changes: 1 addition & 1 deletion superset/models/dashboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@
from superset.models.helpers import AuditMixinNullable, ImportExportMixin
from superset.models.slice import Slice
from superset.models.user_attributes import UserAttribute
from superset.tasks.thumbnails import cache_dashboard_thumbnail
from superset.tasks.utils import get_current_user
from superset.thumbnails.digest import get_dashboard_digest
from superset.thumbnails.tasks import cache_dashboard_thumbnail
from superset.utils import core as utils
from superset.utils.core import get_user_id
from superset.utils.decorators import debounce
Expand Down
2 changes: 1 addition & 1 deletion superset/models/slice.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@
from superset import db, is_feature_enabled, security_manager
from superset.legacy import update_time_range
from superset.models.helpers import AuditMixinNullable, ImportExportMixin
from superset.tasks.thumbnails import cache_chart_thumbnail
from superset.tasks.utils import get_current_user
from superset.thumbnails.digest import get_chart_digest
from superset.thumbnails.tasks import cache_chart_thumbnail
from superset.utils import core as utils
from superset.utils.memoized import memoized
from superset.viz import BaseViz, viz_types
Expand Down
86 changes: 79 additions & 7 deletions superset/tasks/thumbnails.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,87 @@
# specific language governing permissions and limitations
# under the License.

"""Utility functions used across Superset"""

import logging
from typing import cast, Optional

from flask import current_app

# TODO(villebro): deprecate in 3.0
# pylint: disable=unused-wildcard-import,wildcard-import
from superset.thumbnails.tasks import *
from superset import security_manager, thumbnail_cache
from superset.extensions import celery_app
from superset.tasks.utils import get_executor
from superset.utils.core import override_user
from superset.utils.screenshots import ChartScreenshot, DashboardScreenshot
from superset.utils.urls import get_url_path
from superset.utils.webdriver import WindowSize

logger = logging.getLogger(__name__)

logger.warning(
"The import path superset.tasks.thumbnails is deprecated and will be removed in"
"3.0. Please use superset.thumbnails.tasks instead."
)

@celery_app.task(name="cache_chart_thumbnail", soft_time_limit=300)
def cache_chart_thumbnail(
current_user: Optional[str],
chart_id: int,
force: bool = False,
window_size: Optional[WindowSize] = None,
thumb_size: Optional[WindowSize] = None,
) -> None:
# pylint: disable=import-outside-toplevel
from superset.models.slice import Slice

if not thumbnail_cache:
logger.warning("No cache set, refusing to compute")
return None
chart = cast(Slice, Slice.get(chart_id))
url = get_url_path("Superset.slice", slice_id=chart.id, standalone="true")
logger.info("Caching chart: %s", url)
_, username = get_executor(
executor_types=current_app.config["THUMBNAIL_EXECUTE_AS"],
model=chart,
current_user=current_user,
)
user = security_manager.find_user(username)
with override_user(user):
screenshot = ChartScreenshot(url, chart.digest)
screenshot.compute_and_cache(
user=user,
cache=thumbnail_cache,
force=force,
window_size=window_size,
thumb_size=thumb_size,
)
return None


@celery_app.task(name="cache_dashboard_thumbnail", soft_time_limit=300)
def cache_dashboard_thumbnail(
current_user: Optional[str],
dashboard_id: int,
force: bool = False,
thumb_size: Optional[WindowSize] = None,
) -> None:
# pylint: disable=import-outside-toplevel
from superset.models.dashboard import Dashboard

if not thumbnail_cache:
logging.warning("No cache set, refusing to compute")
return
dashboard = Dashboard.get(dashboard_id)
url = get_url_path("Superset.dashboard", dashboard_id_or_slug=dashboard.id)

logger.info("Caching dashboard: %s", url)
_, username = get_executor(
executor_types=current_app.config["THUMBNAIL_EXECUTE_AS"],
model=dashboard,
current_user=current_user,
)
user = security_manager.find_user(username)
with override_user(user):
screenshot = DashboardScreenshot(url, dashboard.digest)
screenshot.compute_and_cache(
user=user,
cache=thumbnail_cache,
force=force,
thumb_size=thumb_size,
)
3 changes: 1 addition & 2 deletions superset/thumbnails/digest.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,7 @@ def get_dashboard_digest(dashboard: Dashboard) -> str:
return func(dashboard, executor_type, executor)

unique_string = (
f"{dashboard.position_json}.{dashboard.css}"
f".{dashboard.json_metadata}.{executor}"
f"{dashboard.position_json}.{dashboard.css}.{dashboard.json_metadata}"
)

unique_string = _adjust_string_for_executor(unique_string, executor_type, executor)
Expand Down
101 changes: 0 additions & 101 deletions superset/thumbnails/tasks.py

This file was deleted.

2 changes: 1 addition & 1 deletion tests/integration_tests/superset_test_config_thumbnails.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def GET_FEATURE_FLAGS_FUNC(ff):

class CeleryConfig(object):
BROKER_URL = f"redis://{REDIS_HOST}:{REDIS_PORT}/{REDIS_CELERY_DB}"
CELERY_IMPORTS = ("superset.sql_lab", "superset.thumbnails.tasks")
CELERY_IMPORTS = ("superset.sql_lab", "superset.tasks.thumbnails")
CELERY_ANNOTATIONS = {"sql_lab.add": {"rate_limit": "10/s"}}
CONCURRENCY = 1

Expand Down
10 changes: 5 additions & 5 deletions tests/unit_tests/thumbnails/test_digest.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,14 @@ def CUSTOM_CHART_FUNC(
[ExecutorType.SELENIUM],
False,
False,
"9dfd9e0685911ca56f041e57b63bd950",
"0f94edd2d7dfd71724ce8236c05a6bf5",
),
(
None,
[ExecutorType.CURRENT_USER],
True,
False,
"55fa9f78f4d8c96464fd5b369a8f2367",
"6b05b0eff27796f266e538a26a155cf7",
),
(
{
Expand All @@ -84,7 +84,7 @@ def CUSTOM_CHART_FUNC(
[ExecutorType.CURRENT_USER],
True,
False,
"9725aa2717974238f03c3fc29bef243b",
"c9c06fd8058f8b1d3bb84e2699aaae0f",
),
(
{
Expand All @@ -93,7 +93,7 @@ def CUSTOM_CHART_FUNC(
[ExecutorType.CURRENT_USER],
True,
False,
"234e168024483a520b705ecf71cf4fca",
"be15e84a631755e537bd2706beeae70a",
),
(
{
Expand All @@ -102,7 +102,7 @@ def CUSTOM_CHART_FUNC(
[ExecutorType.CURRENT_USER],
True,
False,
"430dc5a4ab07928f4465c43a32b4c846",
"9996e1f6cf8ebbf2aebe2938a7d59a0e",
),
(
None,
Expand Down

0 comments on commit fdc2698

Please sign in to comment.