diff --git a/controller/service/iml.go b/controller/service/iml.go index aa5cebef..e35f73d7 100644 --- a/controller/service/iml.go +++ b/controller/service/iml.go @@ -70,24 +70,54 @@ var ( ) type imlServiceController struct { - module service.IServiceModule `autowired:""` - docModule service.IServiceDocModule `autowired:""` - subscribeModule subscribe.ISubscribeModule `autowired:""` - aiAPIModule ai_api.IAPIModule `autowired:""` - routerModule router.IRouterModule `autowired:""` - apiDocModule api_doc.IAPIDocModule `autowired:""` - providerModule ai.IProviderModule `autowired:""` - aiLocalModel ai_local.ILocalModelModule `autowired:""` - appModule service.IAppModule `autowired:""` - upstreamModule upstream.IUpstreamModule `autowired:""` - settingModule system.ISettingModule `autowired:""` - teamModule team.ITeamModule `autowired:""` - catalogueModule catalogue.ICatalogueModule `autowired:""` - monitorModule monitor.IMonitorStatisticModule `autowired:""` - transaction store.ITransaction `autowired:""` + module service.IServiceModule `autowired:""` + docModule service.IServiceDocModule `autowired:""` + subscribeModule subscribe.ISubscribeModule `autowired:""` + aiAPIModule ai_api.IAPIModule `autowired:""` + routerModule router.IRouterModule `autowired:""` + apiDocModule api_doc.IAPIDocModule `autowired:""` + providerModule ai.IProviderModule `autowired:""` + aiLocalModel ai_local.ILocalModelModule `autowired:""` + appModule service.IAppModule `autowired:""` + upstreamModule upstream.IUpstreamModule `autowired:""` + settingModule system.ISettingModule `autowired:""` + teamModule team.ITeamModule `autowired:""` + catalogueModule catalogue.ICatalogueModule `autowired:""` + monitorModule monitor.IMonitorStatisticModule `autowired:""` + monitorConfigModule monitor.IMonitorConfigModule `autowired:""` + transaction store.ITransaction `autowired:""` } -func (i *imlServiceController) AIChartOverview(ctx *gin.Context, serviceId string, start string, end string) (*monitor_dto.ChartAIOverview, error) { +func (i *imlServiceController) ServiceOverview(ctx *gin.Context, serviceId string) (*service_dto.Overview, error) { + o, err := i.module.ServiceOverview(ctx, serviceId) + if err != nil { + return nil, err + } + cfg, err := i.monitorConfigModule.GetMonitorConfig(ctx) + if err != nil { + return nil, err + } + if len(cfg.Config) < 1 { + return o, nil + } + statistics, err := i.monitorModule.ProviderStatistics(ctx, &monitor_dto.StatisticInput{ + Services: []string{serviceId}, + CommonInput: &monitor_dto.CommonInput{ + Start: time.Now().Add(-24 * 30 * time.Hour).Unix(), + End: time.Now().Unix(), + }, + }) + if err != nil { + return nil, err + } + if len(statistics) < 1 { + return o, nil + } + o.InvokeNum = statistics[0].RequestTotal + return o, nil +} + +func (i *imlServiceController) AIChartOverview(ctx *gin.Context, serviceId string, start string, end string) (*monitor_dto.ServiceChartAIOverview, error) { s, e, err := formatTime(start, end) if err != nil { return nil, err @@ -95,10 +125,35 @@ func (i *imlServiceController) AIChartOverview(ctx *gin.Context, serviceId strin if serviceId == "" { return nil, fmt.Errorf("service is required") } - return i.monitorModule.AIChartOverview(ctx, serviceId, s, e) + so, err := i.module.ServiceOverview(ctx, serviceId) + if err != nil { + return nil, err + } + result := &monitor_dto.ServiceChartAIOverview{ + EnableMCP: so.EnableMCP, + SubscriberNum: so.SubscriberNum, + APINum: so.APINum, + ServiceKind: so.ServiceKind, + } + cfg, err := i.monitorConfigModule.GetMonitorConfig(ctx) + if err != nil { + return nil, err + } + if len(cfg.Config) < 1 { + return result, nil + } + + o, err := i.monitorModule.AIChartOverview(ctx, serviceId, s, e) + if err != nil { + return nil, err + } + + result.AvailableMonitor = true + result.ChartAIOverview = o + return result, nil } -func (i *imlServiceController) RestChartOverview(ctx *gin.Context, serviceId string, start string, end string) (*monitor_dto.ChartRestOverview, error) { +func (i *imlServiceController) RestChartOverview(ctx *gin.Context, serviceId string, start string, end string) (*monitor_dto.ServiceChartRestOverview, error) { s, e, err := formatTime(start, end) if err != nil { return nil, err @@ -106,7 +161,30 @@ func (i *imlServiceController) RestChartOverview(ctx *gin.Context, serviceId str if serviceId == "" { return nil, fmt.Errorf("service is required") } - return i.monitorModule.RestChartOverview(ctx, serviceId, s, e) + so, err := i.module.ServiceOverview(ctx, serviceId) + if err != nil { + return nil, err + } + result := &monitor_dto.ServiceChartRestOverview{ + EnableMCP: so.EnableMCP, + SubscriberNum: so.SubscriberNum, + APINum: so.APINum, + ServiceKind: so.ServiceKind, + } + cfg, err := i.monitorConfigModule.GetMonitorConfig(ctx) + if err != nil { + return nil, err + } + if len(cfg.Config) < 1 { + return result, nil + } + o, err := i.monitorModule.RestChartOverview(ctx, serviceId, s, e) + if err != nil { + return nil, err + } + result.AvailableMonitor = true + result.ChartRestOverview = o + return result, nil } func formatTime(start string, end string) (int64, int64, error) { diff --git a/controller/service/service.go b/controller/service/service.go index d443fc85..796a8a2e 100644 --- a/controller/service/service.go +++ b/controller/service/service.go @@ -37,8 +37,10 @@ type IServiceController interface { Top10(ctx *gin.Context, serviceId string, start string, end string) ([]*monitor_dto.TopN, []*monitor_dto.TopN, error) - AIChartOverview(ctx *gin.Context, serviceId string, start string, end string) (*monitor_dto.ChartAIOverview, error) - RestChartOverview(ctx *gin.Context, serviceId string, start string, end string) (*monitor_dto.ChartRestOverview, error) + AIChartOverview(ctx *gin.Context, serviceId string, start string, end string) (*monitor_dto.ServiceChartAIOverview, error) + RestChartOverview(ctx *gin.Context, serviceId string, start string, end string) (*monitor_dto.ServiceChartRestOverview, error) + + ServiceOverview(ctx *gin.Context, serviceId string) (*service_dto.Overview, error) } type IAppController interface { diff --git a/module/monitor/driver/influxdb-v2/executor.go b/module/monitor/driver/influxdb-v2/executor.go index b73b7b92..e939279c 100644 --- a/module/monitor/driver/influxdb-v2/executor.go +++ b/module/monitor/driver/influxdb-v2/executor.go @@ -442,9 +442,16 @@ func (e *executor) aggregateSummary(ctx context.Context, start time.Time, end ti result := make(map[string]*monitor.Aggregate) for _, field := range fields { a := new(monitor.Aggregate) - a.Avg = int64(avgRes[field+"_avg"].(float64)) - a.Min = minRes[field+"_min"].(int64) - a.Max = maxRes[field+"_max"].(int64) + if v, ok := avgRes[field+"_avg"]; ok { + a.Avg = int64(v.(float64)) + } + if v, ok := maxRes[field+"_max"]; ok { + a.Min = v.(int64) + } + if v, ok := minRes[field+"_min"]; ok { + a.Min = v.(int64) + } + result[field] = a } diff --git a/module/monitor/dto/output.go b/module/monitor/dto/output.go index a69f616a..c394b585 100644 --- a/module/monitor/dto/output.go +++ b/module/monitor/dto/output.go @@ -186,6 +186,24 @@ type ChartRestOverview struct { Date []string `json:"date"` } +type ServiceChartRestOverview struct { + EnableMCP bool `json:"enable_mcp"` + SubscriberNum int64 `json:"subscriber_num"` + APINum int64 `json:"api_num"` + ServiceKind string `json:"service_kind"` + AvailableMonitor bool `json:"available_monitor"` + *ChartRestOverview +} + +type ServiceChartAIOverview struct { + EnableMCP bool `json:"enable_mcp"` + SubscriberNum int64 `json:"subscriber_num"` + APINum int64 `json:"api_num"` + ServiceKind string `json:"service_kind"` + AvailableMonitor bool `json:"available_monitor"` + *ChartAIOverview +} + type TopN struct { Id string `json:"id"` Name string `json:"name"` diff --git a/module/service/dto/output.go b/module/service/dto/output.go index d4a8a68b..dc8694dc 100644 --- a/module/service/dto/output.go +++ b/module/service/dto/output.go @@ -221,3 +221,18 @@ type ExportApp struct { Description string `json:"description"` Team string `json:"team"` } + +type Overview struct { + Id string `json:"id"` + Name string `json:"name"` + Description string `json:"description"` + EnableMCP bool `json:"enable_mcp"` + ServiceKind string `json:"service_kind"` + SubscriberNum int64 `json:"subscriber_num"` + InvokeNum int64 `json:"invoke_num"` + Logo string `json:"logo"` + AvailableMonitor bool `json:"available_monitor"` + IsReleased bool `json:"is_released"` + Catalogue auto.Label `json:"catalogue" aolabel:"catalogue"` + APINum int64 `json:"api_num"` +} diff --git a/module/service/iml.go b/module/service/iml.go index d7b05ec9..88423d74 100644 --- a/module/service/iml.go +++ b/module/service/iml.go @@ -79,14 +79,53 @@ type imlServiceModule struct { tagService tag.ITagService `autowired:""` localModelService ai_local.ILocalModelService `autowired:""` - serviceTagService service_tag.ITagService `autowired:""` - apiService api.IAPIService `autowired:""` - apiDocService api_doc.IAPIDocService `autowired:""` - clusterService cluster.IClusterService `autowired:""` - transaction store.ITransaction `autowired:""` + serviceTagService service_tag.ITagService `autowired:""` + apiService api.IAPIService `autowired:""` + apiDocService api_doc.IAPIDocService `autowired:""` + clusterService cluster.IClusterService `autowired:""` + subscribeServer subscribe.ISubscribeService `autowired:""` releaseService release.IReleaseService `autowired:""` serviceModelMappingService service_model_mapping.IServiceModelMappingService `autowired:""` + + transaction store.ITransaction `autowired:""` +} + +func (i *imlServiceModule) ServiceOverview(ctx context.Context, id string) (*service_dto.Overview, error) { + info, err := i.serviceService.Get(ctx, id) + if err != nil { + return nil, err + } + + apiCountMap, err := i.apiDocService.APICountByServices(ctx, id) + if err != nil { + return nil, err + } + subscribeMap, err := i.subscribeServer.CountMapByService(ctx, subscribe.ApplyStatusSubscribe, id) + if err != nil { + return nil, err + } + result := &service_dto.Overview{ + Id: info.Id, + Name: info.Name, + Description: info.Description, + EnableMCP: info.EnableMCP, + ServiceKind: info.Kind.String(), + SubscriberNum: subscribeMap[id], + Logo: info.Logo, + Catalogue: auto.UUID(info.Catalogue), + APINum: apiCountMap[id], + } + _, err = i.releaseService.GetRunning(ctx, id) + if err != nil { + if !errors.Is(err, gorm.ErrRecordNotFound) { + return nil, err + } + } else { + result.IsReleased = true + } + + return result, nil } func (i *imlServiceModule) OnInit() { diff --git a/module/service/module.go b/module/service/module.go index 8f0c709c..b5f9df1b 100644 --- a/module/service/module.go +++ b/module/service/module.go @@ -34,6 +34,8 @@ type IServiceModule interface { //MySimple 获取我的简易项目列表 MySimple(ctx context.Context) ([]*service_dto.SimpleServiceItem, error) + + ServiceOverview(ctx context.Context, id string) (*service_dto.Overview, error) } type IServiceDocModule interface { diff --git a/plugins/core/service.go b/plugins/core/service.go index eb800f30..1a0726ae 100644 --- a/plugins/core/service.go +++ b/plugins/core/service.go @@ -43,5 +43,6 @@ func (p *plugin) ServiceApis() []pm3.Api { pm3.CreateApiWidthDoc(http.MethodGet, "/api/v1/service/monitor/top10", []string{"context", "query:service", "query:start", "query:end"}, []string{"apis", "consumers"}, p.serviceController.Top10, access.SystemWorkspaceServiceViewAll, access.TeamTeamServiceView), pm3.CreateApiWidthDoc(http.MethodGet, "/api/v1/service/overview/monitor/ai", []string{"context", "query:service", "query:start", "query:end"}, []string{"overview"}, p.serviceController.AIChartOverview, access.SystemWorkspaceServiceViewAll, access.TeamTeamServiceView), pm3.CreateApiWidthDoc(http.MethodGet, "/api/v1/service/overview/monitor/rest", []string{"context", "query:service", "query:start", "query:end"}, []string{"overview"}, p.serviceController.RestChartOverview, access.SystemWorkspaceServiceViewAll, access.TeamTeamServiceView), + pm3.CreateApiWidthDoc(http.MethodGet, "/api/v1/service/overview/basic", []string{"context", "query:service"}, []string{"overview"}, p.serviceController.ServiceOverview, access.SystemWorkspaceServiceViewAll, access.TeamTeamServiceView), } }