log.go 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. package dao
  2. import (
  3. "context"
  4. "encoding/json"
  5. "time"
  6. pb "go-common/app/service/main/coin/api"
  7. "go-common/app/service/main/coin/model"
  8. "go-common/library/database/elastic"
  9. "go-common/library/log"
  10. "go-common/library/queue/databus/report"
  11. )
  12. // AddLog .
  13. func (d *Dao) AddLog(mid, ts int64, from, to float64, reason, ip, operator string, oid int64, typ int) {
  14. report.User(&report.UserInfo{
  15. Mid: mid,
  16. Business: model.ReportType,
  17. IP: ip,
  18. Type: typ,
  19. Ctime: time.Unix(ts, 0),
  20. Index: []interface{}{operator},
  21. Oid: oid,
  22. Content: map[string]interface{}{
  23. "from": int64(from * _multi),
  24. "to": int64(to * _multi),
  25. "reason": reason,
  26. },
  27. })
  28. }
  29. // CoinLog .
  30. func (d *Dao) CoinLog(c context.Context, mid int64) (ls []*pb.ModelLog, err error) {
  31. t := time.Now()
  32. from := t.Add(-time.Hour * 24 * 7)
  33. var res struct {
  34. Page struct {
  35. Num int `json:"num"`
  36. Size int `json:"size"`
  37. Total int `json:"total"`
  38. } `json:"page"`
  39. Result []*report.UserActionLog `json:"result"`
  40. }
  41. err = d.es.NewRequest("log_user_action").
  42. IndexByTime("log_user_action_21", elastic.IndexTypeWeek, from, time.Now()).
  43. WhereEq("mid", mid).
  44. WhereRange("ctime", from.Format("2006-01-02 15:04:05"), "", elastic.RangeScopeLcRc).
  45. Pn(1).Ps(1000).Order("ctime", elastic.OrderDesc).
  46. Scan(c, &res)
  47. if err != nil {
  48. log.Error("coineslog mid: %v, err: %v", mid, err)
  49. PromError("log:es")
  50. return
  51. }
  52. for _, r := range res.Result {
  53. ts, _ := time.ParseInLocation("2006-01-02 15:04:05", r.Ctime, time.Local)
  54. var ex struct {
  55. From int64
  56. To int64
  57. Reason string
  58. }
  59. json.Unmarshal([]byte(r.Extra), &ex)
  60. ls = append(ls, &pb.ModelLog{
  61. From: float64(ex.From) / _multi,
  62. To: float64(ex.To) / _multi,
  63. IP: r.IP,
  64. Desc: ex.Reason,
  65. TimeStamp: ts.Unix(),
  66. })
  67. }
  68. return
  69. }