Skip to content

Commit a7523c7

Browse files
authored
Merge pull request #322 from APIParkLab/feature/liujian-1.8
fix: The problem of slow retrieval of service lists and API portal se…
2 parents ca2682f + 590f328 commit a7523c7

16 files changed

Lines changed: 551 additions & 166 deletions

File tree

module/ai-api/iml.go

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import (
88
"net/http"
99
"strings"
1010

11+
service_overview "github.com/APIParkLab/APIPark/service/service-overview"
12+
1113
ai_provider_local "github.com/APIParkLab/APIPark/ai-provider/local"
1214

1315
model_runtime "github.com/APIParkLab/APIPark/ai-provider/model-runtime"
@@ -35,12 +37,13 @@ var (
3537
)
3638

3739
type imlAPIModule struct {
38-
serviceService service.IServiceService `autowired:""`
39-
apiDocService api_doc.IAPIDocService `autowired:""`
40-
aiAPIService ai_api.IAPIService `autowired:""`
41-
aiModelService ai_model.IProviderModelService `autowired:""`
42-
apiService api.IAPIService `autowired:""`
43-
transaction store.ITransaction `autowired:""`
40+
serviceService service.IServiceService `autowired:""`
41+
serviceOverviewService service_overview.IOverviewService `autowired:""`
42+
apiDocService api_doc.IAPIDocService `autowired:""`
43+
aiAPIService ai_api.IAPIService `autowired:""`
44+
aiModelService ai_model.IProviderModelService `autowired:""`
45+
apiService api.IAPIService `autowired:""`
46+
transaction store.ITransaction `autowired:""`
4447
}
4548

4649
func (i *imlAPIModule) getAPIDoc(ctx context.Context, serviceId string) (*openapi3.T, error) {
@@ -77,9 +80,17 @@ func (i *imlAPIModule) updateAPIDoc(ctx context.Context, serviceId, serviceName,
7780
if err != nil {
7881
return err
7982
}
80-
return i.apiDocService.UpdateDoc(ctx, serviceId, &api_doc.UpdateDoc{
81-
ID: uuid.New().String(),
82-
Content: string(result),
83+
return i.transaction.Transaction(ctx, func(ctx context.Context) error {
84+
count, err := i.apiDocService.UpdateDoc(ctx, serviceId, &api_doc.UpdateDoc{
85+
ID: uuid.New().String(),
86+
Content: string(result),
87+
})
88+
if err != nil {
89+
return fmt.Errorf("update api doc error:%v", err)
90+
}
91+
return i.serviceOverviewService.Update(ctx, serviceId, &service_overview.Update{
92+
ApiCount: &count,
93+
})
8394
})
8495
}
8596

@@ -93,10 +104,19 @@ func (i *imlAPIModule) deleteAPIDoc(ctx context.Context, serviceId string, path
93104
if err != nil {
94105
return err
95106
}
96-
return i.apiDocService.UpdateDoc(ctx, serviceId, &api_doc.UpdateDoc{
97-
ID: uuid.New().String(),
98-
Content: string(result),
107+
return i.transaction.Transaction(ctx, func(ctx context.Context) error {
108+
count, err := i.apiDocService.UpdateDoc(ctx, serviceId, &api_doc.UpdateDoc{
109+
ID: uuid.New().String(),
110+
Content: string(result),
111+
})
112+
if err != nil {
113+
return fmt.Errorf("update api doc error:%v", err)
114+
}
115+
return i.serviceOverviewService.Update(ctx, serviceId, &service_overview.Update{
116+
ApiCount: &count,
117+
})
99118
})
119+
100120
}
101121

102122
func (i *imlAPIModule) Create(ctx context.Context, serviceId string, input *ai_api_dto.CreateAPI) error {

module/api-doc/api_doc.go

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@ package api_doc
33
import (
44
"context"
55
"errors"
6+
"fmt"
7+
8+
"github.com/eolinker/go-common/store"
9+
10+
service_overview "github.com/APIParkLab/APIPark/service/service-overview"
611

712
api_doc_dto "github.com/APIParkLab/APIPark/module/api-doc/dto"
813
api_doc "github.com/APIParkLab/APIPark/service/api-doc"
@@ -16,8 +21,10 @@ import (
1621
var _ IAPIDocModule = (*imlAPIDocModule)(nil)
1722

1823
type imlAPIDocModule struct {
19-
apiDocService api_doc.IAPIDocService `autowired:""`
20-
serviceService service.IServiceService `autowired:""`
24+
apiDocService api_doc.IAPIDocService `autowired:""`
25+
serviceService service.IServiceService `autowired:""`
26+
serviceOverviewService service_overview.IOverviewService `autowired:""`
27+
transaction store.ITransaction `autowired:""`
2128
}
2229

2330
func (i *imlAPIDocModule) UpdateDoc(ctx context.Context, serviceId string, input *api_doc_dto.UpdateDoc) (*api_doc_dto.ApiDocDetail, error) {
@@ -29,11 +36,18 @@ func (i *imlAPIDocModule) UpdateDoc(ctx context.Context, serviceId string, input
2936
input.Id = uuid.New().String()
3037
}
3138
// 每个API加上前缀
32-
33-
err = i.apiDocService.UpdateDoc(ctx, serviceId, &api_doc.UpdateDoc{
34-
ID: input.Id,
35-
Content: input.Content,
36-
Prefix: info.Prefix,
39+
err = i.transaction.Transaction(ctx, func(ctx context.Context) error {
40+
count, err := i.apiDocService.UpdateDoc(ctx, serviceId, &api_doc.UpdateDoc{
41+
ID: input.Id,
42+
Content: input.Content,
43+
Prefix: info.Prefix,
44+
})
45+
if err != nil {
46+
return fmt.Errorf("update api doc error:%v", err)
47+
}
48+
return i.serviceOverviewService.Update(ctx, serviceId, &service_overview.Update{
49+
ApiCount: &count,
50+
})
3751
})
3852
if err != nil {
3953
return nil, err

module/catalogue/iml.go

Lines changed: 83 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import (
99
"strings"
1010
"time"
1111

12+
service_overview "github.com/APIParkLab/APIPark/service/service-overview"
13+
1214
mcp_server "github.com/APIParkLab/APIPark/mcp-server"
1315

1416
"github.com/APIParkLab/APIPark/module/monitor/driver"
@@ -58,23 +60,73 @@ var (
5860
)
5961

6062
type imlCatalogueModule struct {
61-
catalogueService catalogue.ICatalogueService `autowired:""`
62-
apiService api.IAPIService `autowired:""`
63-
apiDocService api_doc.IAPIDocService `autowired:""`
64-
serviceService service.IServiceService `autowired:""`
65-
serviceTagService service_tag.ITagService `autowired:""`
66-
serviceDocService service_doc.IDocService `autowired:""`
67-
tagService tag.ITagService `autowired:""`
68-
releaseService release.IReleaseService `autowired:""`
69-
subscribeService subscribe.ISubscribeService `autowired:""`
70-
subscribeApplyService subscribe.ISubscribeApplyService `autowired:""`
71-
transaction store.ITransaction `autowired:""`
72-
clusterService cluster.IClusterService `autowired:""`
73-
settingService setting.ISettingService `autowired:""`
74-
monitorService monitor.IMonitorService `autowired:""`
75-
root *Root
63+
catalogueService catalogue.ICatalogueService `autowired:""`
64+
apiService api.IAPIService `autowired:""`
65+
apiDocService api_doc.IAPIDocService `autowired:""`
66+
serviceService service.IServiceService `autowired:""`
67+
serviceOverviewService service_overview.IOverviewService `autowired:""`
68+
serviceTagService service_tag.ITagService `autowired:""`
69+
serviceDocService service_doc.IDocService `autowired:""`
70+
tagService tag.ITagService `autowired:""`
71+
releaseService release.IReleaseService `autowired:""`
72+
subscribeService subscribe.ISubscribeService `autowired:""`
73+
subscribeApplyService subscribe.ISubscribeApplyService `autowired:""`
74+
transaction store.ITransaction `autowired:""`
75+
clusterService cluster.IClusterService `autowired:""`
76+
settingService setting.ISettingService `autowired:""`
77+
monitorService monitor.IMonitorService `autowired:""`
78+
root *Root
7679
}
7780

81+
//func (i *imlCatalogueModule) OnInit() {
82+
// register.Handle(func(v server.Server) {
83+
// ctx := context.Background()
84+
// list, err := i.releaseService.GetRunningList(ctx)
85+
// if err != nil {
86+
// log.Errorf("onInit: get running list failed:%s", err.Error())
87+
// return
88+
// }
89+
// if len(list) < 1 || list[0].APICount > 0 {
90+
// return
91+
// }
92+
// serviceMap := make(map[string]*release.Release)
93+
// serviceIds := make([]string, 0, len(list))
94+
// for _, v := range list {
95+
// if _, ok := serviceMap[v.Service]; !ok {
96+
// serviceMap[v.Service] = v
97+
// serviceIds = append(serviceIds, v.Service)
98+
// }
99+
// }
100+
// if len(serviceIds) < 1 {
101+
// return
102+
// }
103+
// commitIds, err := i.releaseService.GetRunningApiDocCommits(ctx, serviceIds...)
104+
// if err != nil {
105+
// log.Errorf("onInit: get running api doc commits failed:%s", err.Error())
106+
// return
107+
// }
108+
// if len(commitIds) < 1 {
109+
// return
110+
// }
111+
// listCommits, err := i.apiDocService.ListDocCommit(ctx, commitIds...)
112+
// if err != nil {
113+
// log.Error("onInit: list doc commit failed:", err.Error())
114+
// return
115+
// }
116+
// for _, v := range listCommits {
117+
// m, ok := serviceMap[v.Target]
118+
// if !ok {
119+
// continue
120+
// }
121+
//
122+
// i.releaseService.UpdateRelease(ctx, m.UUID, &release.Update{
123+
// APICount: &v.Data.APICount,
124+
// })
125+
// }
126+
// })
127+
//
128+
//}
129+
78130
func (i *imlCatalogueModule) DefaultCatalogue(ctx context.Context) (*catalogue_dto.Catalogue, error) {
79131
catalogues, err := i.catalogueService.List(ctx)
80132
if err != nil {
@@ -447,25 +499,24 @@ func (i *imlCatalogueModule) Services(ctx context.Context, keyword string) ([]*c
447499
if err != nil {
448500
return nil, err
449501
}
450-
451502
serviceIds := utils.SliceToSlice(items, func(i *service.Service) string {
452503
return i.Id
453-
}, func(s *service.Service) bool {
454-
// 未发布的不给展示
455-
_, err = i.releaseService.GetRunning(ctx, s.Id)
456-
return err == nil
457504
})
458-
if len(serviceIds) < 1 {
459-
return nil, nil
460-
}
461-
462-
commits, err := i.releaseService.GetRunningApiDocCommits(ctx, serviceIds...)
505+
overviewMap, err := i.serviceOverviewService.Map(ctx, serviceIds...)
463506
if err != nil {
464507
return nil, err
465508
}
466-
apiCountMap, err := i.apiDocService.LatestAPICountByCommits(ctx, commits...)
467-
if err != nil {
468-
return nil, err
509+
serviceIds = utils.SliceToSlice(serviceIds, func(s string) string {
510+
return s
511+
}, func(s string) bool {
512+
// 只展示已发布的服务
513+
if info, ok := overviewMap[s]; ok && info.IsReleased {
514+
return true
515+
}
516+
return false
517+
})
518+
if len(serviceIds) < 1 {
519+
return nil, nil
469520
}
470521

471522
subscriberCountMap, err := i.subscribeService.CountMapByService(ctx, subscribe.ApplyStatusSubscribe, serviceIds...)
@@ -479,8 +530,9 @@ func (i *imlCatalogueModule) Services(ctx context.Context, keyword string) ([]*c
479530

480531
result := make([]*catalogue_dto.ServiceItem, 0, len(items))
481532
for _, v := range items {
482-
apiNum, ok := apiCountMap[v.Id]
483-
if !ok || apiNum < 1 {
533+
534+
ov, ok := overviewMap[v.Id]
535+
if !ok || ov.ReleaseApiCount < 1 {
484536
continue
485537
}
486538

@@ -489,8 +541,8 @@ func (i *imlCatalogueModule) Services(ctx context.Context, keyword string) ([]*c
489541
Name: v.Name,
490542
Tags: auto.List(serviceTagMap[v.Id]),
491543
Catalogue: auto.UUID(v.Catalogue),
492-
ApiNum: apiNum,
493544
SubscriberNum: subscriberCountMap[v.Id],
545+
ApiNum: ov.ReleaseApiCount,
494546
Description: v.Description,
495547
Logo: v.Logo,
496548
EnableMCP: v.EnableMCP,

0 commit comments

Comments
 (0)