122 lines
3.1 KiB
Go
122 lines
3.1 KiB
Go
package protocol
|
|
|
|
import (
|
|
"errors"
|
|
|
|
"git.netflux.io/rob/octoplex/internal/domain"
|
|
pb "git.netflux.io/rob/octoplex/internal/generated/grpc"
|
|
"google.golang.org/protobuf/types/known/timestamppb"
|
|
)
|
|
|
|
func containerToProto(c domain.Container) *pb.Container {
|
|
var errString string
|
|
if c.Err != nil {
|
|
errString = c.Err.Error()
|
|
}
|
|
|
|
var exitCode *int32
|
|
if c.ExitCode != nil {
|
|
code := int32(*c.ExitCode)
|
|
exitCode = &code
|
|
}
|
|
|
|
return &pb.Container{
|
|
Id: c.ID,
|
|
Status: c.Status,
|
|
HealthState: c.HealthState,
|
|
CpuPercent: c.CPUPercent,
|
|
MemoryUsageBytes: c.MemoryUsageBytes,
|
|
RxRate: int32(c.RxRate),
|
|
TxRate: int32(c.TxRate),
|
|
RxSince: timestamppb.New(c.RxSince),
|
|
ImageName: c.ImageName,
|
|
PullStatus: c.PullStatus,
|
|
PullProgress: c.PullProgress,
|
|
PullPercent: int32(c.PullPercent),
|
|
RestartCount: int32(c.RestartCount),
|
|
ExitCode: exitCode,
|
|
Err: errString,
|
|
}
|
|
}
|
|
func protoToContainer(pbCont *pb.Container) domain.Container {
|
|
if pbCont == nil {
|
|
return domain.Container{}
|
|
}
|
|
|
|
var exitCode *int
|
|
if pbCont.ExitCode != nil {
|
|
val := int(*pbCont.ExitCode)
|
|
exitCode = &val
|
|
}
|
|
|
|
var err error
|
|
if pbCont.Err != "" {
|
|
err = errors.New(pbCont.Err)
|
|
}
|
|
|
|
return domain.Container{
|
|
ID: pbCont.Id,
|
|
Status: pbCont.Status,
|
|
HealthState: pbCont.HealthState,
|
|
CPUPercent: pbCont.CpuPercent,
|
|
MemoryUsageBytes: pbCont.MemoryUsageBytes,
|
|
RxRate: int(pbCont.RxRate),
|
|
TxRate: int(pbCont.TxRate),
|
|
RxSince: pbCont.RxSince.AsTime(),
|
|
ImageName: pbCont.ImageName,
|
|
PullStatus: pbCont.PullStatus,
|
|
PullProgress: pbCont.PullProgress,
|
|
PullPercent: int(pbCont.PullPercent),
|
|
RestartCount: int(pbCont.RestartCount),
|
|
ExitCode: exitCode,
|
|
Err: err,
|
|
}
|
|
}
|
|
|
|
func destinationsToProto(inDests []domain.Destination) []*pb.Destination {
|
|
destinations := make([]*pb.Destination, 0, len(inDests))
|
|
for _, d := range inDests {
|
|
destinations = append(destinations, destinationToProto(d))
|
|
}
|
|
return destinations
|
|
}
|
|
|
|
func destinationToProto(d domain.Destination) *pb.Destination {
|
|
return &pb.Destination{
|
|
Container: containerToProto(d.Container),
|
|
Status: destinationStatusToProto(d.Status),
|
|
Name: d.Name,
|
|
Url: d.URL,
|
|
}
|
|
}
|
|
|
|
func protoToDestinations(pbDests []*pb.Destination) []domain.Destination {
|
|
if pbDests == nil {
|
|
return nil
|
|
}
|
|
|
|
dests := make([]domain.Destination, 0, len(pbDests))
|
|
for _, pbDest := range pbDests {
|
|
if pbDest == nil {
|
|
continue
|
|
}
|
|
dests = append(dests, domain.Destination{
|
|
Container: protoToContainer(pbDest.Container),
|
|
Status: domain.DestinationStatus(pbDest.Status), // direct cast, same underlying int
|
|
Name: pbDest.Name,
|
|
URL: pbDest.Url,
|
|
})
|
|
}
|
|
return dests
|
|
}
|
|
func destinationStatusToProto(s domain.DestinationStatus) pb.Destination_Status {
|
|
switch s {
|
|
case domain.DestinationStatusStarting:
|
|
return pb.Destination_STATUS_STARTING
|
|
case domain.DestinationStatusLive:
|
|
return pb.Destination_STATUS_LIVE
|
|
default:
|
|
return pb.Destination_STATUS_OFF_AIR
|
|
}
|
|
}
|