From dd7496fa70d751ef61ca695d64a1a63438425322 Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Sun, 31 Mar 2024 17:02:35 +0300 Subject: [PATCH] add timestamps for every commancd run Signed-off-by: Lars Wirzenius --- src/bin/run_log.rs | 8 +++++++- src/run.rs | 5 +++++ src/runlog.rs | 45 +++++++++++++++++++++++++++++++++++++++++++-- 3 files changed, 55 insertions(+), 3 deletions(-) diff --git a/src/bin/run_log.rs b/src/bin/run_log.rs index d830280..e9a6a4a 100644 --- a/src/bin/run_log.rs +++ b/src/bin/run_log.rs @@ -1,4 +1,4 @@ -use std::path::Path; +use std::{path::Path, time::SystemTime}; use radicle_ci_broker::msg::{Oid, RepoId}; @@ -21,12 +21,16 @@ fn main() { run_log.runspec(RunSpec { shell: "echo hello, world".into(), }); + let started = SystemTime::now(); + let ended = SystemTime::now(); run_log.runcmd( &["git", "pull"], Path::new("/tmp"), 0, "This is stdout".as_bytes(), "Error messages go here".as_bytes(), + started, + ended, ); run_log.runcmd( &["cargo", "check", "--all-targets", "--workspace"], @@ -34,6 +38,8 @@ fn main() { 0, "This is stdout".as_bytes(), "Error messages go here".as_bytes(), + started, + ended, ); run_log.write().expect("write html log"); diff --git a/src/run.rs b/src/run.rs index 7b0df71..035a45d 100644 --- a/src/run.rs +++ b/src/run.rs @@ -1,6 +1,7 @@ use std::{ path::{Path, PathBuf}, process::Command, + time::SystemTime, }; use radicle_ci_broker::msg::{Oid, RepoId}; @@ -196,12 +197,14 @@ impl Run { return Err(RunError::EmptyArgv); } + let started = SystemTime::now(); let argv0 = argv[0]; let output = Command::new(argv0) .args(&argv[1..]) .current_dir(cwd) .output() .map_err(|e| RunError::Command(argv.iter().map(|s| s.to_string()).collect(), e))?; + let ended = SystemTime::now(); let exit = output.status.code().unwrap_or(NO_EXIT); self.run_log.runcmd( @@ -211,6 +214,8 @@ impl Run { exit, &output.stdout, &output.stderr, + started, + ended, ); Ok(exit) } diff --git a/src/runlog.rs b/src/runlog.rs index 91bda6d..bd01c28 100644 --- a/src/runlog.rs +++ b/src/runlog.rs @@ -1,6 +1,10 @@ -use std::path::{Path, PathBuf}; +use std::{ + path::{Path, PathBuf}, + time::{Duration, SystemTime}, +}; use html_page::{Document, Element, Tag}; +use time::{macros::format_description, OffsetDateTime}; use radicle_ci_broker::msg::{Oid, RepoId}; @@ -65,8 +69,22 @@ impl RunLog { self.runspec_error = Some(format!("{}", e)); } - pub fn runcmd(&mut self, argv: &[&str], cwd: &Path, exit: i32, stdout: &[u8], stderr: &[u8]) { + // FIXME: refactor this to maybe use a builder pattern? + #[allow(clippy::too_many_arguments)] + pub fn runcmd( + &mut self, + argv: &[&str], + cwd: &Path, + exit: i32, + stdout: &[u8], + stderr: &[u8], + started: SystemTime, + ended: SystemTime, + ) { self.commands.push(Command { + started, + ended, + duration: ended.duration_since(started).expect("compute duration"), argv: argv.iter().map(|a| a.to_string()).collect(), cwd: cwd.into(), exit, @@ -182,6 +200,9 @@ impl RunLog { #[derive(Debug)] struct Command { + started: SystemTime, + ended: SystemTime, + duration: Duration, argv: Vec, cwd: PathBuf, exit: i32, @@ -203,6 +224,21 @@ impl Command { ), ); + body.push_child( + &Element::new(Tag::Ul) + .with_child( + Element::new(Tag::Li) + .with_text(&format!("Started: {}", timestamp(&self.started))), + ) + .with_child( + Element::new(Tag::Li).with_text(&format!("Ended: {}", timestamp(&self.ended))), + ) + .with_child( + Element::new(Tag::Li) + .with_text(&format!("Duration: {} s", self.duration.as_secs())), + ), + ); + body.push_child(&Element::new(Tag::P).with_text("Command arguments:")); let mut ul = Element::new(Tag::Ul); for arg in self.argv.iter() { @@ -236,6 +272,11 @@ impl Command { } } +fn timestamp(when: &SystemTime) -> String { + let fmt = format_description!("[year]-[month]-[day] [hour]:[minute]:[second]Z"); + OffsetDateTime::from(*when).format(fmt).ok().unwrap() +} + #[derive(Debug, Default)] struct ToC { counter: usize,