From 67d05003291840ab4216bae718a7b2a93e8d5060 Mon Sep 17 00:00:00 2001 From: Diego Medina Date: Tue, 26 Apr 2022 23:22:50 -0400 Subject: [PATCH] fix: Query execution time is displayed as invalid date (#19605) * fix: Query execution time is displayed as invalid date * PR comment * Fix test * unify response * lint (cherry picked from commit e3dbe8d8af434f9095309638e1df0a905f7bbf29) --- superset/queries/api.py | 3 ++- superset/queries/schemas.py | 42 +++++++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 1 deletion(-) diff --git a/superset/queries/api.py b/superset/queries/api.py index 611c69d4bd4cf..460e2dd4667e0 100644 --- a/superset/queries/api.py +++ b/superset/queries/api.py @@ -22,7 +22,7 @@ from superset.databases.filters import DatabaseFilter from superset.models.sql_lab import Query from superset.queries.filters import QueryFilter -from superset.queries.schemas import openapi_spec_methods_override +from superset.queries.schemas import openapi_spec_methods_override, QuerySchema from superset.views.base_api import BaseSupersetModelRestApi, RelatedFieldFilter from superset.views.filters import FilterRelatedOwners @@ -94,6 +94,7 @@ class QueryRestApi(BaseSupersetModelRestApi): ] base_filters = [["id", QueryFilter, lambda: []]] base_order = ("changed_on", "desc") + list_model_schema = QuerySchema() openapi_spec_tag = "Queries" openapi_spec_methods = openapi_spec_methods_override diff --git a/superset/queries/schemas.py b/superset/queries/schemas.py index daca322c6371e..f11cf37127756 100644 --- a/superset/queries/schemas.py +++ b/superset/queries/schemas.py @@ -14,6 +14,13 @@ # KIND, either express or implied. See the License for the # specific language governing permissions and limitations # under the License. +from typing import List + +from marshmallow import fields, Schema + +from superset.dashboards.schemas import UserSchema +from superset.models.sql_lab import Query +from superset.sql_parse import Table openapi_spec_methods_override = { "get": {"get": {"description": "Get query detail information."}}, @@ -25,3 +32,38 @@ } }, } + + +class DatabaseSchema(Schema): + database_name = fields.String() + + +class QuerySchema(Schema): + """ + Schema for the ``Query`` model. + """ + + changed_on = fields.DateTime() + database = fields.Nested(DatabaseSchema) + end_time = fields.Float(attribute="end_time") + executed_sql = fields.String() + id = fields.Int() + rows = fields.Int() + schema = fields.String() + sql = fields.String() + sql_tables = fields.Method("get_sql_tables") + start_time = fields.Float(attribute="start_time") + status = fields.String() + tab_name = fields.String() + tmp_table_name = fields.String() + tracking_url = fields.String() + user = fields.Nested(UserSchema) + + class Meta: # pylint: disable=too-few-public-methods + model = Query + load_instance = True + include_relationships = True + + # pylint: disable=no-self-use + def get_sql_tables(self, obj: Query) -> List[Table]: + return obj.sql_tables