You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@superset.apache.org by mi...@apache.org on 2021/12/01 12:08:30 UTC

[superset] branch master updated: feat: Adds a key-value endpoint to store the state of dashboard filters (#17536)

This is an automated email from the ASF dual-hosted git repository.

michaelsmolina pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/superset.git


The following commit(s) were added to refs/heads/master by this push:
     new 2f2e8fe  feat: Adds a key-value endpoint to store the state of dashboard filters (#17536)
2f2e8fe is described below

commit 2f2e8fe412296b76cb17094a4c22fe5c3f8bc504
Author: Michael S. Molina <70...@users.noreply.github.com>
AuthorDate: Wed Dec 1 09:06:49 2021 -0300

    feat: Adds a key-value endpoint to store the state of dashboard filters (#17536)
    
    * feat: Adds a key-value endpoint to store the state of dashboard filters
    
    * Fixes pylint issues
    
    * Adds openapi schemas
    
    * Adds more tests, move logic to commands and use singular form for the endpoint name
    
    * Fixes model description
    
    * Removes database model
    
    * Adds open api specs
    
    * Simplifies the commands
    
    * Adds more tests
    
    * Validates the value content and submits the correct http status code
    
    * Fixes import order
    
    * Skips flakky test
    
    * Fixes tests
    
    * Updates UPDATING.md
---
 UPDATING.md                                        |   2 +
 superset/config.py                                 |   9 +
 superset/dashboards/filter_state/__init__.py       |  16 ++
 superset/dashboards/filter_state/api.py            | 241 +++++++++++++++++++++
 .../dashboards/filter_state/commands/__init__.py   |  16 ++
 .../dashboards/filter_state/commands/create.py     |  32 +++
 .../dashboards/filter_state/commands/delete.py     |  30 +++
 superset/dashboards/filter_state/commands/get.py   |  33 +++
 .../dashboards/filter_state/commands/update.py     |  32 +++
 superset/initialization/__init__.py                |   2 +
 superset/key_value/api.py                          | 124 +++++++++++
 superset/key_value/commands/__init__.py            |  16 ++
 superset/key_value/commands/create.py              |  54 +++++
 superset/key_value/commands/delete.py              |  49 +++++
 superset/key_value/commands/exceptions.py          |  40 ++++
 superset/key_value/commands/get.py                 |  52 +++++
 superset/key_value/commands/update.py              |  52 +++++
 superset/key_value/schemas.py                      |  29 +++
 superset/key_value/utils.py                        |  23 ++
 superset/utils/cache_manager.py                    |  12 +
 .../dashboards/filter_state/__init__.py            |  16 ++
 .../dashboards/filter_state/api_tests.py           | 157 ++++++++++++++
 22 files changed, 1037 insertions(+)

diff --git a/UPDATING.md b/UPDATING.md
index cf52611..56dd8aa 100644
--- a/UPDATING.md
+++ b/UPDATING.md
@@ -38,11 +38,13 @@ 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 [ [...]
 
 ## 1.3.0
 
diff --git a/superset/config.py b/superset/config.py
index 2d7e7db..aabda42 100644
--- a/superset/config.py
+++ b/superset/config.py
@@ -561,6 +561,15 @@ CACHE_CONFIG: CacheConfig = {"CACHE_TYPE": "null"}
 # 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
new file mode 100644
index 0000000..13a8339
--- /dev/null
+++ b/superset/dashboards/filter_state/__init__.py
@@ -0,0 +1,16 @@
+# 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
new file mode 100644
index 0000000..afc7616
--- /dev/null
+++ b/superset/dashboards/filter_state/api.py
@@ -0,0 +1,241 @@
+# 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("/<int:pk>/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("/<int:pk>/filter_state/<string:key>/", 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("/<int:pk>/filter_state/<string:key>/", 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("/<int:pk>/filter_state/<string:key>/", 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
new file mode 100644
index 0000000..13a8339
--- /dev/null
+++ b/superset/dashboards/filter_state/commands/__init__.py
@@ -0,0 +1,16 @@
+# 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
new file mode 100644
index 0000000..4b8a789
--- /dev/null
+++ b/superset/dashboards/filter_state/commands/create.py
@@ -0,0 +1,32 @@
+# 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
new file mode 100644
index 0000000..89ee287
--- /dev/null
+++ b/superset/dashboards/filter_state/commands/delete.py
@@ -0,0 +1,30 @@
+# 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
new file mode 100644
index 0000000..ea9dc9d
--- /dev/null
+++ b/superset/dashboards/filter_state/commands/get.py
@@ -0,0 +1,33 @@
+# 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
new file mode 100644
index 0000000..a432df6
--- /dev/null
+++ b/superset/dashboards/filter_state/commands/update.py
@@ -0,0 +1,32 @@
+# 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 56e8b1b..e33c038 100644
--- a/superset/initialization/__init__.py
+++ b/superset/initialization/__init__.py
@@ -134,6 +134,7 @@ class SupersetAppInitializer:  # pylint: disable=too-many-public-methods
         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
@@ -212,6 +213,7 @@ class SupersetAppInitializer:  # pylint: disable=too-many-public-methods
         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
new file mode 100644
index 0000000..d67852a
--- /dev/null
+++ b/superset/key_value/api.py
@@ -0,0 +1,124 @@
+# 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
new file mode 100644
index 0000000..13a8339
--- /dev/null
+++ b/superset/key_value/commands/__init__.py
@@ -0,0 +1,16 @@
+# 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
new file mode 100644
index 0000000..361a3ad
--- /dev/null
+++ b/superset/key_value/commands/create.py
@@ -0,0 +1,54 @@
+# 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
new file mode 100644
index 0000000..9990c44
--- /dev/null
+++ b/superset/key_value/commands/delete.py
@@ -0,0 +1,49 @@
+# 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
new file mode 100644
index 0000000..f8c9dbd
--- /dev/null
+++ b/superset/key_value/commands/exceptions.py
@@ -0,0 +1,40 @@
+# 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
new file mode 100644
index 0000000..32991fa
--- /dev/null
+++ b/superset/key_value/commands/get.py
@@ -0,0 +1,52 @@
+# 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
new file mode 100644
index 0000000..dac91da
--- /dev/null
+++ b/superset/key_value/commands/update.py
@@ -0,0 +1,52 @@
+# 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
new file mode 100644
index 0000000..3583a0c
--- /dev/null
+++ b/superset/key_value/schemas.py
@@ -0,0 +1,29 @@
+# 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
new file mode 100644
index 0000000..9ba2a8d
--- /dev/null
+++ b/superset/key_value/utils.py
@@ -0,0 +1,23 @@
+# 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 352f62f..5e6e96e 100644
--- a/superset/utils/cache_manager.py
+++ b/superset/utils/cache_manager.py
@@ -25,6 +25,7 @@ class CacheManager:
         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(
@@ -48,6 +49,13 @@ class CacheManager:
                 **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:
@@ -60,3 +68,7 @@ class CacheManager:
     @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
new file mode 100644
index 0000000..13a8339
--- /dev/null
+++ b/tests/integration_tests/dashboards/filter_state/__init__.py
@@ -0,0 +1,16 @@
+# 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
new file mode 100644
index 0000000..1c2beef
--- /dev/null
+++ b/tests/integration_tests/dashboards/filter_state/api_tests.py
@@ -0,0 +1,157 @@
+# 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