Rename util module to format

This commit is contained in:
Joscha 2024-05-13 15:39:03 +02:00
parent a525e58211
commit bc8e2f46ab
9 changed files with 35 additions and 41 deletions

View file

@ -1,6 +1,6 @@
mod format;
mod git;
mod recurring;
mod util;
pub mod web;
mod workers;

View file

@ -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<String> {
pub fn actor(author: IdentityRef<'_>) -> somehow::Result<String> {
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::<Vec<_>>().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::<String>();
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 {

View file

@ -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();

View file

@ -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! {

View file

@ -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:" }

View file

@ -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);

View file

@ -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" }

View file

@ -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::<Vec<_>>()
@ -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) }

View file

@ -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 => {