Skip to content

Commit

Permalink
[resotocore][fix] custom sorting in history (#1914)
Browse files Browse the repository at this point in the history
  • Loading branch information
aquamatthias committed Feb 15, 2024
1 parent b449e30 commit c069a00
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
12 changes: 7 additions & 5 deletions resotocore/resotocore/cli/cli.py
Expand Up @@ -400,6 +400,7 @@ async def parse_query(query_arg: str) -> Query:
additional_commands: List[ExecutableCommand] = []
# We need to remember the first head/tail, since tail will reverse the sort order
first_head_tail_in_a_row: Optional[CLICommand] = None
default_sort = DefaultSort
head_tail_keep_order = True
for command in commands:
part = command.command
Expand All @@ -408,11 +409,12 @@ async def parse_query(query_arg: str) -> Query:
query = query.combine(await parse_query(arg))
elif isinstance(part, HistoryPart):
parsed_options["history"] = True
default_sort = HistorySort
query = query.combine(await parse_query(arg))
if not query.current_part.sort and not query.aggregate:
query = query.set_sort(*HistorySort)
elif isinstance(part, SortPart):
if query.current_part.sort == DefaultSort:
if query.current_part.sort == default_sort:
query = query.set_sort(*sort_args_p.parse(arg))
else:
query = query.add_sort(*sort_args_p.parse(arg))
Expand Down Expand Up @@ -467,7 +469,7 @@ async def parse_query(query_arg: str) -> Query:
query = query.with_limit(size)
p = query.current_part
# the limit might have created a new part - make sure there is a sort order
p = p if p.sort else evolve(p, sort=DefaultSort)
p = p if p.sort else evolve(p, sort=default_sort)
# reverse the sort order -> limit -> reverse the result
query.parts[0] = evolve(p, sort=[s.reversed() for s in p.sort], reverse_result=True)
else:
Expand All @@ -484,14 +486,14 @@ async def parse_query(query_arg: str) -> Query:
# Define default sort order, if not already defined
# A sort order is required to always return the result in a deterministic way to the user.
# Deterministic order is required for head/tail to work
parts = [pt if pt.sort else evolve(pt, sort=DefaultSort) for pt in query.parts]
parts = [pt if pt.sort else evolve(pt, sort=default_sort) for pt in query.parts]
query = evolve(query, parts=parts)

# If the last part is a navigation, we need to add sort which will ingest a new part.
with_sort = query.set_sort(*DefaultSort) if query.current_part.navigation else query
with_sort = query.set_sort(*default_sort) if query.current_part.navigation else query
section = ctx.env.get("section", PathRoot)
# If this is an aggregate query, the default sort needs to be changed
if query.aggregate is not None and query.current_part.sort == DefaultSort:
if query.aggregate is not None and query.current_part.sort == default_sort:
with_sort = query.set_sort(*query.aggregate.sort_by_fn(section))

# When all parts are combined, interpret the result on defined section.
Expand Down
4 changes: 4 additions & 0 deletions resotocore/tests/resotocore/cli/cli_test.py
Expand Up @@ -223,6 +223,10 @@ async def test_create_query_parts(cli: CLI) -> None:
commands = await cli.evaluate_cli_command("search is(volume) sort name | tail -10 | head 5 | head 3 | tail 2")
assert commands[0].executable_commands[0].arg == "'is(\"volume\") sort reported.name desc limit 7, 2 reversed '"

pipes = await cli.evaluate_cli_command("history --change node_vulnerable | sort /changed_at asc | limit 0, 10")
no_pipes = await cli.evaluate_cli_command("history --change node_vulnerable sort /changed_at asc limit 0, 10")
assert pipes[0].executable_commands[0].arg == no_pipes[0].executable_commands[0].arg


@pytest.mark.asyncio
async def test_replacements(cli: CLI) -> None:
Expand Down

0 comments on commit c069a00

Please sign in to comment.