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

@ -7,7 +7,6 @@ mod pages;
pub mod paths;
mod queue;
mod r#static;
mod worker;
use axum::{routing::get, Router};
use axum_extra::routing::RouterExt;
@ -21,9 +20,8 @@ use self::{
post_api_worker_status,
},
index::get_index,
pages::{commit::get_commit_by_hash, run::get_run_by_id},
pages::{commit::get_commit_by_hash, run::get_run_by_id, worker::get_worker_by_name},
queue::{get_queue, get_queue_inner},
worker::get_worker_by_name,
};
use super::Server;

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

View file

@ -11,6 +11,7 @@ use crate::{
#[derive(Clone)]
pub struct WorkerInfo {
pub secret: String,
pub first_seen: OffsetDateTime,
pub last_seen: OffsetDateTime,
pub status: WorkerStatus,
}
@ -19,6 +20,7 @@ impl WorkerInfo {
pub fn new(secret: String, last_seen: OffsetDateTime, status: WorkerStatus) -> Self {
Self {
secret,
first_seen: OffsetDateTime::now_utc(),
last_seen,
status,
}

View file

@ -0,0 +1,31 @@
{% extends "base.html" %}
{% block title %}{{ name }}{% endblock %}
{% block body %}
<h2>Worker</h2>
<div>
<span>worker {{ name }}</span>
<dl>
<dt>Connected:</dt>
<dd>{{ connected }}</dd>
{% match status %}
{% when Status::Idle %}
<dt>Working on:</dt>
<dd>nothing</dd>
{% when Status::Busy %}
<dt>Working on:</dt>
<dd>run for another server</dd>
{% when Status::Working with { link, since } %}
<dt>Working on:</dt>
<dd>{{ link|safe }}</dd>
<dt>Working since:</dt>
<dd>{{ since }}</dd>
{% endmatch %}
</dl>
</div>
{% endblock %}

View file

@ -1,14 +0,0 @@
{% extends "base.html" %}
{% block title %}{{ name }}{% endblock %}
{% block body %}
<h2>Worker</h2>
<div class="worker">
<span class="name">worker {{ name }}</span>
<dl>
<dt>Last seen:</dt>
<dd>{{ last_seen }}</dd>
</dl>
</div>
{% endblock %}