Skip to content

Commit

Permalink
Fix pagination of full error logs
Browse files Browse the repository at this point in the history
  • Loading branch information
ycedres committed Oct 10, 2024
1 parent 3fece3d commit 5f681ed
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 4 deletions.
27 changes: 26 additions & 1 deletion health-check/src/uyuni_health_check/loki/logs_gatherer.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

conf = ConfigLoader()

def show_full_error_logs(from_datetime=None, to_datetime=None, since=7, loki=None):
def NO_show_full_error_logs(from_datetime=None, to_datetime=None, since=7, loki=None):
"""
Get and show the error logs
"""
Expand Down Expand Up @@ -44,6 +44,31 @@ def show_full_error_logs(from_datetime=None, to_datetime=None, since=7, loki=Non
use_print=True,
)
print()

def show_full_error_logs(from_datetime, to_datetime, since, console: "Console", loki=None):
"""
Get and show the error logs stats
"""
print(
Markdown(f"- Getting summary of errors in logs")
)
print()
query = f"{{job=~\".+\"}} |~ \"(?i)error|(?i)severe|(?i)critical|(?i)fatal\""

stdout, stderr = query_loki(from_dt=from_datetime, to_dt=to_datetime, since = since, query=query)
lines = stdout.strip().split("\n")
json_objects = []

for line in lines:
try:
json_data = json.loads(line)
json_objects.append(json_data)
except json.JSONDecodeError as e:
console.print(f"[red]Failed to parse JSON:[/red] {e}")
console.print(f"[yellow]Raw output:[/yellow] {line}")

combined_json = json.dumps(json_objects, indent=4)
outputter.print_paginated_json(combined_json)


def show_error_logs_stats(from_datetime, to_datetime, since, console: "Console", loki=None):
Expand Down
3 changes: 1 addition & 2 deletions health-check/src/uyuni_health_check/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,7 @@ def run(ctx, logs, from_datetime, to_datetime, since):
show_error_logs_stats(from_datetime, to_datetime, since, console)

if logs:
with console.pager():
show_full_error_logs(from_datetime, to_datetime, since)
show_full_error_logs(from_datetime, to_datetime, since, console)

console.print(Markdown("# Execution Finished"))

Expand Down
6 changes: 5 additions & 1 deletion health-check/src/uyuni_health_check/outputter/outputter.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,8 @@ def show(data):
)
),
style="italic green",
)
)

def print_paginated_json(data):
with console.pager():
console.print_json(data)

0 comments on commit 5f681ed

Please sign in to comment.