Skip to content

Commit

Permalink
feat: statistics of Workloads (#73)
Browse files Browse the repository at this point in the history
  • Loading branch information
anrs authored Sep 28, 2021
1 parent 4a2f251 commit bacf570
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 2 deletions.
8 changes: 8 additions & 0 deletions cmd/workload/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,14 @@ func Command() *cli.Command {
Name: "skip-ip",
Usage: "filter out IP",
},
&cli.StringSliceFlag{
Name: "pod",
Usage: "filter by Pod",
},
&cli.BoolFlag{
Name: "statistics",
Usage: "Display the statistics of Workloads",
},
},
},
{
Expand Down
19 changes: 17 additions & 2 deletions cmd/workload/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ type listWorkloadsOptions struct {
labels map[string]string
matchIPs []string
skipIPs []string
podnames []string
statistics bool
}

func (o *listWorkloadsOptions) run(ctx context.Context) error {
Expand Down Expand Up @@ -54,21 +56,31 @@ func (o *listWorkloadsOptions) run(ctx context.Context) error {
ips: o.matchIPs,
skipIPs: o.skipIPs,
nodenames: []string{},
podnames: []string{},
}
if len(o.nodename) > 0 {
f.nodenames = append(f.nodenames, o.nodename)
}
if len(o.podnames) > 0 {
f.podnames = append(f.podnames, o.podnames...)
}

workloads = f.filterIn(workloads)

describe.Workloads(workloads...)
if o.statistics {
describe.WorkloadsStatistics(workloads...)
} else {
describe.Workloads(workloads...)
}

return nil
}

type filter struct {
ips []string
skipIPs []string
nodenames []string
podnames []string
}

func (wf filter) filterIn(workloads []*corepb.Workload) []*corepb.Workload {
Expand Down Expand Up @@ -100,7 +112,8 @@ func (wf filter) skip(workload *corepb.Workload) bool {
}

return (len(wf.ips) > 0 && !wf.hasIntersection(wf.ips, ips)) ||
(len(wf.skipIPs) > 0 && wf.hasIntersection(wf.skipIPs, ips))
(len(wf.skipIPs) > 0 && wf.hasIntersection(wf.skipIPs, ips)) ||
(len(wf.podnames) > 0 && !wf.hasIntersection(wf.podnames, []string{workload.Podname}))
}

func (wf filter) hasIntersection(a, b []string) bool {
Expand Down Expand Up @@ -133,6 +146,8 @@ func cmdWorkloadList(c *cli.Context) error {
limit: c.Int64("limit"),
matchIPs: c.StringSlice("match-ip"),
skipIPs: c.StringSlice("skip-ip"),
podnames: c.StringSlice("pod"),
statistics: c.Bool("statistics"),
}
return o.run(c.Context)
}
40 changes: 40 additions & 0 deletions describe/workload.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,46 @@ func Workloads(workloads ...*corepb.Workload) {
}
}

// WorkloadsStatistics describes the statistics of the Workloads
func WorkloadsStatistics(workloads ...*corepb.Workload) {
stat := struct {
CPUs float64
Memory int64
Storage int64
}{}
for _, w := range workloads {
stat.CPUs += w.Resource.CpuQuotaRequest
stat.Memory += w.Resource.MemoryRequest
stat.Storage += w.Resource.StorageRequest
}

describeStatistics := func() {
t := table.NewWriter()
t.SetOutputMirror(os.Stdout)
t.AppendHeader(table.Row{"CPUs", "Memory", "Storage"})

rows := [][]string{
{fmt.Sprintf("%f", stat.CPUs)},
{fmt.Sprintf("%d", stat.Memory)},
{fmt.Sprintf("%d", stat.Storage)},
}
t.AppendRows(toTableRows(rows))
t.AppendSeparator()

t.SetStyle(table.StyleLight)
t.Render()
}

switch {
case isJSON():
describeAsJSON(stat)
case isYAML():
describeAsYAML(stat)
default:
describeStatistics()
}
}

func describeWorkloads(workloads []*corepb.Workload) {
t := table.NewWriter()
t.SetOutputMirror(os.Stdout)
Expand Down

0 comments on commit bacf570

Please sign in to comment.