Skip to content

Commit

Permalink
Fix log fetching on valkyrie
Browse files Browse the repository at this point in the history
  • Loading branch information
mbriand committed Oct 10, 2024
1 parent d9a2a27 commit ff4b607
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 51 deletions.
37 changes: 0 additions & 37 deletions swattool/logs.py

This file was deleted.

21 changes: 11 additions & 10 deletions swattool/logsview.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import requests
from simple_term_menu import TerminalMenu # type: ignore

from . import logs
from . import swatbuild
from . import utils
from .webrequests import Session
Expand All @@ -32,9 +31,10 @@ def show_logs_menu(build: swatbuild.Build):
"""Show a menu allowing to select log file to analyze."""
def get_failure_line(failure, logname):
return (failure.id, failure.stepnumber, failure.stepname, logname)
entries = [get_failure_line(failure, url)
for failure in build.failures.values()
for url in failure.urls]
logs = [(failure, logname)
for failure in build.failures.values()
for logname in failure.urls]
entries = [get_failure_line(failure, logname) for failure, logname in logs]
default_line = get_failure_line(build.get_first_failure(), 'stdio')
entry = entries.index(default_line)
logs_menu = utils.tabulated_menu(entries, title="Log files",
Expand All @@ -45,7 +45,7 @@ def get_failure_line(failure, logname):
if newentry is None:
break

show_log_menu(build.id, entries[newentry][1], entries[newentry][3])
show_log_menu(*logs[newentry])


def _format_log_line(linenum: int, text: str, colorized_line: Optional[int],
Expand Down Expand Up @@ -124,9 +124,9 @@ def _show_log(loglines: list[str], selected_line: Optional[int],
utils.show_in_less("\n".join(colorlines), startline)


def _load_log(buildid: int, stepnumber: int, logname: str
def _load_log(failure: swatbuild.Failure, logname: str
) -> Optional[str]:
logurl = logs.get_log_raw_url(buildid, stepnumber, logname)
logurl = failure.get_log_raw_url(logname)
if not logurl:
logging.error("Failed to find log")
return None
Expand All @@ -140,9 +140,9 @@ def _load_log(buildid: int, stepnumber: int, logname: str
return logdata


def show_log_menu(buildid: int, stepnumber: int, logname: str):
def show_log_menu(failure: swatbuild.Failure, logname: str):
"""Analyze a failure log file."""
logdata = _load_log(buildid, stepnumber, logname)
logdata = _load_log(failure, logname)
if not logdata:
return

Expand All @@ -163,7 +163,8 @@ def preview(line):
return _format_log_preview(int(line), loglines, highlight_lines,
preview_height)

title = f"Log file: {logname} of build {buildid}, step {stepnumber}"
title = f"Log file: {logname} of build {failure.build.id}, " \
f"step {failure.stepnumber}"
entry = 2
while True:
menu = TerminalMenu(entries, title=title, cursor_index=entry,
Expand Down
2 changes: 1 addition & 1 deletion swattool/review.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ def _handle_view_command(build: swatbuild.Build, command: str) -> bool:
return True
if command == "l": # View stdio log
failure = build.get_first_failure()
logsview.show_log_menu(build.id, failure.stepnumber, 'stdio')
logsview.show_log_menu(failure, 'stdio')
return True
if command == "x": # Explore logs
logsview.show_logs_menu(build)
Expand Down
30 changes: 27 additions & 3 deletions swattool/swatbuild.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,20 @@
"""Interraction with the swatbot Django server."""

import enum
import json
import logging
import shutil
from datetime import datetime
from typing import Any, Iterable, Optional

import click
import requests
import tabulate

from . import logs
from . import swatbotrest
from . import userdata
from . import utils
from .webrequests import Session

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -60,9 +62,26 @@ def open_log_url(self, logname: str = "stdio"):
else:
logger.error("Failed to find %s log", logname)

def get_log_raw_url(self, logname: str = "stdio") -> Optional[str]:
def get_log_raw_url(self, logname: str = "stdio"
) -> Optional[str]:
"""Get the URL of a raw log file."""
return logs.get_log_raw_url(self.build.id, self.stepnumber, logname)
rest_url = self.build.rest_api_url()
info_url = f"{rest_url}/builds/{self.build.id}/steps/" \
f"{self.stepnumber}/logs/{logname}"
logging.debug("Log info URL: %s", info_url)

try:
info_data = Session().get(info_url)
except requests.exceptions.HTTPError:
return None

try:
info_json_data = json.loads(info_data)
except json.decoder.JSONDecodeError:
return None

logid = info_json_data['logs'][0]['logid']
return f"{rest_url}/logs/{logid}/raw"


class Build:
Expand Down Expand Up @@ -231,3 +250,8 @@ def format_field(field):
desc += f"\n\n{Field.USER_NOTES}:\n{wrapped}"

return desc

def rest_api_url(self) -> str:
"""Get the REST API URL prefix for this build."""
url, _, _ = self.autobuilder_url.partition('/#/builders')
return f"{url}/api/v2"

0 comments on commit ff4b607

Please sign in to comment.