Use Timestamp primitive

This commit is contained in:
Joscha 2024-05-13 16:22:07 +02:00
parent 7a6984aedc
commit 5e52c6f2be
13 changed files with 53 additions and 42 deletions

View file

@ -5,7 +5,7 @@ use serde_repr::{Deserialize_repr, Serialize_repr};
use time::{format_description::well_known::Rfc3339, OffsetDateTime};
/// The source of a line of output.
#[derive(Clone, Serialize_repr, Deserialize_repr, sqlx::Type)]
#[derive(Debug, Clone, Serialize_repr, Deserialize_repr, sqlx::Type)]
#[repr(u8)]
pub enum Source {
Internal = 0,
@ -14,7 +14,7 @@ pub enum Source {
}
/// The direction a measured value improves in.
#[derive(Clone, Serialize_repr, Deserialize_repr, sqlx::Type)]
#[derive(Debug, Clone, Serialize_repr, Deserialize_repr, sqlx::Type)]
#[repr(i8)]
pub enum Direction {
LessIsBetter = -1,
@ -32,10 +32,16 @@ pub enum Reachable {
}
/// A time stamp, usually formatted using RFC3339.
#[derive(Clone, Copy, sqlx::Type)]
#[derive(Debug, Clone, Copy, sqlx::Type)]
#[sqlx(transparent)]
pub struct Timestamp(pub OffsetDateTime);
impl Timestamp {
pub fn now() -> Self {
Self(OffsetDateTime::now_utc())
}
}
impl serde::Serialize for Timestamp {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where

View file

@ -3,7 +3,7 @@ use std::time::Duration;
use gix::actor::IdentityRef;
use time::{macros::format_description, OffsetDateTime};
use crate::somehow;
use crate::{primitive::Timestamp, somehow};
pub fn duration(duration: time::Duration) -> String {
let seconds = duration.unsigned_abs().as_secs(); // To nearest second
@ -11,9 +11,9 @@ pub fn duration(duration: time::Duration) -> String {
format!("{formatted}")
}
pub fn delta_from_now(time: OffsetDateTime) -> String {
pub fn delta_from_now(time: Timestamp) -> String {
let now = OffsetDateTime::now_utc();
let delta = time - now;
let delta = time.0 - now;
let seconds = delta.unsigned_abs().as_secs();
let seconds = seconds + 30 - (seconds + 30) % 60; // To nearest minute
if seconds == 0 {
@ -27,8 +27,9 @@ pub fn delta_from_now(time: OffsetDateTime) -> String {
}
}
pub fn time(time: OffsetDateTime) -> String {
pub fn time(time: Timestamp) -> String {
let formatted_time = time
.0
.format(format_description!(
"[year]-[month]-[day] [hour]:[minute]:[second] [offset_hour sign:mandatory][offset_minute]"
))

View file

@ -21,6 +21,7 @@ use time::OffsetDateTime;
use crate::{
config::ServerConfig,
primitive::Timestamp,
server::{
web::paths::{
PathApiWorkerBenchRepoByHashTreeTarGz, PathApiWorkerRepoByHashTreeTarGz,
@ -174,7 +175,7 @@ pub async fn post_api_worker_status(
}
guard.update(
name.clone(),
WorkerInfo::new(request.secret, OffsetDateTime::now_utc(), request.status),
WorkerInfo::new(request.secret, Timestamp::now(), request.status),
);
let work = match request.request_run {
true => guard.find_and_reserve_run(&name, &queue, bench_method),

View file

@ -1,7 +1,10 @@
use maud::{html, Markup};
use time::OffsetDateTime;
use crate::{config::ServerConfig, primitive::Reachable, server::format};
use crate::{
config::ServerConfig,
primitive::{Reachable, Timestamp},
server::format,
};
use super::{
paths::{PathCommitByHash, PathRunById, PathWorkerByName},
@ -60,7 +63,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 {
pub fn link_run_date(config: &ServerConfig, id: String, start: Timestamp) -> Markup {
let start = format::time(start);
let path = config.path(PathRunById { id });

View file

@ -9,7 +9,7 @@ use sqlx::SqlitePool;
use crate::{
config::ServerConfig,
primitive::Reachable,
primitive::{Reachable, Timestamp},
server::{
format,
web::{
@ -32,9 +32,9 @@ pub async fn get_commit_by_hash(
SELECT
hash,
author,
author_date AS "author_date: time::OffsetDateTime",
author_date AS "author_date: Timestamp",
committer,
committer_date AS "committer_date: time::OffsetDateTime",
committer_date AS "committer_date: Timestamp",
message,
reachable AS "reachable: Reachable"
FROM commits
@ -88,7 +88,7 @@ pub async fn get_commit_by_hash(
r#"
SELECT
id,
start AS "start: time::OffsetDateTime"
start AS "start: Timestamp"
FROM runs WHERE hash = ?
"#,
path.hash,

View file

@ -14,7 +14,7 @@ use sqlx::SqlitePool;
use crate::{
config::ServerConfig,
primitive::Reachable,
primitive::{Reachable, Timestamp},
server::{
format,
web::{
@ -122,7 +122,7 @@ async fn get_queue_data(
hash,
message,
reachable AS "reachable: Reachable",
date AS "date: time::OffsetDateTime",
date AS "date: Timestamp",
priority
FROM queue
JOIN commits USING (hash)

View file

@ -9,7 +9,7 @@ use sqlx::SqlitePool;
use crate::{
config::ServerConfig,
primitive::Reachable,
primitive::{Reachable, Timestamp},
server::{
format,
web::{components, page::Page, paths::PathRunById},
@ -39,8 +39,8 @@ async fn from_finished_run(
id,
hash,
bench_method,
start AS "start: time::OffsetDateTime",
end AS "end: time::OffsetDateTime",
start AS "start: Timestamp",
end AS "end: Timestamp",
exit_code,
message,
reachable AS "reachable: Reachable"
@ -115,7 +115,7 @@ async fn from_finished_run(
dd { (format::time(run.end)) }
dt { "Duration:" }
dd { (format::duration(run.end - run.start)) }
dd { (format::duration(run.end.0 - run.start.0)) }
dt { "Exit code:" }
dd { (run.exit_code) }

View file

@ -40,7 +40,7 @@ async fn status(
.await?;
Status::Working {
link: components::link_run_short(config, run.id.clone(), &run.hash, &message),
since: format::time(run.start.0),
since: format::time(run.start),
}
}
})

View file

@ -12,16 +12,16 @@ use crate::{
#[derive(Clone)]
pub struct WorkerInfo {
pub secret: String,
pub first_seen: OffsetDateTime,
pub last_seen: OffsetDateTime,
pub first_seen: Timestamp,
pub last_seen: Timestamp,
pub status: WorkerStatus,
}
impl WorkerInfo {
pub fn new(secret: String, last_seen: OffsetDateTime, status: WorkerStatus) -> Self {
pub fn new(secret: String, last_seen: Timestamp, status: WorkerStatus) -> Self {
Self {
secret,
first_seen: OffsetDateTime::now_utc(),
first_seen: Timestamp::now(),
last_seen,
status,
}
@ -44,7 +44,7 @@ impl Workers {
pub fn clean(&mut self) -> &mut Self {
let now = OffsetDateTime::now_utc();
self.workers
.retain(|_, v| now <= v.last_seen + self.config.worker_timeout);
.retain(|_, v| now <= v.last_seen.0 + self.config.worker_timeout);
self
}
@ -85,7 +85,7 @@ impl Workers {
id,
hash,
bench_method,
start: Timestamp(OffsetDateTime::now_utc()),
start: Timestamp::now(),
};
// Reserve work so other workers don't choose it