From b96328e098c37c8c04647d430c510e7e5fb18298 Mon Sep 17 00:00:00 2001 From: Malcolm Lockyer Date: Fri, 23 May 2025 12:42:45 +1200 Subject: [PATCH] fix(async-perf): In async poll snapshot handling, reduce redundant json marshal [be-11861] (#726) --- api/dataservices/snapshot/snapshot.go | 17 +++++++++++++++++ api/dataservices/snapshot/tx.go | 16 ++++++++++++++++ api/portainer.go | 7 +++++++ 3 files changed, 40 insertions(+) diff --git a/api/dataservices/snapshot/snapshot.go b/api/dataservices/snapshot/snapshot.go index 155077677..c0066317d 100644 --- a/api/dataservices/snapshot/snapshot.go +++ b/api/dataservices/snapshot/snapshot.go @@ -51,3 +51,20 @@ func (service *Service) ReadWithoutSnapshotRaw(ID portainer.EndpointID) (*portai return snapshot, err } + +func (service *Service) ReadRawMessage(ID portainer.EndpointID) (*portainer.SnapshotRawMessage, error) { + var snapshot *portainer.SnapshotRawMessage + + err := service.Connection.ViewTx(func(tx portainer.Transaction) error { + var err error + snapshot, err = service.Tx(tx).ReadRawMessage(ID) + + return err + }) + + return snapshot, err +} + +func (service *Service) CreateRawMessage(snapshot *portainer.SnapshotRawMessage) error { + return service.Connection.CreateObjectWithId(BucketName, int(snapshot.EndpointID), snapshot) +} diff --git a/api/dataservices/snapshot/tx.go b/api/dataservices/snapshot/tx.go index 8a8dcc1c2..45d1df9fc 100644 --- a/api/dataservices/snapshot/tx.go +++ b/api/dataservices/snapshot/tx.go @@ -35,3 +35,19 @@ func (service ServiceTx) ReadWithoutSnapshotRaw(ID portainer.EndpointID) (*porta return &snapshot.Snapshot, nil } + +func (service ServiceTx) ReadRawMessage(ID portainer.EndpointID) (*portainer.SnapshotRawMessage, error) { + var snapshot = portainer.SnapshotRawMessage{} + + identifier := service.Connection.ConvertToKey(int(ID)) + + if err := service.Tx.GetObject(service.Bucket, identifier, &snapshot); err != nil { + return nil, err + } + + return &snapshot, nil +} + +func (service ServiceTx) CreateRawMessage(snapshot *portainer.SnapshotRawMessage) error { + return service.Tx.CreateObjectWithId(BucketName, int(snapshot.EndpointID), snapshot) +} diff --git a/api/portainer.go b/api/portainer.go index 60e5c7a1f..f61bb1af2 100644 --- a/api/portainer.go +++ b/api/portainer.go @@ -13,6 +13,7 @@ import ( gittypes "github.com/portainer/portainer/api/git/types" models "github.com/portainer/portainer/api/http/models/kubernetes" "github.com/portainer/portainer/pkg/featureflags" + "github.com/segmentio/encoding/json" "golang.org/x/oauth2" corev1 "k8s.io/api/core/v1" @@ -1374,6 +1375,12 @@ type ( Kubernetes *KubernetesSnapshot `json:"Kubernetes"` } + SnapshotRawMessage struct { + EndpointID EndpointID `json:"EndpointId"` + Docker json.RawMessage `json:"Docker"` + Kubernetes json.RawMessage `json:"Kubernetes"` + } + // CLIService represents a service for managing CLI CLIService interface { ParseFlags(version string) (*CLIFlags, error)