-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtop.go
76 lines (65 loc) · 1.59 KB
/
top.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package main
import (
"fmt"
"sort"
"text/template"
"time"
"github.com/spf13/cobra"
)
func addTopCommand(cmd *cobra.Command) {
topCmd := cobra.Command{
GroupID: "actiongraph",
Use: "top [-f compile.json] [-n limit]",
Short: "List slowest build steps",
RunE: func(cmd *cobra.Command, args []string) error {
opt, err := loadOptions(cmd)
if err != nil {
return err
}
flags := cmd.Flags()
limit, err := flags.GetInt("limit")
if err != nil {
return err
}
tplStr, err := flags.GetString("tpl")
if err != nil {
return err
}
tpl, err := template.New("top").Funcs(opt.funcs).Parse(tplStr)
if err != nil {
return fmt.Errorf("parsing tpl: %w", err)
}
return top(opt, limit, tpl)
},
}
flags := topCmd.Flags()
flags.IntP("limit", "n", 20, "number of slowest build steps to show")
flags.String("tpl", `{{ .Duration | seconds | right 8 }}{{ .CumulativePercent | percent | right 8 }} {{.Mode}} {{.Package}}`, "template for output")
cmd.AddCommand(&topCmd)
}
func top(opt *options, limit int, tpl *template.Template) error {
actions := opt.actions
sort.Slice(actions, func(i, j int) bool {
return actions[i].Duration >= actions[j].Duration
})
var cum time.Duration
for i, node := range actions {
if limit > 0 && i >= limit {
break
}
cum += node.Duration
err := tpl.Execute(opt.stdout, topAction{
action: node,
CumulativePercent: 100 * float64(cum) / float64(opt.total),
})
if err != nil {
return err
}
fmt.Fprintln(opt.stdout)
}
return nil
}
type topAction struct {
action
CumulativePercent float64
}