ot_apps.go 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. package apis
  2. import (
  3. "fmt"
  4. "github.com/gin-gonic/gin"
  5. "github.com/go-admin-team/go-admin-core/sdk/api"
  6. "github.com/go-admin-team/go-admin-core/sdk/pkg/jwtauth/user"
  7. _ "github.com/go-admin-team/go-admin-core/sdk/pkg/response"
  8. omodels "go-admin/app/observe/models"
  9. "go-admin/utils"
  10. "slices"
  11. "go-admin/app/admin/models"
  12. "go-admin/app/admin/service"
  13. "go-admin/app/admin/service/dto"
  14. "go-admin/common/actions"
  15. )
  16. type OtApps struct {
  17. api.Api
  18. otApi utils.OtApi
  19. }
  20. // GetPage 获取OtApps列表
  21. // @Summary 获取OtApps列表
  22. // @Description 获取OtApps列表
  23. // @Tags OtApps
  24. // @Param name query string false "业务名"
  25. // @Param alias query string false "业务别名"
  26. // @Param contractInfo query string false "管理信息"
  27. // @Param contractPhone query string false "管理员手机号"
  28. // @Param pageSize query int false "页条数"
  29. // @Param pageIndex query int false "页码"
  30. // @Success 200 {object} response.Response{data=response.Page{list=[]models.OtApps}} "{"code": 200, "data": [...]}"
  31. // @Router /api/v1/ot-apps [get]
  32. // @Security Bearer
  33. func (e OtApps) GetPage(c *gin.Context) {
  34. req := dto.OtAppsGetPageReq{}
  35. s := service.OtApps{}
  36. err := e.MakeContext(c).
  37. MakeOrm().
  38. Bind(&req).
  39. MakeService(&s.Service).
  40. Errors
  41. if err != nil {
  42. e.Logger.Error(err)
  43. e.Error(500, err, err.Error())
  44. return
  45. }
  46. p := actions.GetPermissionFromContext(c)
  47. list := make([]models.OtApps, 0)
  48. var count int64
  49. err = s.GetPage(&req, p, &list, &count)
  50. if err != nil {
  51. e.Error(500, err, fmt.Sprintf("获取OtApps失败,\r\n失败信息 %s", err.Error()))
  52. return
  53. }
  54. e.PageOK(list, int(count), req.GetPageIndex(), req.GetPageSize(), "查询成功")
  55. }
  56. type GetOtAppsFullServiceItemVO struct {
  57. ServiceName string `json:"serviceName"`
  58. AppName string `json:"appName"`
  59. }
  60. type GetOtAppServiceList struct {
  61. FullServiceList []GetOtAppsFullServiceItemVO `json:"fullServiceList"`
  62. }
  63. // GetServiceList 获取OtApps可以关联的所有服务列表
  64. // @Summary 获取OtApps可以关联的所有服务列表
  65. // @Description 获取OtApps可以关联的所有服务列表
  66. // @Tags OtApps
  67. // @Success 200 {object} response.Response{data=GetOtAppServiceList} "{"code": 200, "data": [...]}"
  68. // @Router /api/v1/ot-apps/full-service-list [get]
  69. // @Security Bearer
  70. func (e OtApps) GetServiceList(c *gin.Context) {
  71. s := service.OtApps{}
  72. err := e.MakeContext(c).
  73. MakeOrm().
  74. MakeService(&s.Service).
  75. Errors
  76. if err != nil {
  77. e.Logger.Error(err)
  78. e.Error(500, err, err.Error())
  79. return
  80. }
  81. var ret GetOtAppServiceList
  82. var spans []omodels.Trace
  83. chOrm := e.otApi.MakeContext(c).MakeDB().ChOrm
  84. if errGetService := chOrm.
  85. Raw(`select distinct AppAlias, ServiceName from otel.otel_traces where Timestamp > now() - interval 15 minute `).
  86. Find(&spans).Error; errGetService != nil {
  87. e.Error(500, errGetService, "获取AppAlias相关ServiceName失败")
  88. return
  89. }
  90. var overrideServiceAppNames []omodels.DataServiceAppName
  91. if errFindOverrideServiceNames := s.Orm.Find(&overrideServiceAppNames).Error; errFindOverrideServiceNames != nil {
  92. e.Error(500, errFindOverrideServiceNames, "获取data_service_appname覆盖关联失败")
  93. return
  94. }
  95. service2appNameBind := make(map[string]string, len(overrideServiceAppNames))
  96. for _, serviceAppNameModel := range overrideServiceAppNames {
  97. service2appNameBind[serviceAppNameModel.ServiceName] = serviceAppNameModel.AppName
  98. }
  99. for _, span := range spans {
  100. item := GetOtAppsFullServiceItemVO{ServiceName: span.ServiceName}
  101. if appName, find := service2appNameBind[span.ServiceName]; find {
  102. item.AppName = appName
  103. }
  104. ret.FullServiceList = append(ret.FullServiceList, item)
  105. }
  106. e.OK(ret, "查询成功")
  107. }
  108. type GetOtAppsVO struct {
  109. models.OtApps
  110. ServiceNames []string `json:"serviceNames"`
  111. FullServiceList []GetOtAppsFullServiceItemVO `json:"fullServiceList"`
  112. }
  113. // Get 获取OtApps
  114. // @Summary 获取OtApps
  115. // @Description 获取OtApps
  116. // @Tags OtApps
  117. // @Param id path int false "id"
  118. // @Success 200 {object} response.Response{data=GetOtAppsVO} "{"code": 200, "data": [...]}"
  119. // @Router /api/v1/ot-apps/{id} [get]
  120. // @Security Bearer
  121. func (e OtApps) Get(c *gin.Context) {
  122. req := dto.OtAppsGetReq{}
  123. s := service.OtApps{}
  124. err := e.MakeContext(c).
  125. MakeOrm().
  126. Bind(&req).
  127. MakeService(&s.Service).
  128. Errors
  129. if err != nil {
  130. e.Logger.Error(err)
  131. e.Error(500, err, err.Error())
  132. return
  133. }
  134. var object models.OtApps
  135. p := actions.GetPermissionFromContext(c)
  136. err = s.Get(&req, p, &object)
  137. if err != nil {
  138. e.Error(500, err, fmt.Sprintf("获取OtApps失败,\r\n失败信息 %s", err.Error()))
  139. return
  140. }
  141. var ret GetOtAppsVO
  142. ret.OtApps = object
  143. var spans []omodels.Trace
  144. chOrm := e.otApi.MakeContext(c).MakeDB().ChOrm
  145. if errGetService := chOrm.
  146. Raw(`select distinct AppAlias, ServiceName from otel.otel_traces where Timestamp > now() - interval 15 minute `).
  147. Find(&spans).Error; errGetService != nil {
  148. e.Error(500, errGetService, "获取AppAlias相关ServiceName失败")
  149. return
  150. }
  151. var overrideServiceAppNames []omodels.DataServiceAppName
  152. if errFindOverrideServiceNames := s.Orm.Find(&overrideServiceAppNames).Error; errFindOverrideServiceNames != nil {
  153. e.Error(500, errFindOverrideServiceNames, "获取data_service_appname覆盖关联失败")
  154. return
  155. }
  156. service2appNameBind := make(map[string]string, len(overrideServiceAppNames))
  157. for _, serviceAppNameModel := range overrideServiceAppNames {
  158. service2appNameBind[serviceAppNameModel.ServiceName] = serviceAppNameModel.AppName
  159. }
  160. ret.ServiceNames = make([]string, 0, len(spans)+len(overrideServiceAppNames))
  161. for _, span := range spans {
  162. if span.AppAlias == object.Alias {
  163. ret.ServiceNames = append(ret.ServiceNames, span.ServiceName)
  164. }
  165. item := GetOtAppsFullServiceItemVO{ServiceName: span.ServiceName}
  166. if appName, find := service2appNameBind[span.ServiceName]; find {
  167. item.AppName = appName
  168. }
  169. ret.FullServiceList = append(ret.FullServiceList, item)
  170. }
  171. for _, name := range overrideServiceAppNames {
  172. if name.AppName == object.Alias {
  173. ret.ServiceNames = append(ret.ServiceNames, name.ServiceName)
  174. }
  175. }
  176. slices.Sort(ret.ServiceNames)
  177. ret.ServiceNames = slices.Compact(ret.ServiceNames)
  178. e.OK(ret, "查询成功")
  179. }
  180. // Insert 创建OtApps
  181. // @Summary 创建OtApps
  182. // @Description 创建OtApps
  183. // @Tags OtApps
  184. // @Accept application/json
  185. // @Product application/json
  186. // @Param data body dto.OtAppsInsertReq true "data"
  187. // @Success 200 {object} response.Response "{"code": 200, "message": "添加成功"}"
  188. // @Router /api/v1/ot-apps [post]
  189. // @Security Bearer
  190. func (e OtApps) Insert(c *gin.Context) {
  191. req := dto.OtAppsInsertReq{}
  192. s := service.OtApps{}
  193. err := e.MakeContext(c).
  194. MakeOrm().
  195. Bind(&req).
  196. MakeService(&s.Service).
  197. Errors
  198. if err != nil {
  199. e.Logger.Error(err)
  200. e.Error(500, err, err.Error())
  201. return
  202. }
  203. // 设置创建人
  204. req.SetCreateBy(user.GetUserId(c))
  205. err = s.Insert(&req)
  206. if err != nil {
  207. e.Error(500, err, fmt.Sprintf("创建OtApps失败,\r\n失败信息 %s", err.Error()))
  208. return
  209. }
  210. e.OK(req.GetId(), "创建成功")
  211. }
  212. // Update 修改OtApps
  213. // @Summary 修改OtApps
  214. // @Description 修改OtApps
  215. // @Tags OtApps
  216. // @Accept application/json
  217. // @Product application/json
  218. // @Param id path int true "id"
  219. // @Param data body dto.OtAppsUpdateReq true "body"
  220. // @Success 200 {object} response.Response "{"code": 200, "message": "修改成功"}"
  221. // @Router /api/v1/ot-apps/{id} [put]
  222. // @Security Bearer
  223. func (e OtApps) Update(c *gin.Context) {
  224. req := dto.OtAppsUpdateReq{}
  225. s := service.OtApps{}
  226. err := e.MakeContext(c).
  227. MakeOrm().
  228. Bind(&req).
  229. MakeService(&s.Service).
  230. Errors
  231. if err != nil {
  232. e.Logger.Error(err)
  233. e.Error(500, err, err.Error())
  234. return
  235. }
  236. req.SetUpdateBy(user.GetUserId(c))
  237. p := actions.GetPermissionFromContext(c)
  238. err = s.Update(&req, p)
  239. if err != nil {
  240. e.Error(500, err, fmt.Sprintf("修改OtApps失败,\r\n失败信息 %s", err.Error()))
  241. return
  242. }
  243. e.OK(req.GetId(), "修改成功")
  244. }
  245. // Delete 删除OtApps
  246. // @Summary 删除OtApps
  247. // @Description 删除OtApps
  248. // @Tags OtApps
  249. // @Param data body dto.OtAppsDeleteReq true "body"
  250. // @Success 200 {object} response.Response "{"code": 200, "message": "删除成功"}"
  251. // @Router /api/v1/ot-apps [delete]
  252. // @Security Bearer
  253. func (e OtApps) Delete(c *gin.Context) {
  254. s := service.OtApps{}
  255. req := dto.OtAppsDeleteReq{}
  256. err := e.MakeContext(c).
  257. MakeOrm().
  258. Bind(&req).
  259. MakeService(&s.Service).
  260. Errors
  261. if err != nil {
  262. e.Logger.Error(err)
  263. e.Error(500, err, err.Error())
  264. return
  265. }
  266. // req.SetUpdateBy(user.GetUserId(c))
  267. p := actions.GetPermissionFromContext(c)
  268. err = s.Remove(&req, p)
  269. if err != nil {
  270. e.Error(500, err, fmt.Sprintf("删除OtApps失败,\r\n失败信息 %s", err.Error()))
  271. return
  272. }
  273. e.OK(req.GetId(), "删除成功")
  274. }
  275. func (e OtApps) AddDashboard(c *gin.Context) {
  276. s := service.OtApps{}
  277. op := service.OtPlugins{}
  278. ad := service.OtAppDashboard{}
  279. req := dto.OtAppsGetReq{}
  280. err := e.MakeContext(c).
  281. MakeOrm().
  282. Bind(&req).
  283. MakeService(&s.Service).
  284. MakeService(&op.Service).
  285. MakeService(&ad.Service).
  286. Errors
  287. if err != nil {
  288. e.Logger.Error(err)
  289. e.Error(500, err, err.Error())
  290. return
  291. }
  292. var object models.OtApps
  293. p := actions.GetPermissionFromContext(c)
  294. err = s.Get(&req, p, &object)
  295. if err != nil {
  296. e.Error(500, err, fmt.Sprintf("获取OtApps失败, \r\n失败信息 %s", err.Error()))
  297. return
  298. }
  299. // go-test:
  300. // object.Alias = "PICC_CreateTest"
  301. // object.Id = req.Id
  302. if err := s.CreateDashBoard(&ad, &op, &object); err != nil {
  303. e.Logger.Error("AddDashboard::CreateDasbboard err: ", err)
  304. e.Error(500, err, err.Error())
  305. return
  306. }
  307. e.OK(req.GetId(), "创建成功")
  308. }