Skip to content

Adapt parsing of FT.PROFILE response #3242

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 0 additions & 39 deletions redis/commands/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,45 +59,6 @@ def parse_to_list(response):
return res


def parse_list_to_dict(response):
res = {}
for i in range(0, len(response), 2):
if isinstance(response[i], list):
res["Child iterators"].append(parse_list_to_dict(response[i]))
try:
if isinstance(response[i + 1], list):
res["Child iterators"].append(parse_list_to_dict(response[i + 1]))
except IndexError:
pass
elif isinstance(response[i + 1], list):
res["Child iterators"] = [parse_list_to_dict(response[i + 1])]
else:
try:
res[response[i]] = float(response[i + 1])
except (TypeError, ValueError):
res[response[i]] = response[i + 1]
return res


def parse_to_dict(response):
if response is None:
return {}

res = {}
for det in response:
if isinstance(det[1], list):
res[det[0]] = parse_list_to_dict(det[1])
else:
try: # try to set the attribute. may be provided without value
try: # try to convert the value to float
res[det[0]] = float(det[1])
except (TypeError, ValueError):
res[det[0]] = det[1]
except IndexError:
pass
return res


def random_string(length=10):
"""
Returns a random N character long string.
Expand Down
26 changes: 22 additions & 4 deletions redis/commands/search/commands.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import itertools
import time
from typing import Dict, List, Optional, Union
from typing import Any, Dict, List, Optional, Tuple, Union

from redis.client import Pipeline
from redis.utils import deprecated_function

from ..helpers import get_protocol_version, parse_to_dict
from ..._parsers.helpers import pairs_to_dict
from ..helpers import get_protocol_version
from ._util import to_string
from .aggregation import AggregateRequest, AggregateResult, Cursor
from .document import Document
Expand Down Expand Up @@ -85,7 +86,9 @@ def _parse_search(self, res, **kwargs):
def _parse_aggregate(self, res, **kwargs):
return self._get_aggregate_result(res, kwargs["query"], kwargs["has_cursor"])

def _parse_profile(self, res, **kwargs):
def _parse_profile(
self, res, **kwargs
) -> Tuple[Union[AggregateResult, Result], Dict[str, Any]]:
query = kwargs["query"]
if isinstance(query, AggregateRequest):
result = self._get_aggregate_result(res[0], query, query._cursor)
Expand All @@ -98,7 +101,22 @@ def _parse_profile(self, res, **kwargs):
with_scores=query._with_scores,
)

return result, parse_to_dict(res[1])
details = pairs_to_dict(res[1])
details["Coordinator"] = pairs_to_dict(details["Coordinator"])
details["Shards"] = [pairs_to_dict(s) for s in details["Shards"]]
for shard in details["Shards"]:
if "Iterators profile" in shard:
shard["Iterators profile"] = pairs_to_dict(shard["Iterators profile"])
if "Child iterators" in shard["Iterators profile"]:
shard["Iterators profile"]["Child iterators"] = [
pairs_to_dict(c)
for c in shard["Iterators profile"]["Child iterators"]
]
if "Result processors profile" in shard:
shard["Result processors profile"] = [
pairs_to_dict(r) for r in shard["Result processors profile"]
]
return result, details

def _parse_spellcheck(self, res, **kwargs):
corrections = {}
Expand Down
35 changes: 0 additions & 35 deletions tests/test_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
delist,
list_or_args,
nativestr,
parse_to_dict,
parse_to_list,
quote_string,
random_string,
Expand All @@ -26,40 +25,6 @@ def test_parse_to_list():
assert parse_to_list(r) == ["hello", "my name", 45, 555.55, "is simon!", None]


def test_parse_to_dict():
assert parse_to_dict(None) == {}
r = [
["Some number", "1.0345"],
["Some string", "hello"],
[
"Child iterators",
[
"Time",
"0.2089",
"Counter",
3,
"Child iterators",
["Type", "bar", "Time", "0.0729", "Counter", 3],
["Type", "barbar", "Time", "0.058", "Counter", 3],
["Type", "barbarbar", "Time", "0.0234", "Counter", 3],
],
],
]
assert parse_to_dict(r) == {
"Child iterators": {
"Child iterators": [
{"Counter": 3.0, "Time": 0.0729, "Type": "bar"},
{"Counter": 3.0, "Time": 0.058, "Type": "barbar"},
{"Counter": 3.0, "Time": 0.0234, "Type": "barbarbar"},
],
"Counter": 3.0,
"Time": 0.2089,
},
"Some number": 1.0345,
"Some string": "hello",
}


def test_nativestr():
assert nativestr("teststr") == "teststr"
assert nativestr(b"teststr") == "teststr"
Expand Down
Loading
Loading