Skip to content

Commit

Permalink
Merge "analytics: add quoted values and fix 500 errors"
Browse files Browse the repository at this point in the history
  • Loading branch information
Zuul CI authored and Gerrit Code Review committed Dec 18, 2024
2 parents dcc2d7a + ad13182 commit 3721187
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 8 deletions.
19 changes: 15 additions & 4 deletions dci/analytics/query_es_dsl.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@
import pyparsing as pp

_field = pp.Word(pp.alphanums + "_" + ".")
_value = pp.Word(
_word = pp.Word(
pp.alphanums
+ " "
+ "_"
+ "-"
+ "%"
Expand All @@ -33,16 +34,26 @@
+ "["
+ "]"
)
_word = pp.Word(pp.alphanums + "_" + "-" + "." + " " + ":")
_value_with_quotes = pp.Suppress(pp.Literal("'")) + _word + pp.Suppress(pp.Literal("'"))
_value_without_quotes = _word
_value = _value_without_quotes | _value_with_quotes

_value_for_list = pp.Word(pp.alphanums + "_" + "." + "-" + ":" + " ")
_value_for_list_without_quotes = _value_for_list
_value_for_list_with_quotes = (
pp.Suppress(pp.Literal("'")) + _value_for_list + pp.Suppress(pp.Literal("'"))
)
_value_for_list = _value_for_list_without_quotes | _value_for_list_with_quotes

_comma = pp.Suppress(pp.Literal(","))
_lp = pp.Suppress(pp.Literal("("))
_rp = pp.Suppress(pp.Literal(")"))

_lb = pp.Suppress(pp.Literal("["))
_rb = pp.Suppress(pp.Literal("]"))

_comma_string = _comma + _word
_list = _lb + _word + pp.ZeroOrMore(_comma_string) + _rb
_comma_value = _comma + _value_for_list
_list = _lb + _value_for_list + pp.ZeroOrMore(_comma_value) + _rb

_comparison_operators = {"=", "!=", "<=", "<", ">=", ">", "=~"}
_comparison_operators = pp.oneOf(" ".join(_comparison_operators))
Expand Down
8 changes: 7 additions & 1 deletion dci/api/v1/analytics.py
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,13 @@ def tasks_jobs(user):
if user.is_not_super_admin() and user.is_not_epm() and user.is_not_read_only_user():
raise dci_exc.Unauthorized()

es_query = build_es_query(flask.request.args.to_dict())
args = flask.request.args.to_dict()

try:
es_query = build_es_query(args)
except Exception as e:
logger.error(f"error while building the elastic query: {e}")
raise dci_exc.DCIException("error while building the elastic query")

try:
res = requests.get(
Expand Down
27 changes: 27 additions & 0 deletions tests/analytics/test_query_es_dsl.py
Original file line number Diff line number Diff line change
Expand Up @@ -431,3 +431,30 @@ def test_nrt_query_build_nested_regexp():
]
}
}


def test_nrt_query_build_quoted_values():
ret = qed.build(
"(tags in [daily]) and (team.name in [rh-telco-ci,telco-ci-partner,'f5 - openshift'])"
)
assert ret == {
"bool": {
"filter": [
{"terms": {"tags": ["daily"]}},
{
"nested": {
"path": "team",
"query": {
"terms": {
"team.name": [
"rh-telco-ci",
"telco-ci-partner",
"f5 - openshift",
]
}
},
}
},
]
}
}
10 changes: 7 additions & 3 deletions tests/api/v1/test_analytics.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,17 @@ def test_tasks_analytics_pipelines_status(user, team_admin_id):
assert res.status_code == 401


def test_tasks_jobs(user):
def test_tasks_jobs(user, admin):
res = user.get(
"/api/v1/analytics/jobs?team_id=foo",
data={"query": "my-query"},
"/api/v1/analytics/jobs?query=(a=b)",
)
assert res.status_code == 401

res = admin.get(
"/api/v1/analytics/jobs?query=foo",
)
assert res.status_code == 400


def test_handle_es_sort():
res = analytics.handle_es_sort({"sort": "titi"})
Expand Down

0 comments on commit 3721187

Please sign in to comment.