mirror of
https://github.com/amir20/dozzle.git
synced 2025-12-21 13:23:07 +01:00
chore: cleans up proto tranformation (#3791)
This commit is contained in:
@@ -6,7 +6,9 @@ import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/amir20/dozzle/internal/agent/pb"
|
||||
"github.com/amir20/dozzle/internal/utils"
|
||||
"google.golang.org/protobuf/types/known/timestamppb"
|
||||
)
|
||||
|
||||
// Container represents an internal representation of docker containers
|
||||
@@ -30,6 +32,70 @@ type Container struct {
|
||||
FullyLoaded bool `json:"-,omitempty"`
|
||||
}
|
||||
|
||||
func (container Container) ToProto() pb.Container {
|
||||
var pbStats []*pb.ContainerStat
|
||||
for _, stat := range container.Stats.Data() {
|
||||
pbStats = append(pbStats, &pb.ContainerStat{
|
||||
Id: stat.ID,
|
||||
CpuPercent: stat.CPUPercent,
|
||||
MemoryPercent: stat.MemoryPercent,
|
||||
MemoryUsage: stat.MemoryUsage,
|
||||
})
|
||||
}
|
||||
|
||||
return pb.Container{
|
||||
Id: container.ID,
|
||||
Name: container.Name,
|
||||
Image: container.Image,
|
||||
Created: timestamppb.New(container.Created),
|
||||
State: container.State,
|
||||
Health: container.Health,
|
||||
Host: container.Host,
|
||||
Tty: container.Tty,
|
||||
Labels: container.Labels,
|
||||
Group: container.Group,
|
||||
Started: timestamppb.New(container.StartedAt),
|
||||
Finished: timestamppb.New(container.FinishedAt),
|
||||
Stats: pbStats,
|
||||
Command: container.Command,
|
||||
MemoryLimit: container.MemoryLimit,
|
||||
CpuLimit: container.CPULimit,
|
||||
FullyLoaded: container.FullyLoaded,
|
||||
}
|
||||
}
|
||||
|
||||
func FromProto(c *pb.Container) Container {
|
||||
var stats []ContainerStat
|
||||
for _, stat := range c.Stats {
|
||||
stats = append(stats, ContainerStat{
|
||||
ID: stat.Id,
|
||||
CPUPercent: stat.CpuPercent,
|
||||
MemoryPercent: stat.MemoryPercent,
|
||||
MemoryUsage: stat.MemoryUsage,
|
||||
})
|
||||
}
|
||||
|
||||
return Container{
|
||||
ID: c.Id,
|
||||
Name: c.Name,
|
||||
Image: c.Image,
|
||||
Labels: c.Labels,
|
||||
Group: c.Group,
|
||||
Created: c.Created.AsTime(),
|
||||
State: c.State,
|
||||
Health: c.Health,
|
||||
Host: c.Host,
|
||||
Tty: c.Tty,
|
||||
Command: c.Command,
|
||||
StartedAt: c.Started.AsTime(),
|
||||
FinishedAt: c.Finished.AsTime(),
|
||||
Stats: utils.RingBufferFrom(300, stats),
|
||||
MemoryLimit: c.MemoryLimit,
|
||||
CPULimit: c.CpuLimit,
|
||||
FullyLoaded: c.FullyLoaded,
|
||||
}
|
||||
}
|
||||
|
||||
// ContainerStat represent stats instant for a container
|
||||
type ContainerStat struct {
|
||||
ID string `json:"id"`
|
||||
|
||||
25
internal/container/types_test.go
Normal file
25
internal/container/types_test.go
Normal file
@@ -0,0 +1,25 @@
|
||||
package container
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/amir20/dozzle/internal/utils"
|
||||
"github.com/go-faker/faker/v4"
|
||||
"github.com/go-faker/faker/v4/pkg/options"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestProto(t *testing.T) {
|
||||
expected := Container{}
|
||||
faker.FakeData(&expected, options.WithFieldsToIgnore("Stats"))
|
||||
expected.FinishedAt = expected.FinishedAt.UTC()
|
||||
expected.Created = expected.Created.UTC()
|
||||
expected.StartedAt = expected.StartedAt.UTC()
|
||||
expected.Stats = utils.NewRingBuffer[ContainerStat](300)
|
||||
|
||||
pb := expected.ToProto()
|
||||
actual := FromProto(&pb)
|
||||
|
||||
assert.Equal(t, expected, actual)
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user