Skip to content
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

Add pagination support for buildtest history subcommands #1424

Merged
merged 7 commits into from
Mar 20, 2023
Merged
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
8 changes: 4 additions & 4 deletions buildtest/cli/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -413,12 +413,12 @@ def path_menu(subparsers):
path.add_argument("name", help="Name of test")


def history_menu(subparsers, parent_parser):
def history_menu(subparsers, pager_option):
"""This method builds the command line menu for ``buildtest history`` command

Args:
subparsers (argparse._SubParsersAction): Subparser object to add subparser
parent_parser (argparse.ArgumentParser): Parent parser object to add to subparser
pager_option (argparse.ArgumentParser): Parent parser object to add to subparser
"""

history_subcmd = subparsers.add_parser(
Expand All @@ -430,7 +430,7 @@ def history_menu(subparsers, parent_parser):
)

list_parser = history_subparser.add_parser(
"list", help="List a summary of all builds", parents=[parent_parser]
"list", help="List a summary of all builds", parents=[pager_option]
)
list_parser.add_argument(
"-n",
Expand All @@ -446,7 +446,7 @@ def history_menu(subparsers, parent_parser):
)

query = history_subparser.add_parser(
"query", help="Query information for a particular build"
"query", help="Query information for a particular build", parents=[pager_option]
)
query.add_argument("id", type=int, help="Select a build ID")
query.add_argument(
Expand Down
7 changes: 7 additions & 0 deletions buildtest/cli/help.py
Original file line number Diff line number Diff line change
Expand Up @@ -405,9 +405,16 @@ def print_history_help():

table.add_row("buildtest history list", "List all build history files")
table.add_row("buildtest history list --terse", "Print output in terse format")
table.add_row(
"buildtest history list --pager", "Paginate output of the history list"
)
table.add_row(
"buildtest history query 0", "Query content of history build identifier '0'"
)
table.add_row(
"buildtest history query 0 --pager",
"Paginate the query content of history build identifier '0'",
)
table.add_row(
"buildtest history query 0 --log", "Open logfile for build identifier '0'"
)
Expand Down
58 changes: 40 additions & 18 deletions buildtest/cli/history.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@


def build_history(args):
"""This is the entry point for command ``buildtest build history`` command which reports
"""This is the entry point for command ``buildtest history`` command which reports

Args:
args (dict): Parsed arguments from `ArgumentParser.parse_args <https://docs.python.org/3/library/argparse.html#argparse.ArgumentParser.parse_args>`_
Expand All @@ -28,7 +28,9 @@ def build_history(args):
)

if args.history == "query":
query_builds(build_id=args.id, log_option=args.log, output=args.output)
query_builds(
build_id=args.id, log_option=args.log, output=args.output, pager=args.pager
)


def sorted_alphanumeric(data):
Expand All @@ -46,6 +48,28 @@ def sorted_alphanumeric(data):
return sorted(data, key=alphanum_key)


def print_terse(table, no_header=None, consoleColor=None):
"""This method prints the buildtest history list in terse mode which is run via command ``buildtest history list --terse``

Args:
table (dict): Table with columns required for the ``buildtest history list`` command.
no_header (bool, optional): Control whether header columns are displayed with terse format
consoleColor (bool, optional): Select desired color when displaying results
"""

row_entry = [table[key] for key in table.keys()]
transpose_list = [list(i) for i in zip(*row_entry)]
header = "|".join(table.keys())

# We print the table columns if --no-header is not specified
if not no_header:
console.print(header, style=consoleColor)

for row in transpose_list:
line = "|".join(row)
console.print(f"[{consoleColor}]{line}")


def list_build_history(no_header=None, terse=None, pager=None, color=None):
"""This method is entry point for ``buildtest history list`` which prints all previous builds
stored in **BUILD_HISTORY_DIR**. Each directory has a ``build.json`` file that stores content
Expand Down Expand Up @@ -101,20 +125,12 @@ def list_build_history(no_header=None, terse=None, pager=None, color=None):
table["fail_rate"].append(content["test_summary"]["fail_rate"])

if terse:
row_entry = []

for key in table.keys():
row_entry.append(table[key])

transpose_list = [list(i) for i in zip(*row_entry)]
if pager:
with console.pager():
print_terse(table, no_header, consoleColor)
return

# We print the table columns if --no-header is not specified
if not no_header:
console.print("|".join(table.keys()), style=consoleColor)

for row in transpose_list:
line = "|".join(row)
console.print(f"[{consoleColor}]{line}")
print_terse(table, no_header, consoleColor)
return

history_table = Table(
Expand Down Expand Up @@ -170,14 +186,15 @@ def list_build_history(no_header=None, terse=None, pager=None, color=None):
console.print(history_table)


def query_builds(build_id, log_option, output):
def query_builds(build_id, log_option=None, output=None, pager=None):
"""This method is called when user runs `buildtest history query` which will
report the build.json and logfile.

Args:
build_id (int): Build Identifier which is used for querying history file. The indentifier is an integer starting from 0
log_option (bool): Option to control whether log file is opened in editor. This is specified via ``buildtest history query -l <id>``
output (bool): Display output.txt file which contains output of ``buildtest build`` command. This is passed via ``buildtest history query -o``
log_option (bool, optional): Option to control whether log file is opened in editor. This is specified via ``buildtest history query -l <id>``
output (bool, optional): Display output.txt file which contains output of ``buildtest build`` command. This is passed via ``buildtest history query -o``
pager (bool, optional): Print output in paging format
"""

if not is_dir(BUILD_HISTORY_DIR):
Expand Down Expand Up @@ -206,4 +223,9 @@ def query_builds(build_id, log_option, output):
print(output_content)
return

if pager:
with console.pager():
console.print(content)
return

pprint(content)
21 changes: 12 additions & 9 deletions tests/cli/test_history.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,23 @@


def test_build_history_list():
# Testing command: buildtest history list
# buildtest history list
list_build_history(terse=False, no_header=False, pager=False)

# test with a color: buildtest history list --color <color>
# buildtest history list --color <color>
list_build_history(
terse=False, no_header=False, pager=False, color=Color.default().name
)

# test with pager support: buildtest history list --pager
# buildtest history list --pager
list_build_history(terse=False, no_header=False, pager=True)

# test with terse mode and with color: buildtest --color <Color> history list --terse
prathmesh4321 marked this conversation as resolved.
Show resolved Hide resolved
# buildtest --color <Color> history list --terse --pager
list_build_history(
terse=True, no_header=False, pager=False, color=Color.default().name
terse=True, no_header=False, pager=True, color=Color.default().name
)

# test with terse and no header: buildtest history list --terse --no-header
# buildtest history list --terse --no-header
list_build_history(terse=True, no_header=True, pager=False)


Expand All @@ -54,13 +54,16 @@ def test_build_history_query():
build_id = list(range(len(os.listdir(BUILD_HISTORY_DIR))))[-1]
print(build_id)

# run buildtest history query <id>
# buildtest history query <id>
query_builds(build_id=build_id, log_option=False, output=False)

# run buildtest history query <id> --output
# buildtest history query <id> --pager
query_builds(build_id=build_id, log_option=False, output=False, pager=True)

# buildtest history query <id> --output
query_builds(build_id=build_id, log_option=False, output=True)

# run buildtest history query <id> --log
# buildtest history query <id> --log
query_builds(build_id=build_id, log_option=True, output=False)


Expand Down