Skip to content

[usage] Fix report to JSON conversion #12545

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions components/usage/pkg/contentservice/usage_report_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// Copyright (c) 2022 Gitpod GmbH. All rights reserved.
// Licensed under the GNU Affero General Public License (AGPL).
// See License-AGPL.txt in the project root for license information.

package contentservice

import (
"database/sql"
"encoding/json"
"fmt"
"github.com/gitpod-io/gitpod/usage/pkg/db"
"github.com/gitpod-io/gitpod/usage/pkg/db/dbtest"
"github.com/google/uuid"
"github.com/stretchr/testify/require"
"testing"
"time"
)

func TestUsageReport_ToJSON(t *testing.T) {
report := UsageReport{
GenerationTime: time.Now().UTC(),
From: time.Now().UTC(),
To: time.Now().UTC(),
RawSessions: []db.WorkspaceInstanceForUsage{
{
ID: uuid.New(),
WorkspaceID: dbtest.GenerateWorkspaceID(),
OwnerID: uuid.New(),
ProjectID: sql.NullString{
String: "project-id",
Valid: true,
},
WorkspaceClass: "workspace-class",
Type: "regular",
UsageAttributionID: db.NewTeamAttributionID(uuid.New().String()),
CreationTime: db.NewVarcharTime(time.Now()),
StartedTime: db.NewVarcharTime(time.Now()),
StoppingTime: db.NewVarcharTime(time.Now()),
StoppedTime: db.NewVarcharTime(time.Now()),
},
},
InvalidSessions: []InvalidSession{
{
Reason: "some-reason",
Session: db.WorkspaceInstanceForUsage{},
},
},
UsageRecords: []db.WorkspaceInstanceUsage{
dbtest.NewWorkspaceInstanceUsage(t, db.WorkspaceInstanceUsage{}),
},
}

b, err := json.Marshal(report)
require.NoError(t, err)

var actual UsageReport
err = json.Unmarshal(b, &actual)
require.NoError(t, err)

fmt.Println(report, actual)
require.EqualValues(t, report, actual)

}
33 changes: 28 additions & 5 deletions components/usage/pkg/db/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (

func NewVarcharTime(t time.Time) VarcharTime {
return VarcharTime{
t: t,
t: t.UTC(),
valid: true,
}
}
Expand Down Expand Up @@ -92,12 +92,35 @@ func (n VarcharTime) String() string {
return ""
}

func (u VarcharTime) MarshalJSON() ([]byte, error) {
if !u.IsSet() {
return []byte(""), nil
var null = "null"

func (n VarcharTime) MarshalJSON() ([]byte, error) {
if !n.IsSet() {
return []byte(null), nil
}

return n.Time().UTC().MarshalJSON()
}

func (n *VarcharTime) UnmarshalJSON(data []byte) error {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was adding this method necessary to fix the upload or just for the round-trip test?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was necessary for the report download to serialise correctly. We'd convert it into a string, but on unmarshal we'd try to put it into the VarcharTime struct which would fail

if string(data) == null {
n.valid = false
return nil
}

return u.Time().MarshalJSON()
t := time.Time{}
if err := t.UnmarshalJSON(data); err != nil {
return fmt.Errorf("failed to unmarshal VarcharTime %s: %w", string(data), err)
}

if t.IsZero() {
return nil
}

n.valid = true
n.t = t.UTC()

return nil
}

const ISO8601Format = "2006-01-02T15:04:05.000Z"
Expand Down