From f70209cf15efbcc009531879cc4cf9c9082cfb81 Mon Sep 17 00:00:00 2001 From: Dominik Menke Date: Wed, 12 Mar 2025 19:25:43 +0100 Subject: [PATCH] review: move Summary/Replica collection from cmd/ to pkg/ Signed-off-by: Dominik Menke --- cmd/compose/top.go | 14 +++----------- cmd/compose/top_test.go | 6 ++---- pkg/api/api.go | 3 ++- pkg/compose/top.go | 14 +++++++++++--- 4 files changed, 18 insertions(+), 19 deletions(-) diff --git a/cmd/compose/top.go b/cmd/compose/top.go index dc3975621..abc16d05e 100644 --- a/cmd/compose/top.go +++ b/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) } } diff --git a/cmd/compose/top_test.go b/cmd/compose/top_test.go index ab3dfa6c1..3838fdd18 100644 --- a/cmd/compose/top_test.go +++ b/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) diff --git a/pkg/api/api.go b/pkg/api/api.go index 99d1015d9..86bb04d29 100644 --- a/pkg/api/api.go +++ b/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 diff --git a/pkg/compose/top.go b/pkg/compose/top.go index 7e136cca0..fac7f7cb8 100644 --- a/pkg/compose/top.go +++ b/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 }) }