-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial support for dashboard search (#95)
* Initial support for dashboard search * fix lint * Add tests * fix lint * fix mypy issues * Address per comment * revert it back * update pypi * Change default exception * update doc
- Loading branch information
Showing
26 changed files
with
479 additions
and
46 deletions.
There are no files selected for viewing
This file contains 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 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,77 @@ | ||
import logging | ||
from http import HTTPStatus | ||
from typing import Iterable, Any | ||
|
||
from flask_restful import fields, Resource, marshal_with, reqparse # noqa: I201 | ||
from flasgger import swag_from | ||
|
||
from search_service.exception import NotFoundException | ||
from search_service.proxy import get_proxy_client | ||
|
||
|
||
DASHBOARD_INDEX = 'dashboard_search_index' | ||
|
||
LOGGING = logging.getLogger(__name__) | ||
|
||
# todo: Use common to produce this | ||
dashboard_fields = { | ||
"uri": fields.String, | ||
"cluster": fields.String, | ||
"group_name": fields.String, | ||
"group_url": fields.String, | ||
"product": fields.String, | ||
"name": fields.String, | ||
"url": fields.String, | ||
"description": fields.String, | ||
"last_successful_run_timestamp": fields.Integer | ||
} | ||
|
||
search_dashboard_results = { | ||
"total_results": fields.Integer, | ||
"results": fields.Nested(dashboard_fields, default=[]) | ||
} | ||
|
||
|
||
class SearchDashboardAPI(Resource): | ||
""" | ||
Search Dashboard API | ||
""" | ||
|
||
def __init__(self) -> None: | ||
self.proxy = get_proxy_client() | ||
|
||
self.parser = reqparse.RequestParser(bundle_errors=True) | ||
|
||
self.parser.add_argument('query_term', required=True, type=str) | ||
self.parser.add_argument('page_index', required=False, default=0, type=int) | ||
self.parser.add_argument('index', required=False, default=DASHBOARD_INDEX, type=str) | ||
|
||
super(SearchDashboardAPI, self).__init__() | ||
|
||
@marshal_with(search_dashboard_results) | ||
@swag_from('swagger_doc/dashboard/search_dashboard.yml') | ||
def get(self) -> Iterable[Any]: | ||
""" | ||
Fetch dashboard search results based on query_term. | ||
:return: list of dashboard results. List can be empty if query | ||
doesn't match any dashboards | ||
""" | ||
args = self.parser.parse_args(strict=True) | ||
try: | ||
results = self.proxy.fetch_dashboard_search_results( | ||
query_term=args.get('query_term'), | ||
page_index=args['page_index'], | ||
index=args['index'] | ||
) | ||
|
||
return results, HTTPStatus.OK | ||
|
||
except NotFoundException: | ||
return {'message': 'query_term does not exist'}, HTTPStatus.NOT_FOUND | ||
|
||
except Exception: | ||
|
||
err_msg = 'Exception encountered while processing search request' | ||
LOGGING.exception(err_msg) | ||
return {'message': err_msg}, HTTPStatus.INTERNAL_SERVER_ERROR |
39 changes: 39 additions & 0 deletions
39
search_service/api/swagger_doc/dashboard/search_dashboard.yml
This file contains 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,39 @@ | ||
Dashboard search | ||
This is used by the frontend API to search dashboard information. | ||
--- | ||
tags: | ||
- 'search_dashboard' | ||
parameters: | ||
- name: query_term | ||
in: query | ||
type: string | ||
schema: | ||
type: string | ||
required: true | ||
- name: page_index | ||
in: query | ||
type: integer | ||
schema: | ||
type: integer | ||
default: 0 | ||
required: false | ||
- name: index | ||
in: query | ||
type: string | ||
schema: | ||
type: string | ||
default: 'dashboard_search_index' | ||
required: false | ||
responses: | ||
200: | ||
description: dashboard result information | ||
content: | ||
application/json: | ||
schema: | ||
$ref: '#/components/schemas/SearchDashboardResults' | ||
500: | ||
description: Exception encountered while searching | ||
content: | ||
application/json: | ||
schema: | ||
$ref: '#/components/schemas/ErrorResponse' |
This file contains 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 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 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,3 @@ | ||
class NotFoundException(Exception): | ||
def __init__(self, message: str) -> None: | ||
super().__init__(message) |
This file contains 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,42 @@ | ||
from typing import Set | ||
|
||
import attr | ||
from amundsen_common.models.dashboard import DashboardSummary, DashboardSummarySchema | ||
|
||
from search_service.models.base import Base | ||
|
||
|
||
@attr.s(auto_attribs=True, kw_only=True) | ||
class Dashboard(Base, | ||
DashboardSummary): | ||
""" | ||
This represents the part of a dashboard stored in the search proxy | ||
""" | ||
|
||
def get_id(self) -> str: | ||
# uses the table key as the document id in ES | ||
return self.name | ||
|
||
@classmethod | ||
def get_attrs(cls) -> Set: | ||
return { | ||
'uri', | ||
'cluster', | ||
'group_name', | ||
'group_url', | ||
'product', | ||
'name', | ||
'url', | ||
'description', | ||
'last_successful_run_timestamp' | ||
} | ||
|
||
@staticmethod | ||
def get_type() -> str: | ||
return 'dashboard' | ||
|
||
|
||
class DashboardSchema(DashboardSummarySchema): | ||
class Meta: | ||
target = Dashboard | ||
register_as_scheme = True |
This file contains 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 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 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 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
Empty file.
Empty file.
This file contains 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,41 @@ | ||
from search_service.models.dashboard import Dashboard | ||
|
||
|
||
def mock_proxy_results() -> Dashboard: | ||
return Dashboard(uri='dashboard_uri', | ||
cluster='gold', | ||
group_name='mode_dashboard_group', | ||
group_url='mode_dashboard_group_url', | ||
product='mode', | ||
name='mode_dashboard', | ||
url='mode_dashboard_url', | ||
description='test_dashboard', | ||
last_successful_run_timestamp=1000) | ||
|
||
|
||
def mock_json_response() -> dict: | ||
return { | ||
"uri": 'dashboard_uri', | ||
"cluster": 'gold', | ||
"group_name": 'mode_dashboard_group', | ||
"group_url": 'mode_dashboard_group_url', | ||
"product": 'mode', | ||
"name": 'mode_dashboard', | ||
"url": 'mode_dashboard_url', | ||
"description": 'test_dashboard', | ||
"last_successful_run_timestamp": 1000, | ||
} | ||
|
||
|
||
def default_json_response() -> dict: | ||
return { | ||
"uri": None, | ||
"cluster": None, | ||
"group_name": None, | ||
"group_url": None, | ||
"product": None, | ||
"name": None, | ||
"url": None, | ||
"description": None, | ||
"last_successful_run_timestamp": 0, | ||
} |
Oops, something went wrong.