From 215dbf3d6bf7f46dfa81ee89def90d899eed44ce Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Mon, 9 Dec 2024 10:34:22 +0200 Subject: [PATCH] feat: log start time and duration in run log Signed-off-by: Lars Wirzenius --- src/bin/run_log.rs | 7 ++++++- src/runlog.rs | 29 +++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/src/bin/run_log.rs b/src/bin/run_log.rs index 18c4534..f103d8d 100644 --- a/src/bin/run_log.rs +++ b/src/bin/run_log.rs @@ -1,4 +1,7 @@ -use std::{path::Path, time::SystemTime}; +use std::{ + path::Path, + time::{Duration, SystemTime}, +}; use radicle_ci_broker::msg::{Oid, RepoId, RunId}; @@ -7,6 +10,8 @@ use radicle_native_ci::{runlog::RunLog, runspec::RunSpec}; /// The main program. fn main() { let mut run_log = RunLog::new(Path::new("testlog.html")); + run_log.started(SystemTime::now()); + run_log.duration(Duration::from_millis(500)); run_log.adapter_run_id(RunId::from("plugh")); run_log.title("Some Title"); run_log.rid( diff --git a/src/runlog.rs b/src/runlog.rs index 77a9da9..5e2d65b 100644 --- a/src/runlog.rs +++ b/src/runlog.rs @@ -29,6 +29,8 @@ pub struct RunLog { runspec: Option, runspec_error: Option, adapter_run_id: Option, + started: Option, + duration: Option, } impl RunLog { @@ -80,6 +82,14 @@ impl RunLog { self.runspec_error = Some(format!("{}", e)); } + pub fn started(&mut self, ts: SystemTime) { + self.started = Some(ts); + } + + pub fn duration(&mut self, dur: Duration) { + self.duration = Some(dur); + } + // FIXME: refactor this to maybe use a builder pattern? #[allow(clippy::too_many_arguments)] pub fn runcmd( @@ -155,6 +165,25 @@ impl RunLog { } else { Element::new(Tag::Span) }) + .with_child({ + let ts = if let Some(ts) = &self.started { + Element::new(Tag::Span).with_text(×tamp(ts)) + } else { + Element::new(Tag::Span) + }; + Element::new(Tag::Li).with_text("Started: ").with_child(ts) + }) + .with_child({ + let dur = if let Some(dur) = &self.duration { + Element::new(Tag::Span).with_text(&format!("{:1}", dur.as_secs_f64())) + } else { + Element::new(Tag::Span) + }; + Element::new(Tag::Li) + .with_text("Duration: ") + .with_child(dur) + .with_text(" s") + }) .with_child( Element::new(Tag::Li) .with_text("Result: ")