diff --git a/UPDATING.md b/UPDATING.md index 56dd8aa89ef2..cf52611a0ada 100644 --- a/UPDATING.md +++ b/UPDATING.md @@ -38,13 +38,11 @@ assists people when migrating to a new version. - [17539](https://github.com/apache/superset/pull/17539): all Superset CLI commands (init, load_examples and etc) require setting the FLASK_APP environment variable (which is set by default when .flaskenv is loaded) - ### Deprecations ### Other - [16809](https://github.com/apache/incubator-superset/pull/16809): When building the superset frontend assets manually, you should now use Node 16 (previously Node 14 was required/recommended). Node 14 will most likely still work for at least some time, but is no longer actively tested for on CI. -- [17536](https://github.com/apache/superset/pull/17536): introduced a key-value endpoint to store dashboard filter state. This endpoint is backed by Flask-Caching and the default configuration assumes that the values will be stored in the file system. If you are already using another cache backend like Redis or Memchached, you'll probably want to change this setting in `superset_config.py`. The key is `FILTER_STATE_CACHE_CONFIG` and the available settings can be found in Flask-Caching [docs](https://flask-caching.readthedocs.io/en/latest/). ## 1.3.0 diff --git a/superset/config.py b/superset/config.py index aabda42d36ba..2d7e7dbb4696 100644 --- a/superset/config.py +++ b/superset/config.py @@ -561,15 +561,6 @@ def _try_json_readsha(filepath: str, length: int) -> Optional[str]: # Cache for datasource metadata and query results DATA_CACHE_CONFIG: CacheConfig = {"CACHE_TYPE": "null"} -# Cache for filters state -FILTER_STATE_CACHE_CONFIG: CacheConfig = { - "CACHE_TYPE": "filesystem", - "CACHE_DIR": os.path.join(DATA_DIR, "cache"), - "CACHE_DEFAULT_TIMEOUT": int(timedelta(days=90).total_seconds()), - "CACHE_THRESHOLD": 0, - "REFRESH_TIMEOUT_ON_RETRIEVAL": True, -} - # store cache keys by datasource UID (via CacheKey) for custom processing/invalidation STORE_CACHE_KEYS_IN_METADATA_DB = False diff --git a/superset/dashboards/filter_state/__init__.py b/superset/dashboards/filter_state/__init__.py deleted file mode 100644 index 13a83393a912..000000000000 --- a/superset/dashboards/filter_state/__init__.py +++ /dev/null @@ -1,16 +0,0 @@ -# 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. diff --git a/superset/dashboards/filter_state/api.py b/superset/dashboards/filter_state/api.py deleted file mode 100644 index afc7616d9cf0..000000000000 --- a/superset/dashboards/filter_state/api.py +++ /dev/null @@ -1,241 +0,0 @@ -# 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. -import logging -from typing import Type - -from flask import Response -from flask_appbuilder.api import expose, protect, safe - -from superset.dashboards.filter_state.commands.create import CreateFilterStateCommand -from superset.dashboards.filter_state.commands.delete import DeleteFilterStateCommand -from superset.dashboards.filter_state.commands.get import GetFilterStateCommand -from superset.dashboards.filter_state.commands.update import UpdateFilterStateCommand -from superset.extensions import event_logger -from superset.key_value.api import KeyValueRestApi - -logger = logging.getLogger(__name__) - - -class DashboardFilterStateRestApi(KeyValueRestApi): - class_permission_name = "FilterStateRestApi" - resource_name = "dashboard" - openapi_spec_tag = "Filter State" - - def get_create_command(self) -> Type[CreateFilterStateCommand]: - return CreateFilterStateCommand - - def get_update_command(self) -> Type[UpdateFilterStateCommand]: - return UpdateFilterStateCommand - - def get_get_command(self) -> Type[GetFilterStateCommand]: - return GetFilterStateCommand - - def get_delete_command(self) -> Type[DeleteFilterStateCommand]: - return DeleteFilterStateCommand - - @expose("//filter_state", methods=["POST"]) - @protect() - @safe - @event_logger.log_this_with_context( - action=lambda self, *args, **kwargs: f"{self.__class__.__name__}.post", - log_to_statsd=False, - ) - def post(self, pk: int) -> Response: - """Stores a new value. - --- - post: - description: >- - Stores a new value. - parameters: - - in: path - schema: - type: integer - name: pk - requestBody: - required: true - content: - application/json: - schema: - type: object - $ref: '#/components/schemas/KeyValuePostSchema' - responses: - 201: - description: The value was stored successfully. - content: - application/json: - schema: - type: object - properties: - key: - type: string - description: The key to retrieve the value. - 400: - $ref: '#/components/responses/400' - 401: - $ref: '#/components/responses/401' - 422: - $ref: '#/components/responses/422' - 500: - $ref: '#/components/responses/500' - """ - return super().post(pk) - - @expose("//filter_state//", methods=["PUT"]) - @protect() - @safe - @event_logger.log_this_with_context( - action=lambda self, *args, **kwargs: f"{self.__class__.__name__}.put", - log_to_statsd=False, - ) - def put(self, pk: int, key: str) -> Response: - """Updates an existing value. - --- - put: - description: >- - Updates an existing value. - parameters: - - in: path - schema: - type: integer - name: pk - - in: path - schema: - type: string - name: key - requestBody: - required: true - content: - application/json: - schema: - type: object - $ref: '#/components/schemas/KeyValuePutSchema' - responses: - 200: - description: The value was stored successfully. - content: - application/json: - schema: - type: object - properties: - message: - type: string - description: The result of the operation - 400: - $ref: '#/components/responses/400' - 401: - $ref: '#/components/responses/401' - 404: - $ref: '#/components/responses/404' - 422: - $ref: '#/components/responses/422' - 500: - $ref: '#/components/responses/500' - """ - return super().put(pk, key) - - @expose("//filter_state//", methods=["GET"]) - @protect() - @safe - @event_logger.log_this_with_context( - action=lambda self, *args, **kwargs: f"{self.__class__.__name__}.get", - log_to_statsd=False, - ) - def get(self, pk: int, key: str) -> Response: - """Retrives a value. - --- - get: - description: >- - Retrives a value. - parameters: - - in: path - schema: - type: integer - name: pk - - in: path - schema: - type: string - name: key - responses: - 200: - description: Returns the stored value. - content: - application/json: - schema: - type: object - properties: - value: - type: string - description: The stored value - 400: - $ref: '#/components/responses/400' - 401: - $ref: '#/components/responses/401' - 404: - $ref: '#/components/responses/404' - 422: - $ref: '#/components/responses/422' - 500: - $ref: '#/components/responses/500' - """ - return super().get(pk, key) - - @expose("//filter_state//", methods=["DELETE"]) - @protect() - @safe - @event_logger.log_this_with_context( - action=lambda self, *args, **kwargs: f"{self.__class__.__name__}.delete", - log_to_statsd=False, - ) - def delete(self, pk: int, key: str) -> Response: - """Deletes a value. - --- - delete: - description: >- - Deletes a value. - parameters: - - in: path - schema: - type: integer - name: pk - - in: path - schema: - type: string - name: key - description: The value key. - responses: - 200: - description: Deleted the stored value. - content: - application/json: - schema: - type: object - properties: - message: - type: string - description: The result of the operation - 400: - $ref: '#/components/responses/400' - 401: - $ref: '#/components/responses/401' - 404: - $ref: '#/components/responses/404' - 422: - $ref: '#/components/responses/422' - 500: - $ref: '#/components/responses/500' - """ - return super().delete(pk, key) diff --git a/superset/dashboards/filter_state/commands/__init__.py b/superset/dashboards/filter_state/commands/__init__.py deleted file mode 100644 index 13a83393a912..000000000000 --- a/superset/dashboards/filter_state/commands/__init__.py +++ /dev/null @@ -1,16 +0,0 @@ -# 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. diff --git a/superset/dashboards/filter_state/commands/create.py b/superset/dashboards/filter_state/commands/create.py deleted file mode 100644 index 4b8a7890049a..000000000000 --- a/superset/dashboards/filter_state/commands/create.py +++ /dev/null @@ -1,32 +0,0 @@ -# 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 typing import Optional - -from superset.dashboards.dao import DashboardDAO -from superset.extensions import cache_manager -from superset.key_value.commands.create import CreateKeyValueCommand -from superset.key_value.utils import cache_key - - -class CreateFilterStateCommand(CreateKeyValueCommand): - def create(self, resource_id: int, key: str, value: str) -> Optional[bool]: - dashboard = DashboardDAO.get_by_id_or_slug(str(resource_id)) - if dashboard: - return cache_manager.filter_state_cache.set( - cache_key(resource_id, key), value - ) - return False diff --git a/superset/dashboards/filter_state/commands/delete.py b/superset/dashboards/filter_state/commands/delete.py deleted file mode 100644 index 89ee287945e4..000000000000 --- a/superset/dashboards/filter_state/commands/delete.py +++ /dev/null @@ -1,30 +0,0 @@ -# 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 typing import Optional - -from superset.dashboards.dao import DashboardDAO -from superset.extensions import cache_manager -from superset.key_value.commands.delete import DeleteKeyValueCommand -from superset.key_value.utils import cache_key - - -class DeleteFilterStateCommand(DeleteKeyValueCommand): - def delete(self, resource_id: int, key: str) -> Optional[bool]: - dashboard = DashboardDAO.get_by_id_or_slug(str(resource_id)) - if dashboard: - return cache_manager.filter_state_cache.delete(cache_key(resource_id, key)) - return False diff --git a/superset/dashboards/filter_state/commands/get.py b/superset/dashboards/filter_state/commands/get.py deleted file mode 100644 index ea9dc9d9be70..000000000000 --- a/superset/dashboards/filter_state/commands/get.py +++ /dev/null @@ -1,33 +0,0 @@ -# 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 typing import Optional - -from superset.dashboards.dao import DashboardDAO -from superset.extensions import cache_manager -from superset.key_value.commands.get import GetKeyValueCommand -from superset.key_value.utils import cache_key - - -class GetFilterStateCommand(GetKeyValueCommand): - def get(self, resource_id: int, key: str, refreshTimeout: bool) -> Optional[str]: - dashboard = DashboardDAO.get_by_id_or_slug(str(resource_id)) - if dashboard: - value = cache_manager.filter_state_cache.get(cache_key(resource_id, key)) - if refreshTimeout: - cache_manager.filter_state_cache.set(key, value) - return value - return None diff --git a/superset/dashboards/filter_state/commands/update.py b/superset/dashboards/filter_state/commands/update.py deleted file mode 100644 index a432df612dc9..000000000000 --- a/superset/dashboards/filter_state/commands/update.py +++ /dev/null @@ -1,32 +0,0 @@ -# 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 typing import Optional - -from superset.dashboards.dao import DashboardDAO -from superset.extensions import cache_manager -from superset.key_value.commands.update import UpdateKeyValueCommand -from superset.key_value.utils import cache_key - - -class UpdateFilterStateCommand(UpdateKeyValueCommand): - def update(self, resource_id: int, key: str, value: str) -> Optional[bool]: - dashboard = DashboardDAO.get_by_id_or_slug(str(resource_id)) - if dashboard: - return cache_manager.filter_state_cache.set( - cache_key(resource_id, key), value - ) - return False diff --git a/superset/initialization/__init__.py b/superset/initialization/__init__.py index e33c038b11a7..56e8b1bec087 100644 --- a/superset/initialization/__init__.py +++ b/superset/initialization/__init__.py @@ -134,7 +134,6 @@ def init_views(self) -> None: from superset.css_templates.api import CssTemplateRestApi from superset.dashboards.api import DashboardRestApi from superset.dashboards.filter_sets.api import FilterSetRestApi - from superset.dashboards.filter_state.api import DashboardFilterStateRestApi from superset.databases.api import DatabaseRestApi from superset.datasets.api import DatasetRestApi from superset.datasets.columns.api import DatasetColumnsRestApi @@ -213,7 +212,6 @@ def init_views(self) -> None: appbuilder.add_api(ReportScheduleRestApi) appbuilder.add_api(ReportExecutionLogRestApi) appbuilder.add_api(FilterSetRestApi) - appbuilder.add_api(DashboardFilterStateRestApi) # # Setup regular views # diff --git a/superset/key_value/api.py b/superset/key_value/api.py deleted file mode 100644 index d67852a29bbe..000000000000 --- a/superset/key_value/api.py +++ /dev/null @@ -1,124 +0,0 @@ -# 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. -import logging -from abc import ABC, abstractmethod -from typing import Any - -from apispec import APISpec -from flask import g, request, Response -from flask_appbuilder.api import BaseApi -from marshmallow import ValidationError - -from superset.constants import MODEL_API_RW_METHOD_PERMISSION_MAP, RouteMethod -from superset.dashboards.commands.exceptions import ( - DashboardAccessDeniedError, - DashboardNotFoundError, -) -from superset.exceptions import InvalidPayloadFormatError -from superset.key_value.schemas import KeyValuePostSchema, KeyValuePutSchema - -logger = logging.getLogger(__name__) - - -class KeyValueRestApi(BaseApi, ABC): - add_model_schema = KeyValuePostSchema() - edit_model_schema = KeyValuePutSchema() - method_permission_name = MODEL_API_RW_METHOD_PERMISSION_MAP - include_route_methods = { - RouteMethod.POST, - RouteMethod.PUT, - RouteMethod.GET, - RouteMethod.DELETE, - } - allow_browser_login = True - - def add_apispec_components(self, api_spec: APISpec) -> None: - api_spec.components.schema( - KeyValuePostSchema.__name__, schema=KeyValuePostSchema, - ) - api_spec.components.schema( - KeyValuePutSchema.__name__, schema=KeyValuePutSchema, - ) - super().add_apispec_components(api_spec) - - def post(self, pk: int) -> Response: - if not request.is_json: - raise InvalidPayloadFormatError("Request is not JSON") - try: - item = self.add_model_schema.load(request.json) - key = self.get_create_command()(g.user, pk, item["value"]).run() - return self.response(201, key=key) - except ValidationError as error: - return self.response_400(message=error.messages) - except DashboardAccessDeniedError: - return self.response_403() - except DashboardNotFoundError: - return self.response_404() - - def put(self, pk: int, key: str) -> Response: - if not request.is_json: - raise InvalidPayloadFormatError("Request is not JSON") - try: - item = self.edit_model_schema.load(request.json) - result = self.get_update_command()(g.user, pk, key, item["value"]).run() - if not result: - return self.response_404() - return self.response(200, message="Value updated successfully.") - except ValidationError as error: - return self.response_400(message=error.messages) - except DashboardAccessDeniedError: - return self.response_403() - except DashboardNotFoundError: - return self.response_404() - - def get(self, pk: int, key: str) -> Response: - try: - value = self.get_get_command()(g.user, pk, key).run() - if not value: - return self.response_404() - return self.response(200, value=value) - except DashboardAccessDeniedError: - return self.response_403() - except DashboardNotFoundError: - return self.response_404() - - def delete(self, pk: int, key: str) -> Response: - try: - result = self.get_delete_command()(g.user, pk, key).run() - if not result: - return self.response_404() - return self.response(200, message="Deleted successfully") - except DashboardAccessDeniedError: - return self.response_403() - except DashboardNotFoundError: - return self.response_404() - - @abstractmethod - def get_create_command(self) -> Any: - ... - - @abstractmethod - def get_update_command(self) -> Any: - ... - - @abstractmethod - def get_get_command(self) -> Any: - ... - - @abstractmethod - def get_delete_command(self) -> Any: - ... diff --git a/superset/key_value/commands/__init__.py b/superset/key_value/commands/__init__.py deleted file mode 100644 index 13a83393a912..000000000000 --- a/superset/key_value/commands/__init__.py +++ /dev/null @@ -1,16 +0,0 @@ -# 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. diff --git a/superset/key_value/commands/create.py b/superset/key_value/commands/create.py deleted file mode 100644 index 361a3ad22280..000000000000 --- a/superset/key_value/commands/create.py +++ /dev/null @@ -1,54 +0,0 @@ -# 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. -import logging -from abc import ABC, abstractmethod -from secrets import token_urlsafe -from typing import Optional - -from flask_appbuilder.models.sqla import Model -from flask_appbuilder.security.sqla.models import User -from sqlalchemy.exc import SQLAlchemyError - -from superset.commands.base import BaseCommand -from superset.key_value.commands.exceptions import KeyValueCreateFailedError - -logger = logging.getLogger(__name__) - - -class CreateKeyValueCommand(BaseCommand, ABC): - def __init__( - self, user: User, resource_id: int, value: str, - ): - self._actor = user - self._resource_id = resource_id - self._value = value - - def run(self) -> Model: - try: - key = token_urlsafe(48) - self.create(self._resource_id, key, self._value) - return key - except SQLAlchemyError as ex: - logger.exception("Error running create command") - raise KeyValueCreateFailedError() from ex - - def validate(self) -> None: - pass - - @abstractmethod - def create(self, resource_id: int, key: str, value: str) -> Optional[bool]: - ... diff --git a/superset/key_value/commands/delete.py b/superset/key_value/commands/delete.py deleted file mode 100644 index 9990c4450c48..000000000000 --- a/superset/key_value/commands/delete.py +++ /dev/null @@ -1,49 +0,0 @@ -# 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. -import logging -from abc import ABC, abstractmethod -from typing import Optional - -from flask_appbuilder.models.sqla import Model -from flask_appbuilder.security.sqla.models import User -from sqlalchemy.exc import SQLAlchemyError - -from superset.commands.base import BaseCommand -from superset.key_value.commands.exceptions import KeyValueDeleteFailedError - -logger = logging.getLogger(__name__) - - -class DeleteKeyValueCommand(BaseCommand, ABC): - def __init__(self, user: User, resource_id: int, key: str): - self._actor = user - self._resource_id = resource_id - self._key = key - - def run(self) -> Model: - try: - return self.delete(self._resource_id, self._key) - except SQLAlchemyError as ex: - logger.exception("Error running delete command") - raise KeyValueDeleteFailedError() from ex - - def validate(self) -> None: - pass - - @abstractmethod - def delete(self, resource_id: int, key: str) -> Optional[bool]: - ... diff --git a/superset/key_value/commands/exceptions.py b/superset/key_value/commands/exceptions.py deleted file mode 100644 index f8c9dbd4a4ff..000000000000 --- a/superset/key_value/commands/exceptions.py +++ /dev/null @@ -1,40 +0,0 @@ -# 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, - CreateFailedError, - DeleteFailedError, - UpdateFailedError, -) - - -class KeyValueCreateFailedError(CreateFailedError): - message = _("An error occurred while creating the value.") - - -class KeyValueGetFailedError(CommandException): - message = _("An error occurred while accessing the value.") - - -class KeyValueDeleteFailedError(DeleteFailedError): - message = _("An error occurred while deleting the value.") - - -class KeyValueUpdateFailedError(UpdateFailedError): - message = _("An error occurred while updating the value.") diff --git a/superset/key_value/commands/get.py b/superset/key_value/commands/get.py deleted file mode 100644 index 32991fa96f54..000000000000 --- a/superset/key_value/commands/get.py +++ /dev/null @@ -1,52 +0,0 @@ -# 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. -import logging -from abc import ABC, abstractmethod -from typing import Optional - -from flask import current_app as app -from flask_appbuilder.models.sqla import Model -from flask_appbuilder.security.sqla.models import User -from sqlalchemy.exc import SQLAlchemyError - -from superset.commands.base import BaseCommand -from superset.key_value.commands.exceptions import KeyValueGetFailedError - -logger = logging.getLogger(__name__) - - -class GetKeyValueCommand(BaseCommand, ABC): - def __init__(self, user: User, resource_id: int, key: str): - self._actor = user - self._resource_id = resource_id - self._key = key - - def run(self) -> Model: - try: - config = app.config["FILTER_STATE_CACHE_CONFIG"] - refreshTimeout = config.get("REFRESH_TIMEOUT_ON_RETRIEVAL") - return self.get(self._resource_id, self._key, refreshTimeout) - except SQLAlchemyError as ex: - logger.exception("Error running get command") - raise KeyValueGetFailedError() from ex - - def validate(self) -> None: - pass - - @abstractmethod - def get(self, resource_id: int, key: str, refreshTimeout: bool) -> Optional[str]: - ... diff --git a/superset/key_value/commands/update.py b/superset/key_value/commands/update.py deleted file mode 100644 index dac91daf3103..000000000000 --- a/superset/key_value/commands/update.py +++ /dev/null @@ -1,52 +0,0 @@ -# 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. -import logging -from abc import ABC, abstractmethod -from typing import Optional - -from flask_appbuilder.models.sqla import Model -from flask_appbuilder.security.sqla.models import User -from sqlalchemy.exc import SQLAlchemyError - -from superset.commands.base import BaseCommand -from superset.key_value.commands.exceptions import KeyValueUpdateFailedError - -logger = logging.getLogger(__name__) - - -class UpdateKeyValueCommand(BaseCommand, ABC): - def __init__( - self, user: User, resource_id: int, key: str, value: str, - ): - self._actor = user - self._resource_id = resource_id - self._key = key - self._value = value - - def run(self) -> Model: - try: - return self.update(self._resource_id, self._key, self._value) - except SQLAlchemyError as ex: - logger.exception("Error running update command") - raise KeyValueUpdateFailedError() from ex - - def validate(self) -> None: - pass - - @abstractmethod - def update(self, resource_id: int, key: str, value: str) -> Optional[bool]: - ... diff --git a/superset/key_value/schemas.py b/superset/key_value/schemas.py deleted file mode 100644 index 3583a0cb769b..000000000000 --- a/superset/key_value/schemas.py +++ /dev/null @@ -1,29 +0,0 @@ -# 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 marshmallow import fields, Schema - - -class KeyValuePostSchema(Schema): - value = fields.String( - required=True, allow_none=False, description="Any type of JSON supported text." - ) - - -class KeyValuePutSchema(Schema): - value = fields.String( - required=True, allow_none=False, description="Any type of JSON supported text." - ) diff --git a/superset/key_value/utils.py b/superset/key_value/utils.py deleted file mode 100644 index 9ba2a8d03607..000000000000 --- a/superset/key_value/utils.py +++ /dev/null @@ -1,23 +0,0 @@ -# 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 typing import Any - -SEPARATOR = ";" - - -def cache_key(*args: Any) -> str: - return SEPARATOR.join(str(arg) for arg in args) diff --git a/superset/utils/cache_manager.py b/superset/utils/cache_manager.py index 5e6e96ebd253..352f62f0273c 100644 --- a/superset/utils/cache_manager.py +++ b/superset/utils/cache_manager.py @@ -25,7 +25,6 @@ def __init__(self) -> None: self._cache = Cache() self._data_cache = Cache() self._thumbnail_cache = Cache() - self._filter_state_cache = Cache() def init_app(self, app: Flask) -> None: self._cache.init_app( @@ -49,13 +48,6 @@ def init_app(self, app: Flask) -> None: **app.config["THUMBNAIL_CACHE_CONFIG"], }, ) - self._filter_state_cache.init_app( - app, - { - "CACHE_DEFAULT_TIMEOUT": app.config["CACHE_DEFAULT_TIMEOUT"], - **app.config["FILTER_STATE_CACHE_CONFIG"], - }, - ) @property def data_cache(self) -> Cache: @@ -68,7 +60,3 @@ def cache(self) -> Cache: @property def thumbnail_cache(self) -> Cache: return self._thumbnail_cache - - @property - def filter_state_cache(self) -> Cache: - return self._filter_state_cache diff --git a/tests/integration_tests/dashboards/filter_state/__init__.py b/tests/integration_tests/dashboards/filter_state/__init__.py deleted file mode 100644 index 13a83393a912..000000000000 --- a/tests/integration_tests/dashboards/filter_state/__init__.py +++ /dev/null @@ -1,16 +0,0 @@ -# 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. diff --git a/tests/integration_tests/dashboards/filter_state/api_tests.py b/tests/integration_tests/dashboards/filter_state/api_tests.py deleted file mode 100644 index 1c2beef66162..000000000000 --- a/tests/integration_tests/dashboards/filter_state/api_tests.py +++ /dev/null @@ -1,157 +0,0 @@ -# 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. -import json -from typing import Any -from unittest.mock import patch - -import pytest -from flask.testing import FlaskClient -from sqlalchemy.orm import Session - -from superset import app -from superset.dashboards.commands.exceptions import DashboardAccessDeniedError -from superset.extensions import cache_manager -from superset.key_value.utils import cache_key -from superset.models.dashboard import Dashboard -from tests.integration_tests.fixtures.world_bank_dashboard import ( - load_world_bank_dashboard_with_slices, -) -from tests.integration_tests.test_app import app - -dashboardId = 985374 -key = "test-key" -value = "test" - - -class FilterStateTests: - @pytest.fixture - def client(self): - with app.test_client() as client: - with app.app_context(): - yield client - - @pytest.fixture - def dashboard_id(self, load_world_bank_dashboard_with_slices) -> int: - with app.app_context() as ctx: - session: Session = ctx.app.appbuilder.get_session - dashboard = session.query(Dashboard).filter_by(slug="world_health").one() - return dashboard.id - - @pytest.fixture - def cache(self, dashboard_id): - app.config["FILTER_STATE_CACHE_CONFIG"] = {"CACHE_TYPE": "SimpleCache"} - cache_manager.init_app(app) - cache_manager.filter_state_cache.set(cache_key(dashboard_id, key), value) - - def setUp(self): - self.login(username="admin") - - def test_post(self, client, dashboard_id: int): - payload = { - "value": value, - } - resp = client.post( - f"api/v1/dashboard/{dashboard_id}/filter_state", json=payload - ) - assert resp.status_code == 201 - - def test_post_bad_request(self, client, dashboard_id: int): - payload = { - "value": 1234, - } - resp = client.put( - f"api/v1/dashboard/{dashboard_id}/filter_state/{key}/", json=payload - ) - assert resp.status_code == 400 - - @patch("superset.security.SupersetSecurityManager.raise_for_dashboard_access") - def test_post_access_denied( - self, client, mock_raise_for_dashboard_access, dashboard_id: int - ): - mock_raise_for_dashboard_access.side_effect = DashboardAccessDeniedError() - payload = { - "value": value, - } - resp = client.post( - f"api/v1/dashboard/{dashboard_id}/filter_state", json=payload - ) - assert resp.status_code == 403 - - def test_put(self, client, dashboard_id: int): - payload = { - "value": "new value", - } - resp = client.put( - f"api/v1/dashboard/{dashboard_id}/filter_state/{key}/", json=payload - ) - assert resp.status_code == 200 - - def test_put_bad_request(self, client, dashboard_id: int): - payload = { - "value": 1234, - } - resp = client.put( - f"api/v1/dashboard/{dashboard_id}/filter_state/{key}/", json=payload - ) - assert resp.status_code == 400 - - @patch("superset.security.SupersetSecurityManager.raise_for_dashboard_access") - def test_put_access_denied( - self, client, mock_raise_for_dashboard_access, dashboard_id: int - ): - mock_raise_for_dashboard_access.side_effect = DashboardAccessDeniedError() - payload = { - "value": "new value", - } - resp = client.put( - f"api/v1/dashboard/{dashboard_id}/filter_state/{key}/", json=payload - ) - assert resp.status_code == 403 - - def test_get_key_not_found(self, client): - resp = client.get("unknown-key") - assert resp.status_code == 404 - - def test_get_dashboard_not_found(self, client): - resp = client.get(f"api/v1/dashboard/{-1}/filter_state/{key}/") - assert resp.status_code == 404 - - def test_get(self, client, dashboard_id: int): - resp = client.get(f"api/v1/dashboard/{dashboard_id}/filter_state/{key}/") - assert resp.status_code == 200 - data = json.loads(resp.data.decode("utf-8")) - assert value == data.get("value") - - @patch("superset.security.SupersetSecurityManager.raise_for_dashboard_access") - def test_get_access_denied( - self, client, mock_raise_for_dashboard_access, dashboard_id - ): - mock_raise_for_dashboard_access.side_effect = DashboardAccessDeniedError() - resp = client.get(f"api/v1/dashboard/{dashboard_id}/filter_state/{key}/") - assert resp.status_code == 403 - - def test_delete(self, client, dashboard_id: int): - resp = client.delete(f"api/v1/dashboard/{dashboard_id}/filter_state/{key}/") - assert resp.status_code == 200 - - @patch("superset.security.SupersetSecurityManager.raise_for_dashboard_access") - def test_delete_access_denied( - self, client, mock_raise_for_dashboard_access, dashboard_id: int - ): - mock_raise_for_dashboard_access.side_effect = DashboardAccessDeniedError() - resp = client.delete(f"api/v1/dashboard/{dashboard_id}/filter_state/{key}/") - assert resp.status_code == 403