Skip to content

Commit

Permalink
Cronjob cols
Browse files Browse the repository at this point in the history
  • Loading branch information
byrnedo committed Jul 30, 2024
1 parent ed6df88 commit 5e9ed0c
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 9 deletions.
11 changes: 4 additions & 7 deletions src/get/cronjob.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::filestore::ObjectListItem;
use crate::get::{Lister};
use crate::get::lister::NameFilters;
use crate::skatelet::SystemInfo;
use crate::util::NamespacedName;
use crate::util::{age, NamespacedName};

pub(crate) struct CronjobsLister {}

Expand All @@ -20,7 +20,7 @@ impl Lister<ObjectListItem> for CronjobsLister {

fn print(&self, resources: Vec<ObjectListItem>) {
println!(
"{0: <10} {1: <10} {2: <10} {3: <5} {4: <10} {5: <10} {6: <10} {7: <10}",
"{0: <10} {1: <10} {2: <10} {3: <10} {4: <10} {5: <10} {6: <15} {7: <10}",
"NAMESPACE", "NAME", "SCHEDULE", "TIMEZONE", "SUSPEND", "ACTIVE", "LAST SCHEDULE", "AGE"
);
let map = resources.iter().fold(HashMap::<NamespacedName, Vec<ObjectListItem>>::new(), |mut acc, item| {
Expand All @@ -33,13 +33,10 @@ impl Lister<ObjectListItem> for CronjobsLister {
let schedule = spec.schedule;
let timezone = spec.time_zone;
let created = item.first().unwrap().created_at;
let age = match chrono::offset::Local::now().signed_duration_since(created).to_std() {
Ok(age) => humantime::format_duration(age).to_string().split_whitespace().take(2).collect::<Vec<&str>>().join(""),
Err(_) => "".to_string()
};
let age = age(created);

println!(
"{0: <10} {1: <10} {2: <10} {3: <5} {4: <10} {5: <10} {6: <10} {7: <10}",
"{0: <10} {1: <10} {2: <10} {3: <10} {4: <10} {5: <10} {6: <15} {7: <10}",
name.namespace, name.name, "TODO", timezone.unwrap_or("-".to_string()), "False", "-", "-", age
)
}
Expand Down
16 changes: 14 additions & 2 deletions src/util.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::collections::hash_map::DefaultHasher;
use std::fmt::{Display, Formatter};
use std::hash::{Hash, Hasher};
use chrono::{DateTime, Local};
use deunicode::deunicode_char;
use itertools::Itertools;
use k8s_openapi::{Metadata, NamespaceResourceScope};
Expand Down Expand Up @@ -125,10 +126,10 @@ pub struct NamespacedName {
impl From<&str> for NamespacedName {
fn from(s: &str) -> Self {
let parts: Vec<_> = s.split('.').collect();
return Self{
return Self {
name: parts.first().unwrap_or(&"").to_string(),
namespace: parts.last().unwrap_or(&"").to_string(),
}
};
}
}

Expand Down Expand Up @@ -163,6 +164,7 @@ pub fn metadata_name(obj: &impl Metadata<Scope=NamespaceResourceScope, Ty=Object
NamespacedName::new(name.unwrap().clone(), ns.unwrap().clone())
}

// hash_k8s_resource hashes a k8s resource and adds the hash to the labels, also returning it
pub fn hash_k8s_resource(obj: &mut (impl Metadata<Scope=NamespaceResourceScope, Ty=ObjectMeta> + Serialize + Clone)) -> String

{
Expand All @@ -172,4 +174,14 @@ pub fn hash_k8s_resource(obj: &mut (impl Metadata<Scope=NamespaceResourceScope,
labels.insert("skate.io/hash".to_string(), hash.clone());
obj.metadata_mut().labels = Option::from(labels);
hash
}

// age returns the age of a resource in a human-readable format, with only the first 2 resolutions (eg 2d1h4m becomes 2d1h)
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(2).collect::<Vec<&str>>()
.join(""),
Err(_) => "".to_string()
}
}

0 comments on commit 5e9ed0c

Please sign in to comment.