query.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. package service
  2. import (
  3. "context"
  4. "fmt"
  5. "strconv"
  6. "strings"
  7. "time"
  8. "go-common/app/admin/main/search/dao"
  9. "go-common/app/admin/main/search/model"
  10. "go-common/library/log"
  11. )
  12. func (s *Service) loadQueryConfproc() {
  13. for {
  14. if err := s.loadQueryConf(); err != nil {
  15. time.Sleep(time.Second)
  16. continue
  17. }
  18. time.Sleep(time.Minute)
  19. }
  20. }
  21. func (s *Service) loadQueryConf() (err error) {
  22. confs, err := s.dao.QueryConf(context.Background())
  23. if err != nil {
  24. return
  25. }
  26. if len(confs) > 0 {
  27. s.queryConf = confs
  28. }
  29. return
  30. }
  31. // CheckQueryConf check query conf
  32. func (s *Service) CheckQueryConf(c context.Context, sp *model.QueryParams) (err error) {
  33. app, ok := s.queryConf[sp.Business]
  34. if app2, ok2 := model.QueryConf[sp.Business]; ok2 {
  35. app = app2
  36. ok = true
  37. }
  38. if !ok {
  39. err = fmt.Errorf("sp.Business(%s) not exist in queryConf", sp.Business)
  40. return
  41. }
  42. if app.ESCluster == "" {
  43. err = fmt.Errorf("app(%+v) escluster is empty", app)
  44. return
  45. }
  46. max := 1
  47. if app.MaxIndicesNum > 0 {
  48. max = app.MaxIndicesNum
  49. }
  50. indecies := strings.Split(sp.QueryBody.From, ",")
  51. if len(indecies) == 0 {
  52. err = fmt.Errorf("index name is required")
  53. return
  54. }
  55. if len(indecies) > max {
  56. err = fmt.Errorf("too many indecies(%v)", indecies)
  57. return
  58. }
  59. for _, index := range indecies {
  60. if !strings.Contains(index, app.IndexPrefix) {
  61. err = fmt.Errorf("invalid index name(%s)", index)
  62. return
  63. }
  64. }
  65. sp.AppIDConf = app
  66. return
  67. }
  68. // QueryBasic .
  69. func (s *Service) QueryBasic(c context.Context, sp *model.QueryParams) (res *model.QueryResult, debug *model.QueryDebugResult, err error) {
  70. switch sp.Business {
  71. case "log_audit":
  72. t := strings.Split(sp.QueryBody.From, "_")
  73. if len(t) > 2 {
  74. logID, err := strconv.Atoi(t[2])
  75. if err != nil {
  76. log.Error("strconv.Atoi(%s) error(%v)", t[2], err)
  77. }
  78. logBusiness, ok := s.dao.GetLogInfo(sp.Business, logID)
  79. if ok {
  80. sp.AppIDConf.ESCluster = logBusiness.IndexCluster
  81. }
  82. }
  83. case "log_user_action":
  84. t := strings.Split(sp.QueryBody.From, "_")
  85. if len(t) > 3 {
  86. logID, err := strconv.Atoi(t[3])
  87. if err != nil {
  88. log.Error("strconv.Atoi(%s) error(%v)", t[3], err)
  89. }
  90. logBusiness, ok := s.dao.GetLogInfo(sp.Business, logID)
  91. if ok {
  92. sp.AppIDConf.ESCluster = logBusiness.IndexCluster
  93. }
  94. }
  95. }
  96. bQuery, qbDebug := s.dao.QueryBasic(c, sp)
  97. if res, debug, err = s.dao.QueryResult(c, bQuery, sp, qbDebug); err != nil {
  98. dao.PromError(fmt.Sprintf("es:%s 搜索失败", sp.Business), "s.dao.QueryBasic(%v) error(%v)", sp, err)
  99. }
  100. return
  101. }
  102. // QueryExtra .
  103. func (s *Service) QueryExtra(c context.Context, sp *model.QueryParams) (res *model.QueryResult, debug *model.QueryDebugResult, err error) {
  104. switch sp.Business {
  105. case "archive_video_score":
  106. if res, debug, err = s.dao.ArchiveVideoScore(c, sp); err != nil {
  107. dao.PromError(fmt.Sprintf("es:%s 搜索失败", sp.Business), "s.dao.QueryExtra(%v) error(%v)", sp, err)
  108. }
  109. case "archive_score":
  110. if res, debug, err = s.dao.ArchiveScore(c, sp); err != nil {
  111. dao.PromError(fmt.Sprintf("es:%s 搜索失败", sp.Business), "s.dao.QueryExtra(%v) error(%v)", sp, err)
  112. }
  113. case "task_qa_random":
  114. if res, debug, err = s.dao.TaskQaRandom(c, sp); err != nil {
  115. dao.PromError(fmt.Sprintf("es:%s 搜索失败", sp.Business), "s.dao.QueryExtra.TaskQaRandom(%v) error(%v)", sp, err)
  116. }
  117. case "esports_contests_date":
  118. if res, debug, err = s.dao.EsportsContestsDate(c, sp); err != nil {
  119. dao.PromError(fmt.Sprintf("es:%s 搜索失败", sp.Business), "s.dao.QueryExtra.EsportsContestsDate(%v) error(%v)", sp, err)
  120. }
  121. case "creative_archive_search":
  122. if res, debug, err = s.dao.CreativeArchiveSearch(c, sp); err != nil {
  123. dao.PromError(fmt.Sprintf("es:%s 搜索失败", sp.Business), "s.dao.QueryExtra.CreativeArchiveSearch(%v) error(%v)", sp, err)
  124. }
  125. case "creative_archive_staff":
  126. if res, debug, err = s.dao.CreativeArchiveStaff(c, sp); err != nil {
  127. dao.PromError(fmt.Sprintf("es:%s 搜索失败", sp.Business), "s.dao.QueryExtra.CreativeArchiveStaff(%v) error(%v)", sp, err)
  128. }
  129. case "creative_archive_apply":
  130. if res, debug, err = s.dao.CreativeArchiveApply(c, sp); err != nil {
  131. dao.PromError(fmt.Sprintf("es:%s 搜索失败", sp.Business), "s.dao.QueryExtra.CreativeArchiveApply(%v) error(%v)", sp, err)
  132. }
  133. case "dm_history":
  134. if res, debug, err = s.dao.Scroll(c, sp); err != nil {
  135. dao.PromError(fmt.Sprintf("es:%s 搜索失败", sp.Business), "s.dao.QueryExtra.Scroll(%v) error(%v)", sp, err)
  136. }
  137. }
  138. return
  139. }