Show queued tasks

This commit is contained in:
Joscha 2023-08-06 20:31:11 +02:00
parent 2c0a496897
commit ad5da60b5a
8 changed files with 180 additions and 25 deletions

View file

@ -1,18 +1,69 @@
use askama::Template;
use axum::{extract::State, response::IntoResponse};
use futures::TryStreamExt;
use sqlx::SqlitePool;
use crate::{config::Config, somehow};
use crate::{config::Config, somehow, util};
use super::{Base, Tab};
#[derive(Template)]
#[template(path = "queue.html")]
struct CommitTemplate {
base: Base,
struct Task {
id: String,
short: String,
since: String,
priority: i64,
}
pub async fn get(State(config): State<&'static Config>) -> somehow::Result<impl IntoResponse> {
Ok(CommitTemplate {
async fn get_queue(db: &SqlitePool) -> somehow::Result<Vec<Task>> {
sqlx::query!(
"\
SELECT \
id, \
hash, \
message, \
date AS \"date: time::OffsetDateTime\", \
priority \
FROM queue \
JOIN commits USING (hash) \
ORDER BY priority DESC, unixepoch(date) DESC, hash ASC \
"
)
.fetch(db)
.map_ok(|r| Task {
id: r.id,
short: util::format_commit_short(&r.hash, &r.message),
since: util::format_delta_from_now(r.date),
priority: r.priority,
})
.err_into::<somehow::Error>()
.try_collect::<Vec<_>>()
.await
}
#[derive(Template)]
#[template(path = "queue_table.html")]
struct QueueTableTemplate {
tasks: Vec<Task>,
}
pub async fn get_table(State(db): State<SqlitePool>) -> somehow::Result<impl IntoResponse> {
let tasks = get_queue(&db).await?;
Ok(QueueTableTemplate { tasks })
}
#[derive(Template)]
#[template(path = "queue.html")]
struct QueueTemplate {
base: Base,
table: QueueTableTemplate,
}
pub async fn get(
State(config): State<&'static Config>,
State(db): State<SqlitePool>,
) -> somehow::Result<impl IntoResponse> {
let tasks = get_queue(&db).await?;
Ok(QueueTemplate {
base: Base::new(config, Tab::Queue),
table: QueueTableTemplate { tasks },
})
}