top.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. entry := topEntries{
  68. "SERVICE": container.Service,
  69. "#": container.Replica,
  70. }
  71. for i, title := range container.Titles {
  72. if _, exists := header[title]; !exists {
  73. header[title] = len(header)
  74. }
  75. entry[title] = proc[i]
  76. }
  77. entries = append(entries, entry)
  78. }
  79. }
  80. // ensure CMD is the right-most column
  81. if pos, ok := header["CMD"]; ok {
  82. max := pos
  83. for h, i := range header {
  84. if i > max {
  85. max = i
  86. }
  87. if i > pos {
  88. header[h] = i - 1
  89. }
  90. }
  91. header["CMD"] = max
  92. }
  93. return header, entries
  94. }
  95. func topPrint(out io.Writer, headers topHeader, rows []topEntries) error {
  96. if len(rows) == 0 {
  97. return nil
  98. }
  99. w := tabwriter.NewWriter(out, 4, 1, 2, ' ', 0)
  100. // write headers in the order we've encountered them
  101. h := make([]string, len(headers))
  102. for title, index := range headers {
  103. h[index] = title
  104. }
  105. _, _ = fmt.Fprintln(w, strings.Join(h, "\t"))
  106. for _, row := range rows {
  107. // write proc data in header order
  108. r := make([]string, len(headers))
  109. for title, index := range headers {
  110. if v, ok := row[title]; ok {
  111. r[index] = v
  112. } else {
  113. r[index] = "-"
  114. }
  115. }
  116. _, _ = fmt.Fprintln(w, strings.Join(r, "\t"))
  117. }
  118. return w.Flush()
  119. }