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 COUNT = document.getElementById("count")!;
const QUEUE = document.getElementById("queue")!; const INNER = document.getElementById("inner")!;
const REFRESH_SECONDS = 10; const REFRESH_SECONDS = 10;
function update() { function update() {
fetch("table") fetch("inner")
.then(response => response.text()) .then(response => response.text())
.then(text => { .then(text => {
QUEUE.innerHTML = text; INNER.innerHTML = text;
let count = QUEUE.querySelectorAll("tbody tr").length; let count = INNER.querySelector<HTMLElement>("#queue")?.dataset["count"]!;
COUNT.textContent = String(count);
document.title = document.title.replace(/^queue \(\d+\)/, `queue (${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> { pub fn get(&self, name: &str) -> Option<RunnerInfo> {
self.runners.get(name).cloned() 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("/commit/:hash", get(commit::get))
.route("/runner/:name", get(runner::get)) .route("/runner/:name", get(runner::get))
.route("/queue/", get(queue::get)) .route("/queue/", get(queue::get))
.route("/queue/table", get(queue::get_table)) .route("/queue/inner", get(queue::get_inner))
.merge(api::router(&server)) .merge(api::router(&server))
.fallback(get(r#static::static_handler)) .fallback(get(r#static::static_handler))
.with_state(server.clone()); .with_state(server.clone());

View file

@ -7,9 +7,8 @@ use super::Base;
#[derive(Template)] #[derive(Template)]
#[template( #[template(
ext = "html", ext = "html",
source = " source = "\
{% import \"util.html\" as util %} {% import \"util.html\" as util %}
<a href=\"{{ root }}commit/{{ hash }}\" <a href=\"{{ root }}commit/{{ hash }}\"
class=\"{% call util::commit_class(reachable) %}\" class=\"{% call util::commit_class(reachable) %}\"
title=\"{% call util::commit_title(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 askama::Template;
use axum::{extract::State, response::IntoResponse}; use axum::{extract::State, response::IntoResponse};
use futures::TryStreamExt; use futures::TryStreamExt;
use sqlx::SqlitePool; 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 { struct Task {
hash: String, commit: CommitLink,
short: String,
reachable: i64,
since: String, since: String,
priority: i64, priority: i64,
runners: Vec<RunnerLink>,
odd: bool, 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!( let mut tasks = sqlx::query!(
"\ "\
SELECT \ SELECT \
@ -32,9 +111,8 @@ async fn get_queue(db: &SqlitePool) -> somehow::Result<Vec<Task>> {
) )
.fetch(db) .fetch(db)
.map_ok(|r| Task { .map_ok(|r| Task {
short: util::format_commit_short(&r.hash, &r.message), runners: runners_by_commit.remove(&r.hash).unwrap_or_default(),
hash: r.hash, commit: CommitLink::new(base, r.hash, &r.message, r.reachable),
reachable: r.reachable,
since: util::format_delta_from_now(r.date), since: util::format_delta_from_now(r.date),
priority: r.priority, priority: r.priority,
odd: false, odd: false,
@ -56,37 +134,41 @@ async fn get_queue(db: &SqlitePool) -> somehow::Result<Vec<Task>> {
} }
#[derive(Template)] #[derive(Template)]
#[template(path = "queue_table.html")] #[template(path = "queue_inner.html")]
struct QueueTableTemplate { struct QueueInnerTemplate {
base: Base, runners: Vec<Runner>,
tasks: Vec<Task>, tasks: Vec<Task>,
} }
pub async fn get_table( pub async fn get_inner(
State(config): State<&'static Config>, State(config): State<&'static Config>,
State(db): State<SqlitePool>, State(db): State<SqlitePool>,
State(runners): State<Arc<Mutex<Runners>>>,
) -> somehow::Result<impl IntoResponse> { ) -> somehow::Result<impl IntoResponse> {
let tasks = get_queue(&db).await?; let base = Base::new(config, Tab::Queue);
Ok(QueueTableTemplate { let sorted_runners = sorted_runners(&runners);
base: Base::new(config, Tab::Queue), let runners = get_runners(&db, &sorted_runners, &base).await?;
tasks, let tasks = get_queue(&db, &sorted_runners, &base).await?;
}) Ok(QueueInnerTemplate { runners, tasks })
} }
#[derive(Template)] #[derive(Template)]
#[template(path = "queue.html")] #[template(path = "queue.html")]
struct QueueTemplate { struct QueueTemplate {
base: Base, base: Base,
table: QueueTableTemplate, inner: QueueInnerTemplate,
} }
pub async fn get( pub async fn get(
State(config): State<&'static Config>, State(config): State<&'static Config>,
State(db): State<SqlitePool>, State(db): State<SqlitePool>,
State(runners): State<Arc<Mutex<Runners>>>,
) -> somehow::Result<impl IntoResponse> { ) -> somehow::Result<impl IntoResponse> {
let base = Base::new(config, Tab::Queue); 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 { Ok(QueueTemplate {
base: base.clone(), base,
table: QueueTableTemplate { base, tasks }, inner: QueueInnerTemplate { runners, tasks },
}) })
} }

View file

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

View file

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