69 lines
1.9 KiB
Go
69 lines
1.9 KiB
Go
/*
|
|
Copyright 2020 Docker Compose CLI authors
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
you may not use this file except in compliance with the License.
|
|
You may obtain a copy of the License at
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
See the License for the specific language governing permissions and
|
|
limitations under the License.
|
|
*/
|
|
|
|
package compose
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/docker/compose-cli/api/compose"
|
|
moby "github.com/docker/docker/api/types"
|
|
"github.com/docker/docker/api/types/filters"
|
|
"golang.org/x/sync/errgroup"
|
|
)
|
|
|
|
func (s *composeService) Top(ctx context.Context, projectName string, services []string) ([]compose.ContainerProcSummary, error) {
|
|
containers, err := s.apiClient.ContainerList(ctx, moby.ContainerListOptions{
|
|
Filters: filters.NewArgs(projectFilter(projectName)),
|
|
})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
ignore := func(string) bool {
|
|
return false
|
|
}
|
|
if len(services) > 0 {
|
|
ignore = func(s string) bool {
|
|
return !contains(services, s)
|
|
}
|
|
}
|
|
summary := make([]compose.ContainerProcSummary, len(containers))
|
|
eg, ctx := errgroup.WithContext(ctx)
|
|
for i, c := range containers {
|
|
container := c
|
|
service := c.Labels[serviceLabel]
|
|
if ignore(service) {
|
|
continue
|
|
}
|
|
i := i
|
|
eg.Go(func() error {
|
|
topContent, err := s.apiClient.ContainerTop(ctx, container.ID, []string{})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
summary[i] = compose.ContainerProcSummary{
|
|
ID: container.ID,
|
|
Name: getCanonicalContainerName(container),
|
|
Processes: topContent.Processes,
|
|
Titles: topContent.Titles,
|
|
}
|
|
return nil
|
|
})
|
|
}
|
|
return summary, eg.Wait()
|
|
}
|