top.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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 (
  43. topHeader map[string]int // maps a proc title to its output index
  44. topEntries map[string]string
  45. )
  46. func runTop(ctx context.Context, dockerCli command.Cli, backend api.Service, opts topOptions, services []string) error {
  47. projectName, err := opts.toProjectName(ctx, dockerCli)
  48. if err != nil {
  49. return err
  50. }
  51. containers, err := backend.Top(ctx, projectName, services)
  52. if err != nil {
  53. return err
  54. }
  55. sort.Slice(containers, func(i, j int) bool {
  56. return containers[i].Name < containers[j].Name
  57. })
  58. header, entries := collectTop(containers)
  59. return topPrint(dockerCli.Out(), header, entries)
  60. }
  61. func collectTop(containers []api.ContainerProcSummary) (topHeader, []topEntries) {
  62. // map column name to its header (should keep working if backend.Top returns
  63. // varying columns for different containers)
  64. header := topHeader{"SERVICE": 0, "#": 1}
  65. // assume one process per container and grow if needed
  66. entries := make([]topEntries, 0, len(containers))
  67. for _, container := range containers {
  68. for _, proc := range container.Processes {
  69. entry := topEntries{
  70. "SERVICE": container.Service,
  71. "#": container.Replica,
  72. }
  73. for i, title := range container.Titles {
  74. if _, exists := header[title]; !exists {
  75. header[title] = len(header)
  76. }
  77. entry[title] = proc[i]
  78. }
  79. entries = append(entries, entry)
  80. }
  81. }
  82. // ensure CMD is the right-most column
  83. if pos, ok := header["CMD"]; ok {
  84. maxPos := pos
  85. for h, i := range header {
  86. if i > maxPos {
  87. maxPos = i
  88. }
  89. if i > pos {
  90. header[h] = i - 1
  91. }
  92. }
  93. header["CMD"] = maxPos
  94. }
  95. return header, entries
  96. }
  97. func topPrint(out io.Writer, headers topHeader, rows []topEntries) error {
  98. if len(rows) == 0 {
  99. return nil
  100. }
  101. w := tabwriter.NewWriter(out, 4, 1, 2, ' ', 0)
  102. // write headers in the order we've encountered them
  103. h := make([]string, len(headers))
  104. for title, index := range headers {
  105. h[index] = title
  106. }
  107. _, _ = fmt.Fprintln(w, strings.Join(h, "\t"))
  108. for _, row := range rows {
  109. // write proc data in header order
  110. r := make([]string, len(headers))
  111. for title, index := range headers {
  112. if v, ok := row[title]; ok {
  113. r[index] = v
  114. } else {
  115. r[index] = "-"
  116. }
  117. }
  118. _, _ = fmt.Fprintln(w, strings.Join(r, "\t"))
  119. }
  120. return w.Flush()
  121. }