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",
"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": {
"columns": [
{
@ -19,7 +19,7 @@
"type_info": "Int64"
},
{
"name": "date: time::OffsetDateTime",
"name": "date: Timestamp",
"ordinal": 3,
"type_info": "Text"
},
@ -40,5 +40,5 @@
false
]
},
"hash": "af399d06762680bbb967bd4665444a816f936e59e89b4042931de82054a2c067"
"hash": "8ddd3e152d28adc56c56a71701d56e957ba8ccbe33e8ac76f2515495c5ba2946"
}

View file

@ -1,6 +1,6 @@
{
"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": {
"columns": [
{
@ -9,7 +9,7 @@
"type_info": "Text"
},
{
"name": "start: time::OffsetDateTime",
"name": "start: Timestamp",
"ordinal": 1,
"type_info": "Text"
}
@ -22,5 +22,5 @@
false
]
},
"hash": "808b4cccd8740d62cf860782be780a54e5724c38ce415f852e36e2607e3266f2"
"hash": "8f018b9f20bc3e2eb58974932ed389cbd42d924a560ea0ed8b6d19cc18053ec8"
}

View file

@ -1,6 +1,6 @@
{
"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": {
"columns": [
{
@ -19,12 +19,12 @@
"type_info": "Text"
},
{
"name": "start: time::OffsetDateTime",
"name": "start: Timestamp",
"ordinal": 3,
"type_info": "Text"
},
{
"name": "end: time::OffsetDateTime",
"name": "end: Timestamp",
"ordinal": 4,
"type_info": "Text"
},
@ -58,5 +58,5 @@
false
]
},
"hash": "c1db3ac0f29ccf740eb4a798f00d6e2ecba3aa0239462e71dc8d23e405378ed0"
"hash": "c7c5494cf128d946c3548adb773a9b4eb6848e67574ab4c2c28d6d10abf6f25f"
}

View file

@ -1,6 +1,6 @@
{
"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": {
"columns": [
{
@ -14,7 +14,7 @@
"type_info": "Text"
},
{
"name": "author_date: time::OffsetDateTime",
"name": "author_date: Timestamp",
"ordinal": 2,
"type_info": "Text"
},
@ -24,7 +24,7 @@
"type_info": "Text"
},
{
"name": "committer_date: time::OffsetDateTime",
"name": "committer_date: Timestamp",
"ordinal": 4,
"type_info": "Text"
},
@ -52,5 +52,5 @@
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};
/// 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