Skip to content

Commit

Permalink
Add Store/Component interface (#185)
Browse files Browse the repository at this point in the history
* generate store interface

* refactor components and handlers

* add lint/test ci

* update lint timeout

* update lint ci

* update lint ci

---------

Co-authored-by: yiling <[email protected]>
  • Loading branch information
Yiling-J and yiling authored Nov 19, 2024
1 parent ad82453 commit 0745f31
Show file tree
Hide file tree
Showing 138 changed files with 2,330 additions and 1,261 deletions.
41 changes: 41 additions & 0 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
name: Go

on:
push:
branches: ['*', '*/*']
tags: ['v*']
pull_request:
branches: ['*']

permissions:
contents: read

jobs:
lint:
name: lint
runs-on: ubuntu-latest
steps:
- uses: actions/setup-go@v5
with:
go-version: '1.21'
- uses: actions/checkout@v4
- name: golangci-lint
uses: golangci/golangci-lint-action@v6
with:
args: --timeout=5m
test:
name: test
strategy:
matrix:
go: ["1.21.x"]
runs-on: ubuntu-latest
steps:
- name: Setup Go
with:
go-version: ${{ matrix.go }}
uses: actions/setup-go@v2

- uses: actions/checkout@v2

- name: Test
run: go test ./...
17 changes: 11 additions & 6 deletions accounting/component/metering.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,23 @@ import (
"opencsg.com/csghub-server/common/types"
)

type MeteringComponent struct {
ams *database.AccountMeteringStore
type meteringComponentImpl struct {
ams database.AccountMeteringStore
}

func NewMeteringComponent() *MeteringComponent {
ams := &MeteringComponent{
type MeteringComponent interface {
SaveMeteringEventRecord(ctx context.Context, req *types.METERING_EVENT) error
ListMeteringByUserIDAndDate(ctx context.Context, req types.ACCT_STATEMENTS_REQ) ([]database.AccountMetering, int, error)
}

func NewMeteringComponent() MeteringComponent {
ams := &meteringComponentImpl{
ams: database.NewAccountMeteringStore(),
}
return ams
}

func (mc *MeteringComponent) SaveMeteringEventRecord(ctx context.Context, req *types.METERING_EVENT) error {
func (mc *meteringComponentImpl) SaveMeteringEventRecord(ctx context.Context, req *types.METERING_EVENT) error {
am := database.AccountMetering{
EventUUID: req.Uuid,
UserUUID: req.UserUUID,
Expand All @@ -41,7 +46,7 @@ func (mc *MeteringComponent) SaveMeteringEventRecord(ctx context.Context, req *t
return nil
}

func (mc *MeteringComponent) ListMeteringByUserIDAndDate(ctx context.Context, req types.ACCT_STATEMENTS_REQ) ([]database.AccountMetering, int, error) {
func (mc *meteringComponentImpl) ListMeteringByUserIDAndDate(ctx context.Context, req types.ACCT_STATEMENTS_REQ) ([]database.AccountMetering, int, error) {
meters, total, err := mc.ams.ListByUserIDAndTime(ctx, req)
if err != nil {
return nil, 0, fmt.Errorf("failed to list metering by UserIDAndDate, error: %w", err)
Expand Down
2 changes: 1 addition & 1 deletion accounting/consumer/metering.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (

type Metering struct {
sysMQ *mq.NatsHandler
meterComp *component.MeteringComponent
meterComp component.MeteringComponent
}

func NewMetering(natHandler *mq.NatsHandler, config *config.Config) *Metering {
Expand Down
2 changes: 1 addition & 1 deletion accounting/handler/metering.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func NewMeteringHandler() (*MeteringHandler, error) {
}

type MeteringHandler struct {
amc *component.MeteringComponent
amc component.MeteringComponent
}

func (mh *MeteringHandler) QueryMeteringStatementByUserID(ctx *gin.Context) {
Expand Down
2 changes: 1 addition & 1 deletion api/handler/accounting.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
)

type AccountingHandler struct {
ac *component.AccountingComponent
ac component.AccountingComponent
apiToken string
}

Expand Down
2 changes: 1 addition & 1 deletion api/handler/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func NewClusterHandler(config *config.Config) (*ClusterHandler, error) {
}

type ClusterHandler struct {
c *component.ClusterComponent
c component.ClusterComponent
}

// Getclusters godoc
Expand Down
4 changes: 2 additions & 2 deletions api/handler/code.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ func NewCodeHandler(config *config.Config) (*CodeHandler, error) {
}

type CodeHandler struct {
c *component.CodeComponent
sc *component.SensitiveComponent
c component.CodeComponent
sc component.SensitiveComponent
}

// CreateCode godoc
Expand Down
4 changes: 2 additions & 2 deletions api/handler/collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ func NewCollectionHandler(cfg *config.Config) (*CollectionHandler, error) {
}

type CollectionHandler struct {
cc *component.CollectionComponent
sc *component.SensitiveComponent
cc component.CollectionComponent
sc component.SensitiveComponent
}

// GetCollections godoc
Expand Down
17 changes: 12 additions & 5 deletions api/handler/dataset.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,22 @@ func NewDatasetHandler(config *config.Config) (*DatasetHandler, error) {
if err != nil {
return nil, fmt.Errorf("error creating sensitive component:%w", err)
}
repo, err := component.NewRepoComponent(config)
if err != nil {
return nil, fmt.Errorf("error creating repo component:%w", err)
}

return &DatasetHandler{
c: tc,
sc: sc,
c: tc,
sc: sc,
repo: repo,
}, nil
}

type DatasetHandler struct {
c *component.DatasetComponent
sc *component.SensitiveComponent
c component.DatasetComponent
sc component.SensitiveComponent
repo component.RepoComponent
}

// CreateDataset godoc
Expand Down Expand Up @@ -344,7 +351,7 @@ func (h *DatasetHandler) AllFiles(ctx *gin.Context) {
req.RepoType = types.DatasetRepo
req.CurrentUser = httpbase.GetCurrentUser(ctx)
req.Ref = ""
detail, err := h.c.AllFiles(ctx, req)
detail, err := h.repo.AllFiles(ctx, req)
if err != nil {
if errors.Is(err, component.ErrUnauthorized) {
httpbase.UnauthorizedError(ctx, err)
Expand Down
2 changes: 1 addition & 1 deletion api/handler/dataset_viewer.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
)

type DatasetViewerHandler struct {
c *component.DatasetViewerComponent
c component.DatasetViewerComponent
}

func NewDatasetViewerHandler(cfg *config.Config) (*DatasetViewerHandler, error) {
Expand Down
4 changes: 2 additions & 2 deletions api/handler/discussion.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ import (
)

type DiscussionHandler struct {
c *component.DiscussionComponent
sc *component.SensitiveComponent
c component.DiscussionComponent
sc component.SensitiveComponent
}

func NewDiscussionHandler(cfg *config.Config) (*DiscussionHandler, error) {
Expand Down
2 changes: 1 addition & 1 deletion api/handler/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
)

type EventHandler struct {
ec *component.EventComponent
ec component.EventComponent
}

func NewEventHandler() (*EventHandler, error) {
Expand Down
2 changes: 1 addition & 1 deletion api/handler/git_http.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func NewGitHTTPHandler(config *config.Config) (*GitHTTPHandler, error) {
}

type GitHTTPHandler struct {
c *component.GitHTTPComponent
c component.GitHTTPComponent
}

func (h *GitHTTPHandler) InfoRefs(ctx *gin.Context) {
Expand Down
2 changes: 1 addition & 1 deletion api/handler/hf_dataset.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func NewHFDatasetHandler(config *config.Config) (*HFDatasetHandler, error) {
}

type HFDatasetHandler struct {
dc *component.HFDatasetComponent
dc component.HFDatasetComponent
}

func (h *HFDatasetHandler) DatasetPathsInfo(ctx *gin.Context) {
Expand Down
2 changes: 1 addition & 1 deletion api/handler/internal.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func NewInternalHandler(config *config.Config) (*InternalHandler, error) {
}

type InternalHandler struct {
c *component.InternalComponent
c component.InternalComponent
config *config.Config
}

Expand Down
4 changes: 2 additions & 2 deletions api/handler/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ func NewListHandler(config *config.Config) (*ListHandler, error) {
}

type ListHandler struct {
c *component.ListComponent
sc *component.SpaceComponent
c component.ListComponent
sc component.SpaceComponent
}

// ListTrendingModels godoc
Expand Down
2 changes: 1 addition & 1 deletion api/handler/mirror.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func NewMirrorHandler(config *config.Config) (*MirrorHandler, error) {
}

type MirrorHandler struct {
mc *component.MirrorComponent
mc component.MirrorComponent
}

// CreateMirrorRepo godoc
Expand Down
2 changes: 1 addition & 1 deletion api/handler/mirror_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func NewMirrorSourceHandler(config *config.Config) (*MirrorSourceHandler, error)
}

type MirrorSourceHandler struct {
c *component.MirrorSourceComponent
c component.MirrorSourceComponent
}

// CreateMirrorSource godoc
Expand Down
37 changes: 22 additions & 15 deletions api/handler/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,22 @@ func NewModelHandler(config *config.Config) (*ModelHandler, error) {
if err != nil {
return nil, fmt.Errorf("error creating sensitive component:%w", err)
}
repo, err := component.NewRepoComponent(config)
if err != nil {
return nil, fmt.Errorf("error creating repo component:%w", err)
}

return &ModelHandler{
c: uc,
sc: sc,
c: uc,
sc: sc,
repo: repo,
}, nil
}

type ModelHandler struct {
c *component.ModelComponent
sc *component.SensitiveComponent
c component.ModelComponent
sc component.SensitiveComponent
repo component.RepoComponent
}

// GetVisiableModels godoc
Expand Down Expand Up @@ -604,7 +611,7 @@ func (h *ModelHandler) DeployDedicated(ctx *gin.Context) {
httpbase.BadRequest(ctx, err.Error())
return
}
allow, err := h.c.AllowReadAccess(ctx, types.ModelRepo, namespace, name, currentUser)
allow, err := h.repo.AllowReadAccess(ctx, types.ModelRepo, namespace, name, currentUser)
if err != nil {
slog.Error("failed to check user permission", "error", err)
httpbase.ServerError(ctx, errors.New("failed to check user permission"))
Expand Down Expand Up @@ -691,7 +698,7 @@ func (h *ModelHandler) FinetuneCreate(ctx *gin.Context) {
httpbase.BadRequest(ctx, err.Error())
return
}
allow, err := h.c.AllowAdminAccess(ctx, types.ModelRepo, namespace, name, currentUser)
allow, err := h.repo.AllowAdminAccess(ctx, types.ModelRepo, namespace, name, currentUser)
if err != nil {
slog.Error("failed to check user permission", "error", err)
httpbase.ServerError(ctx, errors.New("failed to check user permission"))
Expand Down Expand Up @@ -791,7 +798,7 @@ func (h *ModelHandler) DeployDelete(ctx *gin.Context) {
DeployID: id,
DeployType: types.InferenceType,
}
err = h.c.DeleteDeploy(ctx, delReq)
err = h.repo.DeleteDeploy(ctx, delReq)
if err != nil {
slog.Error("Failed to delete deploy", slog.Any("error", err))
httpbase.ServerError(ctx, err)
Expand Down Expand Up @@ -846,7 +853,7 @@ func (h *ModelHandler) FinetuneDelete(ctx *gin.Context) {
DeployID: id,
DeployType: types.FinetuneType,
}
err = h.c.DeleteDeploy(ctx, delReq)
err = h.repo.DeleteDeploy(ctx, delReq)
if err != nil {
slog.Error("Failed to delete deploy", slog.Any("error", err))
httpbase.ServerError(ctx, err)
Expand Down Expand Up @@ -900,7 +907,7 @@ func (h *ModelHandler) DeployStop(ctx *gin.Context) {
DeployID: id,
DeployType: types.InferenceType,
}
err = h.c.DeployStop(ctx, stopReq)
err = h.repo.DeployStop(ctx, stopReq)
if err != nil {
slog.Error("Failed to stop deploy", slog.Any("error", err))
httpbase.ServerError(ctx, err)
Expand Down Expand Up @@ -956,7 +963,7 @@ func (h *ModelHandler) DeployStart(ctx *gin.Context) {
DeployType: types.InferenceType,
}

err = h.c.DeployStart(ctx, startReq)
err = h.repo.DeployStart(ctx, startReq)
if err != nil {
slog.Error("Failed to start deploy", slog.Any("error", err), slog.Any("repoType", types.ModelRepo), slog.String("namespace", namespace), slog.String("name", name), slog.Any("deployID", id))
httpbase.ServerError(ctx, err)
Expand Down Expand Up @@ -1069,7 +1076,7 @@ func (h *ModelHandler) FinetuneStop(ctx *gin.Context) {
DeployID: id,
DeployType: types.FinetuneType,
}
err = h.c.DeployStop(ctx, stopReq)
err = h.repo.DeployStop(ctx, stopReq)
if err != nil {
slog.Error("Failed to stop deploy", slog.Any("error", err))
httpbase.ServerError(ctx, err)
Expand Down Expand Up @@ -1123,7 +1130,7 @@ func (h *ModelHandler) FinetuneStart(ctx *gin.Context) {
DeployID: id,
DeployType: types.FinetuneType,
}
err = h.c.DeployStart(ctx, startReq)
err = h.repo.DeployStart(ctx, startReq)
if err != nil {
slog.Error("Failed to start deploy", slog.Any("error", err), slog.Any("repoType", types.ModelRepo), slog.String("namespace", namespace), slog.String("name", name), slog.Any("deployID", id))
httpbase.ServerError(ctx, err)
Expand Down Expand Up @@ -1351,7 +1358,7 @@ func (h *ModelHandler) AllFiles(ctx *gin.Context) {
req.Name = name
req.RepoType = types.ModelRepo
req.CurrentUser = httpbase.GetCurrentUser(ctx)
detail, err := h.c.AllFiles(ctx, req)
detail, err := h.repo.AllFiles(ctx, req)
if err != nil {
if errors.Is(err, component.ErrUnauthorized) {
httpbase.UnauthorizedError(ctx, err)
Expand Down Expand Up @@ -1482,7 +1489,7 @@ func (h *ModelHandler) ServerlessStart(ctx *gin.Context) {
DeployType: types.ServerlessType,
}

err = h.c.DeployStart(ctx, startReq)
err = h.repo.DeployStart(ctx, startReq)
if err != nil {
slog.Error("Failed to start deploy", slog.Any("error", err), slog.Any("repoType", types.ModelRepo), slog.String("namespace", namespace), slog.String("name", name), slog.Any("deployID", id))
httpbase.ServerError(ctx, err)
Expand Down Expand Up @@ -1538,7 +1545,7 @@ func (h *ModelHandler) ServerlessStop(ctx *gin.Context) {
DeployType: types.ServerlessType,
}

err = h.c.DeployStop(ctx, stopReq)
err = h.repo.DeployStop(ctx, stopReq)
if err != nil {
slog.Error("Failed to stop deploy", slog.Any("error", err))
httpbase.ServerError(ctx, err)
Expand Down
2 changes: 1 addition & 1 deletion api/handler/multi_sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
)

type SyncHandler struct {
c *component.MultiSyncComponent
c component.MultiSyncComponent
}

func NewSyncHandler(config *config.Config) (*SyncHandler, error) {
Expand Down
Loading

0 comments on commit 0745f31

Please sign in to comment.