diff --git a/components/usage/pkg/contentservice/client.go b/components/usage/pkg/contentservice/client.go index 6234fbfbef47c9..6e09d81221fba3 100644 --- a/components/usage/pkg/contentservice/client.go +++ b/components/usage/pkg/contentservice/client.go @@ -5,19 +5,22 @@ package contentservice import ( + "bytes" + "compress/gzip" "context" + "encoding/json" "fmt" - "io" "net/http" "github.com/gitpod-io/gitpod/common-go/log" "github.com/gitpod-io/gitpod/content-service/api" + "github.com/gitpod-io/gitpod/usage/pkg/db" "google.golang.org/grpc" "google.golang.org/grpc/credentials/insecure" ) type Interface interface { - UploadFile(ctx context.Context, filename string, body io.Reader) error + UploadUsageReport(ctx context.Context, filename string, report map[db.AttributionID][]db.WorkspaceInstanceForUsage) error } type Client struct { @@ -28,13 +31,24 @@ func New(url string) *Client { return &Client{url: url} } -func (c *Client) UploadFile(ctx context.Context, filename string, body io.Reader) error { +func (c *Client) UploadUsageReport(ctx context.Context, filename string, report map[db.AttributionID][]db.WorkspaceInstanceForUsage) error { url, err := c.getSignedUploadUrl(ctx, filename) if err != nil { return fmt.Errorf("failed to obtain signed upload URL: %w", err) } - req, err := http.NewRequest(http.MethodPut, url, body) + reportBytes := &bytes.Buffer{} + gz := gzip.NewWriter(reportBytes) + err = json.NewEncoder(gz).Encode(report) + if err != nil { + return fmt.Errorf("failed to marshal report to JSON: %w", err) + } + err = gz.Close() + if err != nil { + return fmt.Errorf("failed to compress usage report: %w", err) + } + + req, err := http.NewRequest(http.MethodPut, url, reportBytes) if err != nil { return fmt.Errorf("failed to construct http request: %w", err) } diff --git a/components/usage/pkg/contentservice/noop.go b/components/usage/pkg/contentservice/noop.go index 152c407895eec8..d3c974a6f1d943 100644 --- a/components/usage/pkg/contentservice/noop.go +++ b/components/usage/pkg/contentservice/noop.go @@ -6,11 +6,12 @@ package contentservice import ( "context" - "io" + + "github.com/gitpod-io/gitpod/usage/pkg/db" ) type NoOpClient struct{} -func (c *NoOpClient) UploadFile(ctx context.Context, filename string, body io.Reader) error { +func (c *NoOpClient) UploadUsageReport(ctx context.Context, filename string, report map[db.AttributionID][]db.WorkspaceInstanceForUsage) error { return nil } diff --git a/components/usage/pkg/controller/reconciler.go b/components/usage/pkg/controller/reconciler.go index c72af465ac4fb4..4d3a0cca030e6f 100644 --- a/components/usage/pkg/controller/reconciler.go +++ b/components/usage/pkg/controller/reconciler.go @@ -5,11 +5,8 @@ package controller import ( - "bytes" - "compress/gzip" "context" "database/sql" - "encoding/json" "fmt" "math" "time" @@ -75,24 +72,13 @@ func (u *UsageReconciler) Reconcile() (err error) { } log.WithField("usage_reconcile_status", status).Info("Reconcile completed.") - reportBytes := &bytes.Buffer{} - gz := gzip.NewWriter(reportBytes) - err = json.NewEncoder(gz).Encode(report) - if err != nil { - return fmt.Errorf("failed to marshal report to JSON: %w", err) - } - err = gz.Close() - if err != nil { - return fmt.Errorf("failed to compress usage report: %w", err) - } - err = db.CreateUsageRecords(ctx, u.conn, usageReportToUsageRecords(report, u.pricer, u.nowFunc().UTC())) if err != nil { return fmt.Errorf("failed to write usage records to database: %s", err) } filename := fmt.Sprintf("%s.gz", now.Format(time.RFC3339)) - err = u.contentService.UploadFile(ctx, filename, reportBytes) + err = u.contentService.UploadUsageReport(ctx, filename, report) if err != nil { return fmt.Errorf("failed to upload usage report: %w", err) }