Skip to content

Commit

Permalink
🚸 csv output export a date and the kind of the current line
Browse files Browse the repository at this point in the history
  • Loading branch information
davidB committed Aug 19, 2020
1 parent e3c7929 commit 4663e09
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 22 deletions.
1 change: 1 addition & 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 @@ -23,6 +23,7 @@ itertools = "0.9.0"
prettytable-rs = { version = "0.8.0", default-features = false}
structopt = "0.3.16"
openssl = { version = "0.10.30", features = ["vendored"] }
chrono = "0.4.15"

[dev-dependencies]
spectral = "0.6.0"
Expand Down
42 changes: 21 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,34 +133,34 @@ In this case value as expanded as float (with 2 decimal)

```sh
kubectl-view-allocations -o csv
resource,node,pod,Requested,%Requested,Limit,%Limit,Allocatable,Free
cpu,,,59.84,22%,104.10,38%,272.00,167.90
cpu,node-gpu1,,2.31,19%,4.47,37%,12.00,7.53
cpu,node-gpu1,zzz-b8bd56fbd-5x8vq,1.00,,2.00,,,
cpu,node-gpu1,kube-flannel-ds-amd64-7dz9z,0.10,,0.10,,,
cpu,node-gpu1,node-exporter-gpu-b4w7s,0.11,,0.22,,,
cpu,node-gpu1,xxx-backend-7d84544458-46qnh,1.00,,2.00,,,
cpu,node-gpu1,weave-scope-agent-bbdnz,0.10,,0.15,,,
cpu,node-gpu2,,0.31,1%,0.47,2%,24.00,23.53
cpu,node-gpu2,kube-flannel-ds-amd64-b5b4v,0.10,,0.10,,,
cpu,node-gpu2,node-exporter-gpu-796jz,0.11,,0.22,,,
cpu,node-gpu2,weave-scope-agent-8rhnd,0.10,,0.15,,,
cpu,node-gpu3,,3.41,11%,6.67,21%,32.00,25.33
Date,Kind,resource,node,pod,Requested,%Requested,Limit,%Limit,Allocatable,Free
2020-08-19T19:12:48.326605746+00:00,resource,cpu,,,59.94,22%,106.10,39%,272.00,165.90
2020-08-19T19:12:48.326605746+00:00,node,cpu,node-gpu1,,2.31,19%,4.47,37%,12.00,7.53
2020-08-19T19:12:48.326605746+00:00,pod,cpu,node-gpu1,yyy-b8bd56fbd-5x8vq,1.00,,2.00,,,
2020-08-19T19:12:48.326605746+00:00,pod,cpu,node-gpu1,kube-flannel-ds-amd64-7dz9z,0.10,,0.10,,,
2020-08-19T19:12:48.326605746+00:00,pod,cpu,node-gpu1,node-exporter-gpu-b4w7s,0.11,,0.22,,,
2020-08-19T19:12:48.326605746+00:00,pod,cpu,node-gpu1,xxx-backend-7d84544458-46qnh,1.00,,2.00,,,
2020-08-19T19:12:48.326605746+00:00,pod,cpu,node-gpu1,weave-scope-agent-bbdnz,0.10,,0.15,,,
2020-08-19T19:12:48.326605746+00:00,node,cpu,node-gpu2,,0.31,1%,0.47,2%,24.00,23.53
2020-08-19T19:12:48.326605746+00:00,pod,cpu,node-gpu2,kube-flannel-ds-amd64-b5b4v,0.10,,0.10,,,
2020-08-19T19:12:48.326605746+00:00,pod,cpu,node-gpu2,node-exporter-gpu-796jz,0.11,,0.22,,,
2020-08-19T19:12:48.326605746+00:00,pod,cpu,node-gpu2,weave-scope-agent-8rhnd,0.10,,0.15,,,
2020-08-19T19:12:48.326605746+00:00,node,cpu,node-gpu3,,3.41,11%,6.67,21%,32.00,25.33
...
```

It can be combined with "group-by" options.

```sh
kubectl-view-allocations -g resource -o csv
resource,Requested,%Requested,Limit,%Limit,Allocatable,Free
cpu,59.84,22%,104.10,38%,272.00,167.90
ephemeral-storage,0.00,0%,0.00,0%,34462898618662.00,34462898618662.00
hugepages-1Gi,0.00,,0.00,,,
hugepages-2Mi,0.00,,0.00,,,
memory,67989667840.00,9%,216094736384.00,30%,722318667776.00,506223931392.00
nvidia.com/gpu,2.00,18%,2.00,18%,11.00,9.00
pods,0.00,0%,0.00,0%,1540.00,1540.00
Date,Kind,resource,Requested,%Requested,Limit,%Limit,Allocatable,Free
2020-08-19T19:11:49.630864028+00:00,resource,cpu,59.94,22%,106.10,39%,272.00,165.90
2020-08-19T19:11:49.630864028+00:00,resource,ephemeral-storage,0.00,0%,0.00,0%,34462898618662.00,34462898618662.00
2020-08-19T19:11:49.630864028+00:00,resource,hugepages-1Gi,0.00,,0.00,,,
2020-08-19T19:11:49.630864028+00:00,resource,hugepages-2Mi,0.00,,0.00,,,
2020-08-19T19:11:49.630864028+00:00,resource,memory,69063409664.00,10%,224684670976.00,31%,722318667776.00,497633996800.00
2020-08-19T19:11:49.630864028+00:00,resource,nvidia.com/gpu,3.00,27%,3.00,27%,11.00,8.00
2020-08-19T19:11:49.630864028+00:00,resource,pods,0.00,0%,0.00,0%,1540.00,1540.00
```

## Alternatives
Expand Down
11 changes: 10 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ mod qty;
mod tree;
// mod human_format;
use anyhow::{anyhow, Context, Result};
use chrono::prelude::*;
use env_logger;
use itertools::Itertools;
use log::error;
Expand Down Expand Up @@ -386,14 +387,22 @@ async fn do_main(cli_opts: &CliOpts) -> Result<()> {
fn display_as_csv(data: &[(Vec<String>, Option<QtyOfUsage>)], group_by: &[GroupBy]) {
// print header
println!(
"{},Requested,%Requested,Limit,%Limit,Allocatable,Free",
"Date,Kind,{},Requested,%Requested,Limit,%Limit,Allocatable,Free",
group_by.iter().map(|x| x.to_string()).join(",")
);

// print data
let empty = "".to_string();
let datetime = Utc::now().to_rfc3339();
for (k, oqtys) in data {
let mut row = vec![];
row.push(datetime.clone());
row.push(
group_by
.get(k.len() - 1)
.map(|x| x.to_string())
.unwrap_or_else(|| empty.clone()),
);
for i in 0..group_by.len() {
row.push(k.get(i).cloned().unwrap_or_else(|| empty.clone()));
}
Expand Down

0 comments on commit 4663e09

Please sign in to comment.