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

@ -22,20 +22,31 @@ pub fn time_to_offset_datetime(time: Time) -> somehow::Result<OffsetDateTime> {
.to_offset(UtcOffset::from_whole_seconds(time.offset)?))
}
pub fn format_time(time: OffsetDateTime) -> somehow::Result<String> {
let now = OffsetDateTime::now_utc();
let delta = time - now;
let formatted_time = time.format(format_description!(
"[year]-[month]-[day] [hour]:[minute]:[second] [offset_hour sign:mandatory][offset_minute]"
))?;
let formatted_delta =
humantime::format_duration(Duration::from_secs(delta.unsigned_abs().as_secs()));
Ok(if delta.is_positive() {
format!("{formatted_time} (in {formatted_delta})")
pub fn format_delta(delta: time::Duration) -> String {
let seconds = delta.unsigned_abs().as_secs();
let seconds = seconds + 30 - (seconds + 30) % 60; // To nearest minute
let formatted = humantime::format_duration(Duration::from_secs(seconds));
if delta.is_positive() {
format!("in {formatted}")
} else {
format!("{formatted_time} ({formatted_delta} ago)")
})
format!("{formatted} ago")
}
}
pub fn format_delta_from_now(time: OffsetDateTime) -> String {
let now = OffsetDateTime::now_utc();
format_delta(time - now)
}
pub fn format_time(time: OffsetDateTime) -> String {
let formatted_time = time
.format(format_description!(
"[year]-[month]-[day] [hour]:[minute]:[second] [offset_hour sign:mandatory][offset_minute]"
))
.expect("invalid date format");
let formatted_delta = format_delta_from_now(time);
format!("{formatted_time} ({formatted_delta})")
}
pub fn format_actor(author: IdentityRef<'_>) -> somehow::Result<String> {

View file

@ -14,6 +14,7 @@ pub enum Tab {
Queue,
}
#[derive(Clone)]
pub struct Base {
root: String,
repo_name: String,
@ -43,6 +44,7 @@ pub async fn run(state: AppState) -> somehow::Result<()> {
.route("/commit/", get(commit::get))
.route("/commit/:hash", get(commit_hash::get))
.route("/queue/", get(queue::get))
.route("/queue/table", get(queue::get_table))
.fallback(get(r#static::static_handler))
.with_state(state.clone());

View file

@ -101,9 +101,9 @@ pub async fn get(
base: Base::new(config, Tab::Commit),
hash: commit.hash,
author: commit.author,
author_date: util::format_time(commit.author_date)?,
author_date: util::format_time(commit.author_date),
commit: commit.committer,
commit_date: util::format_time(commit.committer_date)?,
commit_date: util::format_time(commit.committer_date),
parents,
children,
summary: util::format_commit_summary(&commit.message),

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 },
})
}