From f24bf6bf870609b7de0075b2af9c1a7bf818709e Mon Sep 17 00:00:00 2001 From: tjoubert Date: Mon, 19 Sep 2022 23:55:49 +0400 Subject: [PATCH 1/2] Implemented query rules endpoint --- arango/aql.py | 23 +++++++++++++++++++++++ arango/exceptions.py | 4 ++++ arango/formatter.py | 44 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 71 insertions(+) diff --git a/arango/aql.py b/arango/aql.py index 649a9438..0589f5dd 100644 --- a/arango/aql.py +++ b/arango/aql.py @@ -22,6 +22,7 @@ AQLQueryTrackingGetError, AQLQueryTrackingSetError, AQLQueryValidateError, + AQLQueryRulesGetError, ) from arango.executor import ApiExecutor from arango.formatter import ( @@ -30,6 +31,7 @@ format_aql_tracking, format_body, format_query_cache_entry, + format_query_rule_item, ) from arango.request import Request from arango.response import Response @@ -632,3 +634,24 @@ def response_handler(resp: Response) -> Union[bool, Json]: return {"deleted": resp.body["deletedCount"]} return self._execute(request, response_handler) + + def query_rules(self) -> Result[Jsons]: + """Return the available optimizer rules for AQL queries + + :return: The available optimizer rules for AQL queries + :rtype: dict + :raise arango.exceptions.AQLQueryRulesGetError: If retrieval fails. + """ + request = Request(method="get", endpoint="/_api/query/rules") + + def response_handler(resp: Response) -> Jsons: + if not resp.is_success: + raise AQLQueryRulesGetError(resp, request) + + rules: Jsons = resp.body + items: Jsons = [] + for rule in rules: + items.append(format_query_rule_item(rule)) + return items + + return self._execute(request, response_handler) diff --git a/arango/exceptions.py b/arango/exceptions.py index 6c78e3cd..4b3ac07b 100644 --- a/arango/exceptions.py +++ b/arango/exceptions.py @@ -153,6 +153,10 @@ class AQLFunctionDeleteError(ArangoServerError): """Failed to delete AQL user function.""" +class AQLQueryRulesGetError(ArangoServerError): + """Failed to retrieve AQL query rules.""" + + ############################## # Async Execution Exceptions # ############################## diff --git a/arango/formatter.py b/arango/formatter.py index 38a2baf7..dce6b920 100644 --- a/arango/formatter.py +++ b/arango/formatter.py @@ -1141,3 +1141,47 @@ def format_query_cache_entry(body: Json) -> Json: result["data_sources"] = body["dataSources"] return verify_format(body, result) + + +def format_query_rule_item(body: Json) -> Json: + """Format AQL query rule item. + + :param body: Input body. + :type body: dict + :return: Formatted body. + :rtype: dict + """ + result = {} + + if "name" in body: + result["name"] = body["name"] + if "flags" in body: + result["flags"] = format_query_rule_item_flags(body["flags"]) + + return verify_format(body, result) + + +def format_query_rule_item_flags(body: Json) -> Json: + """Format AQL query rule item flags. + + :param body: Input body. + :type body: dict + :return: Formatted body. + :rtype: dict + """ + result = {} + + if "hidden" in body: + result["hidden"] = body["hidden"] + if "clusterOnly" in body: + result["clusterOnly"] = body["clusterOnly"] + if "canBeDisabled" in body: + result["canBeDisabled"] = body["canBeDisabled"] + if "canCreateAdditionalPlans" in body: + result["canCreateAdditionalPlans"] = body["canCreateAdditionalPlans"] + if "disabledByDefault" in body: + result["disabledByDefault"] = body["disabledByDefault"] + if "enterpriseOnly" in body: + result["enterpriseOnly"] = body["enterpriseOnly"] + + return verify_format(body, result) From aad43eb455ec69cd2303e0282fdd6d88c53e38af Mon Sep 17 00:00:00 2001 From: tjoubert Date: Tue, 20 Sep 2022 00:01:46 +0400 Subject: [PATCH 2/2] Re-sorted imports --- arango/aql.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/arango/aql.py b/arango/aql.py index 0589f5dd..141d1fb7 100644 --- a/arango/aql.py +++ b/arango/aql.py @@ -19,10 +19,10 @@ AQLQueryExplainError, AQLQueryKillError, AQLQueryListError, + AQLQueryRulesGetError, AQLQueryTrackingGetError, AQLQueryTrackingSetError, AQLQueryValidateError, - AQLQueryRulesGetError, ) from arango.executor import ApiExecutor from arango.formatter import (