Skip to content

Commit

Permalink
Merge pull request #38 from ImperialCollegeLondon/django-tables2
Browse files Browse the repository at this point in the history
Use Django Tables 2 for the index page
  • Loading branch information
AdrianDAlessandro authored Sep 2, 2024
2 parents 18f7f0b + 8247769 commit 59c3a36
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 45 deletions.
6 changes: 0 additions & 6 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,6 @@ repos:
- id: ruff
args: [--fix, --exit-non-zero-on-fix]
- id: ruff-format
- repo: https://github.com/pre-commit/mirrors-mypy
rev: v1.11.1
hooks:
- id: mypy
exclude: manage.py
additional_dependencies: ['django-stubs[compatible-mypy]']
- repo: https://github.com/igorshubovych/markdownlint-cli
rev: v0.41.0
hooks:
Expand Down
29 changes: 17 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
# dune_processes

[Description for project.]

This is a Python application that uses [poetry](https://python-poetry.org) for packaging
and dependency management. It also provides [pre-commit](https://pre-commit.com/) hooks
(for [ruff](https://pypi.org/project/ruff/) and
[mypy](https://mypy.readthedocs.io/en/stable/)) and automated tests using
[pytest](https://pytest.org/) and [GitHub Actions](https://github.com/features/actions).
This repo defines the web app for the Dune Process Manager web interface.

## For developers

Expand Down Expand Up @@ -38,13 +32,24 @@ To get started:
pre-commit install
```

1. Run the main app:
1. Run the main app (this will not receive any data from the drunc process manager):

```bash
python -m dune_processes
python manage.py runserver
```

## Publishing
### Running the App

To run this with a demo version of the drunc process manager, run it with docker compose:

```bash
docker compose up -d
```

Dummy processes can be sent to the server with the `scripts/talk_to_process_manager.py` script:

```bash
docker compose exec app python scripts/talk_to_process_manager.py
```

The GitHub workflow includes an action to publish on release.
To run this action, uncomment the commented portion of `publish.yml`, and modify the steps for the desired behaviour (publishing a Docker image, publishing to PyPI, deploying documentation etc.)
Take the servers down with `docker compose down`
2 changes: 1 addition & 1 deletion dune_processes/settings/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@


# Custom settings
INSTALLED_APPS += ["main"]
INSTALLED_APPS += ["main", "django_tables2"]


MIDDLEWARE.insert(1, "whitenoise.middleware.WhiteNoiseMiddleware")
Expand Down
14 changes: 14 additions & 0 deletions main/tables.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
"""Tables for the main app."""

import django_tables2 as tables


class ProcessTable(tables.Table):
"""Defines and Process Table for the data from the Process Manager."""

uuid = tables.Column(verbose_name="UUID")
name = tables.Column(verbose_name="Name")
user = tables.Column(verbose_name="User")
session = tables.Column(verbose_name="Session")
status_code = tables.Column(verbose_name="Status Code")
exit_code = tables.Column(verbose_name="Exit Code")
22 changes: 2 additions & 20 deletions main/templates/main/index.html
Original file line number Diff line number Diff line change
@@ -1,29 +1,11 @@
{% extends "main/base.html" %}
{% load render_table from django_tables2 %}

{% block title %}Home{% endblock title %}

{% block content %}
Welcome to the index page.

<table border="1">
<tr>
<th>uuid</th>
<th>name</th>
<th>user</th>
<th>session</th>
<th>status code</th>
<th>exit code</th>
</tr>
{% for value in values %}
<tr>
<td>{{ value.uuid.uuid }}</td>
<td>{{ value.process_description.metadata.name }}</td>
<td>{{ value.process_description.metadata.user }}</td>
<td>{{ value.process_description.metadata.session }}</td>
<td>{{ value.status_code }}</td>
<td>{{ value.return_code }}</td>
</tr>
{% endfor %}
</table>
{% render_table table %}

{% endblock content %}
28 changes: 26 additions & 2 deletions main/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,13 @@
from django.shortcuts import render
from drunc.process_manager.process_manager_driver import ProcessManagerDriver
from drunc.utils.shell_utils import create_dummy_token_from_uname
from druncschema.process_manager_pb2 import ProcessInstanceList, ProcessQuery
from druncschema.process_manager_pb2 import (
ProcessInstance,
ProcessInstanceList,
ProcessQuery,
)

from .tables import ProcessTable


async def get_session_info() -> ProcessInstanceList:
Expand All @@ -20,6 +26,24 @@ async def get_session_info() -> ProcessInstanceList:
def index(request: HttpRequest) -> HttpResponse:
"""View that renders the index/home page."""
val = asyncio.run(get_session_info())
context = {"values": val.data.values}

status_enum_lookup = dict(item[::-1] for item in ProcessInstance.StatusCode.items())

table = []
process_instances = val.data.values
for process_instance in process_instances:
metadata = process_instance.process_description.metadata
table.append(
{
"uuid": process_instance.uuid.uuid,
"name": metadata.name,
"user": metadata.user,
"session": metadata.session,
"status_code": status_enum_lookup[process_instance.status_code],
"exit_code": process_instance.return_code,
}
)

context = {"table": ProcessTable(table)}

return render(request=request, context=context, template_name="main/index.html")
19 changes: 18 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ authors = [
python = "^3.12"
django = "^5.1"
whitenoise = "^6.7.0"
drunc = {git = "https://github.com/DUNE-DAQ/drunc.git"}
druncschema = {git = "https://github.com/DUNE-DAQ/druncschema.git"}
drunc = { git = "https://github.com/DUNE-DAQ/drunc.git" }
druncschema = { git = "https://github.com/DUNE-DAQ/druncschema.git" }
django-tables2 = "^2.7.0"

[tool.poetry.group.dev.dependencies]
pytest = "^8.2"
Expand Down Expand Up @@ -42,7 +43,7 @@ module = "tests.*"
disallow_untyped_defs = false

[[tool.mypy.overrides]]
module = ["druncschema.*", "drunc.*"]
module = ["druncschema.*", "drunc.*", "django_tables2.*"]
ignore_missing_imports = true

[tool.django-stubs]
Expand Down

0 comments on commit 59c3a36

Please sign in to comment.