phase_three.go 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. package data
  2. import (
  3. "context"
  4. "encoding/binary"
  5. "strconv"
  6. "time"
  7. "go-common/app/admin/main/up/model/datamodel"
  8. "go-common/app/admin/main/up/util/hbaseutil"
  9. "go-common/library/ecode"
  10. "go-common/library/log"
  11. "github.com/tsuna/gohbase/hrpc"
  12. )
  13. // reverse for string.
  14. func reverseString(s string) string {
  15. rs := []rune(s)
  16. l := len(rs)
  17. for f, t := 0, l-1; f < t; f, t = f+1, t-1 {
  18. rs[f], rs[t] = rs[t], rs[f]
  19. }
  20. ns := string(rs)
  21. if l < 10 {
  22. for i := 0; i < 10-l; i++ {
  23. ns = ns + "0"
  24. }
  25. }
  26. return ns
  27. }
  28. // 粉丝管理 - up_fans_analysis
  29. // mid倒置补0 (补满共10位)+0 +yyyyMMdd – 累计数据
  30. // mid倒置补0 (补满共10位)+1 +yyyyMMdd – 7日数据
  31. // mid倒置补0 (补满共10位)+2 +yyyyMMdd – 30日数据
  32. // mid倒置补0 (补满共10位)+3 +yyyyMMdd – 90日数据
  33. func fansRowKey(id int64, ty int) string {
  34. idStr := strconv.FormatInt(id, 10)
  35. s := reverseString(idStr)
  36. s = s + strconv.Itoa(ty) + time.Now().AddDate(0, 0, -1).Add(-12*time.Hour).Format("20060102")
  37. return s
  38. }
  39. // 稿件索引表 - up_archive_query
  40. // [mid倒置补0(10位)] + [投稿年月(4位)] + [原创/转载(1位)]
  41. func arcQueryKey(id int64, dt string, cp int) string {
  42. idStr := strconv.FormatInt(id, 10)
  43. cpStr := strconv.Itoa(cp)
  44. s := reverseString(idStr)
  45. s = s + dt + cpStr
  46. return s
  47. }
  48. // UpFansAnalysis for web fans analysis.
  49. func (d *Dao) UpFansAnalysis(c context.Context, mid int64, ty int) (res *datamodel.FanInfo, err error) {
  50. var (
  51. result *hrpc.Result
  52. ctx, cancel = context.WithTimeout(c, time.Duration(d.c.HBase.ReadTimeout))
  53. tableName = HBaseUpFansAnalysis
  54. //summary = make(map[string]int32) //total & classify proportion
  55. //rankDr = make(map[string]int32) //播放时长 mid list
  56. //rankVideoAct = make(map[string]int32) //视频互动 mid list
  57. //rankDyAct = make(map[string]int32) //动态互动 mid list
  58. rankMap = make(map[string]map[string]int32) //top 10 rank list
  59. source = make(map[string]int32) //source proportion
  60. rowkey = fansRowKey(mid, ty)
  61. parser = hbaseutil.Parser{}
  62. )
  63. res = new(datamodel.FanInfo)
  64. defer cancel()
  65. log.Info("UpFansAnalysisForWeb mid(%d)|rowkey(%s)", mid, rowkey)
  66. if result, err = d.hbase.GetStr(ctx, tableName, rowkey, hrpc.Families(map[string][]string{"f": nil})); err != nil {
  67. log.Error("d.hbase.GetStr tableName(%s)|mid(%d)|rowkey(%v)|error(%v)", tableName, mid, rowkey, err)
  68. err = ecode.CreativeDataErr
  69. return
  70. }
  71. if result == nil || len(result.Cells) == 0 {
  72. log.Warn("UpFansAnalysisForWeb no data tableName(%s)|mid(%d)|rowkey(%v)", tableName, mid, rowkey)
  73. return
  74. }
  75. var summary datamodel.FanSummaryData
  76. err = parser.Parse(result.Cells, &summary)
  77. if err != nil {
  78. log.Error("convert from hbase fail, err=%v", err)
  79. return
  80. }
  81. /*
  82. for _, c := range result.Cells {
  83. if c == nil {
  84. continue
  85. }
  86. if string(c.Family) == "f" {
  87. k := string(c.Qualifier[:])
  88. var v int32
  89. if len(c.Value) == 4 {
  90. v = byteToInt32(c.Value)
  91. switch k {
  92. case "all": //总粉丝
  93. summary.Total = v
  94. //summary["total"] = v
  95. case "inc": //新增粉丝
  96. summary.Inc = v
  97. case "act": //活跃粉丝
  98. summary.Active = v
  99. // summary["active"] = v
  100. //case "mdl": //领取勋章粉丝
  101. // summary["medal"] = v
  102. //case "elec": //充电粉丝
  103. // summary["elec"] = v
  104. //case "act_diff": //活跃粉丝(增量)
  105. // summary["act_diff"] = v
  106. //case "mdl_diff": //领取勋章粉丝(增量)
  107. // summary["mdl_diff"] = v
  108. //case "elec_diff": //充电粉丝(增量)
  109. // summary["elec_diff"] = v
  110. //case "v": //播放粉丝占比*10000
  111. // summary["v"] = v
  112. //case "dm": //弹幕粉丝占比*10000
  113. // summary["dm"] = v
  114. //case "r": //评论粉丝占比*10000
  115. // summary["r"] = v
  116. //case "c": //投币粉丝占比*10000
  117. // summary["c"] = v
  118. // //新增粉丝活跃占比
  119. //case "inter": //互动活跃度*10000
  120. // summary["inter"] = v
  121. //case "vv": //观看活跃度*10000
  122. // summary["vv"] = v
  123. //case "da": //弹幕粉丝占比*10000
  124. // summary["da"] = v
  125. //case "re": //评论粉丝占比*10000
  126. // summary["re"] = v
  127. //case "co": //投币粉丝占比*10000
  128. // summary["co"] = v
  129. //case "fv": //收藏粉丝占比*10000
  130. // summary["fv"] = v
  131. //case "sh": //分享粉丝占比*10000
  132. // summary["sh"] = v
  133. //case "lk": //点赞粉丝占比*10000
  134. // summary["lk"] = v
  135. }
  136. } else {
  137. log.Error("UpFansAnalysisForWeb family[f] get dirty tableName(%s)|mid(%d)|rowkey(%s)|value(%+v)", tableName, mid, rowkey, c.Value)
  138. }
  139. }
  140. if string(c.Family) == "t" {
  141. var v int32
  142. if len(c.Value) == 4 {
  143. v = int32(binary.BigEndian.Uint32(c.Value))
  144. } else {
  145. log.Error("UpFansAnalysisForWeb family t get dirty tableName(%s)|mid(%d)|rowkey(%s)|value(%+v)", tableName, mid, fansRowKey(mid, ty), c.Value)
  146. }
  147. if strings.Contains(string(c.Qualifier[:]), "dr") {
  148. rankDr[string(c.Qualifier[:])] = v
  149. }
  150. if strings.Contains(string(c.Qualifier[:]), "act") {
  151. rankVideoAct[string(c.Qualifier[:])] = v
  152. }
  153. if strings.Contains(string(c.Qualifier[:]), "dy") {
  154. rankDyAct[string(c.Qualifier[:])] = v
  155. }
  156. }
  157. if string(c.Family) == "s" {
  158. var v int32
  159. if len(c.Value) == 4 {
  160. v = int32(binary.BigEndian.Uint32(c.Value))
  161. } else {
  162. log.Error("UpFansAnalysisForWeb family[t] get dirty data tableName(%s)|mid(%d)|rowkey(%s)|value(%+v)", tableName, mid, rowkey, c.Value)
  163. }
  164. if strings.Contains(string(c.Qualifier[:]), "pf") {
  165. if pk, ok := fanSource[string(c.Qualifier[:])]; ok {
  166. source[pk] = v
  167. }
  168. }
  169. }
  170. }
  171. for _, k := range fanSource { //粉丝来源页面占比如果数据平台未返回,则设置对应的key
  172. if _, ok := source[k]; !ok {
  173. source[k] = 0
  174. }
  175. }
  176. */
  177. log.Info("UpFansAnalysisForWebRankMap mid(%d)|rowkey(%s)|summary(%+v)|rankMap(%+v)|source(%+v)", mid, rowkey, summary, rankMap, source)
  178. res.Summary = summary
  179. return
  180. }
  181. // UpArcQuery for play aids by mid.
  182. func (d *Dao) UpArcQuery(c context.Context, mid int64, dt string, cp int) (res []int64, err error) {
  183. var (
  184. result *hrpc.Result
  185. ctx, cancel = context.WithTimeout(c, time.Duration(d.c.HBase.ReadTimeout))
  186. tableName = HBaseUpArcQuery
  187. rowkey = arcQueryKey(mid, dt, cp)
  188. )
  189. defer cancel()
  190. log.Info("UpArcQuery mid(%d)|rowkey(%s)", mid, rowkey)
  191. if result, err = d.hbase.GetStr(ctx, tableName, rowkey); err != nil {
  192. log.Error("d.hbase.GetStr tableName(%s)|mid(%d)|rowkey(%s)|error(%v)", tableName, mid, rowkey, err)
  193. err = ecode.CreativeDataErr
  194. return
  195. }
  196. if result == nil || len(result.Cells) == 0 {
  197. log.Warn("UpArcQuery no data tableName(%s)|mid(%d)|rowkey(%s)", tableName, mid, rowkey)
  198. return
  199. }
  200. res = make([]int64, 0)
  201. for _, c := range result.Cells {
  202. if c == nil {
  203. continue
  204. }
  205. if string(c.Family) == "av" {
  206. var v int64
  207. if len(c.Value) == 4 {
  208. v = int64(binary.BigEndian.Uint32(c.Value))
  209. } else {
  210. log.Error("UpArcQuery family[av] get dirty value tableName(%s)|rowkey(%s)", tableName, rowkey)
  211. }
  212. res = append(res, v)
  213. }
  214. }
  215. log.Info("UpArcQuery mid(%d)|rowkey(%s)|res(%+v)", mid, rowkey, res)
  216. return
  217. }