dpgaspar commented on code in PR #23853:
URL: https://github.com/apache/superset/pull/23853#discussion_r1183682450
##########
superset/cachekeys/api.py:
##########
@@ -40,10 +42,98 @@ class CacheRestApi(BaseSupersetModelRestApi):
class_permission_name = "CacheRestApi"
include_route_methods = {
"invalidate",
+ "warm_up_cache",
}
openapi_spec_component_schemas = (CacheInvalidationRequestSchema,)
+ @expose("/warm_up_cache", methods=["GET"])
+ @protect()
+ @safe
+ @statsd_metrics
+ @event_logger.log_this_with_context(
+ action=lambda self, *args, **kwargs: f"{self.__class__.__name__}"
+ f".warm_up_cache",
+ log_to_statsd=False,
+ )
+ def warm_up_cache(self) -> Response:
+ """Warms up the cache for the slice or table.
+
+ Note for slices a force refresh occurs.
+
+ In terms of the `extra_filters` these can be obtained from records in
the JSON
+ encoded `logs.json` column associated with the `explore_json` action.
+
+ ---
+ get:
+ description: >-
+ Warms up the cache for the slice or table
+ parameters:
+ - in: query
+ name: slice_id
+ schema:
+ type: integer
+ description: The ID of the chart to warm up cache for
+ - in: query
+ name: dashboard_id
+ schema:
+ type: integer
+ description: The ID of the dashboard to get filters for when
warming cache
+ - in: query
+ name: table_name
+ schema:
+ type: string
+ description: The name of the table to warm up cache for
+ - in: query
+ name: db_name
+ schema:
+ type: string
+ description: The name of the database where the table is located
+ - in: query
+ name: extra_filters
+ schema:
+ type: string
+ description: Extra filters to apply when warming up cache
+ responses:
+ 200:
+ description: Each chart's warmup status
+ content:
+ application/json:
+ schema:
+ type: object
+ properties:
+ result:
+ type: array
+ items:
+ type: object
+ properties:
+ slice_id:
Review Comment:
nit: would be nice to have a marshmallow schema set for the response object
and then reference it here. Also rename the field name to `chart_id`
##########
superset/cachekeys/api.py:
##########
@@ -40,10 +42,98 @@ class CacheRestApi(BaseSupersetModelRestApi):
class_permission_name = "CacheRestApi"
include_route_methods = {
"invalidate",
+ "warm_up_cache",
}
openapi_spec_component_schemas = (CacheInvalidationRequestSchema,)
+ @expose("/warm_up_cache", methods=["GET"])
+ @protect()
+ @safe
+ @statsd_metrics
+ @event_logger.log_this_with_context(
+ action=lambda self, *args, **kwargs: f"{self.__class__.__name__}"
+ f".warm_up_cache",
+ log_to_statsd=False,
+ )
+ def warm_up_cache(self) -> Response:
+ """Warms up the cache for the slice or table.
+
+ Note for slices a force refresh occurs.
+
+ In terms of the `extra_filters` these can be obtained from records in
the JSON
+ encoded `logs.json` column associated with the `explore_json` action.
+
+ ---
+ get:
+ description: >-
Review Comment:
nit: let's start using summary instead, place the method docs into the OAS
description
##########
superset/cachekeys/api.py:
##########
@@ -40,10 +42,98 @@ class CacheRestApi(BaseSupersetModelRestApi):
class_permission_name = "CacheRestApi"
include_route_methods = {
"invalidate",
+ "warm_up_cache",
}
openapi_spec_component_schemas = (CacheInvalidationRequestSchema,)
+ @expose("/warm_up_cache", methods=["GET"])
Review Comment:
this should be a `PUT` or `PATCH` since the state changes. Use the query
parameters as the payload
##########
superset/cachekeys/api.py:
##########
@@ -40,10 +42,98 @@ class CacheRestApi(BaseSupersetModelRestApi):
class_permission_name = "CacheRestApi"
include_route_methods = {
"invalidate",
+ "warm_up_cache",
}
openapi_spec_component_schemas = (CacheInvalidationRequestSchema,)
+ @expose("/warm_up_cache", methods=["GET"])
+ @protect()
+ @safe
+ @statsd_metrics
+ @event_logger.log_this_with_context(
+ action=lambda self, *args, **kwargs: f"{self.__class__.__name__}"
+ f".warm_up_cache",
+ log_to_statsd=False,
+ )
+ def warm_up_cache(self) -> Response:
+ """Warms up the cache for the slice or table.
+
+ Note for slices a force refresh occurs.
+
+ In terms of the `extra_filters` these can be obtained from records in
the JSON
+ encoded `logs.json` column associated with the `explore_json` action.
+
+ ---
+ get:
+ description: >-
+ Warms up the cache for the slice or table
+ parameters:
+ - in: query
+ name: slice_id
Review Comment:
let's rename to `chart_id`
##########
superset/cachekeys/api.py:
##########
@@ -40,10 +42,98 @@ class CacheRestApi(BaseSupersetModelRestApi):
class_permission_name = "CacheRestApi"
include_route_methods = {
"invalidate",
+ "warm_up_cache",
}
openapi_spec_component_schemas = (CacheInvalidationRequestSchema,)
+ @expose("/warm_up_cache", methods=["GET"])
+ @protect()
+ @safe
+ @statsd_metrics
+ @event_logger.log_this_with_context(
+ action=lambda self, *args, **kwargs: f"{self.__class__.__name__}"
+ f".warm_up_cache",
+ log_to_statsd=False,
+ )
+ def warm_up_cache(self) -> Response:
+ """Warms up the cache for the slice or table.
+
+ Note for slices a force refresh occurs.
+
+ In terms of the `extra_filters` these can be obtained from records in
the JSON
+ encoded `logs.json` column associated with the `explore_json` action.
+
+ ---
+ get:
+ description: >-
+ Warms up the cache for the slice or table
+ parameters:
+ - in: query
+ name: slice_id
+ schema:
+ type: integer
+ description: The ID of the chart to warm up cache for
+ - in: query
+ name: dashboard_id
+ schema:
+ type: integer
+ description: The ID of the dashboard to get filters for when
warming cache
+ - in: query
+ name: table_name
+ schema:
+ type: string
+ description: The name of the table to warm up cache for
+ - in: query
+ name: db_name
+ schema:
+ type: string
+ description: The name of the database where the table is located
+ - in: query
+ name: extra_filters
+ schema:
+ type: string
+ description: Extra filters to apply when warming up cache
+ responses:
+ 200:
+ description: Each chart's warmup status
+ content:
+ application/json:
+ schema:
+ type: object
+ properties:
+ result:
+ type: array
+ items:
+ type: object
+ properties:
+ slice_id:
+ type: integer
+ viz_error:
+ type: string
+ viz_status:
+ type: string
+ 400:
+ $ref: '#/components/responses/400'
+ 404:
+ $ref: '#/components/responses/404'
+ 500:
+ $ref: '#/components/responses/500'
+ """
+ slice_id = request.args.get("slice_id")
+ dashboard_id = request.args.get("dashboard_id")
+ table_name = request.args.get("table_name")
+ db_name = request.args.get("db_name")
+ extra_filters = request.args.get("extra_filters")
Review Comment:
nit: we could use query parameters here or Rison parameters, rison is a less
adopted standard but it would get us type validation for free. how does the API
behaves if `slice_id` is a string?
##########
superset/cachekeys/commands/exceptions.py:
##########
@@ -0,0 +1,44 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements. See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership. The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied. See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+from flask_babel import lazy_gettext as _
+
+from superset.commands.exceptions import CommandException
+
+
+class WarmUpCacheParametersExpectedError(CommandException):
+ status = 400
+ message = _(
+ "Malformed request. slice_id or table_name "
+ "and db_name arguments are expected"
+ )
+
+
+class WarmUpCacheChartNotFoundError(CommandException):
+ def __init__(self, chart_id: int):
+ message = f"Chart {chart_id} not found"
+ super().__init__(message)
+
+ status = 404
+
+
+class WarmUpCacheTableNotFoundError(CommandException):
+ def __init__(self, table_name: str, db_name: str):
+ message = f"Table {table_name} wasn't found in the database {db_name}"
Review Comment:
let's make sure we don't log or alert user input
##########
tests/integration_tests/cachekeys/api_tests.py:
##########
@@ -165,3 +176,40 @@ def test_invalidate_existing_caches(invalidate):
.datasource_uid
== "X__table"
)
+
+
+class TestWarmUpCache(SupersetTestCase):
+ @pytest.mark.usefixtures(
+ "load_energy_table_with_slice",
"load_birth_names_dashboard_with_slices"
+ )
+ def test_warm_up_cache(self):
Review Comment:
can you add test cases for chart_id, dashboard_id, table_name not found and
other possible failures at the API level.
Would be great to add unittests at the command level
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]