mirror of
https://github.com/apache/superset.git
synced 2026-04-19 08:04:53 +00:00
fix(celery cache warmup): add auth and use warm_up_cache endpoint (#21076)
This commit is contained in:
@@ -38,9 +38,9 @@ from superset.models.core import Log
|
||||
from superset.models.tags import get_tag, ObjectTypes, TaggedObject, TagTypes
|
||||
from superset.tasks.cache import (
|
||||
DashboardTagsStrategy,
|
||||
get_form_data,
|
||||
TopNDashboardsStrategy,
|
||||
)
|
||||
from superset.utils.urls import get_url_host
|
||||
|
||||
from .base_tests import SupersetTestCase
|
||||
from .dashboard_utils import create_dashboard, create_slice, create_table_metadata
|
||||
@@ -49,7 +49,6 @@ from .fixtures.unicode_dashboard import (
|
||||
load_unicode_data,
|
||||
)
|
||||
|
||||
URL_PREFIX = "http://0.0.0.0:8081"
|
||||
|
||||
mock_positions = {
|
||||
"DASHBOARD_VERSION_KEY": "v2",
|
||||
@@ -69,128 +68,6 @@ mock_positions = {
|
||||
|
||||
|
||||
class TestCacheWarmUp(SupersetTestCase):
|
||||
def test_get_form_data_chart_only(self):
|
||||
chart_id = 1
|
||||
result = get_form_data(chart_id, None)
|
||||
expected = {"slice_id": chart_id}
|
||||
self.assertEqual(result, expected)
|
||||
|
||||
def test_get_form_data_no_dashboard_metadata(self):
|
||||
chart_id = 1
|
||||
dashboard = MagicMock()
|
||||
dashboard.json_metadata = None
|
||||
dashboard.position_json = json.dumps(mock_positions)
|
||||
result = get_form_data(chart_id, dashboard)
|
||||
expected = {"slice_id": chart_id}
|
||||
self.assertEqual(result, expected)
|
||||
|
||||
def test_get_form_data_immune_slice(self):
|
||||
chart_id = 1
|
||||
filter_box_id = 2
|
||||
dashboard = MagicMock()
|
||||
dashboard.position_json = json.dumps(mock_positions)
|
||||
dashboard.json_metadata = json.dumps(
|
||||
{
|
||||
"filter_scopes": {
|
||||
str(filter_box_id): {
|
||||
"name": {"scope": ["ROOT_ID"], "immune": [chart_id]}
|
||||
}
|
||||
},
|
||||
"default_filters": json.dumps(
|
||||
{str(filter_box_id): {"name": ["Alice", "Bob"]}}
|
||||
),
|
||||
}
|
||||
)
|
||||
result = get_form_data(chart_id, dashboard)
|
||||
expected = {"slice_id": chart_id}
|
||||
self.assertEqual(result, expected)
|
||||
|
||||
def test_get_form_data_no_default_filters(self):
|
||||
chart_id = 1
|
||||
dashboard = MagicMock()
|
||||
dashboard.json_metadata = json.dumps({})
|
||||
dashboard.position_json = json.dumps(mock_positions)
|
||||
result = get_form_data(chart_id, dashboard)
|
||||
expected = {"slice_id": chart_id}
|
||||
self.assertEqual(result, expected)
|
||||
|
||||
def test_get_form_data_immune_fields(self):
|
||||
chart_id = 1
|
||||
filter_box_id = 2
|
||||
dashboard = MagicMock()
|
||||
dashboard.position_json = json.dumps(mock_positions)
|
||||
dashboard.json_metadata = json.dumps(
|
||||
{
|
||||
"default_filters": json.dumps(
|
||||
{
|
||||
str(filter_box_id): {
|
||||
"name": ["Alice", "Bob"],
|
||||
"__time_range": "100 years ago : today",
|
||||
}
|
||||
}
|
||||
),
|
||||
"filter_scopes": {
|
||||
str(filter_box_id): {
|
||||
"__time_range": {"scope": ["ROOT_ID"], "immune": [chart_id]}
|
||||
}
|
||||
},
|
||||
}
|
||||
)
|
||||
result = get_form_data(chart_id, dashboard)
|
||||
expected = {
|
||||
"slice_id": chart_id,
|
||||
"extra_filters": [{"col": "name", "op": "in", "val": ["Alice", "Bob"]}],
|
||||
}
|
||||
self.assertEqual(result, expected)
|
||||
|
||||
def test_get_form_data_no_extra_filters(self):
|
||||
chart_id = 1
|
||||
filter_box_id = 2
|
||||
dashboard = MagicMock()
|
||||
dashboard.position_json = json.dumps(mock_positions)
|
||||
dashboard.json_metadata = json.dumps(
|
||||
{
|
||||
"default_filters": json.dumps(
|
||||
{str(filter_box_id): {"__time_range": "100 years ago : today"}}
|
||||
),
|
||||
"filter_scopes": {
|
||||
str(filter_box_id): {
|
||||
"__time_range": {"scope": ["ROOT_ID"], "immune": [chart_id]}
|
||||
}
|
||||
},
|
||||
}
|
||||
)
|
||||
result = get_form_data(chart_id, dashboard)
|
||||
expected = {"slice_id": chart_id}
|
||||
self.assertEqual(result, expected)
|
||||
|
||||
def test_get_form_data(self):
|
||||
chart_id = 1
|
||||
filter_box_id = 2
|
||||
dashboard = MagicMock()
|
||||
dashboard.position_json = json.dumps(mock_positions)
|
||||
dashboard.json_metadata = json.dumps(
|
||||
{
|
||||
"default_filters": json.dumps(
|
||||
{
|
||||
str(filter_box_id): {
|
||||
"name": ["Alice", "Bob"],
|
||||
"__time_range": "100 years ago : today",
|
||||
}
|
||||
}
|
||||
)
|
||||
}
|
||||
)
|
||||
result = get_form_data(chart_id, dashboard)
|
||||
expected = {
|
||||
"slice_id": chart_id,
|
||||
"extra_filters": [
|
||||
{"col": "name", "op": "in", "val": ["Alice", "Bob"]},
|
||||
{"col": "__time_range", "op": "==", "val": "100 years ago : today"},
|
||||
],
|
||||
}
|
||||
self.assertEqual(result, expected)
|
||||
|
||||
@pytest.mark.usefixtures("load_birth_names_dashboard_with_slices")
|
||||
def test_top_n_dashboards_strategy(self):
|
||||
# create a top visited dashboard
|
||||
@@ -202,7 +79,12 @@ class TestCacheWarmUp(SupersetTestCase):
|
||||
|
||||
strategy = TopNDashboardsStrategy(1)
|
||||
result = sorted(strategy.get_urls())
|
||||
expected = sorted([f"{URL_PREFIX}{slc.url}" for slc in dash.slices])
|
||||
expected = sorted(
|
||||
[
|
||||
f"{get_url_host()}superset/warm_up_cache/?slice_id={slc.id}&dashboard_id={dash.id}"
|
||||
for slc in dash.slices
|
||||
]
|
||||
)
|
||||
self.assertEqual(result, expected)
|
||||
|
||||
def reset_tag(self, tag):
|
||||
@@ -228,7 +110,12 @@ class TestCacheWarmUp(SupersetTestCase):
|
||||
# tag dashboard 'births' with `tag1`
|
||||
tag1 = get_tag("tag1", db.session, TagTypes.custom)
|
||||
dash = self.get_dash_by_slug("births")
|
||||
tag1_urls = sorted([f"{URL_PREFIX}{slc.url}" for slc in dash.slices])
|
||||
tag1_urls = sorted(
|
||||
[
|
||||
f"{get_url_host()}superset/warm_up_cache/?slice_id={slc.id}"
|
||||
for slc in dash.slices
|
||||
]
|
||||
)
|
||||
tagged_object = TaggedObject(
|
||||
tag_id=tag1.id, object_id=dash.id, object_type=ObjectTypes.dashboard
|
||||
)
|
||||
@@ -248,7 +135,7 @@ class TestCacheWarmUp(SupersetTestCase):
|
||||
# tag first slice
|
||||
dash = self.get_dash_by_slug("unicode-test")
|
||||
slc = dash.slices[0]
|
||||
tag2_urls = [f"{URL_PREFIX}{slc.url}"]
|
||||
tag2_urls = [f"{get_url_host()}superset/warm_up_cache/?slice_id={slc.id}"]
|
||||
object_id = slc.id
|
||||
tagged_object = TaggedObject(
|
||||
tag_id=tag2.id, object_id=object_id, object_type=ObjectTypes.chart
|
||||
|
||||
Reference in New Issue
Block a user