diff --git a/src/server.rs b/src/server.rs index 6fd48c5..2838556 100644 --- a/src/server.rs +++ b/src/server.rs @@ -1,6 +1,6 @@ +mod format; mod git; mod recurring; -mod util; pub mod web; mod workers; diff --git a/src/server/util.rs b/src/server/format.rs similarity index 79% rename from src/server/util.rs rename to src/server/format.rs index cfcc10f..907dc96 100644 --- a/src/server/util.rs +++ b/src/server/format.rs @@ -5,13 +5,13 @@ use time::{macros::format_description, OffsetDateTime}; use crate::somehow; -pub fn format_duration(duration: time::Duration) -> String { +pub fn duration(duration: time::Duration) -> String { let seconds = duration.unsigned_abs().as_secs(); // To nearest second let formatted = humantime::format_duration(Duration::from_secs(seconds)); format!("{formatted}") } -pub fn format_delta_from_now(time: OffsetDateTime) -> String { +pub fn delta_from_now(time: OffsetDateTime) -> String { let now = OffsetDateTime::now_utc(); let delta = time - now; let seconds = delta.unsigned_abs().as_secs(); @@ -27,24 +27,24 @@ pub fn format_delta_from_now(time: OffsetDateTime) -> String { } } -pub fn format_time(time: OffsetDateTime) -> String { +pub fn 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); + let formatted_delta = delta_from_now(time); format!("{formatted_time} ({formatted_delta})") } -pub fn format_actor(author: IdentityRef<'_>) -> somehow::Result { +pub fn actor(author: IdentityRef<'_>) -> somehow::Result { let mut buffer = vec![]; author.trim().write_to(&mut buffer)?; Ok(String::from_utf8_lossy(&buffer).to_string()) } -pub fn format_commit_summary(message: &str) -> String { +pub fn commit_summary(message: &str) -> String { // Take everything up to the first double newline let title = message .split_once("\n\n") @@ -55,13 +55,13 @@ pub fn format_commit_summary(message: &str) -> String { title.split_whitespace().collect::>().join(" ") } -pub fn format_commit_short(hash: &str, message: &str) -> String { +pub fn commit_short(hash: &str, message: &str) -> String { let short_hash = hash.chars().take(8).collect::(); - let summary = format_commit_summary(message); + let summary = commit_summary(message); format!("{short_hash} ({summary})") } -pub fn format_value(value: f64) -> String { +pub fn measurement_value(value: f64) -> String { if value.abs() >= 1e6 { format!("{value:.3e}") } else if value.fract() == 0.0 { diff --git a/src/server/recurring/repo.rs b/src/server/recurring/repo.rs index 5f1d780..62cb60c 100644 --- a/src/server/recurring/repo.rs +++ b/src/server/recurring/repo.rs @@ -9,7 +9,7 @@ use sqlx::{Acquire, SqliteConnection, SqlitePool}; use time::{OffsetDateTime, UtcOffset}; use crate::{ - server::{util, Repo}, + server::{format, Repo}, somehow, }; @@ -82,10 +82,10 @@ async fn insert_new_commits( let commit = id.attach(repo).object()?.try_into_commit()?; let hash = commit.id.to_string(); let author_info = commit.author()?; - let author = util::format_actor(author_info.actor())?; + let author = format::actor(author_info.actor())?; let author_date = time_to_offset_datetime(author_info.time)?; let committer_info = commit.committer()?; - let committer = util::format_actor(committer_info.actor())?; + let committer = format::actor(committer_info.actor())?; let committer_date = time_to_offset_datetime(committer_info.time)?; let message = commit.message_raw()?.to_string(); diff --git a/src/server/web/components.rs b/src/server/web/components.rs index 33268ca..b665148 100644 --- a/src/server/web/components.rs +++ b/src/server/web/components.rs @@ -1,7 +1,7 @@ use maud::{html, Markup}; use time::OffsetDateTime; -use crate::{config::ServerConfig, server::util}; +use crate::{config::ServerConfig, server::format}; use super::{ paths::{PathCommitByHash, PathRunById, PathWorkerByName}, @@ -37,7 +37,7 @@ pub fn commit_class_and_title(reachable: i64) -> (&'static str, &'static str) { } pub fn link_commit(config: &ServerConfig, hash: String, message: &str, reachable: i64) -> Markup { - let short = util::truncate(&util::format_commit_short(&hash, message), 80); + let short = format::truncate(&format::commit_short(&hash, message), 80); let path = config.path(PathCommitByHash { hash }); let (class, title) = commit_class_and_title(reachable); @@ -48,7 +48,7 @@ pub fn link_commit(config: &ServerConfig, hash: String, message: &str, reachable /// Link to a run by its commit's short message. pub fn link_run_short(config: &ServerConfig, id: String, hash: &str, message: &str) -> Markup { - let short = util::truncate(&util::format_commit_short(hash, message), 80); + let short = format::truncate(&format::commit_short(hash, message), 80); let path = config.path(PathRunById { id }); html! { @@ -58,7 +58,7 @@ pub fn link_run_short(config: &ServerConfig, id: String, hash: &str, message: &s /// Link to a run by its start time. pub fn link_run_date(config: &ServerConfig, id: String, start: OffsetDateTime) -> Markup { - let start = util::format_time(start); + let start = format::time(start); let path = config.path(PathRunById { id }); html! { diff --git a/src/server/web/pages/commit.rs b/src/server/web/pages/commit.rs index 306b468..89266a9 100644 --- a/src/server/web/pages/commit.rs +++ b/src/server/web/pages/commit.rs @@ -10,7 +10,7 @@ use sqlx::SqlitePool; use crate::{ config::ServerConfig, server::{ - util, + format, web::{ components, page::Page, @@ -92,7 +92,7 @@ pub async fn get_commit_by_hash( let (class, title) = components::commit_class_and_title(commit.reachable); let html = Page::new(config) - .title(util::format_commit_summary(&commit.message)) + .title(format::commit_summary(&commit.message)) .body(html! { h2 { "Commit" } div .commit-like .commit { @@ -102,13 +102,13 @@ pub async fn get_commit_by_hash( dd { (commit.author) } dt { "AuthorDate:" } - dd { (util::format_time(commit.author_date)) } + dd { (format::time(commit.author_date)) } dt { "Commit:" } dd { (commit.committer) } dt { "CommitDate:" } - dd { (util::format_time(commit.committer_date)) } + dd { (format::time(commit.committer_date)) } @for commit in parents { dt { "Parent:" } diff --git a/src/server/web/pages/graph.rs b/src/server/web/pages/graph.rs index c391345..c69c361 100644 --- a/src/server/web/pages/graph.rs +++ b/src/server/web/pages/graph.rs @@ -11,7 +11,7 @@ use time::OffsetDateTime; use crate::{ config::ServerConfig, server::{ - util, + format, web::{ page::{Page, Tab}, paths::{PathGraph, PathGraphCommits, PathGraphMeasurements, PathGraphMetrics}, @@ -108,7 +108,7 @@ pub async fn get_graph_commits( hash_by_hash.push(row.hash); author_by_hash.push(row.author); committer_date_by_hash.push(row.committer_date.unix_timestamp()); - summary_by_hash.push(util::format_commit_summary(&row.message)); + summary_by_hash.push(format::commit_summary(&row.message)); } drop(rows); diff --git a/src/server/web/pages/queue.rs b/src/server/web/pages/queue.rs index 8584855..a0335bf 100644 --- a/src/server/web/pages/queue.rs +++ b/src/server/web/pages/queue.rs @@ -15,7 +15,7 @@ use sqlx::SqlitePool; use crate::{ config::ServerConfig, server::{ - util, + format, web::{ components, page::{Page, Tab}, @@ -138,7 +138,7 @@ async fn get_queue_data( link_decrease: config.path(PathAdminQueueDecrease {}), hash: r.hash.clone(), commit: components::link_commit(config, r.hash, &r.message, r.reachable), - since: util::format_delta_from_now(r.date), + since: format::delta_from_now(r.date), priority: r.priority, odd: false, }) @@ -291,10 +291,7 @@ pub async fn get_queue_delete( let commit = components::link_commit(config, r.hash.clone(), &r.message, r.reachable); let html = Page::new(config) - .title(format!( - "del {}", - util::format_commit_short(&r.hash, &r.message) - )) + .title(format!("del {}", format::commit_short(&r.hash, &r.message))) .tab(Tab::Queue) .body(html! { h2 { "Delete commit from queue" } diff --git a/src/server/web/pages/run.rs b/src/server/web/pages/run.rs index 5d700ac..cae82b0 100644 --- a/src/server/web/pages/run.rs +++ b/src/server/web/pages/run.rs @@ -10,7 +10,7 @@ use sqlx::SqlitePool; use crate::{ config::ServerConfig, server::{ - util, + format, web::{components, page::Page, paths::PathRunById}, }, somehow, @@ -70,7 +70,7 @@ async fn from_finished_run( .fetch(db) .map_ok(|r| Measurement { metric: r.metric, - value: util::format_value(r.value), + value: format::measurement_value(r.value), unit: r.unit.unwrap_or_default(), }) .try_collect::>() @@ -95,10 +95,7 @@ async fn from_finished_run( let commit = components::link_commit(config, run.hash, &run.message, run.reachable); let html = Page::new(config) - .title(format!( - "Run of {}", - util::format_commit_summary(&run.message) - )) + .title(format!("Run of {}", format::commit_summary(&run.message))) .body(html! { h2 { "Run" } div .commit-like .run { @@ -111,13 +108,13 @@ async fn from_finished_run( dd { (run.bench_method) } dt { "Start:" } - dd { (util::format_time(run.start)) } + dd { (format::time(run.start)) } dt { "End:" } - dd { (util::format_time(run.end)) } + dd { (format::time(run.end)) } dt { "Duration:" } - dd { (util::format_duration(run.end - run.start)) } + dd { (format::duration(run.end - run.start)) } dt { "Exit code:" } dd { (run.exit_code) } diff --git a/src/server/web/pages/worker.rs b/src/server/web/pages/worker.rs index 033d844..ff642a8 100644 --- a/src/server/web/pages/worker.rs +++ b/src/server/web/pages/worker.rs @@ -11,7 +11,7 @@ use sqlx::SqlitePool; use crate::{ config::ServerConfig, server::{ - util, + format, web::{components, page::Page, paths::PathWorkerByName}, workers::Workers, }, @@ -40,7 +40,7 @@ async fn status( .await?; Status::Working { link: components::link_run_short(config, run.id.clone(), &run.hash, &message), - since: util::format_time(run.start.0), + since: format::time(run.start.0), } } }) @@ -67,7 +67,7 @@ pub async fn get_worker_by_name( span .title { "worker " (path.name) } dl { dt { "Connected:" } - dd { (util::format_time(info.first_seen)) } + dd { (format::time(info.first_seen)) } @match status { Status::Idle => {