From c8e543dd8ba9204c8df13f65184009cb7640c965 Mon Sep 17 00:00:00 2001 From: Gary Date: Thu, 1 Aug 2024 15:41:30 +0800 Subject: [PATCH] feat(catalog): rename endpoint from kb to catalog (#56) Because we need to rename kb to catalog This commit change naming of the kb to catalog --- Makefile | 2 +- go.mod | 2 +- go.sum | 4 +- pkg/handler/knowledgebase.go | 95 ++++++++++++++++--------------- pkg/handler/knowledgebasefiles.go | 78 ++++++++++++------------- pkg/handler/retrieval.go | 16 +++--- pkg/service/retrieval.go | 2 +- 7 files changed, 100 insertions(+), 99 deletions(-) diff --git a/Makefile b/Makefile index 0735d2b..d24cf50 100644 --- a/Makefile +++ b/Makefile @@ -26,7 +26,7 @@ run-local: echo "A container named ${SERVICE_NAME} is already running. \nRestarting..."; \ make rm; \ fi - @docker run --rm \ + @docker run --rm \ -p ${SERVICE_PORT}:${SERVICE_PORT} \ --network instill-network \ --name ${SERVICE_NAME} \ diff --git a/go.mod b/go.mod index 2fb759e..e83edb9 100644 --- a/go.mod +++ b/go.mod @@ -12,7 +12,7 @@ require ( github.com/grpc-ecosystem/go-grpc-middleware v1.3.0 github.com/grpc-ecosystem/grpc-gateway/v2 v2.19.1 github.com/influxdata/influxdb-client-go/v2 v2.12.3 - github.com/instill-ai/protogen-go v0.3.3-alpha.0.20240725140016-18f3ff64c952 + github.com/instill-ai/protogen-go v0.3.3-alpha.0.20240731142712-0dc717e8cea4 github.com/instill-ai/usage-client v0.3.0-alpha.0.20240319060111-4a3a39f2fd61 github.com/instill-ai/x v0.3.0-alpha.0.20231219052200-6230a89e386c github.com/knadh/koanf v1.5.0 diff --git a/go.sum b/go.sum index 7bd4fbc..eb10942 100644 --- a/go.sum +++ b/go.sum @@ -342,8 +342,8 @@ github.com/influxdata/influxdb-client-go/v2 v2.12.3 h1:28nRlNMRIV4QbtIUvxhWqaxn0 github.com/influxdata/influxdb-client-go/v2 v2.12.3/go.mod h1:IrrLUbCjjfkmRuaCiGQg4m2GbkaeJDcuWoxiWdQEbA0= github.com/influxdata/line-protocol v0.0.0-20200327222509-2487e7298839 h1:W9WBk7wlPfJLvMCdtV4zPulc4uCPrlywQOmbFOhgQNU= github.com/influxdata/line-protocol v0.0.0-20200327222509-2487e7298839/go.mod h1:xaLFMmpvUxqXtVkUJfg9QmT88cDaCJ3ZKgdZ78oO8Qo= -github.com/instill-ai/protogen-go v0.3.3-alpha.0.20240725140016-18f3ff64c952 h1:gtc9vYwc5sEWIs5H1bdu0vx5IgZb0CKcvBa0VWtU8VE= -github.com/instill-ai/protogen-go v0.3.3-alpha.0.20240725140016-18f3ff64c952/go.mod h1:2blmpUwiTwxIDnrjIqT6FhR5ewshZZF554wzjXFvKpQ= +github.com/instill-ai/protogen-go v0.3.3-alpha.0.20240731142712-0dc717e8cea4 h1:YPaJSRkzdjmlNmIN0FbR9FaX3mnZkhpYNrprF3CZa8A= +github.com/instill-ai/protogen-go v0.3.3-alpha.0.20240731142712-0dc717e8cea4/go.mod h1:2blmpUwiTwxIDnrjIqT6FhR5ewshZZF554wzjXFvKpQ= github.com/instill-ai/usage-client v0.3.0-alpha.0.20240319060111-4a3a39f2fd61 h1:smPTvmXDhn/QC7y/TPXyMTqbbRd0gvzmFgWBChwTfhE= github.com/instill-ai/usage-client v0.3.0-alpha.0.20240319060111-4a3a39f2fd61/go.mod h1:/TAHs4ybuylk5icuy+MQtHRc4XUnIyXzeNKxX9qDFhw= github.com/instill-ai/x v0.3.0-alpha.0.20231219052200-6230a89e386c h1:a2RVkpIV2QcrGnSHAou+t/L+vBsaIfFvk5inVg5Uh4s= diff --git a/pkg/handler/knowledgebase.go b/pkg/handler/knowledgebase.go index 2c1e4d1..4948215 100644 --- a/pkg/handler/knowledgebase.go +++ b/pkg/handler/knowledgebase.go @@ -20,15 +20,15 @@ import ( type ErrorMsg map[int]string -const ErrorCreateKnowledgeBaseMsg = "failed to create knowledge base: %w" -const ErrorListKnowledgeBasesMsg = "failed to get knowledge bases: %w " -const ErrorUpdateKnowledgeBaseMsg = "failed to update knowledge base: %w" -const ErrorDeleteKnowledgeBaseMsg = "failed to delete knowledge base: %w" +const ErrorCreateKnowledgeBaseMsg = "failed to create catalog: %w" +const ErrorListKnowledgeBasesMsg = "failed to get catalogs: %w " +const ErrorUpdateKnowledgeBaseMsg = "failed to update catalog: %w" +const ErrorDeleteKnowledgeBaseMsg = "failed to delete catalog: %w" // Note: in the future, we might have different max count for different user types const KnowledgeBaseMaxCount = 3 -func (ph *PublicHandler) CreateKnowledgeBase(ctx context.Context, req *artifactpb.CreateKnowledgeBaseRequest) (*artifactpb.CreateKnowledgeBaseResponse, error) { +func (ph *PublicHandler) CreateCatalog(ctx context.Context, req *artifactpb.CreateCatalogRequest) (*artifactpb.CreateCatalogResponse, error) { log, _ := logger.GetZapLogger(ctx) authUID, err := getUserUIDFromContext(ctx) if err != nil { @@ -36,7 +36,7 @@ func (ph *PublicHandler) CreateKnowledgeBase(ctx context.Context, req *artifactp return nil, err } - // ACL check user's permission to create knowledge base in the user or org context(namespace) + // ACL check user's permission to create catalog in the user or org context(namespace) ns, err := ph.service.GetNamespaceByNsID(ctx, req.GetNamespaceId()) if err != nil { log.Error( @@ -55,16 +55,16 @@ func (ph *PublicHandler) CreateKnowledgeBase(ctx context.Context, req *artifactp zap.String("auth_uid", authUID)) return nil, fmt.Errorf(ErrorCreateKnowledgeBaseMsg, err) } - // check if user has reached the maximum number of knowledge bases + // check if user has reached the maximum number of catalogs // note: the simple implementation have race condition to bypass the check, // but it is okay for now kbCount, err := ph.service.Repository.GetKnowledgeBaseCountByOwner(ctx, authUID) if err != nil { - log.Error("failed to get knowledge base count", zap.Error(err)) + log.Error("failed to get catalog count", zap.Error(err)) return nil, fmt.Errorf(ErrorCreateKnowledgeBaseMsg, err) } if kbCount >= KnowledgeBaseMaxCount { - err := fmt.Errorf("user has reached the 3 maximum number of knowledge bases: %v. ", kbCount) + err := fmt.Errorf("user has reached the 3 maximum number of catalogs: %v. ", kbCount) return nil, err } @@ -75,7 +75,8 @@ func (ph *PublicHandler) CreateKnowledgeBase(ctx context.Context, req *artifactp } nameOk := isValidName(req.Name) if !nameOk { - msg := "kb name is invalid: %v. err: %w" + msg := "the catalog name should be lowercase without any space or special character besides the hyphen, " + + "it can not start with number or hyphen, and should be less than 32 characters. name: %v. err: %w" return nil, fmt.Errorf(msg, req.Name, customerror.ErrInvalidArgument) } @@ -96,7 +97,7 @@ func (ph *PublicHandler) CreateKnowledgeBase(ctx context.Context, req *artifactp return nil, err } - // external service call - create knowledge base collection and set ACL in openFAG + // external service call - create catalog collection and set ACL in openFAG callExternalService := func(kbUID string) error { err := ph.service.MilvusClient.CreateKnowledgeBaseCollection(ctx, kbUID) if err != nil { @@ -104,7 +105,7 @@ func (ph *PublicHandler) CreateKnowledgeBase(ctx context.Context, req *artifactp return err } - // set the owner of the knowledge base + // set the owner of the catalog kbUIDuuid, err := uuid.FromString(kbUID) if err != nil { log.Error("failed to parse kb uid", zap.String("kb_uid", kbUID), zap.Error(err)) @@ -119,7 +120,7 @@ func (ph *PublicHandler) CreateKnowledgeBase(ctx context.Context, req *artifactp return nil } - // create knowledge base + // create catalog dbData, err := ph.service.Repository.CreateKnowledgeBase(ctx, repository.KnowledgeBase{ Name: req.Name, @@ -135,10 +136,10 @@ func (ph *PublicHandler) CreateKnowledgeBase(ctx context.Context, req *artifactp return nil, err } - return &artifactpb.CreateKnowledgeBaseResponse{ - KnowledgeBase: &artifactpb.KnowledgeBase{ + return &artifactpb.CreateCatalogResponse{ + Catalog: &artifactpb.Catalog{ Name: dbData.Name, - KbId: dbData.KbID, + CatalogId: dbData.KbID, Description: dbData.Description, Tags: dbData.Tags, OwnerName: dbData.Owner, @@ -155,7 +156,7 @@ func (ph *PublicHandler) CreateKnowledgeBase(ctx context.Context, req *artifactp }, nil } -func (ph *PublicHandler) ListKnowledgeBases(ctx context.Context, req *artifactpb.ListKnowledgeBasesRequest) (*artifactpb.ListKnowledgeBasesResponse, error) { +func (ph *PublicHandler) ListCatalogs(ctx context.Context, req *artifactpb.ListCatalogsRequest) (*artifactpb.ListCatalogsResponse, error) { log, _ := logger.GetZapLogger(ctx) // get user id from context authUID, err := getUserUIDFromContext(ctx) @@ -164,7 +165,7 @@ func (ph *PublicHandler) ListKnowledgeBases(ctx context.Context, req *artifactpb return nil, fmt.Errorf(ErrorListKnowledgeBasesMsg, err) } - // ACL - check user(authUid)'s permission to list knowledge bases in + // ACL - check user(authUid)'s permission to list catalogs in // the user or org context(namespace) ns, err := ph.service.GetNamespaceByNsID(ctx, req.GetNamespaceId()) if err != nil { @@ -187,7 +188,7 @@ func (ph *PublicHandler) ListKnowledgeBases(ctx context.Context, req *artifactpb dbData, err := ph.service.Repository.ListKnowledgeBases(ctx, ns.NsUID.String()) if err != nil { - log.Error("failed to get knowledge bases", zap.Error(err)) + log.Error("failed to get catalogs", zap.Error(err)) return nil, fmt.Errorf(ErrorListKnowledgeBasesMsg, err) } @@ -206,11 +207,11 @@ func (ph *PublicHandler) ListKnowledgeBases(ctx context.Context, req *artifactpb log.Error("failed to get token counts", zap.Error(err)) return nil, fmt.Errorf(ErrorListKnowledgeBasesMsg, err) } - kbs := make([]*artifactpb.KnowledgeBase, len(dbData)) + kbs := make([]*artifactpb.Catalog, len(dbData)) for i, kb := range dbData { - kbs[i] = &artifactpb.KnowledgeBase{ + kbs[i] = &artifactpb.Catalog{ Name: kb.Name, - KbId: kb.KbID, + CatalogId: kb.KbID, Description: kb.Description, Tags: kb.Tags, CreateTime: kb.CreateTime.String(), @@ -227,11 +228,11 @@ func (ph *PublicHandler) ListKnowledgeBases(ctx context.Context, req *artifactpb UsedStorage: uint64(kb.Usage), } } - return &artifactpb.ListKnowledgeBasesResponse{ - KnowledgeBases: kbs, + return &artifactpb.ListCatalogsResponse{ + Catalogs: kbs, }, nil } -func (ph *PublicHandler) UpdateKnowledgeBase(ctx context.Context, req *artifactpb.UpdateKnowledgeBaseRequest) (*artifactpb.UpdateKnowledgeBaseResponse, error) { +func (ph *PublicHandler) UpdateCatalog(ctx context.Context, req *artifactpb.UpdateCatalogRequest) (*artifactpb.UpdateCatalogResponse, error) { log, _ := logger.GetZapLogger(ctx) authUID, err := getUserUIDFromContext(ctx) if err != nil { @@ -239,7 +240,7 @@ func (ph *PublicHandler) UpdateKnowledgeBase(ctx context.Context, req *artifactp return nil, err } // check name if it is empty - if req.KbId == "" { + if req.CatalogId == "" { log.Error("kb_id is empty", zap.Error(ErrCheckRequiredFields)) return nil, fmt.Errorf("kb_id is empty. err: %w", ErrCheckRequiredFields) } @@ -253,10 +254,10 @@ func (ph *PublicHandler) UpdateKnowledgeBase(ctx context.Context, req *artifactp zap.String("auth_uid", authUID)) return nil, fmt.Errorf("failed to get namespace. err: %w", err) } - // ACL - check user's permission to update knowledge base - kb, err := ph.service.Repository.GetKnowledgeBaseByOwnerAndKbID(ctx, ns.NsUID.String(), req.KbId) + // ACL - check user's permission to update catalog + kb, err := ph.service.Repository.GetKnowledgeBaseByOwnerAndKbID(ctx, ns.NsUID.String(), req.CatalogId) if err != nil { - log.Error("failed to get knowledge base", zap.Error(err)) + log.Error("failed to get catalog", zap.Error(err)) return nil, fmt.Errorf(ErrorListKnowledgeBasesMsg, err) } granted, err := ph.service.ACLClient.CheckPermission(ctx, "knowledgebase", kb.UID, "writer") @@ -265,24 +266,24 @@ func (ph *PublicHandler) UpdateKnowledgeBase(ctx context.Context, req *artifactp return nil, fmt.Errorf(ErrorUpdateKnowledgeBaseMsg, err) } if !granted { - log.Error("no permission to update knowledge base") + log.Error("no permission to update catalog") return nil, fmt.Errorf(ErrorUpdateKnowledgeBaseMsg, customerror.ErrNoPermission) } - // update knowledge base + // update catalog kb, err = ph.service.Repository.UpdateKnowledgeBase( ctx, ns.NsUID.String(), repository.KnowledgeBase{ // Name: req.KbId, - KbID: req.KbId, + KbID: req.CatalogId, Description: req.Description, Tags: req.Tags, Owner: ns.NsUID.String(), }, ) if err != nil { - log.Error("failed to update knowledge base", zap.Error(err)) + log.Error("failed to update catalog", zap.Error(err)) return nil, err } fileCounts, err := ph.service.Repository.GetCountFilesByListKnowledgeBaseUID(ctx, []uuid.UUID{kb.UID}) @@ -296,10 +297,10 @@ func (ph *PublicHandler) UpdateKnowledgeBase(ctx context.Context, req *artifactp return nil, fmt.Errorf(ErrorListKnowledgeBasesMsg, err) } // populate response - return &artifactpb.UpdateKnowledgeBaseResponse{ - KnowledgeBase: &artifactpb.KnowledgeBase{ + return &artifactpb.UpdateCatalogResponse{ + Catalog: &artifactpb.Catalog{ Name: kb.Name, - KbId: kb.KbID, + CatalogId: kb.KbID, Description: kb.Description, Tags: kb.Tags, CreateTime: kb.CreateTime.String(), @@ -317,7 +318,7 @@ func (ph *PublicHandler) UpdateKnowledgeBase(ctx context.Context, req *artifactp }, }, nil } -func (ph *PublicHandler) DeleteKnowledgeBase(ctx context.Context, req *artifactpb.DeleteKnowledgeBaseRequest) (*artifactpb.DeleteKnowledgeBaseResponse, error) { +func (ph *PublicHandler) DeleteCatalog(ctx context.Context, req *artifactpb.DeleteCatalogRequest) (*artifactpb.DeleteCatalogResponse, error) { log, _ := logger.GetZapLogger(ctx) authUID, err := getUserUIDFromContext(ctx) if err != nil { @@ -334,10 +335,10 @@ func (ph *PublicHandler) DeleteKnowledgeBase(ctx context.Context, req *artifactp zap.String("auth_uid", authUID)) return nil, fmt.Errorf("failed to get namespace. err: %w", err) } - // ACL - check user's permission to write knowledge base - kb, err := ph.service.Repository.GetKnowledgeBaseByOwnerAndKbID(ctx, ns.NsUID.String(), req.KbId) + // ACL - check user's permission to write catalog + kb, err := ph.service.Repository.GetKnowledgeBaseByOwnerAndKbID(ctx, ns.NsUID.String(), req.CatalogId) if err != nil { - log.Error("failed to get knowledge base", zap.Error(err)) + log.Error("failed to get catalog", zap.Error(err)) return nil, fmt.Errorf(ErrorListKnowledgeBasesMsg, err) } granted, err := ph.service.ACLClient.CheckPermission(ctx, "knowledgebase", kb.UID, "writer") @@ -346,24 +347,24 @@ func (ph *PublicHandler) DeleteKnowledgeBase(ctx context.Context, req *artifactp return nil, fmt.Errorf(ErrorUpdateKnowledgeBaseMsg, err) } if !granted { - log.Error("no permission to delete knowledge base") + log.Error("no permission to delete catalog") return nil, fmt.Errorf(ErrorDeleteKnowledgeBaseMsg, customerror.ErrNoPermission) } - deletedKb, err := ph.service.Repository.DeleteKnowledgeBase(ctx, ns.NsUID.String(), req.KbId) + deletedKb, err := ph.service.Repository.DeleteKnowledgeBase(ctx, ns.NsUID.String(), req.CatalogId) if err != nil { return nil, err } err = ph.service.ACLClient.Purge(ctx, "knowledgebase", deletedKb.UID) if err != nil { - log.Error("failed to purge knowledge base", zap.Error(err)) + log.Error("failed to purge catalog", zap.Error(err)) return nil, fmt.Errorf(ErrorDeleteKnowledgeBaseMsg, err) } - return &artifactpb.DeleteKnowledgeBaseResponse{ - KnowledgeBase: &artifactpb.KnowledgeBase{ + return &artifactpb.DeleteCatalogResponse{ + Catalog: &artifactpb.Catalog{ Name: deletedKb.Name, - KbId: deletedKb.KbID, + CatalogId: deletedKb.KbID, Description: deletedKb.Description, Tags: deletedKb.Tags, CreateTime: deletedKb.CreateTime.String(), @@ -391,7 +392,7 @@ func getUserUIDFromContext(ctx context.Context) (string, error) { // than 32 characters. func isValidName(name string) bool { // Define the regular expression pattern - pattern := `^[a-z]([a-z-]{0,30}[a-z])?$` + pattern := "^[a-z][-a-z_0-9]{0,31}$" // Compile the regular expression re := regexp.MustCompile(pattern) // Match the name against the regular expression diff --git a/pkg/handler/knowledgebasefiles.go b/pkg/handler/knowledgebasefiles.go index 3aed007..6f19114 100644 --- a/pkg/handler/knowledgebasefiles.go +++ b/pkg/handler/knowledgebasefiles.go @@ -20,7 +20,7 @@ const maxFileSize = 15 * 1024 * 1024 // 1GB const KnowledgeBaseMaxUsage = 1024 * 1024 * 1024 -func (ph *PublicHandler) UploadKnowledgeBaseFile(ctx context.Context, req *artifactpb.UploadKnowledgeBaseFileRequest) (*artifactpb.UploadKnowledgeBaseFileResponse, error) { +func (ph *PublicHandler) UploadCatalogFile(ctx context.Context, req *artifactpb.UploadCatalogFileRequest) (*artifactpb.UploadCatalogFileResponse, error) { log, _ := logger.GetZapLogger(ctx) authUID, err := getUserUIDFromContext(ctx) if err != nil { @@ -44,10 +44,10 @@ func (ph *PublicHandler) UploadKnowledgeBaseFile(ctx context.Context, req *artif zap.String("auth_uid", authUID)) return nil, fmt.Errorf("failed to get namespace. err: %w", err) } - // ACL - check user's permission to write knowledge base - kb, err := ph.service.Repository.GetKnowledgeBaseByOwnerAndKbID(ctx, ns.NsUID.String(), req.KbId) + // ACL - check user's permission to write catalog + kb, err := ph.service.Repository.GetKnowledgeBaseByOwnerAndKbID(ctx, ns.NsUID.String(), req.CatalogId) if err != nil { - log.Error("failed to get knowledge base", zap.Error(err)) + log.Error("failed to get catalog", zap.Error(err)) return nil, fmt.Errorf(ErrorListKnowledgeBasesMsg, err) } granted, err := ph.service.ACLClient.CheckPermission(ctx, "knowledgebase", kb.UID, "writer") @@ -56,7 +56,7 @@ func (ph *PublicHandler) UploadKnowledgeBaseFile(ctx context.Context, req *artif return nil, fmt.Errorf(ErrorUpdateKnowledgeBaseMsg, err) } if !granted { - log.Error("no permission to delete knowledge base") + log.Error("no permission to delete catalog") return nil, fmt.Errorf(ErrorDeleteKnowledgeBaseMsg, customerror.ErrNoPermission) } @@ -76,7 +76,7 @@ func (ph *PublicHandler) UploadKnowledgeBaseFile(ctx context.Context, req *artif } if kb.Usage+fileSize > KnowledgeBaseMaxUsage { - return nil, fmt.Errorf("knowledge base 1 GB exceeded. err: %w", customerror.ErrInvalidArgument) + return nil, fmt.Errorf("catalog 1 GB exceeded. err: %w", customerror.ErrInvalidArgument) } destination := ph.service.MinIO.GetUploadedFilePathInKnowledgeBase(kb.UID.String(), req.File.Name) @@ -91,7 +91,7 @@ func (ph *PublicHandler) UploadKnowledgeBaseFile(ctx context.Context, req *artif Size: fileSize, } - // create knowledge base file + // create catalog file res, err = ph.service.Repository.CreateKnowledgeBaseFile(ctx, kbFile, func(FileUID string) error { // upload file to minio err := ph.service.MinIO.UploadBase64File(ctx, destination, req.File.Content, fileTypeConvertToMime(req.File.Type)) @@ -103,24 +103,24 @@ func (ph *PublicHandler) UploadKnowledgeBaseFile(ctx context.Context, req *artif }) if err != nil { - log.Error("failed to create knowledge base file", zap.Error(err)) + log.Error("failed to create catalog file", zap.Error(err)) return nil, err } - // increase knowledge base usage + // increase catalog usage err = ph.service.Repository.IncreaseKnowledgeBaseUsage(ctx, kb.UID.String(), int(fileSize)) if err != nil { - log.Error("failed to increase knowledge base usage", zap.Error(err)) + log.Error("failed to increase catalog usage", zap.Error(err)) return nil, err } } - return &artifactpb.UploadKnowledgeBaseFileResponse{ + return &artifactpb.UploadCatalogFileResponse{ File: &artifactpb.File{ FileUid: res.UID.String(), OwnerUid: res.Owner.String(), CreatorUid: res.CreatorUID.String(), - KbUid: res.KnowledgeBaseUID.String(), + CatalogUid: res.KnowledgeBaseUID.String(), Name: res.Name, Type: req.File.Type, CreateTime: timestamppb.New(*res.CreateTime), @@ -164,11 +164,11 @@ func getFileSize(base64String string) (int64, string) { return int64(decodedSize), fmt.Sprintf("%.1f %cB", size, "KMGTPE"[exp]) } -func checkUploadKnowledgeBaseFileRequest(req *artifactpb.UploadKnowledgeBaseFileRequest) error { +func checkUploadKnowledgeBaseFileRequest(req *artifactpb.UploadCatalogFileRequest) error { if req.GetNamespaceId() == "" { return fmt.Errorf("owner uid is required. err: %w", ErrCheckRequiredFields) - } else if req.KbId == "" { - return fmt.Errorf("knowledge base uid is required. err: %w", ErrCheckRequiredFields) + } else if req.CatalogId == "" { + return fmt.Errorf("catalog uid is required. err: %w", ErrCheckRequiredFields) } else if req.File == nil { return fmt.Errorf("file is required. err: %w", ErrCheckRequiredFields) } else if req.File.Name == "" { @@ -194,7 +194,7 @@ func checkValidFileType(t artifactpb.FileType) bool { return false } -func (ph *PublicHandler) ListKnowledgeBaseFiles(ctx context.Context, req *artifactpb.ListKnowledgeBaseFilesRequest) (*artifactpb.ListKnowledgeBaseFilesResponse, error) { +func (ph *PublicHandler) ListCatalogFiles(ctx context.Context, req *artifactpb.ListCatalogFilesRequest) (*artifactpb.ListCatalogFilesResponse, error) { log, _ := logger.GetZapLogger(ctx) authUID, err := getUserUIDFromContext(ctx) @@ -204,7 +204,7 @@ func (ph *PublicHandler) ListKnowledgeBaseFiles(ctx context.Context, req *artifa return nil, err } - // ACL - check if the creator can list files in this knowledge base. ACL using uid to check the certain namespace resource. + // ACL - check if the creator can list files in this catalog. ACL using uid to check the certain namespace resource. ns, err := ph.service.GetNamespaceByNsID(ctx, req.GetNamespaceId()) if err != nil { log.Error( @@ -214,10 +214,10 @@ func (ph *PublicHandler) ListKnowledgeBaseFiles(ctx context.Context, req *artifa zap.String("auth_uid", authUID)) return nil, fmt.Errorf("failed to get namespace. err: %w", err) } - // ACL - check user's permission to write knowledge base - kb, err := ph.service.Repository.GetKnowledgeBaseByOwnerAndKbID(ctx, ns.NsUID.String(), req.KbId) + // ACL - check user's permission to write catalog + kb, err := ph.service.Repository.GetKnowledgeBaseByOwnerAndKbID(ctx, ns.NsUID.String(), req.CatalogId) if err != nil { - log.Error("failed to get knowledge base", zap.Error(err)) + log.Error("failed to get catalog", zap.Error(err)) return nil, fmt.Errorf(ErrorListKnowledgeBasesMsg, err) } granted, err := ph.service.ACLClient.CheckPermission(ctx, "knowledgebase", kb.UID, "reader") @@ -226,23 +226,23 @@ func (ph *PublicHandler) ListKnowledgeBaseFiles(ctx context.Context, req *artifa return nil, fmt.Errorf(ErrorUpdateKnowledgeBaseMsg, err) } if !granted { - log.Error("no permission to delete knowledge base") + log.Error("no permission to delete catalog") return nil, fmt.Errorf(ErrorDeleteKnowledgeBaseMsg, customerror.ErrNoPermission) } - // fetch the knowledge base files + // fetch the catalog files var files []*artifactpb.File var totalSize int var nextPageToken string { if req.Filter == nil { - req.Filter = &artifactpb.ListKnowledgeBaseFilesFilter{ + req.Filter = &artifactpb.ListCatalogFilesFilter{ FileUids: []string{}, } } kbFiles, size, nextToken, err := ph.service.Repository.ListKnowledgeBaseFiles(ctx, authUID, ns.NsUID.String(), kb.UID.String(), req.PageSize, req.PageToken, req.Filter.FileUids) if err != nil { - log.Error("failed to list knowledge base files", zap.Error(err)) + log.Error("failed to list catalog files", zap.Error(err)) return nil, err } // get the tokens and chunks using the source table and source uid @@ -270,7 +270,7 @@ func (ph *PublicHandler) ListKnowledgeBaseFiles(ctx context.Context, req *artifa FileUid: kbFile.UID.String(), OwnerUid: kbFile.Owner.String(), CreatorUid: kbFile.CreatorUID.String(), - KbUid: kbFile.KnowledgeBaseUID.String(), + CatalogUid: kbFile.KnowledgeBaseUID.String(), Name: kbFile.Name, Type: artifactpb.FileType(artifactpb.FileType_value[kbFile.Type]), CreateTime: timestamppb.New(*kbFile.CreateTime), @@ -283,7 +283,7 @@ func (ph *PublicHandler) ListKnowledgeBaseFiles(ctx context.Context, req *artifa } } - return &artifactpb.ListKnowledgeBaseFilesResponse{ + return &artifactpb.ListCatalogFilesResponse{ Files: files, TotalSize: int32(totalSize), NextPageToken: nextPageToken, @@ -291,10 +291,10 @@ func (ph *PublicHandler) ListKnowledgeBaseFiles(ctx context.Context, req *artifa }, nil } -func (ph *PublicHandler) DeleteKnowledgeBaseFile( +func (ph *PublicHandler) DeleteCatalogFile( ctx context.Context, - req *artifactpb.DeleteKnowledgeBaseFileRequest) ( - *artifactpb.DeleteKnowledgeBaseFileResponse, error) { + req *artifactpb.DeleteCatalogFileRequest) ( + *artifactpb.DeleteCatalogFileResponse, error) { log, _ := logger.GetZapLogger(ctx) // authUID, err := getUserUIDFromContext(ctx) // if err != nil { @@ -302,10 +302,10 @@ func (ph *PublicHandler) DeleteKnowledgeBaseFile( // return nil, err // } - // ACL - check user's permission to write knowledge base of kb file + // ACL - check user's permission to write catalog of kb file kbfs, err := ph.service.Repository.GetKnowledgeBaseFilesByFileUIDs(ctx, []uuid.UUID{uuid.FromStringOrNil(req.FileUid)}) if err != nil && len(kbfs) == 0 { - log.Error("failed to get knowledge base", zap.Error(err)) + log.Error("failed to get catalog", zap.Error(err)) return nil, fmt.Errorf(ErrorListKnowledgeBasesMsg, err) } granted, err := ph.service.ACLClient.CheckPermission(ctx, "knowledgebase", kbfs[0].KnowledgeBaseUID, "writer") @@ -314,7 +314,7 @@ func (ph *PublicHandler) DeleteKnowledgeBaseFile( return nil, fmt.Errorf(ErrorUpdateKnowledgeBaseMsg, err) } if !granted { - log.Error("no permission to delete knowledge base") + log.Error("no permission to delete catalog") return nil, fmt.Errorf(ErrorDeleteKnowledgeBaseMsg, customerror.ErrNoPermission) } // check if file uid is empty @@ -341,19 +341,19 @@ func (ph *PublicHandler) DeleteKnowledgeBaseFile( return nil, err } - // decrease knowledge base usage + // decrease catalog usage err = ph.service.Repository.IncreaseKnowledgeBaseUsage(ctx, files[0].KnowledgeBaseUID.String(), int(-files[0].Size)) if err != nil { return nil, err } - return &artifactpb.DeleteKnowledgeBaseFileResponse{ + return &artifactpb.DeleteCatalogFileResponse{ FileUid: req.FileUid, }, nil } -func (ph *PublicHandler) ProcessKnowledgeBaseFiles(ctx context.Context, req *artifactpb.ProcessKnowledgeBaseFilesRequest) (*artifactpb.ProcessKnowledgeBaseFilesResponse, error) { +func (ph *PublicHandler) ProcessCatalogFiles(ctx context.Context, req *artifactpb.ProcessCatalogFilesRequest) (*artifactpb.ProcessCatalogFilesResponse, error) { // uid, err := getUserIDFromContext(ctx) // if err != nil { // err := fmt.Errorf("failed to get user id from header: %v. err: %w", err, customerror.ErrUnauthenticated) @@ -377,14 +377,14 @@ func (ph *PublicHandler) ProcessKnowledgeBaseFiles(ctx context.Context, req *art if len(kbfs) == 0 { return nil, fmt.Errorf("file not found. err: %w", customerror.ErrNotFound) } - // check write permission for the knowledge base + // check write permission for the catalog for _, kbf := range kbfs { granted, err := ph.service.ACLClient.CheckPermission(ctx, "knowledgebase", kbf.KnowledgeBaseUID, "writer") if err != nil { return nil, err } if !granted { - return nil, fmt.Errorf("no permission to process knowledge base file.fileUID:%s err: %w", kbf.UID, customerror.ErrNoPermission) + return nil, fmt.Errorf("no permission to process catalog file.fileUID:%s err: %w", kbf.UID, customerror.ErrNoPermission) } } @@ -400,7 +400,7 @@ func (ph *PublicHandler) ProcessKnowledgeBaseFiles(ctx context.Context, req *art FileUid: file.UID.String(), OwnerUid: file.Owner.String(), CreatorUid: file.CreatorUID.String(), - KbUid: file.KnowledgeBaseUID.String(), + CatalogUid: file.KnowledgeBaseUID.String(), Name: file.Name, Type: artifactpb.FileType(artifactpb.FileType_value[file.Type]), CreateTime: timestamppb.New(*file.CreateTime), @@ -408,7 +408,7 @@ func (ph *PublicHandler) ProcessKnowledgeBaseFiles(ctx context.Context, req *art ProcessStatus: artifactpb.FileProcessStatus(artifactpb.FileProcessStatus_value[file.ProcessStatus]), }) } - return &artifactpb.ProcessKnowledgeBaseFilesResponse{ + return &artifactpb.ProcessCatalogFilesResponse{ Files: resFiles, }, nil } diff --git a/pkg/handler/retrieval.go b/pkg/handler/retrieval.go index 200f4bd..1a4267f 100644 --- a/pkg/handler/retrieval.go +++ b/pkg/handler/retrieval.go @@ -41,14 +41,14 @@ func (ph *PublicHandler) SimilarityChunksSearch( log.Info("get namespace by ns id", zap.Duration("duration", time.Since(t))) t = time.Now() ownerUID := ns.NsUID - kb, err := ph.service.Repository.GetKnowledgeBaseByOwnerAndKbID(ctx, ownerUID.String(), req.KbId) + kb, err := ph.service.Repository.GetKnowledgeBaseByOwnerAndKbID(ctx, ownerUID.String(), req.CatalogId) if err != nil { - log.Error("failed to get knowledge base by owner and kb id", zap.Error(err)) - return nil, fmt.Errorf("failed to get knowledge base by owner and kb id. err: %w", err) + log.Error("failed to get catalog by owner and kb id", zap.Error(err)) + return nil, fmt.Errorf("failed to get catalog by owner and kb id. err: %w", err) } - log.Info("get knowledge base by owner and kb id", zap.Duration("duration", time.Since(t))) + log.Info("get catalog by owner and kb id", zap.Duration("duration", time.Since(t))) t = time.Now() - // ACL : check user has access to the knowledge base + // ACL : check user has access to the catalog granted, err := ph.service.ACLClient.CheckPermission(ctx, "knowledgebase", kb.UID, "reader") if err != nil { log.Error("failed to check permission", zap.Error(err)) @@ -106,13 +106,13 @@ func (ph *PublicHandler) SimilarityChunksSearch( files, err := ph.service.Repository.GetKnowledgeBaseFilesByFileUIDs( ctx, fileUids, repository.KnowledgeBaseFileColumn.UID, repository.KnowledgeBaseFileColumn.Name) if err != nil { - log.Error("failed to get knowledge base files by file uids", zap.Error(err)) - return nil, fmt.Errorf("failed to get knowledge base files by file uids. err: %w", err) + log.Error("failed to get catalog files by file uids", zap.Error(err)) + return nil, fmt.Errorf("failed to get catalog files by file uids. err: %w", err) } for _, file := range files { fileUIDMapName[file.UID] = file.Name } - log.Info("get knowledge base files by file uids", zap.Duration("duration", time.Since(t))) + log.Info("get catalog files by file uids", zap.Duration("duration", time.Since(t))) // prepare the response simChunks := make([]*artifactv1alpha.SimilarityChunk, 0, len(chunks)) diff --git a/pkg/service/retrieval.go b/pkg/service/retrieval.go index 8500962..e3b62fd 100644 --- a/pkg/service/retrieval.go +++ b/pkg/service/retrieval.go @@ -27,7 +27,7 @@ func (s *Service) SimilarityChunksSearch(ctx context.Context, caller uuid.UUID, log.Info("vectorize text", zap.Duration("duration", time.Since(t))) t = time.Now() // get kb by kb_id and owner uid - kb, err := s.Repository.GetKnowledgeBaseByOwnerAndKbID(ctx, ownerUID, req.KbId) + kb, err := s.Repository.GetKnowledgeBaseByOwnerAndKbID(ctx, ownerUID, req.CatalogId) if err != nil { log.Error("failed to get knowledge base by owner and id", zap.Error(err)) return nil, fmt.Errorf("failed to get knowledge base by owner and id. err: %w", err)