Skip to content

Commit

Permalink
Merge pull request #60 from ImperialCollegeLondon/logs-view
Browse files Browse the repository at this point in the history
Add ability to view process logs
  • Loading branch information
cc-a authored Sep 10, 2024
2 parents 9a9b7d4 + 04c5522 commit 7ed5e87
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 2 deletions.
3 changes: 3 additions & 0 deletions main/tables.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
)
)

logs_column_template = "<a href=\"{% url 'logs' record.uuid %}\">LOGS</a>"


class ProcessTable(tables.Table):
"""Defines and Process Table for the data from the Process Manager."""
Expand All @@ -28,5 +30,6 @@ class ProcessTable(tables.Table):
session = tables.Column(verbose_name="Session")
status_code = tables.Column(verbose_name="Status Code")
exit_code = tables.Column(verbose_name="Exit Code")
logs = tables.TemplateColumn(logs_column_template, verbose_name="Logs")
restart = tables.TemplateColumn(restart_column_template, verbose_name="Restart")
kill = tables.TemplateColumn(kill_column_template, verbose_name="Kill")
13 changes: 13 additions & 0 deletions main/templates/main/logs.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{% extends "main/base.html" %}

{% block title %}Logs{% endblock title %}

{% block content %}

<div style="white-space: pre-wrap;">
{{ log_text }}
</div>

<a href="{% url 'index' %}">Return to table</a>

{% endblock content %}
1 change: 1 addition & 0 deletions main/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@
path("", views.index, name="index"),
path("restart/<uuid:uuid>", views.restart_process, name="restart"),
path("kill/<uuid:uuid>", views.kill_process, name="kill"),
path("logs/<uuid:uuid>", views.logs, name="logs"),
]
33 changes: 32 additions & 1 deletion main/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@
from django.shortcuts import render
from django.urls import reverse
from drunc.process_manager.process_manager_driver import ProcessManagerDriver
from drunc.utils.shell_utils import create_dummy_token_from_uname
from drunc.utils.shell_utils import DecodedResponse, create_dummy_token_from_uname
from druncschema.process_manager_pb2 import (
LogRequest,
ProcessInstance,
ProcessInstanceList,
ProcessQuery,
Expand Down Expand Up @@ -117,3 +118,33 @@ def kill_process(request: HttpRequest, uuid: uuid.UUID) -> HttpResponse:
"""
asyncio.run(_process_call(str(uuid), ProcessAction.KILL))
return HttpResponseRedirect(reverse("index"))


async def _get_process_logs(uuid: str) -> list[DecodedResponse]:
"""Retrieve logs for a process from the process manager.
Args:
uuid: UUID of the process.
Returns:
The process logs.
"""
pmd = get_process_manager_driver()
query = ProcessQuery(uuids=[ProcessUUID(uuid=uuid)])
request = LogRequest(query=query, how_far=100)
return [item async for item in pmd.logs(request)]


def logs(request: HttpRequest, uuid: uuid.UUID) -> HttpResponse:
"""Display the logs of a process.
Args:
request: the triggering request.
uuid: identifier for the process.
Returns:
The rendered page.
"""
logs_response = asyncio.run(_get_process_logs(str(uuid)))
context = dict(log_text="\n".join(val.data.line for val in logs_response))
return render(request=request, context=context, template_name="main/logs.html")
4 changes: 3 additions & 1 deletion scripts/talk_to_process_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
"""

import asyncio
import random
import string

from drunc.process_manager.process_manager_driver import ProcessManagerDriver
from drunc.utils.shell_utils import create_dummy_token_from_uname
Expand All @@ -30,7 +32,7 @@ async def create_session(pmd: ProcessManagerDriver) -> list[ProcessInstance]:
item
async for item in pmd.dummy_boot(
user="root",
session_name="sess_name",
session_name="".join(random.choices(string.ascii_lowercase, k=10)),
n_processes=1,
sleep=5,
n_sleeps=4,
Expand Down
16 changes: 16 additions & 0 deletions tests/test_views.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
from uuid import uuid4

from django.urls import reverse
from pytest_django.asserts import assertTemplateUsed


Expand All @@ -10,3 +13,16 @@ def test_index(client, admin_client, mocker):

response = admin_client.get("/")
assert response.status_code == 200


def test_logs(client, mocker):
"""Test the logs view."""
mock = mocker.patch("main.views._get_process_logs")

uuid = uuid4()
with assertTemplateUsed(template_name="main/logs.html"):
response = client.get(reverse("logs", kwargs=dict(uuid=uuid)))
assert response.status_code == 200

mock.assert_called_once_with(str(uuid))
assert "log_text" in response.context

0 comments on commit 7ed5e87

Please sign in to comment.