From 7a4e188a7e63c5944dfbe2a3e3e392b0fc113fb7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Bidoul?= Date: Sat, 20 Nov 2021 17:17:35 +0100 Subject: [PATCH] builds.html: add builds oldest first So new builds being created appear left or top. --- src/runboat/api.py | 2 ++ src/runboat/db.py | 29 +++++++++++++++++++++++++---- src/runboat/webui/builds.html | 5 +++-- tests/test_db.py | 25 ++++++++++++++++++++++++- 4 files changed, 54 insertions(+), 7 deletions(-) diff --git a/src/runboat/api.py b/src/runboat/api.py index e832330..3de887b 100644 --- a/src/runboat/api.py +++ b/src/runboat/api.py @@ -11,6 +11,7 @@ from starlette.status import HTTP_404_NOT_FOUND from . import github, models from .controller import Controller, controller +from .db import SortOrder from .deps import authenticated router = APIRouter() @@ -239,6 +240,7 @@ class BuildEventSource: branch=self.branch, pr=self.pr, name=self.build_name, + sort=SortOrder.asc, ): yield self._serialize(models.BuildEvent.modified, build) while True: diff --git a/src/runboat/db.py b/src/runboat/db.py index a0d4edf..4fe0ab3 100644 --- a/src/runboat/db.py +++ b/src/runboat/db.py @@ -1,5 +1,6 @@ import logging import sqlite3 +from enum import Enum from typing import Iterator, Protocol, cast from weakref import WeakSet @@ -8,6 +9,13 @@ from .models import Build, BuildEvent, BuildInitStatus, BuildStatus, Repo _logger = logging.getLogger(__name__) +class SortOrder(Enum): + # commits of pr, then commits of branches, oldest first + asc = 1 + # commit os branches, then commits of prs, newest first + desc = 2 + + class BuildListener(Protocol): def on_build_event(self, event: BuildEvent, build: Build) -> None: ... @@ -195,6 +203,7 @@ class BuildsDb: branch: str | None = None, pr: int | None = None, name: str | None = None, + sort: SortOrder = SortOrder.desc, ) -> Iterator[Build]: query = "SELECT * FROM builds " where = [] @@ -217,10 +226,22 @@ class BuildsDb: params.append(name) if where: query += "WHERE " + " AND ".join(where) - query += ( - " ORDER BY" - " repo, target_branch DESC, COALESCE(pr, 999999) DESC, created DESC" - ) + if sort == SortOrder.desc: + query += ( + " ORDER BY" + " repo DESC," + " COALESCE(pr, 999999) DESC," + " target_branch DESC," + " created DESC" + ) + elif sort == SortOrder.asc: + query += ( + " ORDER BY" + " repo ASC," + " COALESCE(pr, 999999) ASC," + " target_branch ASC," + " created ASC" + ) rows = self._con.execute(query, params).fetchall() for row in rows: yield self._build_from_row(row) diff --git a/src/runboat/webui/builds.html b/src/runboat/webui/builds.html index 542edac..b94cf8f 100644 --- a/src/runboat/webui/builds.html +++ b/src/runboat/webui/builds.html @@ -43,13 +43,14 @@ rowElement = document.createElement("div"); rowElement.classList.add("row"); rowElement.id = rowId; - document.getElementById("builds").appendChild(rowElement); + const buildsElement = document.getElementById("builds"); + buildsElement.insertBefore(rowElement, buildsElement.firstChild); } // add build element to row buildElement = document.createElement("runboat-build"); buildElement.id = oEvent.build.name; buildElement.build = oEvent.build; - rowElement.appendChild(buildElement); + rowElement.insertBefore(buildElement, rowElement.firstChild); } } else if (oEvent.event == "del") { const buildElement = document.getElementById(oEvent.build.name); diff --git a/tests/test_db.py b/tests/test_db.py index 2a0d89e..e96ebbb 100644 --- a/tests/test_db.py +++ b/tests/test_db.py @@ -1,7 +1,7 @@ import datetime from unittest.mock import MagicMock -from runboat.db import BuildsDb +from runboat.db import BuildsDb, SortOrder from runboat.models import Build, BuildInitStatus, BuildStatus, Repo @@ -94,6 +94,29 @@ def test_search_by_branch_and_pr() -> None: assert list(db.search(pr=1)) == [build2] +def test_search_sort() -> None: + db = BuildsDb() + db.add(build1 := _make_build(name="b1", target_branch="15.0", pr=None)) + db.add(build2 := _make_build(name="b2", target_branch="15.0", pr=1)) + db.add(build3 := _make_build(name="b3", target_branch="14.0", pr=None)) + db.add(build4 := _make_build(name="b4", target_branch="14.0", pr=2)) + db.add(build5 := _make_build(name="b5", target_branch="10.0", pr=3)) + assert [b.name for b in db.search(sort=SortOrder.asc)] == [ + build2.name, + build4.name, + build5.name, + build3.name, + build1.name, + ] + assert [b.name for b in db.search()] == [ + build1.name, + build3.name, + build5.name, + build4.name, + build2.name, + ] + + def test_count_by_status() -> None: db = BuildsDb() db.add(_make_build(name="b1", status=BuildStatus.started))