Skip to content

Commit

Permalink
Own age impl
Browse files Browse the repository at this point in the history
  • Loading branch information
byrnedo committed Jul 30, 2024
1 parent 9f09f8f commit 6100f26
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 18 deletions.
7 changes: 0 additions & 7 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,3 @@ fs2 = "0.4.3"
log = "0.4.20"
handlebars = "5.1.2"
cron = "0.12.1"
humantime = "2.1.0"
1 change: 0 additions & 1 deletion src/get/cronjob.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use std::collections::HashMap;
use chrono::SecondsFormat;
use humantime;
use k8s_openapi::api::batch::v1::CronJob;
use crate::filestore::ObjectListItem;
use crate::get::{Lister};
Expand Down
22 changes: 16 additions & 6 deletions src/get/ingress.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use crate::filestore::ObjectListItem;
use crate::get::{Lister};
use crate::get::lister::NameFilters;
use crate::skatelet::SystemInfo;
use crate::util::{age, NamespacedName};

pub(crate) struct IngresssLister {}

Expand All @@ -16,18 +17,27 @@ impl Lister<ObjectListItem> for IngresssLister {
}

fn print(&self, resources: Vec<ObjectListItem>) {
macro_rules! cols {
() => ("{0: <15} {1: <15} {2: <15} {3: <15} {4: <15} {5: <15} {6: <15}")
}
println!(
"{0: <30} {1: <5} {2: <20}",
"NAME", "#", "CREATED",
cols!(),
"NAMESPACE", "NAME", "CLASS", "HOSTS", "ADDRESS", "PORTS", "AGE"
);
let map = resources.iter().fold(HashMap::<String, Vec<ObjectListItem>>::new(), |mut acc, item| {
acc.entry(item.name.to_string()).or_insert(vec![]).push(item.clone());
let map = resources.iter().fold(HashMap::<NamespacedName, Vec<ObjectListItem>>::new(), |mut acc, item| {
acc.entry(item.name.clone()).or_insert(vec![]).push(item.clone());
acc
});

for (name, item) in map {
let hosts = "TODO";
let age = age(item.first().unwrap().created_at);
let address = "TODO";
let class = "TODO";
let ports = "TODO";
println!(
"{0: <30} {1: <5} {2: <20}",
name, item.len(), item.first().unwrap().created_at.to_rfc3339_opts(SecondsFormat::Secs, true)
cols!(),
name.namespace, name.name, class, hosts, address, ports, age
)
}
}
Expand Down
19 changes: 16 additions & 3 deletions src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,9 +179,22 @@ pub fn hash_k8s_resource(obj: &mut (impl Metadata<Scope=NamespaceResourceScope,
// age returns the age of a resource in a human-readable format, with only the first segment of resolution (eg 2d1h4m becomes 2d)
pub fn age(date_time: DateTime<Local>) -> String {
match Local::now().signed_duration_since(date_time).to_std() {
Ok(age) => humantime::format_duration(age).to_string()
.split_whitespace().take(1).collect::<Vec<&str>>()
.join(""),
Ok(duration) => {
if duration.as_secs() < 60 {
return format!("{}s", duration.as_secs());
}
let minutes = (duration.as_secs() / 60) % 60;
if minutes < 60 {
return format!("{}m", minutes);
}
let hours = duration.as_secs() / 60 * 60;
if hours < 24 {
return format!("{}h", hours);
}

let days = duration.as_secs() / 60 * 60 * 24;
return format!("{}d", days);
}
Err(_) => "".to_string()
}
}

0 comments on commit 6100f26

Please sign in to comment.