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

@ -1,6 +1,6 @@
{ {
"db_name": "SQLite", "db_name": "SQLite",
"query": "\n SELECT\n hash,\n message,\n reachable AS \"reachable: Reachable\",\n date AS \"date: time::OffsetDateTime\",\n priority\n FROM queue\n JOIN commits USING (hash)\n ORDER BY priority DESC, unixepoch(date) DESC, hash ASC\n ", "query": "\n SELECT\n hash,\n message,\n reachable AS \"reachable: Reachable\",\n date AS \"date: Timestamp\",\n priority\n FROM queue\n JOIN commits USING (hash)\n ORDER BY priority DESC, unixepoch(date) DESC, hash ASC\n ",
"describe": { "describe": {
"columns": [ "columns": [
{ {
@ -19,7 +19,7 @@
"type_info": "Int64" "type_info": "Int64"
}, },
{ {
"name": "date: time::OffsetDateTime", "name": "date: Timestamp",
"ordinal": 3, "ordinal": 3,
"type_info": "Text" "type_info": "Text"
}, },
@ -40,5 +40,5 @@
false false
] ]
}, },
"hash": "af399d06762680bbb967bd4665444a816f936e59e89b4042931de82054a2c067" "hash": "8ddd3e152d28adc56c56a71701d56e957ba8ccbe33e8ac76f2515495c5ba2946"
} }

View file

@ -1,6 +1,6 @@
{ {
"db_name": "SQLite", "db_name": "SQLite",
"query": "\n SELECT\n id,\n start AS \"start: time::OffsetDateTime\"\n FROM runs WHERE hash = ?\n ", "query": "\n SELECT\n id,\n start AS \"start: Timestamp\"\n FROM runs WHERE hash = ?\n ",
"describe": { "describe": {
"columns": [ "columns": [
{ {
@ -9,7 +9,7 @@
"type_info": "Text" "type_info": "Text"
}, },
{ {
"name": "start: time::OffsetDateTime", "name": "start: Timestamp",
"ordinal": 1, "ordinal": 1,
"type_info": "Text" "type_info": "Text"
} }
@ -22,5 +22,5 @@
false false
] ]
}, },
"hash": "808b4cccd8740d62cf860782be780a54e5724c38ce415f852e36e2607e3266f2" "hash": "8f018b9f20bc3e2eb58974932ed389cbd42d924a560ea0ed8b6d19cc18053ec8"
} }

View file

@ -1,6 +1,6 @@
{ {
"db_name": "SQLite", "db_name": "SQLite",
"query": "\n SELECT\n id,\n hash,\n bench_method,\n start AS \"start: time::OffsetDateTime\",\n end AS \"end: time::OffsetDateTime\",\n exit_code,\n message,\n reachable AS \"reachable: Reachable\"\n FROM runs\n JOIN commits USING (hash)\n WHERE id = ?\n ", "query": "\n SELECT\n id,\n hash,\n bench_method,\n start AS \"start: Timestamp\",\n end AS \"end: Timestamp\",\n exit_code,\n message,\n reachable AS \"reachable: Reachable\"\n FROM runs\n JOIN commits USING (hash)\n WHERE id = ?\n ",
"describe": { "describe": {
"columns": [ "columns": [
{ {
@ -19,12 +19,12 @@
"type_info": "Text" "type_info": "Text"
}, },
{ {
"name": "start: time::OffsetDateTime", "name": "start: Timestamp",
"ordinal": 3, "ordinal": 3,
"type_info": "Text" "type_info": "Text"
}, },
{ {
"name": "end: time::OffsetDateTime", "name": "end: Timestamp",
"ordinal": 4, "ordinal": 4,
"type_info": "Text" "type_info": "Text"
}, },
@ -58,5 +58,5 @@
false false
] ]
}, },
"hash": "c1db3ac0f29ccf740eb4a798f00d6e2ecba3aa0239462e71dc8d23e405378ed0" "hash": "c7c5494cf128d946c3548adb773a9b4eb6848e67574ab4c2c28d6d10abf6f25f"
} }

View file

@ -1,6 +1,6 @@
{ {
"db_name": "SQLite", "db_name": "SQLite",
"query": "\n SELECT\n hash,\n author,\n author_date AS \"author_date: time::OffsetDateTime\",\n committer,\n committer_date AS \"committer_date: time::OffsetDateTime\",\n message,\n reachable AS \"reachable: Reachable\"\n FROM commits\n WHERE hash = ?\n ", "query": "\n SELECT\n hash,\n author,\n author_date AS \"author_date: Timestamp\",\n committer,\n committer_date AS \"committer_date: Timestamp\",\n message,\n reachable AS \"reachable: Reachable\"\n FROM commits\n WHERE hash = ?\n ",
"describe": { "describe": {
"columns": [ "columns": [
{ {
@ -14,7 +14,7 @@
"type_info": "Text" "type_info": "Text"
}, },
{ {
"name": "author_date: time::OffsetDateTime", "name": "author_date: Timestamp",
"ordinal": 2, "ordinal": 2,
"type_info": "Text" "type_info": "Text"
}, },
@ -24,7 +24,7 @@
"type_info": "Text" "type_info": "Text"
}, },
{ {
"name": "committer_date: time::OffsetDateTime", "name": "committer_date: Timestamp",
"ordinal": 4, "ordinal": 4,
"type_info": "Text" "type_info": "Text"
}, },
@ -52,5 +52,5 @@
false false
] ]
}, },
"hash": "9ffcba9ec91ee686072b0c06b59d6bbcb59e36c51141ca5ea57ea69c21ba119b" "hash": "dc3242efee0a20dc80d5d91bdec7595bfc93f7b80ed03b64b69f7999cb11ac33"
} }

View file

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

View file

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

View file

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

View file

@ -1,7 +1,10 @@
use maud::{html, Markup}; 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::{ use super::{
paths::{PathCommitByHash, PathRunById, PathWorkerByName}, 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. /// 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 start = format::time(start);
let path = config.path(PathRunById { id }); let path = config.path(PathRunById { id });

View file

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

View file

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

View file

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

View file

@ -40,7 +40,7 @@ async fn status(
.await?; .await?;
Status::Working { Status::Working {
link: components::link_run_short(config, run.id.clone(), &run.hash, &message), 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)] #[derive(Clone)]
pub struct WorkerInfo { pub struct WorkerInfo {
pub secret: String, pub secret: String,
pub first_seen: OffsetDateTime, pub first_seen: Timestamp,
pub last_seen: OffsetDateTime, pub last_seen: Timestamp,
pub status: WorkerStatus, pub status: WorkerStatus,
} }
impl WorkerInfo { 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 { Self {
secret, secret,
first_seen: OffsetDateTime::now_utc(), first_seen: Timestamp::now(),
last_seen, last_seen,
status, status,
} }
@ -44,7 +44,7 @@ impl Workers {
pub fn clean(&mut self) -> &mut Self { pub fn clean(&mut self) -> &mut Self {
let now = OffsetDateTime::now_utc(); let now = OffsetDateTime::now_utc();
self.workers self.workers
.retain(|_, v| now <= v.last_seen + self.config.worker_timeout); .retain(|_, v| now <= v.last_seen.0 + self.config.worker_timeout);
self self
} }
@ -85,7 +85,7 @@ impl Workers {
id, id,
hash, hash,
bench_method, bench_method,
start: Timestamp(OffsetDateTime::now_utc()), start: Timestamp::now(),
}; };
// Reserve work so other workers don't choose it // Reserve work so other workers don't choose it