-
Notifications
You must be signed in to change notification settings - Fork 17.4k
feat: Adds a key-value endpoint to store the state of dashboard filters #17536
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
michael-s-molina
merged 14 commits into
apache:master
from
michael-s-molina:add-filter-state-endpoint
Dec 1, 2021
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
8074963
feat: Adds a key-value endpoint to store the state of dashboard filters
michael-s-molina 588a016
Fixes pylint issues
michael-s-molina 2f8f429
Adds openapi schemas
michael-s-molina d1f8bcb
Adds more tests, move logic to commands and use singular form for the…
michael-s-molina adc0409
Fixes model description
michael-s-molina b9004e0
Removes database model
michael-s-molina db29e6f
Adds open api specs
michael-s-molina 0f44be5
Simplifies the commands
michael-s-molina 7c76172
Adds more tests
michael-s-molina 4af801a
Validates the value content and submits the correct http status code
michael-s-molina 458d468
Fixes import order
michael-s-molina 01249af
Skips flakky test
michael-s-molina ff22b33
Fixes tests
michael-s-molina b0e5a42
Updates UPDATING.md
michael-s-molina File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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"]) | ||
|
villebro marked this conversation as resolved.
Outdated
|
||
| @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) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.