urlmapping.go 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. package apis
  2. import (
  3. "errors"
  4. "fmt"
  5. "go-admin/app/observe/service"
  6. "go-admin/app/observe/service/dto"
  7. "go-admin/config"
  8. "go-admin/utils"
  9. "net/http"
  10. "time"
  11. "github.com/gin-gonic/gin"
  12. "github.com/go-admin-team/go-admin-core/sdk/pkg/jwtauth/user"
  13. )
  14. type UrlMapping struct {
  15. utils.OtApi
  16. }
  17. // GetPage 获取OtUrlMapping列表
  18. func (e UrlMapping) GetPage(c *gin.Context) {
  19. req := dto.UrlMappingGetPageReq{}
  20. svc := service.UrlMapping{}
  21. if err := e.Init(c, &req, &svc.OtService); err != nil {
  22. return
  23. }
  24. list := make([]dto.UrlMappingListResp, 0)
  25. var count int64
  26. if err := svc.GetPage(&req, &list, &count); err != nil {
  27. e.Error(http.StatusInternalServerError, err, err.Error())
  28. return
  29. }
  30. e.PageOK(list, int(count), req.GetPageIndex(), req.GetPageSize(), "查询成功")
  31. }
  32. func (e UrlMapping) Favors(c *gin.Context) {
  33. req := dto.UrlMappingGetFavorsReq{}
  34. svc := service.UrlMapping{}
  35. if err := e.Init(c, &req, &svc.OtService); err != nil {
  36. return
  37. }
  38. if req.StartTime == 0 || req.EndTime == 0 {
  39. req.EndTime = time.Now().Unix()
  40. req.StartTime = req.EndTime - int64((60 * time.Minute).Seconds())
  41. }
  42. resp := make([]dto.UrlMappingListResp, 0)
  43. if err := svc.GetFavors(&req, &resp); err != nil {
  44. e.Error(http.StatusInternalServerError, err, err.Error())
  45. return
  46. }
  47. e.OK(resp, "success")
  48. }
  49. // Get 获取UrlMapping
  50. func (e UrlMapping) Get(c *gin.Context) {
  51. req := dto.UrlMappingGetReq{}
  52. s := service.UrlMapping{}
  53. err := e.MakeContext(c).
  54. MakeOrm().
  55. Bind(&req).
  56. MakeService(&s.OtService).
  57. Errors
  58. if err != nil {
  59. e.Logger.Error(err)
  60. e.Error(http.StatusInternalServerError, err, err.Error())
  61. return
  62. }
  63. result := dto.UrlMappingGetResp{}
  64. err = s.Get(&req, &result)
  65. if err != nil {
  66. e.Error(http.StatusInternalServerError, err, fmt.Sprintf("获取UrlMapping失败: %s", err.Error()))
  67. return
  68. }
  69. e.OK(result, "查询成功")
  70. }
  71. func (e UrlMapping) GetDetail(c *gin.Context) {
  72. req := dto.UrlMappingGetDetailReq{}
  73. s := service.UrlMapping{}
  74. // err := e.MakeContext(c).MakeOrm().Bind(&req).MakeService(&s.OtService).Errors
  75. err := e.MakeContext(c).
  76. MakeDB().
  77. Bind(&req).
  78. MakeService(&s.OtService).
  79. Errors
  80. if err != nil {
  81. e.Error(http.StatusInternalServerError, err, err.Error())
  82. return
  83. }
  84. result, count := []dto.UrlMappingGetDetailResp{}, int64(0)
  85. err = s.GetDetail(&req, &result, &count)
  86. if err != nil {
  87. e.Error(http.StatusInternalServerError, err, err.Error())
  88. return
  89. }
  90. e.PageOK(result, int(count), req.PageIndex, req.PageSize, "success")
  91. }
  92. // Insert 创建UrlMapping
  93. func (e UrlMapping) Insert(c *gin.Context) {
  94. req := dto.UrlMappingInsertReq{}
  95. s := service.UrlMapping{}
  96. err := e.MakeContext(c).
  97. MakeOrm().
  98. Bind(&req).
  99. MakeService(&s.OtService).
  100. Errors
  101. if err != nil {
  102. e.Logger.Error(err)
  103. e.Error(http.StatusInternalServerError, err, err.Error())
  104. return
  105. }
  106. // 设置创建人
  107. req.SetCreateBy(user.GetUserId(c))
  108. err = s.Insert(&req)
  109. if err != nil {
  110. e.Error(http.StatusInternalServerError, err, fmt.Sprintf("创建UrlMapping失败: %s", err.Error()))
  111. return
  112. }
  113. e.OK(req.GetId(), "创建成功")
  114. }
  115. // Update 修改UrlMapping
  116. func (e UrlMapping) Update(c *gin.Context) {
  117. req := dto.UrlMappingUpdateReq{}
  118. s := service.UrlMapping{}
  119. err := e.MakeContext(c).
  120. MakeOrm().
  121. Bind(&req).
  122. MakeService(&s.OtService).
  123. Errors
  124. if err != nil {
  125. e.Logger.Error(err)
  126. e.Error(http.StatusInternalServerError, err, err.Error())
  127. return
  128. }
  129. // req.SetUpdateBy(user.GetUserId(c))
  130. // p := actions.GetPermissionFromContext(c)
  131. // err = s.Update(&req, p)
  132. err = s.Update(&req)
  133. if err != nil {
  134. e.Error(500, err, fmt.Sprintf("修改UrlMapping失败,\r\n失败信息 %s", err.Error()))
  135. return
  136. }
  137. e.OK(req.GetId(), "修改成功")
  138. }
  139. func (e UrlMapping) Favor(c *gin.Context) {
  140. req := dto.UrlMappingFavorReq{}
  141. svc := service.UrlMapping{}
  142. if err := e.Init(c, &req, &svc.OtService); err != nil {
  143. return
  144. }
  145. favor := "收藏"
  146. if req.Favor == "0" {
  147. favor = "取消收藏"
  148. }
  149. if err := svc.Favor(&req); err != nil {
  150. e.Error(500, err, fmt.Sprintf("%s失败 %s", favor, err.Error()))
  151. return
  152. }
  153. e.OK(req.Id, fmt.Sprintf("已%s", favor))
  154. }
  155. // Delete 删除OtUrlMapping
  156. func (e UrlMapping) Delete(c *gin.Context) {
  157. svc := service.UrlMapping{}
  158. req := dto.UrlMappingDeleteReq{}
  159. if err := e.Init(c, &req, &svc.OtService); err != nil {
  160. return
  161. }
  162. if err := svc.Remove(&req); err != nil {
  163. e.Error(http.StatusInternalServerError, err, fmt.Sprintf("删除OtUrlMapping失败,\r\n失败信息 %s", err.Error()))
  164. return
  165. }
  166. e.OK(req.GetIds(), "删除成功")
  167. }
  168. // Sankey 业务映射 桑基图
  169. func (e UrlMapping) Sankey(c *gin.Context) {
  170. req := dto.UrlMappingSankeyReq{}
  171. s := service.UrlMapping{}
  172. if err := e.MakeContext(c).
  173. MakeDB().
  174. Bind(&req).
  175. MakeService(&s.OtService).
  176. Errors; err != nil {
  177. e.Logger.Error(err)
  178. e.Error(http.StatusInternalServerError, err, err.Error())
  179. return
  180. }
  181. if req.StartTime == 0 || req.EndTime == 0 {
  182. //不传值则默认显示1分钟内的sankey数据
  183. req.EndTime = time.Now().Unix()
  184. req.StartTime = req.EndTime - int64((1 * time.Minute).Seconds())
  185. }
  186. result := dto.UrlMappingSankeyResp{}
  187. if err := s.Sankey(&req, &result); err != nil {
  188. e.Error(http.StatusInternalServerError, err, fmt.Sprintf("获取桑基图数据失败: %s", err.Error()))
  189. return
  190. }
  191. e.OK(result, "success")
  192. }
  193. // Quality 质量接口
  194. func (e UrlMapping) Quality(c *gin.Context) {
  195. req := dto.UrlMappingQualityReq{}
  196. svc := service.UrlMapping{}
  197. if err := e.Init(c, &req, &svc.OtService); err != nil {
  198. return
  199. }
  200. resp := new(dto.UrlMappingQualityResp)
  201. if config.ExtConfig.ClickhouseMetrics {
  202. if err := svc.QualityFromClickhouse(req, resp); err != nil {
  203. return
  204. }
  205. } else {
  206. if err := svc.Quality(req, resp); err != nil {
  207. return
  208. }
  209. }
  210. e.OK(resp, "success")
  211. }
  212. func (e UrlMapping) Errors(c *gin.Context) {
  213. req := dto.UrlMappingErrorsReq{}
  214. svc := service.UrlMapping{}
  215. if err := e.Init(c, &req, &svc.OtService); err != nil {
  216. return
  217. }
  218. resp := new(dto.UrlMappingErrorsResp)
  219. if config.ExtConfig.ClickhouseMetrics {
  220. if err := svc.ErrorsFromClickhouse(req, resp); err != nil {
  221. return
  222. }
  223. } else {
  224. if err := svc.Errors(req, resp); err != nil {
  225. return
  226. }
  227. }
  228. e.OK(resp, "success")
  229. }
  230. // 最慢接口排行(平均值最慢)
  231. func (e UrlMapping) SlowTop(c *gin.Context) {
  232. req := dto.UrlMappingSlowTopReq{}
  233. svc := service.UrlMapping{}
  234. if err := e.Init(c, &req, &svc.OtService); err != nil {
  235. return
  236. }
  237. resp := make([]dto.UrlMappingSlowTopResp, 0)
  238. if err := svc.SlowTop(&req, &resp); err != nil {
  239. e.Error(http.StatusInternalServerError, err, err.Error())
  240. return
  241. }
  242. e.OK(resp, "success")
  243. }
  244. // 单个接口映射 数字视图
  245. func (e UrlMapping) SubDigits(c *gin.Context) {
  246. req := dto.UrlMappingSubDigitsReq{}
  247. svc := service.UrlMapping{}
  248. if err := e.Init(c, &req, &svc.OtService); err != nil {
  249. return
  250. }
  251. resp := dto.UrlMappingSubDigitsResp{}
  252. if err := svc.SubDigits(&req, &resp); err != nil {
  253. e.Error(http.StatusInternalServerError, err, err.Error())
  254. return
  255. }
  256. e.OK(resp, "success")
  257. }
  258. // 单个接口映射 列表
  259. func (e UrlMapping) SubList(c *gin.Context) {
  260. req := dto.UrlMappingSubListReq{}
  261. svc := service.UrlMapping{}
  262. if err := e.Init(c, &req, &svc.OtService); err != nil {
  263. return
  264. }
  265. resp := []dto.UrlMappingSubListResp{}
  266. count := int64(0)
  267. if err := svc.SubList(&req, &resp, &count); err != nil {
  268. e.Error(http.StatusInternalServerError, err, err.Error())
  269. return
  270. }
  271. e.PageOK(resp, int(count), req.GetPageIndex(), req.GetPageSize(), "success")
  272. }
  273. func (e UrlMapping) Chart(c *gin.Context) {
  274. req := dto.UrlMappingChartReq{}
  275. svc := service.UrlMapping{}
  276. if err := e.Init(c, &req, &svc.OtService); err != nil {
  277. return
  278. }
  279. resp := dto.UrlMappingChartResp{}
  280. if req.Type == "latency" {
  281. if config.ExtConfig.ClickhouseMetrics {
  282. if err := svc.ChartLatencyFromClickhouse(&req, &resp); err != nil {
  283. e.Error(http.StatusInternalServerError, err, err.Error())
  284. return
  285. }
  286. } else {
  287. if err := svc.ChartLatency(&req, &resp); err != nil {
  288. e.Error(http.StatusInternalServerError, err, err.Error())
  289. return
  290. }
  291. }
  292. } else if req.Type == "request_trend" || req.Type == "status_trend" || req.Type == "" {
  293. if config.ExtConfig.ClickhouseMetrics {
  294. if err := svc.ChartQPSFromClickhouse(&req, &resp); err != nil {
  295. e.Error(http.StatusInternalServerError, err, err.Error())
  296. return
  297. }
  298. } else {
  299. if err := svc.ChartQPS(&req, &resp); err != nil {
  300. e.Error(http.StatusInternalServerError, err, err.Error())
  301. return
  302. }
  303. }
  304. } else {
  305. err := errors.New("不支持的type参数")
  306. e.Error(http.StatusBadRequest, err, err.Error())
  307. return
  308. }
  309. e.OK(resp, "success")
  310. }
  311. func (e UrlMapping) BaseInfo(c *gin.Context) {
  312. req := dto.UrlMappingBaseInfoReq{}
  313. svc := service.UrlMapping{}
  314. if err := e.Init(c, &req, &svc.OtService); err != nil {
  315. return
  316. }
  317. resp := dto.UrlMappingBaseInfoResp{}
  318. if err := svc.BaseInfo(&req, &resp); err != nil {
  319. e.Error(http.StatusInternalServerError, err, err.Error())
  320. return
  321. }
  322. e.OK(resp, "success")
  323. }
  324. func (e UrlMapping) Match(c *gin.Context) {
  325. req := dto.UrlMappingMatchReq{}
  326. svc := service.UrlMapping{}
  327. if err := e.Init(c, &req, &svc.OtService); err != nil {
  328. return
  329. }
  330. total := int64(0)
  331. resp := []dto.UrlMappingMatchResp{}
  332. if err := svc.Match(&req, &resp, &total); err != nil {
  333. e.Error(http.StatusInternalServerError, err, err.Error())
  334. return
  335. }
  336. e.PageOK(resp, int(total), req.GetPageIndex(), req.GetPageSize(), "success")
  337. }