es.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. package dao
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. "sort"
  7. "time"
  8. "go-common/app/admin/main/answer/model"
  9. "go-common/library/log"
  10. xtime "go-common/library/time"
  11. )
  12. // Page search page info
  13. type Page struct {
  14. Num int64 `json:"num"`
  15. Size int64 `json:"size"`
  16. Total int64 `json:"total"`
  17. }
  18. // SearchSubjectLog get subject logs
  19. type SearchSubjectLog struct {
  20. Page *Page
  21. Result []*struct {
  22. MID int64 `json:"mid"`
  23. Business string `json:"business"`
  24. Action string `json:"action"`
  25. ExtraData string `json:"extra_data"`
  26. Ctime string `json:"ctime"`
  27. }
  28. }
  29. // const .
  30. const (
  31. answerLogID = 15
  32. answerUpdate = "answer_update"
  33. basePass = "basePass"
  34. extraStartTime = "extraStartTime"
  35. extraCheck = "extraCheck"
  36. proQues = "proQues"
  37. proCheck = "proCheck"
  38. captcha = "captchaPass"
  39. level = "level"
  40. )
  41. // HistoryES search archives by es.
  42. func (d *Dao) HistoryES(c context.Context, arg *model.ArgHistory) (reply []*model.AnswerHistoryDB, err error) {
  43. var index []string
  44. for year := time.Now().Year(); year >= 2018; year-- { // 2018.12 开始接入日志
  45. index = append(index, fmt.Sprintf("log_user_action_%d_%d", answerLogID, year))
  46. }
  47. r := d.es.NewRequest("log_user_action").Index(index...)
  48. r.Fields("mid", "action", "ctime", "extra_data")
  49. r.WhereEq("mid", arg.Mid)
  50. r.Pn(arg.Pn).Ps(arg.Ps)
  51. res := &SearchSubjectLog{}
  52. if err = r.Scan(c, &res); err != nil || res == nil {
  53. log.Error("HistoryES:Scan params(%s) error(%v)", r.Params(), err)
  54. return
  55. }
  56. log.Error("HistoryES:%+v", res)
  57. mmh := make(map[int64]map[string]*model.AnswerHistory)
  58. for _, v := range res.Result {
  59. mh := make(map[string]*model.AnswerHistory)
  60. if err = json.Unmarshal([]byte(v.ExtraData), &mh); err != nil {
  61. log.Error("json.Unmarshal(%s) error(%v)", v.ExtraData, err)
  62. return
  63. }
  64. for k, v := range mh {
  65. if mmh[v.Hid] == nil {
  66. mmh[v.Hid] = make(map[string]*model.AnswerHistory)
  67. }
  68. mmh[v.Hid][k] = v
  69. }
  70. }
  71. data := make([]*model.AnswerHistory, 0)
  72. for _, v := range mmh {
  73. if v == nil {
  74. continue
  75. }
  76. h := new(model.AnswerHistory)
  77. if eh, ok := v[basePass]; ok && eh != nil {
  78. h = eh
  79. }
  80. if eh, ok := v[extraStartTime]; ok && eh != nil {
  81. h.StepExtraStartTime = eh.StepExtraStartTime
  82. h.Mtime = eh.Mtime
  83. }
  84. if eh, ok := v[extraCheck]; ok && eh != nil {
  85. h.StepExtraCompleteTime = eh.StepExtraCompleteTime
  86. h.Score = eh.Score
  87. h.StepExtraScore = eh.StepExtraScore
  88. h.Mtime = eh.Mtime
  89. }
  90. if eh, ok := v[proQues]; ok && eh != nil {
  91. h.StepTwoStartTime = eh.StepTwoStartTime
  92. h.Mtime = eh.Mtime
  93. }
  94. if eh, ok := v[proCheck]; ok && eh != nil {
  95. h.CompleteResult = eh.CompleteResult
  96. h.CompleteTime = eh.CompleteTime
  97. h.Score = eh.Score
  98. h.IsFirstPass = eh.IsFirstPass
  99. h.RankID = eh.RankID
  100. h.Mtime = eh.Mtime
  101. }
  102. if eh, ok := v[captcha]; ok && eh != nil {
  103. h.IsPassCaptcha = eh.IsPassCaptcha
  104. h.Mtime = eh.Mtime
  105. }
  106. if eh, ok := v[level]; ok && eh != nil {
  107. h.IsFirstPass = eh.IsFirstPass
  108. h.PassedLevel = eh.PassedLevel
  109. h.Mtime = eh.Mtime
  110. }
  111. data = append(data, h)
  112. }
  113. sort.Sort(model.Histories(data))
  114. return convent(data), nil
  115. }
  116. func convent(src []*model.AnswerHistory) []*model.AnswerHistoryDB {
  117. res := make([]*model.AnswerHistoryDB, 0, len(src))
  118. for _, s := range src {
  119. r := &model.AnswerHistoryDB{
  120. ID: s.ID,
  121. Hid: s.Hid,
  122. Mid: s.Mid,
  123. StartTime: xtime.Time(s.StartTime.Unix()),
  124. StepOneErrTimes: s.StepOneErrTimes,
  125. StepOneCompleteTime: s.StepOneCompleteTime,
  126. StepExtraStartTime: xtime.Time(s.StepExtraStartTime.Unix()),
  127. StepExtraCompleteTime: s.StepExtraCompleteTime,
  128. StepExtraScore: s.StepExtraScore,
  129. StepTwoStartTime: xtime.Time(s.StepTwoStartTime.Unix()),
  130. CompleteTime: xtime.Time(s.CompleteTime.Unix()),
  131. CompleteResult: s.CompleteResult,
  132. Score: s.Score,
  133. IsFirstPass: s.IsFirstPass,
  134. IsPassCaptcha: s.IsPassCaptcha,
  135. PassedLevel: s.PassedLevel,
  136. RankID: s.RankID,
  137. Ctime: xtime.Time(s.Ctime.Unix()),
  138. Mtime: xtime.Time(s.Mtime.Unix()),
  139. }
  140. res = append(res, r)
  141. }
  142. return res
  143. }