package apis import ( "fmt" "github.com/gin-gonic/gin" "github.com/go-admin-team/go-admin-core/sdk/api" "github.com/go-admin-team/go-admin-core/sdk/pkg/jwtauth/user" _ "github.com/go-admin-team/go-admin-core/sdk/pkg/response" omodels "go-admin/app/observe/models" "go-admin/utils" "slices" "go-admin/app/admin/models" "go-admin/app/admin/service" "go-admin/app/admin/service/dto" "go-admin/common/actions" ) type OtApps struct { api.Api otApi utils.OtApi } // GetPage 获取OtApps列表 // @Summary 获取OtApps列表 // @Description 获取OtApps列表 // @Tags OtApps // @Param name query string false "业务名" // @Param alias query string false "业务别名" // @Param contractInfo query string false "管理信息" // @Param contractPhone query string false "管理员手机号" // @Param pageSize query int false "页条数" // @Param pageIndex query int false "页码" // @Success 200 {object} response.Response{data=response.Page{list=[]models.OtApps}} "{"code": 200, "data": [...]}" // @Router /api/v1/ot-apps [get] // @Security Bearer func (e OtApps) GetPage(c *gin.Context) { req := dto.OtAppsGetPageReq{} s := service.OtApps{} err := e.MakeContext(c). MakeOrm(). Bind(&req). MakeService(&s.Service). Errors if err != nil { e.Logger.Error(err) e.Error(500, err, err.Error()) return } p := actions.GetPermissionFromContext(c) list := make([]models.OtApps, 0) var count int64 err = s.GetPage(&req, p, &list, &count) if err != nil { e.Error(500, err, fmt.Sprintf("获取OtApps失败,\r\n失败信息 %s", err.Error())) return } e.PageOK(list, int(count), req.GetPageIndex(), req.GetPageSize(), "查询成功") } type GetOtAppsFullServiceItemVO struct { ServiceName string `json:"serviceName"` AppName string `json:"appName"` } type GetOtAppServiceList struct { FullServiceList []GetOtAppsFullServiceItemVO `json:"fullServiceList"` } // GetServiceList 获取OtApps可以关联的所有服务列表 // @Summary 获取OtApps可以关联的所有服务列表 // @Description 获取OtApps可以关联的所有服务列表 // @Tags OtApps // @Success 200 {object} response.Response{data=GetOtAppServiceList} "{"code": 200, "data": [...]}" // @Router /api/v1/ot-apps/full-service-list [get] // @Security Bearer func (e OtApps) GetServiceList(c *gin.Context) { s := service.OtApps{} err := e.MakeContext(c). MakeOrm(). MakeService(&s.Service). Errors if err != nil { e.Logger.Error(err) e.Error(500, err, err.Error()) return } var ret GetOtAppServiceList var spans []omodels.Trace chOrm := e.otApi.MakeContext(c).MakeDB().ChOrm if errGetService := chOrm. Raw(`select distinct AppAlias, ServiceName from otel.otel_traces where Timestamp > now() - interval 15 minute `). Find(&spans).Error; errGetService != nil { e.Error(500, errGetService, "获取AppAlias相关ServiceName失败") return } var overrideServiceAppNames []omodels.DataServiceAppName if errFindOverrideServiceNames := s.Orm.Find(&overrideServiceAppNames).Error; errFindOverrideServiceNames != nil { e.Error(500, errFindOverrideServiceNames, "获取data_service_appname覆盖关联失败") return } service2appNameBind := make(map[string]string, len(overrideServiceAppNames)) for _, serviceAppNameModel := range overrideServiceAppNames { service2appNameBind[serviceAppNameModel.ServiceName] = serviceAppNameModel.AppName } for _, span := range spans { item := GetOtAppsFullServiceItemVO{ServiceName: span.ServiceName} if appName, find := service2appNameBind[span.ServiceName]; find { item.AppName = appName } ret.FullServiceList = append(ret.FullServiceList, item) } e.OK(ret, "查询成功") } type GetOtAppsVO struct { models.OtApps ServiceNames []string `json:"serviceNames"` FullServiceList []GetOtAppsFullServiceItemVO `json:"fullServiceList"` } // Get 获取OtApps // @Summary 获取OtApps // @Description 获取OtApps // @Tags OtApps // @Param id path int false "id" // @Success 200 {object} response.Response{data=GetOtAppsVO} "{"code": 200, "data": [...]}" // @Router /api/v1/ot-apps/{id} [get] // @Security Bearer func (e OtApps) Get(c *gin.Context) { req := dto.OtAppsGetReq{} s := service.OtApps{} err := e.MakeContext(c). MakeOrm(). Bind(&req). MakeService(&s.Service). Errors if err != nil { e.Logger.Error(err) e.Error(500, err, err.Error()) return } var object models.OtApps p := actions.GetPermissionFromContext(c) err = s.Get(&req, p, &object) if err != nil { e.Error(500, err, fmt.Sprintf("获取OtApps失败,\r\n失败信息 %s", err.Error())) return } var ret GetOtAppsVO ret.OtApps = object var spans []omodels.Trace chOrm := e.otApi.MakeContext(c).MakeDB().ChOrm if errGetService := chOrm. Raw(`select distinct AppAlias, ServiceName from otel.otel_traces where Timestamp > now() - interval 15 minute `). Find(&spans).Error; errGetService != nil { e.Error(500, errGetService, "获取AppAlias相关ServiceName失败") return } var overrideServiceAppNames []omodels.DataServiceAppName if errFindOverrideServiceNames := s.Orm.Find(&overrideServiceAppNames).Error; errFindOverrideServiceNames != nil { e.Error(500, errFindOverrideServiceNames, "获取data_service_appname覆盖关联失败") return } service2appNameBind := make(map[string]string, len(overrideServiceAppNames)) for _, serviceAppNameModel := range overrideServiceAppNames { service2appNameBind[serviceAppNameModel.ServiceName] = serviceAppNameModel.AppName } ret.ServiceNames = make([]string, 0, len(spans)+len(overrideServiceAppNames)) for _, span := range spans { if span.AppAlias == object.Alias { ret.ServiceNames = append(ret.ServiceNames, span.ServiceName) } item := GetOtAppsFullServiceItemVO{ServiceName: span.ServiceName} if appName, find := service2appNameBind[span.ServiceName]; find { item.AppName = appName } ret.FullServiceList = append(ret.FullServiceList, item) } for _, name := range overrideServiceAppNames { if name.AppName == object.Alias { ret.ServiceNames = append(ret.ServiceNames, name.ServiceName) } } slices.Sort(ret.ServiceNames) ret.ServiceNames = slices.Compact(ret.ServiceNames) e.OK(ret, "查询成功") } // Insert 创建OtApps // @Summary 创建OtApps // @Description 创建OtApps // @Tags OtApps // @Accept application/json // @Product application/json // @Param data body dto.OtAppsInsertReq true "data" // @Success 200 {object} response.Response "{"code": 200, "message": "添加成功"}" // @Router /api/v1/ot-apps [post] // @Security Bearer func (e OtApps) Insert(c *gin.Context) { req := dto.OtAppsInsertReq{} s := service.OtApps{} err := e.MakeContext(c). MakeOrm(). Bind(&req). MakeService(&s.Service). Errors if err != nil { e.Logger.Error(err) e.Error(500, err, err.Error()) return } // 设置创建人 req.SetCreateBy(user.GetUserId(c)) err = s.Insert(&req) if err != nil { e.Error(500, err, fmt.Sprintf("创建OtApps失败,\r\n失败信息 %s", err.Error())) return } e.OK(req.GetId(), "创建成功") } // Update 修改OtApps // @Summary 修改OtApps // @Description 修改OtApps // @Tags OtApps // @Accept application/json // @Product application/json // @Param id path int true "id" // @Param data body dto.OtAppsUpdateReq true "body" // @Success 200 {object} response.Response "{"code": 200, "message": "修改成功"}" // @Router /api/v1/ot-apps/{id} [put] // @Security Bearer func (e OtApps) Update(c *gin.Context) { req := dto.OtAppsUpdateReq{} s := service.OtApps{} err := e.MakeContext(c). MakeOrm(). Bind(&req). MakeService(&s.Service). Errors if err != nil { e.Logger.Error(err) e.Error(500, err, err.Error()) return } req.SetUpdateBy(user.GetUserId(c)) p := actions.GetPermissionFromContext(c) err = s.Update(&req, p) if err != nil { e.Error(500, err, fmt.Sprintf("修改OtApps失败,\r\n失败信息 %s", err.Error())) return } e.OK(req.GetId(), "修改成功") } // Delete 删除OtApps // @Summary 删除OtApps // @Description 删除OtApps // @Tags OtApps // @Param data body dto.OtAppsDeleteReq true "body" // @Success 200 {object} response.Response "{"code": 200, "message": "删除成功"}" // @Router /api/v1/ot-apps [delete] // @Security Bearer func (e OtApps) Delete(c *gin.Context) { s := service.OtApps{} req := dto.OtAppsDeleteReq{} err := e.MakeContext(c). MakeOrm(). Bind(&req). MakeService(&s.Service). Errors if err != nil { e.Logger.Error(err) e.Error(500, err, err.Error()) return } // req.SetUpdateBy(user.GetUserId(c)) p := actions.GetPermissionFromContext(c) err = s.Remove(&req, p) if err != nil { e.Error(500, err, fmt.Sprintf("删除OtApps失败,\r\n失败信息 %s", err.Error())) return } e.OK(req.GetId(), "删除成功") } func (e OtApps) AddDashboard(c *gin.Context) { s := service.OtApps{} op := service.OtPlugins{} ad := service.OtAppDashboard{} req := dto.OtAppsGetReq{} err := e.MakeContext(c). MakeOrm(). Bind(&req). MakeService(&s.Service). MakeService(&op.Service). MakeService(&ad.Service). Errors if err != nil { e.Logger.Error(err) e.Error(500, err, err.Error()) return } var object models.OtApps p := actions.GetPermissionFromContext(c) err = s.Get(&req, p, &object) if err != nil { e.Error(500, err, fmt.Sprintf("获取OtApps失败, \r\n失败信息 %s", err.Error())) return } // go-test: // object.Alias = "PICC_CreateTest" // object.Id = req.Id if err := s.CreateDashBoard(&ad, &op, &object); err != nil { e.Logger.Error("AddDashboard::CreateDasbboard err: ", err) e.Error(500, err, err.Error()) return } e.OK(req.GetId(), "创建成功") }