search2.go 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. package dao
  2. import (
  3. "context"
  4. "net/url"
  5. "strconv"
  6. "time"
  7. "go-common/app/admin/main/workflow/model/manager"
  8. "go-common/app/admin/main/workflow/model/search"
  9. "go-common/library/database/elastic"
  10. "go-common/library/ecode"
  11. "go-common/library/log"
  12. "go-common/library/xstr"
  13. )
  14. const (
  15. _unameURI = "http://manager.bilibili.co/x/admin/manager/users/unames"
  16. _srhAuditLogURI = "http://bili-search.bilibili.co/x/admin/search/log"
  17. )
  18. // SearchGroup will search group by given conditions
  19. func (d *Dao) SearchGroup(c context.Context, cond *search.GroupSearchCommonCond) (resp *search.GroupSearchCommonResp, err error) {
  20. start := time.Now()
  21. var r *elastic.Request
  22. defer func() {
  23. log.Info("SearchGroup params(%s) group search ts %s err_or(%v)", r.Params(), time.Since(start).String(), err)
  24. }()
  25. r = d.es.NewRequest(search.GroupSrhComID).Index(search.GroupSrhComID).Fields(cond.Fields...).
  26. WhereEq("business", cond.Business).WhereIn("round", cond.Rounds).WhereIn("tid", cond.Tids).
  27. WhereIn("state", cond.States).WhereIn("mid", cond.Mids).WhereIn("oid", cond.Oids).WhereIn("typeid", cond.TypeIDs).
  28. WhereIn("fid", cond.FID).WhereIn("rid", cond.RID).WhereIn("eid", cond.EID).
  29. WhereIn("report_mid", cond.ReportMID).WhereIn("first_user_tid", cond.FirstUserTid).
  30. Order(cond.Order, cond.Sort).
  31. Pn(int(cond.PN)).Ps(int(cond.PS))
  32. // 是否关键字匹配优先
  33. if cond.KWPriority == true {
  34. r.OrderScoreFirst(true)
  35. } else {
  36. r.OrderScoreFirst(false)
  37. }
  38. if len(cond.KWFields) > 0 && len(cond.KWFields) == len(cond.KW) {
  39. r.WhereLike(cond.KWFields, cond.KW, true, elastic.LikeLevelMiddle)
  40. }
  41. r.WhereRange("ctime", cond.CTimeFrom, cond.CTimeTo, elastic.RangeScopeLcRc)
  42. if err = r.Scan(c, &resp); err != nil {
  43. log.Error("r.Scan(%+v) error(%v) params(%s)", resp, err, r.Params())
  44. }
  45. return
  46. }
  47. // SearchGroupMultiPage .
  48. func (d *Dao) SearchGroupMultiPage(c context.Context, cond *search.GroupSearchCommonCond) (result []*search.GroupSearchCommonData, err error) {
  49. var resp *search.GroupSearchCommonResp
  50. cond.PS = 1000
  51. cond.PN = 1
  52. result = make([]*search.GroupSearchCommonData, 0, len(cond.IDs))
  53. for {
  54. if resp, err = d.SearchGroup(c, cond); err != nil {
  55. return
  56. }
  57. result = append(result, resp.Result...)
  58. if len(resp.Result) < resp.Page.Size {
  59. break
  60. }
  61. cond.PN++
  62. }
  63. return
  64. }
  65. // SearchChallenge will search challenge by given conditions
  66. func (d *Dao) SearchChallenge(c context.Context, cond *search.ChallSearchCommonCond) (resp *search.ChallSearchCommonResp, err error) {
  67. start := time.Now()
  68. var r *elastic.Request
  69. defer func() {
  70. log.Info("SearchChallenge params(%s) challenge search ts %s err_or(%v)", r.Params(), time.Since(start).String(), err)
  71. }()
  72. r = d.es.NewRequest(search.ChallSrhComID).Fields(cond.Fields...).
  73. WhereIn("id", cond.IDs).WhereIn("round", cond.Rounds).WhereIn("tid", cond.Tids).WhereIn("state", cond.States).
  74. WhereIn("business_state", cond.BusinessStates).WhereIn("mid", cond.Mids).WhereIn("oid", cond.Oids).WhereIn("typeid", cond.TypeIDs).
  75. WhereIn("gid", cond.Gids).WhereIn("assignee_adminid", cond.AssigneeAdminIDs).WhereIn("adminid", cond.AdminIDs)
  76. if cond.Business > 0 {
  77. r.WhereEq("business", cond.Business)
  78. }
  79. if len(cond.KWFields) > 0 && len(cond.KWFields) == len(cond.KW) {
  80. r.WhereLike(cond.KWFields, cond.KW, true, elastic.LikeLevelLow)
  81. }
  82. if cond.Order == "" {
  83. cond.Order = "id"
  84. }
  85. if cond.Sort == "" {
  86. cond.Sort = "desc"
  87. }
  88. r.Order(cond.Order, cond.Sort)
  89. r.WhereRange("ctime", cond.CTimeFrom, cond.CTimeTo, elastic.RangeScopeLcRc)
  90. if len(cond.Distinct) > 0 {
  91. for _, g := range cond.Distinct {
  92. r.GroupBy("distinct", g, nil)
  93. }
  94. }
  95. r.Index(search.ChallSrhComID)
  96. r.Pn(1)
  97. r.Ps(50)
  98. if cond.PN != 0 {
  99. r.Pn(int(cond.PN))
  100. }
  101. if cond.PS != 0 {
  102. r.Ps(int(cond.PS))
  103. }
  104. if err = r.Scan(c, &resp); err != nil {
  105. log.Error("r.Scan(%+v) error(%v) params(%s)", resp, err, r.Params())
  106. }
  107. return
  108. }
  109. // SearchChallengeMultiPage .
  110. func (d *Dao) SearchChallengeMultiPage(c context.Context, cond *search.ChallSearchCommonCond) (result []*search.ChallSearchCommonData, err error) {
  111. var resp *search.ChallSearchCommonResp
  112. cond.PS = 1000
  113. cond.PN = 1
  114. result = make([]*search.ChallSearchCommonData, 0, len(cond.IDs))
  115. for {
  116. if resp, err = d.SearchChallenge(c, cond); err != nil {
  117. return
  118. }
  119. result = append(result, resp.Result...)
  120. if len(resp.Result) < resp.Page.Size {
  121. break
  122. }
  123. cond.PN++
  124. // return if result too long
  125. if cond.PN > 10 {
  126. log.Warn("cond(%+v) result is too long to degrade", cond)
  127. return
  128. }
  129. }
  130. return
  131. }
  132. // BatchUNameByUID will search unames by uids
  133. func (d *Dao) BatchUNameByUID(c context.Context, uids []int64) (UNames map[int64]string, err error) {
  134. //todo: local cache uname
  135. uri := _unameURI
  136. uv := url.Values{}
  137. UNames = make(map[int64]string)
  138. if len(uids) == 0 {
  139. return
  140. }
  141. uv.Set("uids", xstr.JoinInts(uids))
  142. unameSchRes := new(manager.UNameSearchResult)
  143. if err = d.httpRead.Get(c, uri, "", uv, unameSchRes); err != nil {
  144. return
  145. }
  146. if unameSchRes.Code != ecode.OK.Code() {
  147. log.Error("search uname failed: %s?%s, error code(%d)", uri, uv.Get("uids"), unameSchRes.Code)
  148. err = ecode.Int(unameSchRes.Code)
  149. return
  150. }
  151. UNames = unameSchRes.Data
  152. return
  153. }
  154. // SearchAuditLogGroup search archive audit log from log platform
  155. func (d *Dao) SearchAuditLogGroup(c context.Context, cond *search.AuditLogGroupSearchCond) (auditLogSchRes *search.AuditLogSearchResult, err error) {
  156. uri := _srhAuditLogURI
  157. uv := cond.Query()
  158. auditLogSchRes = new(search.AuditLogSearchResult)
  159. if err = d.httpRead.Get(c, uri, "", uv, auditLogSchRes); err != nil {
  160. log.Error("call search audit log %s error(%v)", uri, err)
  161. return
  162. }
  163. if auditLogSchRes.Code != ecode.OK.Code() {
  164. log.Error("call search audit log %s result error code(%d), message(%s)", uri, auditLogSchRes.Code, auditLogSchRes.Message)
  165. err = ecode.Int(auditLogSchRes.Code)
  166. }
  167. return
  168. }
  169. // SearchAuditReportLog .
  170. func (d *Dao) SearchAuditReportLog(c context.Context, cond *search.AuditReportSearchCond) (resp *search.AuditLogSearchCommonResult, err error) {
  171. if len(cond.Fields) == 0 {
  172. return
  173. }
  174. r := d.es.NewRequest(search.LogAuditAction).Fields(cond.Fields...).WhereIn("uid", cond.UID).WhereIn("oid", cond.Oid).
  175. WhereEq("business", cond.Business).WhereIn("type", cond.Type).Order(cond.Order, cond.Sort).Pn(1).Ps(1000)
  176. indexPrefix := search.LogAuditAction + "_" + strconv.Itoa(cond.Business)
  177. if cond.IndexTimeType != "" {
  178. switch cond.IndexTimeType {
  179. case "year":
  180. r.IndexByTime(indexPrefix, elastic.IndexTypeYear, cond.IndexTimeFrom, cond.IndexTimeEnd)
  181. case "month":
  182. r.IndexByTime(indexPrefix, elastic.IndexTypeMonth, cond.IndexTimeFrom, cond.IndexTimeEnd)
  183. case "week":
  184. r.IndexByTime(indexPrefix, elastic.IndexTypeWeek, cond.IndexTimeFrom, cond.IndexTimeEnd)
  185. case "day":
  186. r.IndexByTime(indexPrefix, elastic.IndexTypeDay, cond.IndexTimeFrom, cond.IndexTimeEnd)
  187. default:
  188. r.Index(indexPrefix + "_all")
  189. }
  190. } else {
  191. r.Index(indexPrefix + "_all")
  192. }
  193. r.WhereIn("int_0", cond.Int0).WhereIn("int_1", cond.Int1).WhereIn("int_2", cond.Int2)
  194. if cond.Str0 != "" {
  195. r.WhereEq("str_0", cond.Str0)
  196. }
  197. if cond.Str1 != "" {
  198. r.WhereEq("str_1", cond.Str1)
  199. }
  200. if cond.Str2 != "" {
  201. r.WhereEq("str_2", cond.Str2)
  202. }
  203. if cond.Group != "" {
  204. r.GroupBy(elastic.EnhancedModeGroupBy, cond.Group, []map[string]string{{"ctime": "desc"}})
  205. }
  206. if cond.Distinct != "" {
  207. r.GroupBy(elastic.EnhancedModeDistinct, cond.Distinct, []map[string]string{{"ctime": "desc"}})
  208. }
  209. if err = r.Scan(c, &resp); err != nil {
  210. log.Error("r.Scan(%+v) error(%v) params(%s)", resp, err, r.Params())
  211. }
  212. log.Info("SearchAuditReportLog end param(%v) err(%v)", r.Params(), err)
  213. return
  214. }