Skip to content

Commit

Permalink
Support _return_query in supersearchfacet (#140)
Browse files Browse the repository at this point in the history
  • Loading branch information
willkg committed Oct 15, 2024
1 parent 30b2bec commit fb573fb
Show file tree
Hide file tree
Showing 2 changed files with 150 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/crashstats_tools/cmd_supersearchfacet.py
Original file line number Diff line number Diff line change
Expand Up @@ -503,12 +503,30 @@ def supersearchfacet(
if verbose:
console.print(f"Params: {params}")

if "_return_query" in params:
if not api_token:
console.print(
"[red]No API token provided, so _return_query cannot be used.[/red]"
)
ctx.exit(1)

query = supersearch_facet(
params=params,
api_token=api_token,
host=host,
logger=ConsoleLogger(console) if verbose else None,
)

console.print(query)
return

facet_data_payload = supersearch_facet(
params=params,
api_token=api_token,
host=host,
logger=ConsoleLogger(console) if verbose else None,
)

if (
"signature" not in params["_facets"]
and "signature" in facet_data_payload["facets"]
Expand Down
132 changes: 132 additions & 0 deletions tests/test_supersearchfacet.py
Original file line number Diff line number Diff line change
Expand Up @@ -1267,3 +1267,135 @@ def test_json():
}
"""
)


@freezegun.freeze_time("2024-10-15 13:02:00")
@responses.activate
def test_return_query():
# This makes sure that passing in _retury_query=1 works
api_token = "935e136cdfe14b83abae0e0cd97b634f"
query_data = {
"query": {
"query": {
"filtered": {
"query": {"match_all": {}},
"filter": {
"bool": {
"must": [
{
"bool": {
"must": [
{
"range": {
"processed_crash.date_processed": {
"lt": "2024-10-15T23:59:59+00:00"
}
}
},
{
"range": {
"processed_crash.date_processed": {
"gte": "2024-10-08T00:00:00+00:00"
}
}
},
]
}
}
]
}
},
}
},
"aggs": {
"signature": {
"terms": {"field": "processed_crash.signature.full", "size": 50}
}
},
"from": 0,
"size": 0,
"fields": [
"processed_crash.uuid",
"processed_crash.date_processed",
"processed_crash.signature",
"processed_crash.product",
"processed_crash.version",
],
},
"indices": ["socorro202441", "socorro202442"],
}

responses.add(
responses.GET,
DEFAULT_HOST + "/api/SuperSearch/",
match=[
responses.matchers.header_matcher({"Auth-Token": api_token}),
responses.matchers.query_param_matcher(
{
"date": [">=2024-10-08 00:00:00", "<2024-10-15 23:59:59"],
"_return_query": "1",
"_results_number": "0",
}
),
],
status=200,
json=query_data,
)

runner = CliRunner()
result = runner.invoke(
cli=cmd_supersearchfacet.supersearchfacet,
args=["--_return_query=1"],
env={"CRASHSTATS_API_TOKEN": api_token, "COLUMNS": "100"},
)
assert result.exit_code == 0
assert result.output == dedent(
"""\
{
'query': {
'query': {
'filtered': {
'query': {'match_all': {}},
'filter': {
'bool': {
'must': [
{
'bool': {
'must': [
{
'range': {
'processed_crash.date_processed': {
'lt': '2024-10-15T23:59:59+00:00'
}
}
},
{
'range': {
'processed_crash.date_processed': {
'gte': '2024-10-08T00:00:00+00:00'
}
}
}
]
}
}
]
}
}
}
},
'aggs': {'signature': {'terms': {'field': 'processed_crash.signature.full', 'size': 50}}},
'from': 0,
'size': 0,
'fields': [
'processed_crash.uuid',
'processed_crash.date_processed',
'processed_crash.signature',
'processed_crash.product',
'processed_crash.version'
]
},
'indices': ['socorro202441', 'socorro202442']
}
"""
)

0 comments on commit fb573fb

Please sign in to comment.