Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Condense output of compose top #12391

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 74 additions & 17 deletions cmd/compose/top.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ func topCommand(p *ProjectOptions, dockerCli command.Cli, backend api.Service) *
return topCmd
}

type topHeader map[string]int // maps a proc title to its output index
type topEntries map[string]string

func runTop(ctx context.Context, dockerCli command.Cli, backend api.Service, opts topOptions, services []string) error {
projectName, err := opts.toProjectName(ctx, dockerCli)
if err != nil {
Expand All @@ -63,30 +66,84 @@ func runTop(ctx context.Context, dockerCli command.Cli, backend api.Service, opt
return containers[i].Name < containers[j].Name
})

header, entries := collectTop(containers)
return topPrint(dockerCli.Out(), header, entries)
}

func collectTop(containers []api.ContainerProcSummary) (topHeader, []topEntries) {
// map column name to its header (should keep working if backend.Top returns
// varying columns for different containers)
header := topHeader{"SERVICE": 0, "#": 1}

// assume one process per container and grow if needed
entries := make([]topEntries, 0, len(containers))

for _, container := range containers {
_, _ = fmt.Fprintf(dockerCli.Out(), "%s\n", container.Name)
err := psPrinter(dockerCli.Out(), func(w io.Writer) {
for _, proc := range container.Processes {
info := []interface{}{}
for _, p := range proc {
info = append(info, p)
for _, proc := range container.Processes {
svc := container.Name
if tmp, ok := container.Labels[api.ServiceLabel]; ok {
svc = tmp
}
replica := "-"
if tmp, ok := container.Labels[api.ContainerNumberLabel]; ok {
replica = tmp
}

entry := topEntries{"SERVICE": svc, "#": replica}

for i, title := range container.Titles {
if _, exists := header[title]; !exists {
header[title] = len(header)
}
_, _ = fmt.Fprintf(w, strings.Repeat("%s\t", len(info))+"\n", info...)
entry[title] = proc[i]
}

entries = append(entries, entry)
}
}

// ensure CMD is the right-most column
if pos, ok := header["CMD"]; ok {
max := pos
for h, i := range header {
if i > max {
max = i
}
if i > pos {
header[h] = i - 1
}
_, _ = fmt.Fprintln(w)
},
container.Titles...)
if err != nil {
return err
}
header["CMD"] = max
}
return nil

return header, entries
}

func psPrinter(out io.Writer, printer func(writer io.Writer), headers ...string) error {
w := tabwriter.NewWriter(out, 5, 1, 3, ' ', 0)
_, _ = fmt.Fprintln(w, strings.Join(headers, "\t"))
printer(w)
func topPrint(out io.Writer, headers topHeader, rows []topEntries) error {
if len(rows) == 0 {
return nil
}

w := tabwriter.NewWriter(out, 4, 1, 2, ' ', 0)

// write headers in the order we've encountered them
h := make([]string, len(headers))
for title, index := range headers {
h[index] = title
}
_, _ = fmt.Fprintln(w, strings.Join(h, "\t"))

for _, row := range rows {
// write proc data in header order
r := make([]string, len(headers))
for title, index := range headers {
if v, ok := row[title]; ok {
r[index] = v
} else {
r[index] = "-"
}
}
_, _ = fmt.Fprintln(w, strings.Join(r, "\t"))
}
return w.Flush()
}
Loading
Loading