relation_log.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. package dao
  2. import (
  3. "context"
  4. "io"
  5. "strconv"
  6. "strings"
  7. "time"
  8. "go-common/app/admin/main/relation/model"
  9. "go-common/library/log"
  10. )
  11. // reverse returns its argument string reversed rune-wise left to right.
  12. func reverse(s string) string {
  13. r := []rune(s)
  14. for i, j := 0, len(r)-1; i < len(r)/2; i, j = i+1, j-1 {
  15. r[i], r[j] = r[j], r[i]
  16. }
  17. return string(r)
  18. }
  19. func rpad(s string, l int) string {
  20. dt := l - len(s)
  21. if dt <= 0 {
  22. return s
  23. }
  24. return s + strings.Repeat("0", dt)
  25. }
  26. func logKey(mid, fid, ts int64) string {
  27. midStr := rpad(reverse(strconv.FormatInt(mid, 10)), 10)
  28. fidStr := rpad(reverse(strconv.FormatInt(fid, 10)), 10)
  29. tsStr := strconv.FormatInt(ts, 10)
  30. return midStr + fidStr + tsStr
  31. }
  32. // RelationLogs is used to retriev relation log.
  33. func (d *Dao) RelationLogs(ctx context.Context, mid, fid int64, from time.Time, to time.Time) (model.RelationLogList, error) {
  34. scanner, err := d.hbase.ScanRangeStr(ctx, d.c.LogTable, logKey(mid, fid, from.Unix()), logKey(mid, fid, to.Unix()))
  35. if err != nil {
  36. log.Error("Failed to d.hbase.Scan(): %+v", err)
  37. return nil, err
  38. }
  39. logs := make(model.RelationLogList, 0)
  40. for {
  41. r, err := scanner.Next()
  42. if err != nil {
  43. if err != io.EOF {
  44. return nil, err
  45. }
  46. break
  47. }
  48. l := &model.RelationLog{
  49. Mid: mid,
  50. Fid: fid,
  51. }
  52. for _, c := range r.Cells {
  53. key := string(c.Row)
  54. qf := string(c.Qualifier)
  55. v := string(c.Value)
  56. log.Info("Retrieving relation log: mid(%d) fid(%d) key(%s) qualifier(%s) value(%s)", mid, fid, key, qf, v)
  57. // fill fields
  58. switch qf {
  59. case "att":
  60. l.Attention = model.ParseAction(v)
  61. case "bl":
  62. l.Black = model.ParseAction(v)
  63. case "wh":
  64. l.Whisper = model.ParseAction(v)
  65. case "src":
  66. l.Source = model.ParseSource(v)
  67. case "mt":
  68. l.MTime, _ = model.ParseLogTime(v)
  69. }
  70. }
  71. l.FillAttrField()
  72. logs = append(logs, l)
  73. }
  74. return logs, nil
  75. }