From 5ec294207d91a9e457a6caf18d96606c3266cdce Mon Sep 17 00:00:00 2001 From: Liujian <824010343@qq.com> Date: Thu, 26 Dec 2024 16:46:11 +0800 Subject: [PATCH 1/2] update service list api --- controller/service/iml.go | 2 +- controller/service/service.go | 2 +- module/service/iml.go | 23 ----------------------- plugins/core/my-team.go | 2 +- plugins/core/service.go | 2 +- 5 files changed, 4 insertions(+), 27 deletions(-) diff --git a/controller/service/iml.go b/controller/service/iml.go index 2e0d8cff..f75d66bf 100644 --- a/controller/service/iml.go +++ b/controller/service/iml.go @@ -318,7 +318,7 @@ func (i *imlServiceController) Get(ctx *gin.Context, id string) (*service_dto.Se return i.module.Get(ctx, id) } -func (i *imlServiceController) Search(ctx *gin.Context, teamIDs string, keyword string, page string, pageSize string, sort string, asc string, serviceKind string) ([]*service_dto.ServiceItem, error) { +func (i *imlServiceController) Search(ctx *gin.Context, teamIDs string, keyword string) ([]*service_dto.ServiceItem, error) { return i.module.Search(ctx, teamIDs, keyword) } diff --git a/controller/service/service.go b/controller/service/service.go index 0509fe9a..c46ad21f 100644 --- a/controller/service/service.go +++ b/controller/service/service.go @@ -15,7 +15,7 @@ type IServiceController interface { Get(ctx *gin.Context, id string) (*service_dto.Service, error) // SearchMyServices 搜索服务 SearchMyServices(ctx *gin.Context, teamID string, keyword string) ([]*service_dto.ServiceItem, error) - Search(ctx *gin.Context, teamIDs string, keyword string, page string, pageSize string, sort string, asc string, serviceKind string) ([]*service_dto.ServiceItem, error) + Search(ctx *gin.Context, teamIDs string, keyword string) ([]*service_dto.ServiceItem, error) // Create 创建 Create(ctx *gin.Context, teamID string, input *service_dto.CreateService) (*service_dto.Service, error) // Edit 编辑 diff --git a/module/service/iml.go b/module/service/iml.go index 377990bd..8edf26d3 100644 --- a/module/service/iml.go +++ b/module/service/iml.go @@ -92,11 +92,6 @@ func (i *imlServiceModule) ExportAll(ctx context.Context) ([]*service_dto.Export serviceTagMap[st.Sid] = append(serviceTagMap[st.Sid], tagMap[st.Tid].Name) } - //docMap, err := i.serviceDocService.Map(ctx, serviceIds...) - //if err != nil { - // return nil, err - //} - items := make([]*service_dto.ExportService, 0, len(services)) for _, s := range services { info := &service_dto.ExportService{ @@ -171,24 +166,6 @@ func (i *imlServiceModule) SearchMyServices(ctx context.Context, teamId string, return items, nil } -//func (i *imlServiceModule) SimpleAPPS(ctx context.Context, keyword string) ([]*service_dto.SimpleServiceItem, error) { -// w := make(map[string]interface{}) -// w["as_app"] = true -// services, err := i.serviceService.SearchByDriver(ctx, keyword, w) -// if err != nil { -// return nil, err -// } -// return utils.SliceToSlice(services, func(p *service.Service) *service_dto.SimpleServiceItem { -// return &service_dto.SimpleServiceItem{ -// Id: p.Id, -// Name: p.Name, -// Description: p.Description, -// -// Team: auto.UUID(p.Team), -// } -// }), nil -//} - func (i *imlServiceModule) Simple(ctx context.Context) ([]*service_dto.SimpleServiceItem, error) { w := make(map[string]interface{}) w["as_server"] = true diff --git a/plugins/core/my-team.go b/plugins/core/my-team.go index 7b556606..e8d963a0 100644 --- a/plugins/core/my-team.go +++ b/plugins/core/my-team.go @@ -22,7 +22,7 @@ func (p *plugin) MyTeamApi() []pm3.Api { pm3.CreateApiWidthDoc(http.MethodPut, "/api/v1/team/member/role", []string{"context", "query:team", "body"}, nil, p.myTeamController.UpdateMemberRole, access.SystemWorkspaceTeamManager, access.TeamTeamMemberManager), // 团队项目操作 - pm3.CreateApiWidthDoc(http.MethodGet, "/api/v1/team/services", []string{"context", "query:team", "query:keyword", "query:page", "query:page_size", "query:sort", "query:asc", "query:status"}, []string{"services"}, p.serviceController.Search, access.SystemWorkspaceServiceViewAll, access.TeamTeamServiceView), + pm3.CreateApiWidthDoc(http.MethodGet, "/api/v1/team/services", []string{"context", "query:team", "query:keyword"}, []string{"services"}, p.serviceController.Search, access.SystemWorkspaceServiceViewAll, access.TeamTeamServiceView), pm3.CreateApiWidthDoc(http.MethodPost, "/api/v1/team/service", []string{"context", "query:team", "body"}, []string{"service"}, p.serviceController.Create, access.SystemWorkspaceServiceManagerAll, access.TeamTeamServiceManager), pm3.CreateApiWidthDoc(http.MethodPost, "/api/v1/team/app", []string{"context", "query:team", "body"}, []string{"app"}, p.appController.CreateApp, access.SystemWorkspaceApplicationManagerAll, access.TeamTeamConsumerManager), pm3.CreateApiWidthDoc(http.MethodDelete, "/api/v1/team/service", []string{"context", "query:service"}, nil, p.serviceController.Delete, access.SystemWorkspaceServiceManagerAll, access.TeamTeamServiceManager), diff --git a/plugins/core/service.go b/plugins/core/service.go index 95a062d0..e929e6ef 100644 --- a/plugins/core/service.go +++ b/plugins/core/service.go @@ -17,7 +17,7 @@ func (p *plugin) ServiceApis() []pm3.Api { pm3.CreateApiWidthDoc(http.MethodPut, "/api/v1/service/info", []string{"context", "query:service", "body"}, []string{"service"}, p.serviceController.Edit, access.SystemWorkspaceServiceManagerAll, access.TeamTeamServiceManager), pm3.CreateApiWidthDoc(http.MethodDelete, "/api/v1/service/info", []string{"context", "query:service"}, nil, p.serviceController.Delete, access.SystemWorkspaceServiceManagerAll, access.TeamTeamServiceManager), pm3.CreateApiWidthDoc(http.MethodGet, "/api/v1/my_services", []string{"context", "query:team", "query:keyword"}, []string{"services"}, p.serviceController.SearchMyServices), - pm3.CreateApiWidthDoc(http.MethodGet, "/api/v1/services", []string{"context", "query:team", "query:keyword", "query:page", "query:page_size", "query:sort", "query:asc", "query:workspace"}, []string{"services"}, p.serviceController.Search, access.SystemWorkspaceServiceViewAll, access.TeamTeamServiceView), + pm3.CreateApiWidthDoc(http.MethodGet, "/api/v1/services", []string{"context", "query:team", "query:keyword"}, []string{"services"}, p.serviceController.Search, access.SystemWorkspaceServiceViewAll, access.TeamTeamServiceView), pm3.CreateApiWidthDoc(http.MethodGet, "/api/v1/simple/services", []string{"context"}, []string{"services"}, p.serviceController.Simple), pm3.CreateApiWidthDoc(http.MethodGet, "/api/v1/simple/services/mine", []string{"context"}, []string{"services"}, p.serviceController.MySimple), // 应用相关 From 1bdd4720bb5e1e10938d18f4f9e508fc78ee0335 Mon Sep 17 00:00:00 2001 From: Liujian <824010343@qq.com> Date: Thu, 26 Dec 2024 17:25:36 +0800 Subject: [PATCH 2/2] ai api list add filter condition --- controller/ai/controller.go | 2 +- controller/ai/iml.go | 25 +++++++++++++------ module/ai/dto/output.go | 10 ++++++++ module/ai/iml.go | 50 ++++++++++++++++++++++++++++++------- module/ai/module.go | 2 +- plugins/core/ai.go | 2 +- service/service/iml.go | 2 +- 7 files changed, 73 insertions(+), 20 deletions(-) diff --git a/controller/ai/controller.go b/controller/ai/controller.go index bb6e7c9f..14a2822a 100644 --- a/controller/ai/controller.go +++ b/controller/ai/controller.go @@ -26,7 +26,7 @@ type IProviderController interface { } type IStatisticController interface { - APIs(ctx *gin.Context, keyword string, providerId string, start string, end string, page string, pageSize string, sortCondition string, asc string) ([]*ai_dto.APIItem, int64, error) + APIs(ctx *gin.Context, keyword string, providerId string, start string, end string, page string, pageSize string, sortCondition string, asc string, models string, services string) ([]*ai_dto.APIItem, *ai_dto.Condition, int64, error) } func init() { diff --git a/controller/ai/iml.go b/controller/ai/iml.go index 1ca21cdc..019a6564 100644 --- a/controller/ai/iml.go +++ b/controller/ai/iml.go @@ -1,6 +1,7 @@ package ai import ( + "encoding/json" "strconv" "github.com/APIParkLab/APIPark/module/ai" @@ -71,21 +72,21 @@ type imlStatisticController struct { module ai.IAIAPIModule `autowired:""` } -func (i *imlStatisticController) APIs(ctx *gin.Context, keyword string, providerId string, start string, end string, page string, pageSize string, sortCondition string, asc string) ([]*ai_dto.APIItem, int64, error) { +func (i *imlStatisticController) APIs(ctx *gin.Context, keyword string, providerId string, start string, end string, page string, pageSize string, sortCondition string, asc string, models string, services string) ([]*ai_dto.APIItem, *ai_dto.Condition, int64, error) { s, err := strconv.ParseInt(start, 10, 64) if err != nil { - return nil, 0, err + return nil, nil, 0, err } e, err := strconv.ParseInt(end, 10, 64) if err != nil { - return nil, 0, err + return nil, nil, 0, err } p, err := strconv.Atoi(page) if err != nil { if page != "" { - return nil, 0, err + return nil, nil, 0, err } p = 1 } @@ -93,9 +94,19 @@ func (i *imlStatisticController) APIs(ctx *gin.Context, keyword string, provider ps, err := strconv.Atoi(pageSize) if err != nil { if pageSize != "" { - return nil, 0, err + return nil, nil, 0, err } - ps = 15 + ps = 20 } - return i.module.APIs(ctx, keyword, providerId, s, e, p, ps, sortCondition, asc == "true") + ms := make([]string, 0) + if models != "" { + json.Unmarshal([]byte(models), &ms) + ms = append(ms, models) + } + ss := make([]string, 0) + if services != "" { + json.Unmarshal([]byte(services), &ss) + ss = append(ss, services) + } + return i.module.APIs(ctx, keyword, providerId, s, e, p, ps, sortCondition, asc == "true", ms, ss) } diff --git a/module/ai/dto/output.go b/module/ai/dto/output.go index 3e9350f6..bfb8ef09 100644 --- a/module/ai/dto/output.go +++ b/module/ai/dto/output.go @@ -77,3 +77,13 @@ type APIItem struct { UseToken int `json:"use_token"` Disable bool `json:"disable"` } + +type Condition struct { + Models []*BasicInfo `json:"models"` + Services []*BasicInfo `json:"services"` +} + +type BasicInfo struct { + Id string `json:"id"` + Name string `json:"name"` +} diff --git a/module/ai/iml.go b/module/ai/iml.go index ea6dcb45..10009114 100644 --- a/module/ai/iml.go +++ b/module/ai/iml.go @@ -9,6 +9,8 @@ import ( "sort" "time" + "github.com/APIParkLab/APIPark/service/service" + ai_key_dto "github.com/APIParkLab/APIPark/module/ai-key/dto" ai_key "github.com/APIParkLab/APIPark/service/ai-key" @@ -691,26 +693,55 @@ func (i *imlProviderModule) syncGateway(ctx context.Context, clusterId string, r var _ IAIAPIModule = (*imlAIApiModule)(nil) type imlAIApiModule struct { - aiAPIService ai_api.IAPIService `autowired:""` - aiAPIUseService ai_api.IAPIUseService `autowired:""` + aiAPIService ai_api.IAPIService `autowired:""` + aiAPIUseService ai_api.IAPIUseService `autowired:""` + serviceService service.IServiceService `autowired:""` } -func (i *imlAIApiModule) APIs(ctx context.Context, keyword string, providerId string, start int64, end int64, page int, pageSize int, sortCondition string, asc bool) ([]*ai_dto.APIItem, int64, error) { +func (i *imlAIApiModule) APIs(ctx context.Context, keyword string, providerId string, start int64, end int64, page int, pageSize int, sortCondition string, asc bool, models []string, serviceIds []string) ([]*ai_dto.APIItem, *ai_dto.Condition, int64, error) { + p, has := model_runtime.GetProvider(providerId) + if !has { + return nil, nil, 0, fmt.Errorf("ai provider not found") + } sortRule := "desc" if asc { sortRule = "asc" } + services, err := i.serviceService.ServiceListByKind(ctx, service.AIService) + if err != nil { + return nil, nil, 0, err + } + serviceItems := utils.SliceToSlice(services, func(e *service.Service) *ai_dto.BasicInfo { + return &ai_dto.BasicInfo{ + Id: e.Id, + Name: e.Name, + } + }) + modelItems := utils.SliceToSlice(p.Models(), func(e model_runtime.IModel) *ai_dto.BasicInfo { + return &ai_dto.BasicInfo{ + Id: e.ID(), + Name: e.ID(), + } + }) + condition := &ai_dto.Condition{Services: serviceItems, Models: modelItems} switch sortCondition { default: - apis, err := i.aiAPIService.Search(ctx, keyword, map[string]interface{}{ + w := map[string]interface{}{ "provider": providerId, - }, "update_at desc") + } + if len(models) > 0 { + w["model"] = models + } + if len(serviceIds) > 0 { + w["service"] = serviceIds + } + apis, err := i.aiAPIService.Search(ctx, keyword, w, "update_at desc") if err != nil { - return nil, 0, err + return nil, nil, 0, err } if len(apis) <= 0 { - return nil, 0, nil + return nil, condition, 0, nil } apiMap := make(map[string]*ai_api.API) apiIds := make([]string, 0, len(apis)) @@ -721,7 +752,7 @@ func (i *imlAIApiModule) APIs(ctx context.Context, keyword string, providerId st offset := (page - 1) * pageSize results, _, err := i.aiAPIUseService.SumByApisPage(ctx, providerId, start, end, offset, pageSize, fmt.Sprintf("total_token %s", sortRule), apiIds...) if err != nil { - return nil, 0, err + return nil, nil, 0, err } apiItems := utils.SliceToSlice(results, func(e *ai_api.APIUse) *ai_dto.APIItem { @@ -768,7 +799,8 @@ func (i *imlAIApiModule) APIs(ctx context.Context, keyword string, providerId st for i := offset; i < offset+size && i < len(sortApis); i++ { apiItems = append(apiItems, sortApis[i]) } + total := int64(len(apis)) - return apiItems, total, nil + return apiItems, condition, total, nil } } diff --git a/module/ai/module.go b/module/ai/module.go index 1037aea7..b5387dd6 100644 --- a/module/ai/module.go +++ b/module/ai/module.go @@ -26,7 +26,7 @@ type IProviderModule interface { } type IAIAPIModule interface { - APIs(ctx context.Context, keyword string, providerId string, start int64, end int64, page int, pageSize int, sortCondition string, asc bool) ([]*ai_dto.APIItem, int64, error) + APIs(ctx context.Context, keyword string, providerId string, start int64, end int64, page int, pageSize int, sortCondition string, asc bool, models []string, services []string) ([]*ai_dto.APIItem, *ai_dto.Condition, int64, error) } func init() { diff --git a/plugins/core/ai.go b/plugins/core/ai.go index eea9eb22..57749315 100644 --- a/plugins/core/ai.go +++ b/plugins/core/ai.go @@ -20,7 +20,7 @@ func (p *plugin) aiAPIs() []pm3.Api { pm3.CreateApiWidthDoc(http.MethodPut, "/api/v1/ai/provider/sort", []string{"context", "body"}, nil, p.aiProviderController.Sort), pm3.CreateApiWidthDoc(http.MethodPut, "/api/v1/ai/provider/config", []string{"context", "query:provider", "body"}, nil, p.aiProviderController.UpdateProviderConfig, access.SystemSettingsAiProviderManager), - pm3.CreateApiWidthDoc(http.MethodGet, "/api/v1/ai/apis", []string{"context", "query:keyword", "query:provider", "query:start", "query:end", "query:page", "query:page_size", "query:sort", "query:asc"}, []string{"apis", "total"}, p.aiStatisticController.APIs), + pm3.CreateApiWidthDoc(http.MethodGet, "/api/v1/ai/apis", []string{"context", "query:keyword", "query:provider", "query:start", "query:end", "query:page", "query:page_size", "query:sort", "query:asc", "query:models", "query:services"}, []string{"apis", "condition", "total"}, p.aiStatisticController.APIs), } } diff --git a/service/service/iml.go b/service/service/iml.go index c3090ea0..719e55e7 100644 --- a/service/service/iml.go +++ b/service/service/iml.go @@ -46,7 +46,7 @@ func (i *imlServiceService) ServiceListByKind(ctx context.Context, kind Kind, se w["uuid"] = serviceIds } w["as_server"] = true - w["kind"] = kind + w["kind"] = kind.Int() w["is_delete"] = false list, err := i.store.List(ctx, w) if err != nil {