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

Signed-off-by: Dominik Menke <dom@digineo.de>
This commit is contained in:
Dominik Menke 2025-03-12 19:25:43 +01:00 committed by Nicolas De loof
parent 62e832eb50
commit f70209cf15
4 changed files with 18 additions and 19 deletions

View File

@ -80,24 +80,16 @@ func collectTop(containers []api.ContainerProcSummary) (topHeader, []topEntries)
for _, container := range containers { for _, container := range containers {
for _, proc := range container.Processes { for _, proc := range container.Processes {
svc := container.Name entry := topEntries{
if tmp, ok := container.Labels[api.ServiceLabel]; ok { "SERVICE": container.Service,
svc = tmp "#": container.Replica,
} }
replica := "-"
if tmp, ok := container.Labels[api.ContainerNumberLabel]; ok {
replica = tmp
}
entry := topEntries{"SERVICE": svc, "#": replica}
for i, title := range container.Titles { for i, title := range container.Titles {
if _, exists := header[title]; !exists { if _, exists := header[title]; !exists {
header[title] = len(header) header[title] = len(header)
} }
entry[title] = proc[i] entry[title] = proc[i]
} }
entries = append(entries, entry) entries = append(entries, entry)
} }
} }

View File

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

View File

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

View File

@ -42,13 +42,21 @@ func (s *composeService) Top(ctx context.Context, projectName string, services [
if err != nil { if err != nil {
return err return err
} }
summary[i] = api.ContainerProcSummary{ name := getCanonicalContainerName(ctr)
s := api.ContainerProcSummary{
ID: ctr.ID, ID: ctr.ID,
Name: getCanonicalContainerName(ctr), Name: name,
Processes: topContent.Processes, Processes: topContent.Processes,
Titles: topContent.Titles, 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 return nil
}) })
} }