top.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /*
  2. Copyright 2020 Docker Compose CLI authors
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package compose
  14. import (
  15. "context"
  16. "fmt"
  17. "io"
  18. "sort"
  19. "strings"
  20. "text/tabwriter"
  21. "github.com/docker/cli/cli/command"
  22. "github.com/spf13/cobra"
  23. "github.com/docker/compose/v2/pkg/api"
  24. )
  25. type topOptions struct {
  26. *ProjectOptions
  27. }
  28. func topCommand(p *ProjectOptions, dockerCli command.Cli, backend api.Service) *cobra.Command {
  29. opts := topOptions{
  30. ProjectOptions: p,
  31. }
  32. topCmd := &cobra.Command{
  33. Use: "top [SERVICES...]",
  34. Short: "Display the running processes",
  35. RunE: Adapt(func(ctx context.Context, args []string) error {
  36. return runTop(ctx, dockerCli, backend, opts, args)
  37. }),
  38. ValidArgsFunction: completeServiceNames(dockerCli, p),
  39. }
  40. return topCmd
  41. }
  42. type topHeader map[string]int // maps a proc title to its output index
  43. type topEntries map[string]string
  44. func runTop(ctx context.Context, dockerCli command.Cli, backend api.Service, opts topOptions, services []string) error {
  45. projectName, err := opts.toProjectName(ctx, dockerCli)
  46. if err != nil {
  47. return err
  48. }
  49. containers, err := backend.Top(ctx, projectName, services)
  50. if err != nil {
  51. return err
  52. }
  53. sort.Slice(containers, func(i, j int) bool {
  54. return containers[i].Name < containers[j].Name
  55. })
  56. header, entries := collectTop(containers)
  57. return topPrint(dockerCli.Out(), header, entries)
  58. }
  59. func collectTop(containers []api.ContainerProcSummary) (topHeader, []topEntries) {
  60. // map column name to its header (should keep working if backend.Top returns
  61. // varying columns for different containers)
  62. header := topHeader{"SERVICE": 0, "#": 1}
  63. // assume one process per container and grow if needed
  64. entries := make([]topEntries, 0, len(containers))
  65. for _, container := range containers {
  66. for _, proc := range container.Processes {
  67. svc := container.Name
  68. if tmp, ok := container.Labels[api.ServiceLabel]; ok {
  69. svc = tmp
  70. }
  71. replica := "-"
  72. if tmp, ok := container.Labels[api.ContainerNumberLabel]; ok {
  73. replica = tmp
  74. }
  75. entry := topEntries{"SERVICE": svc, "#": replica}
  76. for i, title := range container.Titles {
  77. if _, exists := header[title]; !exists {
  78. header[title] = len(header)
  79. }
  80. entry[title] = proc[i]
  81. }
  82. entries = append(entries, entry)
  83. }
  84. }
  85. // ensure CMD is the right-most column
  86. if pos, ok := header["CMD"]; ok {
  87. max := pos
  88. for h, i := range header {
  89. if i > max {
  90. max = i
  91. }
  92. if i > pos {
  93. header[h] = i - 1
  94. }
  95. }
  96. header["CMD"] = max
  97. }
  98. return header, entries
  99. }
  100. func topPrint(out io.Writer, headers topHeader, rows []topEntries) error {
  101. if len(rows) == 0 {
  102. return nil
  103. }
  104. w := tabwriter.NewWriter(out, 4, 1, 2, ' ', 0)
  105. // write headers in the order we've encountered them
  106. h := make([]string, len(headers))
  107. for title, index := range headers {
  108. h[index] = title
  109. }
  110. _, _ = fmt.Fprintln(w, strings.Join(h, "\t"))
  111. for _, row := range rows {
  112. // write proc data in header order
  113. r := make([]string, len(headers))
  114. for title, index := range headers {
  115. if v, ok := row[title]; ok {
  116. r[index] = v
  117. } else {
  118. r[index] = "-"
  119. }
  120. }
  121. _, _ = fmt.Fprintln(w, strings.Join(r, "\t"))
  122. }
  123. return w.Flush()
  124. }