article.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. package data
  2. import (
  3. "context"
  4. "strconv"
  5. "time"
  6. "go-common/app/interface/main/creative/model/data"
  7. "go-common/library/ecode"
  8. "go-common/library/log"
  9. "github.com/tsuna/gohbase/hrpc"
  10. )
  11. var (
  12. trendTBLNameMap = map[byte]string{
  13. data.ArtView: ArtViewTBL,
  14. data.ArtReply: ArtReplyTBL,
  15. data.ArtShare: ArtShareTBL,
  16. data.ArtCoin: ArtCoinTBL,
  17. data.ArtFavTBL: ArtFavTBL,
  18. data.ArtLikeTBL: ArtLikeTBL,
  19. }
  20. rankTBLNameMap = map[byte]string{
  21. data.ArtView: ArtViewIncTBL,
  22. data.ArtReply: ArtReplyIncTBL,
  23. data.ArtShare: ArtShareIncTBL,
  24. data.ArtCoin: ArtCoinIncTBL,
  25. data.ArtFavTBL: ArtFavIncTBL,
  26. data.ArtLikeTBL: ArtLikeIncTBL,
  27. }
  28. )
  29. // ArtThirtyDay for article trend 30 days.
  30. func (d *Dao) ArtThirtyDay(c context.Context, mid int64, ty byte) (res []*data.ArtTrend, err error) {
  31. var (
  32. result *hrpc.Result
  33. ctx, cancel = context.WithTimeout(c, time.Duration(d.c.HBase.ReadTimeout))
  34. key = hbaseMd5Key(mid)
  35. )
  36. defer cancel()
  37. tableName, ok := trendTBLNameMap[ty]
  38. if !ok {
  39. log.Error("ArtThirtyDay not exist type(%d)|mid(%d)", ty, mid)
  40. return
  41. }
  42. log.Info("ArtThirtyDay mid(%d)|tableName(%s)|family(u)|rowkey(%s)", mid, tableName, key)
  43. if result, err = d.hbase.GetStr(ctx, tableName, key); err != nil {
  44. log.Error("ArtThirtyDay d.hbase.GetStr tableName(%s)|mid(%d)|key(%v)|error(%v)", tableName, mid, key, err)
  45. err = ecode.CreativeDataErr
  46. return
  47. }
  48. if result == nil || len(result.Cells) == 0 {
  49. log.Warn("ArtThirtyDay no data tableName(%s)|mid(%d)|key(%v)", tableName, mid, key)
  50. return
  51. }
  52. res = make([]*data.ArtTrend, 0, len(result.Cells))
  53. for _, c := range result.Cells {
  54. if c == nil {
  55. continue
  56. }
  57. qual := string(c.Qualifier[:])
  58. val := string(c.Value[:])
  59. if string(c.Family) == "u" {
  60. t, v, err := parseKeyValue(qual, val)
  61. if err != nil {
  62. break
  63. }
  64. td := &data.ArtTrend{}
  65. td.DateKey = t
  66. td.TotalIncr = v
  67. res = append(res, td)
  68. }
  69. }
  70. log.Info("ArtThirtyDay mid(%d)|tableName(%s)|family(u)|rowkey(%s)|res(%+v)", mid, tableName, key, res)
  71. return
  72. }
  73. // ArtRank for article rank
  74. func (d *Dao) ArtRank(c context.Context, mid int64, ty byte, date string) (res *data.ArtRankMap, err error) {
  75. var (
  76. result *hrpc.Result
  77. ctx, cancel = context.WithTimeout(c, time.Duration(d.c.HBase.ReadTimeout))
  78. key = hbaseMd5Key(mid)
  79. )
  80. defer cancel()
  81. tableName, ok := rankTBLNameMap[ty]
  82. if !ok {
  83. log.Error("ArtRank not exist type(%d)|mid(%d)", ty, mid)
  84. return
  85. }
  86. tableName += date
  87. log.Info("ArtRank mid(%d)|tableName(%s)|family(rd,v)|rowkey(%s)", mid, tableName, key)
  88. if result, err = d.hbase.GetStr(ctx, tableName, key); err != nil {
  89. log.Error("ArtRank d.hbase.GetStr tableName(%s)|mid(%d)|key(%v)|error(%v)", tableName, mid, key, err)
  90. err = ecode.CreativeDataErr
  91. return
  92. }
  93. if result == nil || len(result.Cells) == 0 {
  94. log.Warn("ArtRank no data tableName(%s)|mid(%d)|key(%v)", tableName, mid, key)
  95. return
  96. }
  97. aids := make(map[int]int64)
  98. incrs := make(map[int]int)
  99. for _, c := range result.Cells {
  100. if c == nil {
  101. continue
  102. }
  103. if string(c.Family) == "rd" {
  104. k, _ := strconv.Atoi(string(c.Qualifier[:]))
  105. v, _ := strconv.ParseInt(string(c.Value[:]), 10, 64)
  106. aids[k] = v
  107. } else if string(c.Family) == "v" {
  108. k, _ := strconv.Atoi(string(c.Qualifier[:]))
  109. v, _ := strconv.Atoi(string(c.Value[:]))
  110. incrs[k] = v
  111. }
  112. }
  113. res = &data.ArtRankMap{}
  114. res.AIDs = aids
  115. res.Incrs = incrs
  116. log.Info("ArtRank mid(%d)|tableName(%s)|family(rd,v)|rowkey(%s)|res(%+v)", mid, tableName, key, res)
  117. return
  118. }
  119. // 专栏阅读来源分析 rowkey mid倒置补(10位)+ yyyyMMdd
  120. func readSourceKey(id int64) string {
  121. idStr := strconv.FormatInt(id, 10)
  122. s := reverseString(idStr)
  123. s = s + time.Now().AddDate(0, 0, -1).Add(-12*time.Hour).Format("20060102")
  124. return s
  125. }
  126. // ReadAnalysis for article read source.
  127. func (d *Dao) ReadAnalysis(c context.Context, mid int64) (res *data.ArtRead, err error) {
  128. var (
  129. result *hrpc.Result
  130. ctx, cancel = context.WithTimeout(c, time.Duration(d.c.HBase.ReadTimeout))
  131. tableName = ArtReadAnalysisTBL
  132. key = readSourceKey(mid)
  133. )
  134. defer cancel()
  135. log.Info("ReadAnalysis mid(%d)|tableName(%s)|family(f)|rowkey(%s)", mid, tableName, key)
  136. if result, err = d.hbase.GetStr(ctx, tableName, key); err != nil {
  137. log.Error("ReadAnalysis d.hbase.GetStr tableName(%s)|mid(%d)|key(%v)|error(%v)", tableName, mid, key, err)
  138. err = ecode.CreativeDataErr
  139. return
  140. }
  141. if result == nil || len(result.Cells) == 0 {
  142. log.Warn("ReadAnalysis no data tableName(%s)|mid(%d)|key(%v)", tableName, mid, key)
  143. return
  144. }
  145. var cells data.ArtRead
  146. err = parser.Parse(result.Cells, &cells)
  147. if err != nil {
  148. log.Error("ReadAnalysis parser.Parse tableName(%s)|mid(%d)|rowkey(%+v)|error(%v)", tableName, mid, key, err)
  149. err = ecode.CreativeDataErr
  150. return
  151. }
  152. res = &cells
  153. log.Info("ReadAnalysis mid(%d)|tableName(%s)|family(f)|rowkey(%s)|res(%+v)", mid, tableName, key, res)
  154. return
  155. }