1
0

top.go 3.7 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/docker/compose/v2/pkg/compose"
  23. "github.com/spf13/cobra"
  24. "github.com/docker/compose/v2/pkg/api"
  25. )
  26. type topOptions struct {
  27. *ProjectOptions
  28. }
  29. func topCommand(p *ProjectOptions, dockerCli command.Cli, backendOptions *BackendOptions) *cobra.Command {
  30. opts := topOptions{
  31. ProjectOptions: p,
  32. }
  33. topCmd := &cobra.Command{
  34. Use: "top [SERVICES...]",
  35. Short: "Display the running processes",
  36. RunE: Adapt(func(ctx context.Context, args []string) error {
  37. return runTop(ctx, dockerCli, backendOptions, opts, args)
  38. }),
  39. ValidArgsFunction: completeServiceNames(dockerCli, p),
  40. }
  41. return topCmd
  42. }
  43. type (
  44. topHeader map[string]int // maps a proc title to its output index
  45. topEntries map[string]string
  46. )
  47. func runTop(ctx context.Context, dockerCli command.Cli, backendOptions *BackendOptions, opts topOptions, services []string) error {
  48. projectName, err := opts.toProjectName(ctx, dockerCli)
  49. if err != nil {
  50. return err
  51. }
  52. backend, err := compose.NewComposeService(dockerCli, backendOptions.Options...)
  53. if err != nil {
  54. return err
  55. }
  56. containers, err := backend.Top(ctx, projectName, services)
  57. if err != nil {
  58. return err
  59. }
  60. sort.Slice(containers, func(i, j int) bool {
  61. return containers[i].Name < containers[j].Name
  62. })
  63. header, entries := collectTop(containers)
  64. return topPrint(dockerCli.Out(), header, entries)
  65. }
  66. func collectTop(containers []api.ContainerProcSummary) (topHeader, []topEntries) {
  67. // map column name to its header (should keep working if backend.Top returns
  68. // varying columns for different containers)
  69. header := topHeader{"SERVICE": 0, "#": 1}
  70. // assume one process per container and grow if needed
  71. entries := make([]topEntries, 0, len(containers))
  72. for _, container := range containers {
  73. for _, proc := range container.Processes {
  74. entry := topEntries{
  75. "SERVICE": container.Service,
  76. "#": container.Replica,
  77. }
  78. for i, title := range container.Titles {
  79. if _, exists := header[title]; !exists {
  80. header[title] = len(header)
  81. }
  82. entry[title] = proc[i]
  83. }
  84. entries = append(entries, entry)
  85. }
  86. }
  87. // ensure CMD is the right-most column
  88. if pos, ok := header["CMD"]; ok {
  89. maxPos := pos
  90. for h, i := range header {
  91. if i > maxPos {
  92. maxPos = i
  93. }
  94. if i > pos {
  95. header[h] = i - 1
  96. }
  97. }
  98. header["CMD"] = maxPos
  99. }
  100. return header, entries
  101. }
  102. func topPrint(out io.Writer, headers topHeader, rows []topEntries) error {
  103. if len(rows) == 0 {
  104. return nil
  105. }
  106. w := tabwriter.NewWriter(out, 4, 1, 2, ' ', 0)
  107. // write headers in the order we've encountered them
  108. h := make([]string, len(headers))
  109. for title, index := range headers {
  110. h[index] = title
  111. }
  112. _, _ = fmt.Fprintln(w, strings.Join(h, "\t"))
  113. for _, row := range rows {
  114. // write proc data in header order
  115. r := make([]string, len(headers))
  116. for title, index := range headers {
  117. if v, ok := row[title]; ok {
  118. r[index] = v
  119. } else {
  120. r[index] = "-"
  121. }
  122. }
  123. _, _ = fmt.Fprintln(w, strings.Join(r, "\t"))
  124. }
  125. return w.Flush()
  126. }