rank.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. package service
  2. import (
  3. "net/url"
  4. "sort"
  5. "time"
  6. "go-common/app/admin/ep/melloi/model"
  7. "go-common/library/log"
  8. )
  9. //TreesQuery Get service tree
  10. func (s *Service) TreesQuery() (*model.TreeList, error) {
  11. return s.dao.TreesQuery()
  12. }
  13. //TreeNumQuery Get service tree num
  14. func (s *Service) TreeNumQuery() (*model.NumList, error) {
  15. return s.dao.TreeNumQuery()
  16. }
  17. //TopHttpQuery Get top 10 url
  18. func (s *Service) TopHttpQuery() (res *model.TopAPIRes, err error) {
  19. if res, err = s.dao.TopHttpQuery(); err != nil {
  20. log.Error("service.rank error:(%v)", err)
  21. return
  22. }
  23. for _, api := range res.APIList {
  24. u, errURL := url.Parse(api.URL)
  25. if errURL != nil {
  26. log.Error("service.rank error:(%v)", errURL)
  27. return
  28. }
  29. api.URL = u.Host + u.Path
  30. }
  31. return
  32. }
  33. //TopGrpcQuery Top Grpc Query
  34. func (s *Service) TopGrpcQuery() (*model.GrpcRes, error) {
  35. return s.dao.TopGrpcQuery()
  36. }
  37. //TopSceneQuery Top Scene Query
  38. func (s *Service) TopSceneQuery() (*model.SceneRes, error) {
  39. return s.dao.TopSceneQuery()
  40. }
  41. //TopDeptQuery Get top 10 department
  42. func (s *Service) TopDeptQuery() (*model.TopDeptRes, error) {
  43. return s.dao.TopDeptQuery()
  44. }
  45. //BuildLineQuery Get test line
  46. func (s *Service) BuildLineQuery(rank *model.Rank, summary *model.ReportSummary) (res *model.BuildLineRes, err error) {
  47. var timePart time.Duration
  48. //根据传入时间
  49. timeLayout := "2006-01-02 15:04:05"
  50. if rank.StartTime == "" && rank.EndTime == "" {
  51. rank.StartTime = time.Now().Add(time.Hour * -24).Format(timeLayout)
  52. rank.EndTime = time.Now().Format(timeLayout)
  53. } else if rank.StartTime == "" {
  54. loc, _ := time.LoadLocation("Local") //重要:获取时区
  55. theTime, _ := time.ParseInLocation(timeLayout, rank.EndTime, loc) //使用模板在对应时区转化为time.time类型
  56. if timePart, err = time.ParseDuration("-24h"); err != nil {
  57. log.Error("service.rank error:(%v)", err)
  58. return
  59. }
  60. rank.StartTime = theTime.Add(timePart).Format(timeLayout)
  61. } else if rank.EndTime == "" {
  62. rank.EndTime = time.Now().Format(timeLayout)
  63. }
  64. if res, err = s.dao.BuildLineQuery(rank, summary); err != nil {
  65. log.Error("service.rank error:(%v)", err)
  66. return
  67. }
  68. var myDateMap = make(map[string]int)
  69. for _, bu := range res.BuildList {
  70. if _, ok := myDateMap[bu.Date]; ok {
  71. myDateMap[bu.Date]++
  72. } else {
  73. myDateMap[bu.Date] = 1
  74. }
  75. }
  76. sortedKeys := make([]string, 0)
  77. for k := range myDateMap {
  78. sortedKeys = append(sortedKeys, k)
  79. }
  80. sort.Strings(sortedKeys)
  81. var mySortedMap = make(map[string]int)
  82. res.BuildList = []*model.Build{}
  83. for _, k := range sortedKeys {
  84. mySortedMap[k] = myDateMap[k]
  85. bu := model.Build{Date: k, Count: myDateMap[k]}
  86. res.BuildList = append(res.BuildList, &bu)
  87. }
  88. return
  89. }
  90. //StateLineQuery Get test state line
  91. func (s *Service) StateLineQuery() (*model.StateLineRes, error) {
  92. return s.dao.StateLineQuery()
  93. }