Port worker page
This commit is contained in:
parent
36f275c290
commit
3bfae9c0ad
7 changed files with 121 additions and 63 deletions
|
|
@ -7,7 +7,6 @@ mod pages;
|
||||||
pub mod paths;
|
pub mod paths;
|
||||||
mod queue;
|
mod queue;
|
||||||
mod r#static;
|
mod r#static;
|
||||||
mod worker;
|
|
||||||
|
|
||||||
use axum::{routing::get, Router};
|
use axum::{routing::get, Router};
|
||||||
use axum_extra::routing::RouterExt;
|
use axum_extra::routing::RouterExt;
|
||||||
|
|
@ -21,9 +20,8 @@ use self::{
|
||||||
post_api_worker_status,
|
post_api_worker_status,
|
||||||
},
|
},
|
||||||
index::get_index,
|
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},
|
queue::{get_queue, get_queue_inner},
|
||||||
worker::get_worker_by_name,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
use super::Server;
|
use super::Server;
|
||||||
|
|
|
||||||
|
|
@ -1,2 +1,3 @@
|
||||||
pub mod commit;
|
pub mod commit;
|
||||||
pub mod run;
|
pub mod run;
|
||||||
|
pub mod worker;
|
||||||
|
|
|
||||||
86
src/server/web/pages/worker.rs
Normal file
86
src/server/web/pages/worker.rs
Normal 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())
|
||||||
|
}
|
||||||
|
|
@ -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())
|
|
||||||
}
|
|
||||||
|
|
@ -11,6 +11,7 @@ use crate::{
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct WorkerInfo {
|
pub struct WorkerInfo {
|
||||||
pub secret: String,
|
pub secret: String,
|
||||||
|
pub first_seen: OffsetDateTime,
|
||||||
pub last_seen: OffsetDateTime,
|
pub last_seen: OffsetDateTime,
|
||||||
pub status: WorkerStatus,
|
pub status: WorkerStatus,
|
||||||
}
|
}
|
||||||
|
|
@ -19,6 +20,7 @@ impl WorkerInfo {
|
||||||
pub fn new(secret: String, last_seen: OffsetDateTime, status: WorkerStatus) -> Self {
|
pub fn new(secret: String, last_seen: OffsetDateTime, status: WorkerStatus) -> Self {
|
||||||
Self {
|
Self {
|
||||||
secret,
|
secret,
|
||||||
|
first_seen: OffsetDateTime::now_utc(),
|
||||||
last_seen,
|
last_seen,
|
||||||
status,
|
status,
|
||||||
}
|
}
|
||||||
|
|
|
||||||
31
templates/pages/worker.html
Normal file
31
templates/pages/worker.html
Normal 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 %}
|
||||||
|
|
@ -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 %}
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue