hbase.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. package dao
  2. import (
  3. "bytes"
  4. "context"
  5. "crypto/md5"
  6. "encoding/hex"
  7. "strconv"
  8. "time"
  9. "go-common/app/interface/main/space/model"
  10. "go-common/library/log"
  11. "github.com/tsuna/gohbase/hrpc"
  12. )
  13. const (
  14. _hBaseArticleTable = "read_auth_stats_daily"
  15. _hBaseUpStatTablePrefix = "up_stats_"
  16. )
  17. func hbaseMd5Key(mid int64) string {
  18. hasher := md5.New()
  19. hasher.Write([]byte(strconv.Itoa(int(mid))))
  20. return hex.EncodeToString(hasher.Sum(nil))
  21. }
  22. // UpArcStat get up archive stat.
  23. func (d *Dao) UpArcStat(c context.Context, mid int64, date string) (stat *model.UpArcStat, err error) {
  24. var (
  25. result *hrpc.Result
  26. ctx, cancel = context.WithTimeout(c, time.Duration(d.c.HBase.ReadTimeout))
  27. key = hbaseMd5Key(mid)
  28. tableName = _hBaseUpStatTablePrefix + date // change table at 12:00am
  29. )
  30. defer cancel()
  31. if result, err = d.hbase.GetStr(ctx, tableName, key); err != nil {
  32. log.Error("UpArcStat d.hbase.GetStr tableName(%s)|mid(%d)|key(%v)|error(%v)", tableName, mid, key, err)
  33. return
  34. }
  35. if result == nil {
  36. return
  37. }
  38. stat = &model.UpArcStat{}
  39. for _, c := range result.Cells {
  40. if c == nil {
  41. continue
  42. }
  43. v, _ := strconv.ParseInt(string(c.Value[:]), 10, 64)
  44. if !bytes.Equal(c.Family, []byte("u")) {
  45. continue
  46. }
  47. switch {
  48. case bytes.Equal(c.Qualifier, []byte("play")):
  49. stat.View = v
  50. case bytes.Equal(c.Qualifier, []byte("dm")):
  51. stat.Dm = v
  52. case bytes.Equal(c.Qualifier, []byte("reply")):
  53. stat.Reply = v
  54. case bytes.Equal(c.Qualifier, []byte("fans")):
  55. stat.Fans = v
  56. }
  57. }
  58. return
  59. }
  60. // UpArtStat get up article stat.
  61. func (d *Dao) UpArtStat(c context.Context, mid int64) (stat *model.UpArtStat, err error) {
  62. var (
  63. result *hrpc.Result
  64. ctx, cancel = context.WithTimeout(c, time.Duration(d.c.HBase.ReadTimeout))
  65. key = hbaseMd5Key(mid)
  66. tableName = _hBaseArticleTable
  67. )
  68. defer cancel()
  69. if result, err = d.hbase.GetStr(ctx, tableName, key); err != nil {
  70. log.Error("UpArtStat d.hbase.GetStr tableName(%s)|mid(%d)|key(%v)|error(%v)", tableName, mid, key, err)
  71. return
  72. }
  73. if result == nil {
  74. return
  75. }
  76. stat = &model.UpArtStat{}
  77. for _, c := range result.Cells {
  78. if c == nil {
  79. continue
  80. }
  81. v, _ := strconv.ParseInt(string(c.Value[:]), 10, 64)
  82. if !bytes.Equal(c.Family, []byte("r")) {
  83. continue
  84. }
  85. switch {
  86. case bytes.Equal(c.Qualifier, []byte("view1")):
  87. stat.View = v
  88. case bytes.Equal(c.Qualifier, []byte("reply1")):
  89. stat.Reply = v
  90. case bytes.Equal(c.Qualifier, []byte("coin1")):
  91. stat.Coin = v
  92. case bytes.Equal(c.Qualifier, []byte("like1")):
  93. stat.Like = v
  94. case bytes.Equal(c.Qualifier, []byte("fav1")):
  95. stat.Fav = v
  96. case bytes.Equal(c.Qualifier, []byte("view0")):
  97. stat.PreView = v
  98. case bytes.Equal(c.Qualifier, []byte("reply0")):
  99. stat.PreReply = v
  100. case bytes.Equal(c.Qualifier, []byte("coin0")):
  101. stat.PreCoin = v
  102. case bytes.Equal(c.Qualifier, []byte("like0")):
  103. stat.PreLike = v
  104. case bytes.Equal(c.Qualifier, []byte("fav0")):
  105. stat.PreFav = v
  106. }
  107. }
  108. stat.IncrView = stat.View - stat.PreView
  109. stat.IncrReply = stat.Reply - stat.PreReply
  110. stat.IncrCoin = stat.Coin - stat.PreCoin
  111. stat.IncrLike = stat.Like - stat.PreLike
  112. stat.IncrFav = stat.Fav - stat.PreFav
  113. return
  114. }