123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370 |
- package apis
- import (
- "errors"
- "fmt"
- "go-admin/app/observe/service"
- "go-admin/app/observe/service/dto"
- "go-admin/config"
- "go-admin/utils"
- "net/http"
- "time"
- "github.com/gin-gonic/gin"
- "github.com/go-admin-team/go-admin-core/sdk/pkg/jwtauth/user"
- )
- type UrlMapping struct {
- utils.OtApi
- }
- // GetPage 获取OtUrlMapping列表
- func (e UrlMapping) GetPage(c *gin.Context) {
- req := dto.UrlMappingGetPageReq{}
- svc := service.UrlMapping{}
- if err := e.Init(c, &req, &svc.OtService); err != nil {
- return
- }
- list := make([]dto.UrlMappingListResp, 0)
- var count int64
- if err := svc.GetPage(&req, &list, &count); err != nil {
- e.Error(http.StatusInternalServerError, err, err.Error())
- return
- }
- e.PageOK(list, int(count), req.GetPageIndex(), req.GetPageSize(), "查询成功")
- }
- func (e UrlMapping) Favors(c *gin.Context) {
- req := dto.UrlMappingGetFavorsReq{}
- svc := service.UrlMapping{}
- if err := e.Init(c, &req, &svc.OtService); err != nil {
- return
- }
- if req.StartTime == 0 || req.EndTime == 0 {
- req.EndTime = time.Now().Unix()
- req.StartTime = req.EndTime - int64((60 * time.Minute).Seconds())
- }
- resp := make([]dto.UrlMappingListResp, 0)
- if err := svc.GetFavors(&req, &resp); err != nil {
- e.Error(http.StatusInternalServerError, err, err.Error())
- return
- }
- e.OK(resp, "success")
- }
- // Get 获取UrlMapping
- func (e UrlMapping) Get(c *gin.Context) {
- req := dto.UrlMappingGetReq{}
- s := service.UrlMapping{}
- err := e.MakeContext(c).
- MakeOrm().
- Bind(&req).
- MakeService(&s.OtService).
- Errors
- if err != nil {
- e.Logger.Error(err)
- e.Error(http.StatusInternalServerError, err, err.Error())
- return
- }
- result := dto.UrlMappingGetResp{}
- err = s.Get(&req, &result)
- if err != nil {
- e.Error(http.StatusInternalServerError, err, fmt.Sprintf("获取UrlMapping失败: %s", err.Error()))
- return
- }
- e.OK(result, "查询成功")
- }
- func (e UrlMapping) GetDetail(c *gin.Context) {
- req := dto.UrlMappingGetDetailReq{}
- s := service.UrlMapping{}
- // err := e.MakeContext(c).MakeOrm().Bind(&req).MakeService(&s.OtService).Errors
- err := e.MakeContext(c).
- MakeDB().
- Bind(&req).
- MakeService(&s.OtService).
- Errors
- if err != nil {
- e.Error(http.StatusInternalServerError, err, err.Error())
- return
- }
- result, count := []dto.UrlMappingGetDetailResp{}, int64(0)
- err = s.GetDetail(&req, &result, &count)
- if err != nil {
- e.Error(http.StatusInternalServerError, err, err.Error())
- return
- }
- e.PageOK(result, int(count), req.PageIndex, req.PageSize, "success")
- }
- // Insert 创建UrlMapping
- func (e UrlMapping) Insert(c *gin.Context) {
- req := dto.UrlMappingInsertReq{}
- s := service.UrlMapping{}
- err := e.MakeContext(c).
- MakeOrm().
- Bind(&req).
- MakeService(&s.OtService).
- Errors
- if err != nil {
- e.Logger.Error(err)
- e.Error(http.StatusInternalServerError, err, err.Error())
- return
- }
- // 设置创建人
- req.SetCreateBy(user.GetUserId(c))
- err = s.Insert(&req)
- if err != nil {
- e.Error(http.StatusInternalServerError, err, fmt.Sprintf("创建UrlMapping失败: %s", err.Error()))
- return
- }
- e.OK(req.GetId(), "创建成功")
- }
- // Update 修改UrlMapping
- func (e UrlMapping) Update(c *gin.Context) {
- req := dto.UrlMappingUpdateReq{}
- s := service.UrlMapping{}
- err := e.MakeContext(c).
- MakeOrm().
- Bind(&req).
- MakeService(&s.OtService).
- Errors
- if err != nil {
- e.Logger.Error(err)
- e.Error(http.StatusInternalServerError, err, err.Error())
- return
- }
- // req.SetUpdateBy(user.GetUserId(c))
- // p := actions.GetPermissionFromContext(c)
- // err = s.Update(&req, p)
- err = s.Update(&req)
- if err != nil {
- e.Error(500, err, fmt.Sprintf("修改UrlMapping失败,\r\n失败信息 %s", err.Error()))
- return
- }
- e.OK(req.GetId(), "修改成功")
- }
- func (e UrlMapping) Favor(c *gin.Context) {
- req := dto.UrlMappingFavorReq{}
- svc := service.UrlMapping{}
- if err := e.Init(c, &req, &svc.OtService); err != nil {
- return
- }
- favor := "收藏"
- if req.Favor == "0" {
- favor = "取消收藏"
- }
- if err := svc.Favor(&req); err != nil {
- e.Error(500, err, fmt.Sprintf("%s失败 %s", favor, err.Error()))
- return
- }
- e.OK(req.Id, fmt.Sprintf("已%s", favor))
- }
- // Delete 删除OtUrlMapping
- func (e UrlMapping) Delete(c *gin.Context) {
- svc := service.UrlMapping{}
- req := dto.UrlMappingDeleteReq{}
- if err := e.Init(c, &req, &svc.OtService); err != nil {
- return
- }
- if err := svc.Remove(&req); err != nil {
- e.Error(http.StatusInternalServerError, err, fmt.Sprintf("删除OtUrlMapping失败,\r\n失败信息 %s", err.Error()))
- return
- }
- e.OK(req.GetIds(), "删除成功")
- }
- // Sankey 业务映射 桑基图
- func (e UrlMapping) Sankey(c *gin.Context) {
- req := dto.UrlMappingSankeyReq{}
- s := service.UrlMapping{}
- if err := e.MakeContext(c).
- MakeDB().
- Bind(&req).
- MakeService(&s.OtService).
- Errors; err != nil {
- e.Logger.Error(err)
- e.Error(http.StatusInternalServerError, err, err.Error())
- return
- }
- if req.StartTime == 0 || req.EndTime == 0 {
- //不传值则默认显示1分钟内的sankey数据
- req.EndTime = time.Now().Unix()
- req.StartTime = req.EndTime - int64((1 * time.Minute).Seconds())
- }
- result := dto.UrlMappingSankeyResp{}
- if err := s.Sankey(&req, &result); err != nil {
- e.Error(http.StatusInternalServerError, err, fmt.Sprintf("获取桑基图数据失败: %s", err.Error()))
- return
- }
- e.OK(result, "success")
- }
- // Quality 质量接口
- func (e UrlMapping) Quality(c *gin.Context) {
- req := dto.UrlMappingQualityReq{}
- svc := service.UrlMapping{}
- if err := e.Init(c, &req, &svc.OtService); err != nil {
- return
- }
- resp := new(dto.UrlMappingQualityResp)
- if config.ExtConfig.ClickhouseMetrics {
- if err := svc.QualityFromClickhouse(req, resp); err != nil {
- return
- }
- } else {
- if err := svc.Quality(req, resp); err != nil {
- return
- }
- }
- e.OK(resp, "success")
- }
- func (e UrlMapping) Errors(c *gin.Context) {
- req := dto.UrlMappingErrorsReq{}
- svc := service.UrlMapping{}
- if err := e.Init(c, &req, &svc.OtService); err != nil {
- return
- }
- resp := new(dto.UrlMappingErrorsResp)
- if config.ExtConfig.ClickhouseMetrics {
- if err := svc.ErrorsFromClickhouse(req, resp); err != nil {
- return
- }
- } else {
- if err := svc.Errors(req, resp); err != nil {
- return
- }
- }
- e.OK(resp, "success")
- }
- // 最慢接口排行(平均值最慢)
- func (e UrlMapping) SlowTop(c *gin.Context) {
- req := dto.UrlMappingSlowTopReq{}
- svc := service.UrlMapping{}
- if err := e.Init(c, &req, &svc.OtService); err != nil {
- return
- }
- resp := make([]dto.UrlMappingSlowTopResp, 0)
- if err := svc.SlowTop(&req, &resp); err != nil {
- e.Error(http.StatusInternalServerError, err, err.Error())
- return
- }
- e.OK(resp, "success")
- }
- // 单个接口映射 数字视图
- func (e UrlMapping) SubDigits(c *gin.Context) {
- req := dto.UrlMappingSubDigitsReq{}
- svc := service.UrlMapping{}
- if err := e.Init(c, &req, &svc.OtService); err != nil {
- return
- }
- resp := dto.UrlMappingSubDigitsResp{}
- if err := svc.SubDigits(&req, &resp); err != nil {
- e.Error(http.StatusInternalServerError, err, err.Error())
- return
- }
- e.OK(resp, "success")
- }
- // 单个接口映射 列表
- func (e UrlMapping) SubList(c *gin.Context) {
- req := dto.UrlMappingSubListReq{}
- svc := service.UrlMapping{}
- if err := e.Init(c, &req, &svc.OtService); err != nil {
- return
- }
- resp := []dto.UrlMappingSubListResp{}
- count := int64(0)
- if err := svc.SubList(&req, &resp, &count); err != nil {
- e.Error(http.StatusInternalServerError, err, err.Error())
- return
- }
- e.PageOK(resp, int(count), req.GetPageIndex(), req.GetPageSize(), "success")
- }
- func (e UrlMapping) Chart(c *gin.Context) {
- req := dto.UrlMappingChartReq{}
- svc := service.UrlMapping{}
- if err := e.Init(c, &req, &svc.OtService); err != nil {
- return
- }
- resp := dto.UrlMappingChartResp{}
- if req.Type == "latency" {
- if config.ExtConfig.ClickhouseMetrics {
- if err := svc.ChartLatencyFromClickhouse(&req, &resp); err != nil {
- e.Error(http.StatusInternalServerError, err, err.Error())
- return
- }
- } else {
- if err := svc.ChartLatency(&req, &resp); err != nil {
- e.Error(http.StatusInternalServerError, err, err.Error())
- return
- }
- }
- } else if req.Type == "request_trend" || req.Type == "status_trend" || req.Type == "" {
- if config.ExtConfig.ClickhouseMetrics {
- if err := svc.ChartQPSFromClickhouse(&req, &resp); err != nil {
- e.Error(http.StatusInternalServerError, err, err.Error())
- return
- }
- } else {
- if err := svc.ChartQPS(&req, &resp); err != nil {
- e.Error(http.StatusInternalServerError, err, err.Error())
- return
- }
- }
- } else {
- err := errors.New("不支持的type参数")
- e.Error(http.StatusBadRequest, err, err.Error())
- return
- }
- e.OK(resp, "success")
- }
- func (e UrlMapping) BaseInfo(c *gin.Context) {
- req := dto.UrlMappingBaseInfoReq{}
- svc := service.UrlMapping{}
- if err := e.Init(c, &req, &svc.OtService); err != nil {
- return
- }
- resp := dto.UrlMappingBaseInfoResp{}
- if err := svc.BaseInfo(&req, &resp); err != nil {
- e.Error(http.StatusInternalServerError, err, err.Error())
- return
- }
- e.OK(resp, "success")
- }
- func (e UrlMapping) Match(c *gin.Context) {
- req := dto.UrlMappingMatchReq{}
- svc := service.UrlMapping{}
- if err := e.Init(c, &req, &svc.OtService); err != nil {
- return
- }
- total := int64(0)
- resp := []dto.UrlMappingMatchResp{}
- if err := svc.Match(&req, &resp, &total); err != nil {
- e.Error(http.StatusInternalServerError, err, err.Error())
- return
- }
- e.PageOK(resp, int(total), req.GetPageIndex(), req.GetPageSize(), "success")
- }
|