List runners in queue

This commit is contained in:
Joscha 2023-08-10 23:04:34 +02:00
parent c3c597897c
commit f3d646c8d5
10 changed files with 243 additions and 61 deletions

View file

@ -0,0 +1,20 @@
{
"db_name": "SQLite",
"query": "SELECT message FROM commits WHERE hash = ?",
"describe": {
"columns": [
{
"name": "message",
"ordinal": 0,
"type_info": "Text"
}
],
"parameters": {
"Right": 1
},
"nullable": [
false
]
},
"hash": "f7d4cee8d4ff00232bcf7d56c4c8d246f25c0b0e3c54c54c43c51b59372c14fb"
}

View file

@ -1,14 +1,13 @@
const COUNT = document.getElementById("count")!;
const QUEUE = document.getElementById("queue")!;
const INNER = document.getElementById("inner")!;
const REFRESH_SECONDS = 10;
function update() {
fetch("table")
fetch("inner")
.then(response => response.text())
.then(text => {
QUEUE.innerHTML = text;
let count = QUEUE.querySelectorAll("tbody tr").length;
COUNT.textContent = String(count);
INNER.innerHTML = text;
let count = INNER.querySelector<HTMLElement>("#queue")?.dataset["count"]!;
document.title = document.title.replace(/^queue \(\d+\)/, `queue (${count})`);
});
}

View file

@ -86,4 +86,8 @@ impl Runners {
pub fn get(&self, name: &str) -> Option<RunnerInfo> {
self.runners.get(name).cloned()
}
pub fn get_all(&self) -> HashMap<String, RunnerInfo> {
self.runners.clone()
}
}

View file

@ -48,7 +48,7 @@ pub async fn run(server: Server) -> somehow::Result<()> {
.route("/commit/:hash", get(commit::get))
.route("/runner/:name", get(runner::get))
.route("/queue/", get(queue::get))
.route("/queue/table", get(queue::get_table))
.route("/queue/inner", get(queue::get_inner))
.merge(api::router(&server))
.fallback(get(r#static::static_handler))
.with_state(server.clone());

View file

@ -7,9 +7,8 @@ use super::Base;
#[derive(Template)]
#[template(
ext = "html",
source = "
source = "\
{% import \"util.html\" as util %}
<a href=\"{{ root }}commit/{{ hash }}\"
class=\"{% call util::commit_class(reachable) %}\"
title=\"{% call util::commit_title(reachable) %}\">
@ -34,3 +33,50 @@ impl CommitLink {
}
}
}
#[derive(Template)]
#[template(
ext = "html",
source = "\
<a href=\"{{ root }}run/{{ id }}\">
Run of {{ short }}
</a>
"
)]
pub struct RunLink {
root: String,
id: String,
short: String,
}
impl RunLink {
pub fn new(base: &Base, id: String, hash: &str, message: &str) -> Self {
Self {
root: base.root.clone(),
id,
short: util::format_commit_short(hash, message),
}
}
}
#[derive(Template)]
#[template(
ext = "html",
source = "\
<a href=\"{{ root }}runner/{{ name }}\">
{{ name }}
</a>
"
)]
pub struct RunnerLink {
root: String,
name: String,
}
impl RunnerLink {
pub fn new(base: &Base, name: String) -> Self {
Self {
root: base.root.clone(),
name,
}
}
}

View file

@ -1,22 +1,101 @@
use std::{
collections::HashMap,
sync::{Arc, Mutex},
};
use askama::Template;
use axum::{extract::State, response::IntoResponse};
use futures::TryStreamExt;
use sqlx::SqlitePool;
use crate::{config::Config, server::util, somehow};
use crate::{
config::Config,
server::{
runners::{RunnerInfo, Runners},
util,
},
shared::RunnerStatus,
somehow,
};
use super::{Base, Tab};
use super::{
link::{CommitLink, RunLink, RunnerLink},
Base, Tab,
};
enum Status {
Idle,
Busy,
Working(RunLink),
}
struct Runner {
link: RunnerLink,
status: Status,
}
struct Task {
hash: String,
short: String,
reachable: i64,
commit: CommitLink,
since: String,
priority: i64,
runners: Vec<RunnerLink>,
odd: bool,
}
async fn get_queue(db: &SqlitePool) -> somehow::Result<Vec<Task>> {
fn sorted_runners(runners: &Mutex<Runners>) -> Vec<(String, RunnerInfo)> {
let mut runners = runners
.lock()
.unwrap()
.get_all()
.into_iter()
.collect::<Vec<_>>();
runners.sort_unstable_by(|(a, _), (b, _)| a.cmp(b));
runners
}
async fn get_runners(
db: &SqlitePool,
runners: &[(String, RunnerInfo)],
base: &Base,
) -> somehow::Result<Vec<Runner>> {
let mut result = vec![];
for (name, info) in runners {
let status = match &info.status {
RunnerStatus::Idle => Status::Idle,
RunnerStatus::Busy => Status::Busy,
RunnerStatus::Working { id, hash, .. } => {
let message =
sqlx::query_scalar!("SELECT message FROM commits WHERE hash = ?", hash)
.fetch_one(db)
.await?;
Status::Working(RunLink::new(base, id.clone(), hash, &message))
}
};
result.push(Runner {
link: RunnerLink::new(base, name.clone()),
status,
})
}
Ok(result)
}
async fn get_queue(
db: &SqlitePool,
runners: &[(String, RunnerInfo)],
base: &Base,
) -> somehow::Result<Vec<Task>> {
// Group runners by commit hash
let mut runners_by_commit: HashMap<String, Vec<RunnerLink>> = HashMap::new();
for (name, info) in runners {
if let RunnerStatus::Working { hash, .. } = &info.status {
runners_by_commit
.entry(hash.clone())
.or_default()
.push(RunnerLink::new(base, name.clone()));
}
}
let mut tasks = sqlx::query!(
"\
SELECT \
@ -32,9 +111,8 @@ async fn get_queue(db: &SqlitePool) -> somehow::Result<Vec<Task>> {
)
.fetch(db)
.map_ok(|r| Task {
short: util::format_commit_short(&r.hash, &r.message),
hash: r.hash,
reachable: r.reachable,
runners: runners_by_commit.remove(&r.hash).unwrap_or_default(),
commit: CommitLink::new(base, r.hash, &r.message, r.reachable),
since: util::format_delta_from_now(r.date),
priority: r.priority,
odd: false,
@ -56,37 +134,41 @@ async fn get_queue(db: &SqlitePool) -> somehow::Result<Vec<Task>> {
}
#[derive(Template)]
#[template(path = "queue_table.html")]
struct QueueTableTemplate {
base: Base,
#[template(path = "queue_inner.html")]
struct QueueInnerTemplate {
runners: Vec<Runner>,
tasks: Vec<Task>,
}
pub async fn get_table(
pub async fn get_inner(
State(config): State<&'static Config>,
State(db): State<SqlitePool>,
State(runners): State<Arc<Mutex<Runners>>>,
) -> somehow::Result<impl IntoResponse> {
let tasks = get_queue(&db).await?;
Ok(QueueTableTemplate {
base: Base::new(config, Tab::Queue),
tasks,
})
let base = Base::new(config, Tab::Queue);
let sorted_runners = sorted_runners(&runners);
let runners = get_runners(&db, &sorted_runners, &base).await?;
let tasks = get_queue(&db, &sorted_runners, &base).await?;
Ok(QueueInnerTemplate { runners, tasks })
}
#[derive(Template)]
#[template(path = "queue.html")]
struct QueueTemplate {
base: Base,
table: QueueTableTemplate,
inner: QueueInnerTemplate,
}
pub async fn get(
State(config): State<&'static Config>,
State(db): State<SqlitePool>,
State(runners): State<Arc<Mutex<Runners>>>,
) -> somehow::Result<impl IntoResponse> {
let base = Base::new(config, Tab::Queue);
let tasks = get_queue(&db).await?;
let sorted_runners = sorted_runners(&runners);
let runners = get_runners(&db, &sorted_runners, &base).await?;
let tasks = get_queue(&db, &sorted_runners, &base).await?;
Ok(QueueTemplate {
base: base.clone(),
table: QueueTableTemplate { base, tasks },
base,
inner: QueueInnerTemplate { runners, tasks },
})
}

View file

@ -146,15 +146,15 @@ nav a:hover {
/* Queue */
.queue td:nth-child(2),
.queue td:nth-child(3) {
#queue td:nth-child(2),
#queue td:nth-child(3) {
text-align: right;
}
.queue .odd {
#queue .odd {
background-color: #eee;
}
.queue .odd:hover {
#queue .odd:hover {
background-color: #ddd;
}

View file

@ -1,12 +1,11 @@
{% extends "base.html" %}
{% block title %}queue ({{ table.tasks.len() }}){% endblock %}
{% block title %}queue ({{ inner.tasks.len() }}){% endblock %}
{% block head %}
<script type="module" src="main.js"></script>
{% endblock %}
{% block body %}
<h2>Queue (<span id="count">{{ table.tasks.len() }}</span>)</h2>
<div id="queue">{{ table|safe }}</div>
<div id="inner">{{ inner|safe }}</div>
{% endblock %}

View file

@ -0,0 +1,56 @@
{% import "util.html" as util %}
<h2>Runners</h2>
{% if runners.is_empty() %}
<p>No runners connected</p>
{% else %}
<table>
<thead>
<tr>
<th>runner</th>
<th>status</th>
</tr>
</thead>
<tbody>
{% for runner in runners %}
<tr>
<td>{{ runner.link|safe }}</td>
{% match runner.status %}
{% when Status::Idle %}
<td>idle</td>
{% when Status::Busy %}
<td>busy</td>
{% when Status::Working with (link) %}
<td>{{ link|safe }}</td>
{% endmatch %}
</tr>
{% endfor %}
</tbody>
</table>
{% endif %}
<h2>Queue ({{ tasks.len() }})</h2>
<table id="queue" data-count="{{ tasks.len() }}">
<thead>
<tr>
<th>commit</th>
<th>since</th>
<th>prio</th>
<th>runner</th>
</tr>
</thead>
<tbody>
{% for task in tasks %}
<tr {% if task.odd %} class="odd" {% endif %}>
<td>{{ task.commit|safe }}</td>
<td>{{ task.since }}</td>
<td>{{ task.priority }}</td>
{% if task.runners.is_empty() %}
<td>-</td>
{% else %}
<td>{{ task.runners|join(", ")|safe }}</td>
{% endif %}
</tr>
{% endfor %}
</tbody>
</table>

View file

@ -1,24 +0,0 @@
{% import "util.html" as util %}
<table class="queue">
<thead>
<tr>
<th>commit</th>
<th>since</th>
<th>prio</th>
</tr>
</thead>
<tbody>
{% for task in tasks %}
<tr {% if task.odd %} class="odd" {% endif %}>
<td>
<a href="{{ base.root }}commit/{{ task.hash }}">
{# {% call util::commit_short(task.short, task.reachable) %} #}
</a>
</td>
<td>{{ task.since }}</td>
<td>{{ task.priority }}</td>
</tr>
{% endfor %}
</tbody>
</table>