Skip to content

Commit

Permalink
refactor: pass defaultUserID into usage struct (#169)
Browse files Browse the repository at this point in the history
Because

- we don't want to use constant directly in `usage` struct

This commit

- pass `defaultUserID` into `usage` struct
  • Loading branch information
donch1989 authored Dec 31, 2023
1 parent 24187be commit 64d61dc
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 14 deletions.
2 changes: 1 addition & 1 deletion cmd/main/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ func main() {
logger.Info("try to start usage reporter")
go func() {
for {
usg = usage.NewUsage(ctx, service, usageServiceClient, config.Config.Server.Edition)
usg = usage.NewUsage(ctx, service, usageServiceClient, config.Config.Server.Edition, constant.DefaultUserID)
if usg != nil {
usg.StartReporter(ctx)
logger.Info("usage reporter started")
Expand Down
27 changes: 14 additions & 13 deletions pkg/usage/usage.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"fmt"
"time"

"github.com/instill-ai/mgmt-backend/pkg/constant"
"github.com/instill-ai/mgmt-backend/pkg/logger"
"github.com/instill-ai/mgmt-backend/pkg/service"
"github.com/instill-ai/x/repo"
Expand All @@ -25,14 +24,15 @@ type Usage interface {
}

type usage struct {
service service.Service
reporter usageReporter.Reporter
edition string
version string
service service.Service
reporter usageReporter.Reporter
edition string
version string
defaultUserID string
}

// NewUsage initiates a usage instance
func NewUsage(ctx context.Context, s service.Service, usc usagePB.UsageServiceClient, edition string) Usage {
func NewUsage(ctx context.Context, s service.Service, usc usagePB.UsageServiceClient, edition string, defaultUserID string) Usage {
logger, _ := logger.GetZapLogger(ctx)

version, err := repo.ReadReleaseManifest("release-please/manifest.json")
Expand All @@ -42,7 +42,7 @@ func NewUsage(ctx context.Context, s service.Service, usc usagePB.UsageServiceCl
}

var defaultOwnerUID string
if user, err := s.GetUserAdmin(ctx, constant.DefaultUserID); err == nil {
if user, err := s.GetUserAdmin(ctx, defaultUserID); err == nil {
defaultOwnerUID = *user.Uid
} else {
logger.Error(err.Error())
Expand All @@ -55,10 +55,11 @@ func NewUsage(ctx context.Context, s service.Service, usc usagePB.UsageServiceCl
}

return &usage{
service: s,
reporter: reporter,
edition: edition,
version: version,
service: s,
reporter: reporter,
edition: edition,
version: version,
defaultUserID: defaultUserID,
}
}

Expand Down Expand Up @@ -103,7 +104,7 @@ func (u *usage) StartReporter(ctx context.Context) {
logger, _ := logger.GetZapLogger(ctx)

var defaultOwnerUID string
if user, err := u.service.GetUserAdmin(ctx, constant.DefaultUserID); err == nil {
if user, err := u.service.GetUserAdmin(ctx, u.defaultUserID); err == nil {
defaultOwnerUID = *user.Uid
} else {
logger.Error(err.Error())
Expand All @@ -126,7 +127,7 @@ func (u *usage) TriggerSingleReporter(ctx context.Context) {
logger, _ := logger.GetZapLogger(ctx)

var defaultOwnerUID string
if user, err := u.service.GetUserAdmin(ctx, constant.DefaultUserID); err == nil {
if user, err := u.service.GetUserAdmin(ctx, u.defaultUserID); err == nil {
defaultOwnerUID = *user.Uid
} else {
logger.Error(err.Error())
Expand Down

0 comments on commit 64d61dc

Please sign in to comment.