Skip to content

Commit

Permalink
Got age
Browse files Browse the repository at this point in the history
  • Loading branch information
byrnedo committed Jul 29, 2024
1 parent a8212ad commit ed6df88
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 10 deletions.
7 changes: 7 additions & 0 deletions Cargo.lock

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

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

pub (crate) struct CronjobsLister {}
pub(crate) struct CronjobsLister {}

impl Lister<ObjectListItem> for CronjobsLister {
fn selector(&self, si: &SystemInfo, ns: &str, id: &str) -> Option<Vec<ObjectListItem>> {
Expand All @@ -17,17 +20,27 @@ impl Lister<ObjectListItem> for CronjobsLister {

fn print(&self, resources: Vec<ObjectListItem>) {
println!(
"{0: <30} {1: <5} {2: <20}",
"NAME", "#", "CREATED",
"{0: <10} {1: <10} {2: <10} {3: <5} {4: <10} {5: <10} {6: <10} {7: <10}",
"NAMESPACE", "NAME", "SCHEDULE", "TIMEZONE", "SUSPEND", "ACTIVE", "LAST SCHEDULE", "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 cronjob: CronJob = serde_yaml::from_value(item.first().as_ref().unwrap().manifest.as_ref().unwrap().clone()).unwrap_or_default();
let spec = cronjob.spec.unwrap_or_default();
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()
};

println!(
"{0: <30} {1: <5} {2: <20}",
name, item.len(), item.first().unwrap().created_at.to_rfc3339_opts(SecondsFormat::Secs, true)
"{0: <10} {1: <10} {2: <10} {3: <5} {4: <10} {5: <10} {6: <10} {7: <10}",
name.namespace, name.name, "TODO", timezone.unwrap_or("-".to_string()), "False", "-", "-", age
)
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/get/lister.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ pub(crate) trait NameFilters {
fn name(&self) -> String;
fn namespace(&self) -> String;
fn filter_names(&self, name: &str, ns: &str) -> bool {
let ns = match ns.is_empty(){
let ns = match ns.is_empty() {
true => &"default",
false => ns
};

if !ns.is_empty() && self.namespace() != ns {
return false;
}
if !name.is_empty() && ( self.id() != name || self.name() != name ) {
if !name.is_empty() && (self.id() != name || self.name() != name) {
return false;
}
if ns.is_empty() && name.is_empty() && self.namespace() == "skate" {
Expand Down
2 changes: 1 addition & 1 deletion src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ pub fn calc_k8s_resource_hash(obj: (impl Metadata<Scope=NamespaceResourceScope,
}


#[derive(Serialize, Deserialize, Debug, Clone)]
#[derive(Serialize, Deserialize, Debug, Clone, Eq, Hash, PartialEq)]
pub struct NamespacedName {
pub name: String,
pub namespace: String,
Expand Down

0 comments on commit ed6df88

Please sign in to comment.