Skip to content

Commit

Permalink
fix up
Browse files Browse the repository at this point in the history
  • Loading branch information
easyCZ committed Jun 30, 2022
1 parent 70db5d6 commit 69e5cac
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 61 deletions.
2 changes: 1 addition & 1 deletion components/usage/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ require (
github.com/gitpod-io/gitpod/common-go v0.0.0-00010101000000-000000000000
github.com/go-sql-driver/mysql v1.6.0
github.com/google/uuid v1.1.2
github.com/prometheus/client_golang v1.12.1
github.com/relvacode/iso8601 v1.1.0
github.com/robfig/cron v1.2.0
github.com/sirupsen/logrus v1.8.1
Expand All @@ -84,7 +85,6 @@ require (
github.com/matttproud/golang_protobuf_extensions v1.0.1 // indirect
github.com/opentracing/opentracing-go v1.2.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/prometheus/client_golang v1.12.1 // indirect
github.com/prometheus/client_model v0.2.0 // indirect
github.com/prometheus/common v0.32.1 // indirect
github.com/prometheus/procfs v0.7.3 // indirect
Expand Down
2 changes: 1 addition & 1 deletion components/usage/pkg/controller/billing.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ type WorkspacePricer struct {
creditMinutesByWorkspaceClass map[string]float64
}

func (p *WorkspacePricer) Credits(workspaceClass string, runtimeInSeconds uint64) int64 {
func (p *WorkspacePricer) Credits(workspaceClass string, runtimeInSeconds int64) int64 {
inMinutes := float64(runtimeInSeconds) / 60
return int64(math.Ceil(p.CreditsPerMinuteForClass(workspaceClass) * inMinutes))
}
Expand Down
62 changes: 62 additions & 0 deletions components/usage/pkg/controller/billing_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// 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 controller

import (
"github.com/stretchr/testify/require"
"testing"
)

func TestWorkspacePricer_Default(t *testing.T) {
testCases := []struct {
Name string
Seconds int64
ExpectedCredits int64
}{
{
Name: "0 seconds",
Seconds: 0,
ExpectedCredits: 0,
},
{
Name: "1 second",
Seconds: 1,
ExpectedCredits: 1,
},
{
Name: "60 seconds",
Seconds: 60,
ExpectedCredits: 1,
},
{
Name: "90 seconds",
Seconds: 90,
ExpectedCredits: 1,
},
{
Name: "6 minutes",
Seconds: 360,
ExpectedCredits: 1,
},
{
Name: "6 minutes and 1 second",
Seconds: 361,
ExpectedCredits: 2,
},
{
Name: "1 hour",
Seconds: 3600,
ExpectedCredits: 10,
},
}

for _, tc := range testCases {
t.Run(tc.Name, func(t *testing.T) {
actualCredits := DefaultWorkspacePricer.Credits(defaultWorkspaceClass, tc.Seconds)

require.Equal(t, tc.ExpectedCredits, actualCredits)
})
}
}
5 changes: 0 additions & 5 deletions components/usage/pkg/controller/reconciler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,6 @@ func TestUsageReconciler_ReconcileTimeRange(t *testing.T) {
}),
}

expectedRuntime := instances[0].WorkspaceRuntimeSeconds(scenarioRunTime) + instances[1].WorkspaceRuntimeSeconds(scenarioRunTime)

conn := dbtest.ConnectForTests(t)
dbtest.CreateWorkspaceInstances(t, conn, instances...)

Expand All @@ -66,7 +64,4 @@ func TestUsageReconciler_ReconcileTimeRange(t *testing.T) {
WorkspaceInstances: 2,
InvalidWorkspaceInstances: 1,
}, status)
require.Equal(t, map[string]int64{
teamID.String(): int64(expectedRuntime),
}, report.CreditSummaryForTeams(scenarioRunTime))
}
4 changes: 2 additions & 2 deletions components/usage/pkg/db/workspace_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ type WorkspaceInstance struct {

// WorkspaceRuntimeSeconds computes how long this WorkspaceInstance has been running.
// If the instance is still running (no stop time set), maxStopTime is used to to compute the duration - this is an upper bound on stop
func (i *WorkspaceInstance) WorkspaceRuntimeSeconds(maxStopTime time.Time) uint64 {
func (i *WorkspaceInstance) WorkspaceRuntimeSeconds(maxStopTime time.Time) int64 {
start := i.CreationTime.Time()
stop := maxStopTime

Expand All @@ -56,7 +56,7 @@ func (i *WorkspaceInstance) WorkspaceRuntimeSeconds(maxStopTime time.Time) uint6
}
}

return uint64(stop.Sub(start).Round(time.Second).Seconds())
return int64(stop.Sub(start).Round(time.Second).Seconds())
}

// TableName sets the insert table name for this struct type
Expand Down
52 changes: 0 additions & 52 deletions components/usage/pkg/stripe/stripe_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,55 +83,3 @@ func TestCustomerQueriesForTeamIds_MultipleQueries(t *testing.T) {
})
}
}

func TestWorkspaceSecondsToCreditsCalcuation(t *testing.T) {
testCases := []struct {
Name string
Seconds int64
ExpectedCredits int64
}{
{
Name: "0 seconds",
Seconds: 0,
ExpectedCredits: 0,
},
{
Name: "1 second",
Seconds: 1,
ExpectedCredits: 1,
},
{
Name: "60 seconds",
Seconds: 60,
ExpectedCredits: 1,
},
{
Name: "90 seconds",
Seconds: 90,
ExpectedCredits: 1,
},
{
Name: "6 minutes",
Seconds: 360,
ExpectedCredits: 1,
},
{
Name: "6 minutes and 1 second",
Seconds: 361,
ExpectedCredits: 2,
},
{
Name: "1 hour",
Seconds: 3600,
ExpectedCredits: 10,
},
}

for _, tc := range testCases {
t.Run(tc.Name, func(t *testing.T) {
actualCredits := workspaceSecondsToCredits(tc.Seconds)

require.Equal(t, tc.ExpectedCredits, actualCredits)
})
}
}

0 comments on commit 69e5cac

Please sign in to comment.