Skip to content

Commit

Permalink
feat: CLI now shows exact time for detail conf setting
Browse files Browse the repository at this point in the history
Previously the CLI would only show an approximate, friendly time for a
certain context. This has been enhanced now to work with the command line
setting `detail`. Enabling this setting will now cause Gofer to print
exact timestamps in all areas where previous approximations were given.

Additionally, this includes a minor refactor for the function that is
used to glean this time.
  • Loading branch information
clintjedwards committed Dec 30, 2024
1 parent 04ad3e1 commit b132306
Show file tree
Hide file tree
Showing 15 changed files with 144 additions and 83 deletions.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ Gofer is setup such that the base run mode is the development mode. So simply ru
without any additional flags allows easy auth-less development. You can read more about how to deploy Gofer in a
production environment [here](https://clintjedwards.com/gofer/ref/server_configuration/index.html)

This is really helpful for users and developers alike since it allows easy access to a runnable server to test pipelines
against.

### You'll need to install the following first:

To run Gofer dev mode:
Expand All @@ -86,6 +89,14 @@ export GOFER_WEB_API__LOG_LEVEL=debug
cargo run --bin gofer -- service start
```

### Env aware configuration

To avoid issues when developing Gofer, the development build of Gofer(`any binary that was not built with --release`)
looks for the CLI config file at `.gofer_dev.toml` instead of `.gofer.toml`.

This avoids the headache of having to swap configuration files while actively developing Gofer. But is noted here since
it can be confusing if not known.

### Editing OpenAPI spec files

#### Where are the openapi spec files?
Expand Down
6 changes: 4 additions & 2 deletions gofer/src/cli/event/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::cli::{humanize_relative_duration, Cli};
use crate::cli::Cli;
use anyhow::{anyhow, bail, Context, Result};
use chrono::TimeZone;
use clap::{Args, Subcommand};
Expand Down Expand Up @@ -135,7 +135,9 @@ impl Cli {
let mut context = tera::Context::new();
context.insert(
"emitted",
&humanize_relative_duration(event.emitted).unwrap_or_else(|| "Unknown".to_string()),
&self
.format_time(event.emitted)
.unwrap_or_else(|| "Unknown".to_string()),
);
context.insert("id", &event.id);
context.insert("kind", &format!("{:#?}", event.kind));
Expand Down
8 changes: 4 additions & 4 deletions gofer/src/cli/extension/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
use crate::cli::{
colorize_status_text, colorize_status_text_comfy, humanize_relative_duration, Cli,
};
use crate::cli::{colorize_status_text, colorize_status_text_comfy, Cli};
use anyhow::{anyhow, bail, Context, Result};
use clap::{Args, Subcommand};
use colored::Colorize;
Expand Down Expand Up @@ -198,7 +196,9 @@ impl Cli {
let mut context = tera::Context::new();
context.insert(
"started",
&humanize_relative_duration(extension.started).unwrap_or_else(|| "Not yet".to_string()),
&self
.format_time(extension.started)
.unwrap_or_else(|| "Not yet".to_string()),
);
context.insert("url", &extension.url);
context.insert("config_params", &extension.documentation.config_params);
Expand Down
54 changes: 35 additions & 19 deletions gofer/src/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,25 @@ impl Cli {
Commands::Token(token) => self.handle_token_subcommands(token).await,
}
}

/// Uses the 'detail' flag for the CLI to either print a friendly duration if detail = false
/// or the exact timestamp if detail = true.
/// Expects to be given unix milliseconds.
pub fn format_time(&self, time: u64) -> Option<String> {
if time == 0 {
return None;
};

if self.conf.detail {
Some(
chrono::DateTime::from_timestamp_millis(time as i64)
.unwrap()
.to_rfc2822(),
)
} else {
format_duration(time)
}
}
}

/// Return the current epoch time in milliseconds.
Expand All @@ -224,29 +243,26 @@ pub fn epoch_milli() -> u64 {
/// Transforms the given time into a humanized duration string from the current time.
/// or if time is not valid returns None.
/// (i.e. 'about an hour ago' )
fn humanize_relative_duration(time: u64) -> Option<String> {
/// (i.e. 'in about an hour')
fn format_duration(time: u64) -> Option<String> {
if time == 0 {
return None;
}

let time_diff = epoch_milli() - time;
let time_diff_duration = chrono::Duration::milliseconds(-(time_diff as i64));
Some(HumanTime::from(time_diff_duration).to_string())
}

/// Transforms the given time into a humanized duration string from the current time.
/// or if time is not valid returns None.
/// (i.e. 'in an hour ago' )
fn humanize_future_time(time: u64) -> Option<String> {
let now = chrono::Utc::now().timestamp_millis() as u64;
let duration_until_expires = time.saturating_sub(now);
let chrono_duration = chrono::Duration::milliseconds(duration_until_expires as i64);
let duration_string = HumanTime::from(chrono_duration).to_text_en(
chrono_humanize::Accuracy::Rough,
chrono_humanize::Tense::Future,
);

Some(duration_string)
let time_diff = epoch_milli() as i64 - time as i64;
let time_diff_duration = chrono::Duration::milliseconds(time_diff);

if time_diff.is_positive() {
Some(HumanTime::from(time_diff_duration).to_text_en(
chrono_humanize::Accuracy::Rough,
chrono_humanize::Tense::Past,
))
} else {
Some(HumanTime::from(time_diff_duration).to_text_en(
chrono_humanize::Accuracy::Rough,
chrono_humanize::Tense::Future,
))
}
}

/// Creates a new HTTP client that is set up to talk to Gofer.
Expand Down
8 changes: 5 additions & 3 deletions gofer/src/cli/namespace/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::cli::{humanize_relative_duration, Cli};
use crate::cli::Cli;
use anyhow::{Context, Result};
use clap::{Args, Subcommand};
use comfy_table::{presets::ASCII_MARKDOWN, Cell, CellAlignment, Color, ContentArrangement};
Expand Down Expand Up @@ -113,7 +113,7 @@ impl Cli {
Cell::new(namespace.name),
Cell::new(namespace.description),
Cell::new(
humanize_relative_duration(namespace.created)
self.format_time(namespace.created)
.unwrap_or_else(|| "Unknown".to_string()),
),
]);
Expand Down Expand Up @@ -148,7 +148,9 @@ Created {{created}}
context.insert("description", &namespace.description);
context.insert(
"created",
&humanize_relative_duration(namespace.created).unwrap_or_else(|| "Unknown".to_string()),
&self
.format_time(namespace.created)
.unwrap_or_else(|| "Unknown".to_string()),
);

let content = tera.render("main", &context)?;
Expand Down
20 changes: 13 additions & 7 deletions gofer/src/cli/pipeline/config.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
use crate::cli::{
colorize_status_text, colorize_status_text_comfy, humanize_relative_duration, Cli,
};
use crate::cli::{colorize_status_text, colorize_status_text_comfy, Cli};
use anyhow::{Context, Result};
use clap::{Args, Subcommand};
use colored::Colorize;
Expand Down Expand Up @@ -105,9 +103,13 @@ impl Cli {
Cell::new(config.version).fg(Color::Green),
Cell::new(config.state).fg(colorize_status_text_comfy(config.state)),
Cell::new(
humanize_relative_duration(config.registered).unwrap_or("Unknown".to_string()),
self.format_time(config.registered)
.unwrap_or("Unknown".to_string()),
),
Cell::new(
self.format_time(config.deprecated)
.unwrap_or("Never".into()),
),
Cell::new(humanize_relative_duration(config.deprecated).unwrap_or("Never".into())),
]);
}

Expand Down Expand Up @@ -159,11 +161,15 @@ impl Cli {
context.insert("tasks", &config.config.tasks);
context.insert(
"registered",
&humanize_relative_duration(config.config.registered).unwrap_or("Unknown".to_string()),
&self
.format_time(config.config.registered)
.unwrap_or("Unknown".to_string()),
);
context.insert(
"deprecated",
&humanize_relative_duration(config.config.deprecated).unwrap_or("Never".to_string()),
&self
.format_time(config.config.deprecated)
.unwrap_or("Never".to_string()),
);

let content = tera.render("main", &context)?;
Expand Down
15 changes: 8 additions & 7 deletions gofer/src/cli/pipeline/deployment.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
use crate::cli::{
colorize_status_text, colorize_status_text_comfy, duration, humanize_relative_duration, Cli,
};
use crate::cli::{colorize_status_text, colorize_status_text_comfy, duration, Cli};
use anyhow::{Context, Result};
use clap::{Args, Subcommand};
use colored::Colorize;
Expand Down Expand Up @@ -105,9 +103,10 @@ impl Cli {
deployment.start_version, deployment.end_version
)),
Cell::new(
humanize_relative_duration(deployment.started).unwrap_or("Not Yet".to_string()),
self.format_time(deployment.started)
.unwrap_or("Not Yet".to_string()),
),
Cell::new(humanize_relative_duration(deployment.ended).unwrap_or("Never".into())),
Cell::new(self.format_time(deployment.ended).unwrap_or("Never".into())),
Cell::new(deployment.state).fg(colorize_status_text_comfy(deployment.state)),
Cell::new(deployment.status).fg(colorize_status_text_comfy(deployment.status)),
]);
Expand Down Expand Up @@ -173,12 +172,14 @@ impl Cli {
);
context.insert(
"started",
&humanize_relative_duration(deployment.deployment.started)
&self
.format_time(deployment.deployment.started)
.unwrap_or("Unknown".to_string()),
);
context.insert(
"ended",
&humanize_relative_duration(deployment.deployment.ended)
&self
.format_time(deployment.deployment.ended)
.unwrap_or("Unknown".to_string()),
);
context.insert("state", &colorize_status_text(deployment.deployment.state));
Expand Down
18 changes: 11 additions & 7 deletions gofer/src/cli/pipeline/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ mod deployment;
mod object;

use crate::cli::{
colorize_status_text, colorize_status_text_comfy, dependencies, duration,
humanize_relative_duration, Cli, TitleCase,
colorize_status_text, colorize_status_text_comfy, dependencies, duration, Cli, TitleCase,
};
use anyhow::{bail, Context, Result};
use clap::{Args, Subcommand};
Expand Down Expand Up @@ -215,9 +214,13 @@ impl Cli {
Cell::new(pipeline.pipeline_id).fg(Color::Green),
Cell::new(pipeline.state).fg(colorize_status_text_comfy(pipeline.state)),
Cell::new(
humanize_relative_duration(pipeline.created).unwrap_or("Unknown".to_string()),
self.format_time(pipeline.created)
.unwrap_or("Unknown".to_string()),
),
Cell::new(
self.format_time(last_run_time)
.unwrap_or("Never".to_string()),
),
Cell::new(humanize_relative_duration(last_run_time).unwrap_or("Never".to_string())),
]);
}

Expand Down Expand Up @@ -286,7 +289,7 @@ impl Cli {
Cell::new(format!("{}:", run.run_id)).fg(Color::Blue),
Cell::new(format!(
"{} by {}",
humanize_relative_duration(run.started).unwrap_or("Never".into()),
self.format_time(run.started).unwrap_or("Never".into()),
run.initiator.user.title()
)),
Cell::new(format!(
Expand Down Expand Up @@ -381,12 +384,13 @@ impl Cli {
context.insert("subscriptions", &subscription_table.to_string());
context.insert(
"created",
&humanize_relative_duration(pipeline_metadata.created)
&self
.format_time(pipeline_metadata.created)
.unwrap_or_else(|| "Unknown".to_string()),
);
context.insert(
"last_run",
&humanize_relative_duration(last_run_time).unwrap_or("Never".into()),
&self.format_time(last_run_time).unwrap_or("Never".into()),
);

let content = tera.render("main", &context)?;
Expand Down
5 changes: 3 additions & 2 deletions gofer/src/cli/pipeline/object.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::cli::{humanize_relative_duration, Cli};
use crate::cli::Cli;
use anyhow::{Context, Result};
use clap::{Args, Subcommand};
use comfy_table::{Cell, CellAlignment, Color, ContentArrangement};
Expand Down Expand Up @@ -126,7 +126,8 @@ impl Cli {
table.add_row(vec![
Cell::new(object.key).fg(Color::Green),
Cell::new(
humanize_relative_duration(object.created).unwrap_or("Unknown".to_string()),
self.format_time(object.created)
.unwrap_or("Unknown".to_string()),
),
]);
}
Expand Down
19 changes: 11 additions & 8 deletions gofer/src/cli/run/mod.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
mod object;

use crate::cli::{
colorize_status_text, colorize_status_text_comfy, dependencies, duration,
humanize_relative_duration, Cli,
};
use crate::cli::{colorize_status_text, colorize_status_text_comfy, dependencies, duration, Cli};
use anyhow::{bail, Context, Result};
use clap::{Args, Subcommand};
use colored::Colorize;
Expand Down Expand Up @@ -168,8 +165,11 @@ impl Cli {
for run in runs {
table.add_row(vec![
Cell::new(run.run_id).fg(Color::Green),
Cell::new(humanize_relative_duration(run.started).unwrap_or("Unknown".to_string())),
Cell::new(humanize_relative_duration(run.ended).unwrap_or("Unknown".to_string())),
Cell::new(
self.format_time(run.started)
.unwrap_or("Unknown".to_string()),
),
Cell::new(self.format_time(run.ended).unwrap_or("Unknown".to_string())),
Cell::new(duration(run.started as i64, run.ended as i64)),
Cell::new(run.state).fg(colorize_status_text_comfy(run.state)),
Cell::new(run.status).fg(colorize_status_text_comfy(run.status)),
Expand Down Expand Up @@ -228,7 +228,8 @@ impl Cli {
Cell::new(format!("• {}", task.task_id.clone())).fg(Color::Blue),
Cell::new(format!(
"Started {}",
humanize_relative_duration(task.started).unwrap_or("Not yet".to_string())
self.format_time(task.started)
.unwrap_or("Not yet".to_string())
)),
Cell::new(format!(
"{} {}",
Expand Down Expand Up @@ -290,7 +291,9 @@ impl Cli {
context.insert("initiator_name", &run.initiator.user.cyan().to_string());
context.insert(
"started",
&humanize_relative_duration(run.started).unwrap_or_else(|| "Not yet".to_string()),
&self
.format_time(run.started)
.unwrap_or_else(|| "Not yet".to_string()),
);
context.insert("duration", &duration(run.started as i64, run.ended as i64));
context.insert("objects_expired", &run.store_objects_expired);
Expand Down
5 changes: 3 additions & 2 deletions gofer/src/cli/run/object.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::cli::{humanize_relative_duration, Cli};
use crate::cli::Cli;
use anyhow::{Context, Result};
use clap::{Args, Subcommand};
use comfy_table::{Cell, CellAlignment, Color, ContentArrangement};
Expand Down Expand Up @@ -159,7 +159,8 @@ impl Cli {
table.add_row(vec![
Cell::new(object.key).fg(Color::Green),
Cell::new(
humanize_relative_duration(object.created).unwrap_or("Unknown".to_string()),
self.format_time(object.created)
.unwrap_or("Unknown".to_string()),
),
]);
}
Expand Down
9 changes: 6 additions & 3 deletions gofer/src/cli/secret/global.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::cli::{humanize_relative_duration, validate_identifier, Cli};
use crate::cli::{validate_identifier, Cli};
use anyhow::{Context, Result};
use clap::{Args, Subcommand};
use comfy_table::{Cell, CellAlignment, Color, ContentArrangement};
Expand Down Expand Up @@ -113,7 +113,8 @@ impl Cli {
Cell::new(secret.key).fg(Color::Green),
Cell::new(format!("{:?}", secret.namespaces)),
Cell::new(
humanize_relative_duration(secret.created).unwrap_or("Unknown".to_string()),
self.format_time(secret.created)
.unwrap_or("Unknown".to_string()),
),
]);
}
Expand Down Expand Up @@ -152,7 +153,9 @@ impl Cli {
context.insert("namespaces", &secret.metadata.namespaces);
context.insert(
"created",
&humanize_relative_duration(secret.metadata.created).unwrap_or("Unknown".to_string()),
&self
.format_time(secret.metadata.created)
.unwrap_or("Unknown".to_string()),
);

let content = tera.render("main", &context)?;
Expand Down
Loading

0 comments on commit b132306

Please sign in to comment.