Browse Source

review: move Summary/Replica collection from cmd/ to pkg/

Signed-off-by: Dominik Menke <[email protected]>
Dominik Menke 8 months ago
parent
commit
f70209cf15
4 changed files with 18 additions and 19 deletions
  1. 3 11
      cmd/compose/top.go
  2. 2 4
      cmd/compose/top_test.go
  3. 2 1
      pkg/api/api.go
  4. 11 3
      pkg/compose/top.go

+ 3 - 11
cmd/compose/top.go

@@ -80,24 +80,16 @@ func collectTop(containers []api.ContainerProcSummary) (topHeader, []topEntries)
 
 	for _, container := range containers {
 		for _, proc := range container.Processes {
-			svc := container.Name
-			if tmp, ok := container.Labels[api.ServiceLabel]; ok {
-				svc = tmp
+			entry := topEntries{
+				"SERVICE": container.Service,
+				"#":       container.Replica,
 			}
-			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)
 				}
 				entry[title] = proc[i]
 			}
-
 			entries = append(entries, entry)
 		}
 	}

+ 2 - 4
cmd/compose/top_test.go

@@ -213,10 +213,8 @@ func TestRunTopCore(t *testing.T) {
 			Name:      "not used",
 			Titles:    tc.titles,
 			Processes: tc.procs,
-			Labels: map[string]string{
-				api.ServiceLabel:         tc.name,
-				api.ContainerNumberLabel: "1",
-			},
+			Service:   tc.name,
+			Replica:   "1",
 		}
 		all = append(all, summary)
 

+ 2 - 1
pkg/api/api.go

@@ -523,7 +523,8 @@ type ContainerProcSummary struct {
 	Name      string
 	Processes [][]string
 	Titles    []string
-	Labels    map[string]string
+	Service   string
+	Replica   string
 }
 
 // ImageSummary holds container image description

+ 11 - 3
pkg/compose/top.go

@@ -42,13 +42,21 @@ func (s *composeService) Top(ctx context.Context, projectName string, services [
 			if err != nil {
 				return err
 			}
-			summary[i] = api.ContainerProcSummary{
+			name := getCanonicalContainerName(ctr)
+			s := api.ContainerProcSummary{
 				ID:        ctr.ID,
-				Name:      getCanonicalContainerName(ctr),
+				Name:      name,
 				Processes: topContent.Processes,
 				Titles:    topContent.Titles,
-				Labels:    container.Labels,
+				Service:   name,
 			}
+			if service, exists := ctr.Labels[api.ServiceLabel]; exists {
+				s.Service = service
+			}
+			if replica, exists := ctr.Labels[api.ContainerNumberLabel]; exists {
+				s.Replica = replica
+			}
+			summary[i] = s
 			return nil
 		})
 	}