Port worker page

This commit is contained in:
Joscha 2023-08-13 22:53:37 +02:00
parent 36f275c290
commit 3bfae9c0ad
7 changed files with 121 additions and 63 deletions

View file

@ -1,2 +1,3 @@
pub mod commit;
pub mod run;
pub mod worker;

View file

@ -0,0 +1,86 @@
use std::sync::{Arc, Mutex};
use askama::Template;
use axum::{
extract::State,
http::StatusCode,
response::{IntoResponse, Response},
};
use sqlx::SqlitePool;
use crate::{
config::Config,
server::{
util,
web::{
base::{Base, Tab},
link::LinkRunShort,
paths::PathWorkerByName,
},
workers::Workers,
},
shared::WorkerStatus,
somehow,
};
enum Status {
Idle,
Busy,
Working { link: LinkRunShort, since: String },
}
#[derive(Template)]
#[template(path = "pages/worker.html")]
struct Page {
base: Base,
name: String,
connected: String,
status: Status,
}
async fn status(status: &WorkerStatus, db: &SqlitePool, base: &Base) -> somehow::Result<Status> {
Ok(match status {
WorkerStatus::Idle => Status::Idle,
WorkerStatus::Busy => Status::Busy,
WorkerStatus::Working(unfinished) => {
let message = sqlx::query_scalar!(
"SELECT message FROM commits WHERE hash = ?",
unfinished.run.hash
)
.fetch_one(db)
.await?;
Status::Working {
link: LinkRunShort::new(
base,
unfinished.run.id.clone(),
&unfinished.run.hash,
&message,
),
since: util::format_time(unfinished.run.start),
}
}
})
}
pub async fn get_worker_by_name(
path: PathWorkerByName,
State(config): State<&'static Config>,
State(db): State<SqlitePool>,
State(workers): State<Arc<Mutex<Workers>>>,
) -> somehow::Result<Response> {
let info = workers.lock().unwrap().clean().get(&path.name);
let Some(info) = info else {
return Ok(StatusCode::NOT_FOUND.into_response());
};
let base = Base::new(config, Tab::None);
Ok(Page {
name: path.name,
connected: util::format_time(info.first_seen),
status: status(&info.status, &db, &base).await?,
base,
}
.into_response())
}

View file

@ -1,46 +0,0 @@
use std::sync::{Arc, Mutex};
use askama::Template;
use axum::{
extract::State,
http::StatusCode,
response::{IntoResponse, Response},
};
use crate::{
config::Config,
server::{util, workers::Workers},
somehow,
};
use super::{
base::{Base, Tab},
paths::PathWorkerByName,
};
#[derive(Template)]
#[template(path = "worker.html")]
struct WorkerTemplate {
base: Base,
name: String,
last_seen: String,
// TODO Status
}
pub async fn get_worker_by_name(
path: PathWorkerByName,
State(config): State<&'static Config>,
State(workers): State<Arc<Mutex<Workers>>>,
) -> somehow::Result<Response> {
let info = workers.lock().unwrap().clean().get(&path.name);
let Some(info) = info else {
return Ok(StatusCode::NOT_FOUND.into_response());
};
Ok(WorkerTemplate {
base: Base::new(config, Tab::None),
name: path.name,
last_seen: util::format_time(info.last_seen),
}
.into_response())
}