hbase.go 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. package data
  2. import (
  3. "bytes"
  4. "crypto/md5"
  5. "encoding/hex"
  6. "sort"
  7. "strconv"
  8. "time"
  9. "go-common/app/interface/main/creative/model/data"
  10. "go-common/library/ecode"
  11. "go-common/library/log"
  12. "github.com/tsuna/gohbase/hrpc"
  13. "golang.org/x/net/context"
  14. )
  15. func hbaseMd5Key(aid int64) string {
  16. hasher := md5.New()
  17. hasher.Write([]byte(strconv.Itoa(int(aid))))
  18. return hex.EncodeToString(hasher.Sum(nil))
  19. }
  20. // VideoQuitPoints get video quit points.
  21. func (d *Dao) VideoQuitPoints(c context.Context, cid int64) (res []int64, err error) {
  22. var (
  23. ctx, cancel = context.WithTimeout(c, time.Duration(d.c.HBase.ReadTimeout))
  24. tableName = HBaseVideoTablePrefix + time.Now().AddDate(0, 0, -1).Add(-12*time.Hour).Format("20060102") // change table at 12:00am
  25. backupTableName = HBaseVideoTablePrefix + time.Now().AddDate(0, 0, -2).Add(-12*time.Hour).Format("20060102") // change table at 12:00am
  26. key = hbaseMd5Key(cid)
  27. )
  28. defer cancel()
  29. result, err := d.hbase.GetStr(ctx, tableName, key)
  30. if err != nil {
  31. if result, err = d.hbase.GetStr(ctx, backupTableName, key); err != nil {
  32. log.Error("VideoQuitPoints d.hbase.GetStr backupTableName(%s)|cid(%d)|key(%v)|error(%v)", backupTableName, cid, key, err)
  33. err = ecode.CreativeDataErr
  34. return
  35. }
  36. }
  37. if result == nil {
  38. return
  39. }
  40. // get parts and max part for fill res
  41. partMap := make(map[int]int64)
  42. maxPart := 0
  43. for _, c := range result.Cells {
  44. if c != nil {
  45. part, _ := strconv.Atoi(string(c.Qualifier[:]))
  46. per, _ := strconv.ParseInt(string(c.Value[:]), 10, 64)
  47. partMap[part] = per
  48. if part > maxPart {
  49. maxPart = part
  50. }
  51. }
  52. }
  53. var restPercent int64 = 10000 // start from 100%
  54. for i := 1; i <= maxPart; i++ {
  55. if _, ok := partMap[i]; ok {
  56. restPercent = restPercent - partMap[i]
  57. }
  58. res = append(res, restPercent)
  59. }
  60. return
  61. }
  62. // ArchiveStat get the stat of archive.
  63. func (d *Dao) ArchiveStat(c context.Context, aid int64) (stat *data.ArchiveData, err error) {
  64. var (
  65. ctx, cancel = context.WithTimeout(c, time.Duration(d.c.HBase.ReadTimeout))
  66. tableName = HBaseArchiveTablePrefix + time.Now().AddDate(0, 0, -1).Add(-12*time.Hour).Format("20060102") // change table at 12:00am
  67. backupTableName = HBaseArchiveTablePrefix + time.Now().AddDate(0, 0, -2).Add(-12*time.Hour).Format("20060102") // change table at 12:00am
  68. key = hbaseMd5Key(aid)
  69. )
  70. defer cancel()
  71. result, err := d.hbase.GetStr(ctx, tableName, key)
  72. if err != nil {
  73. if result, err = d.hbase.GetStr(ctx, backupTableName, key); err != nil {
  74. log.Error("ArchiveStat d.hbase.GetStr backupTableName(%s)|aid(%d)|key(%v)|error(%v)", backupTableName, aid, key, err)
  75. err = ecode.CreativeDataErr
  76. return
  77. }
  78. }
  79. if result == nil {
  80. return
  81. }
  82. stat = &data.ArchiveData{}
  83. stat.ArchiveSource = &data.ArchiveSource{}
  84. stat.ArchiveGroup = &data.ArchiveGroup{}
  85. stat.ArchiveStat = &data.ArchiveStat{}
  86. for _, c := range result.Cells {
  87. if c == nil {
  88. continue
  89. }
  90. v, _ := strconv.ParseInt(string(c.Value[:]), 10, 64)
  91. if !bytes.Equal(c.Family, HBaseFamilyPlat) {
  92. continue
  93. }
  94. switch {
  95. case bytes.Equal(c.Qualifier, HBaseColumnWebPC):
  96. stat.ArchiveSource.WebPC = v
  97. case bytes.Equal(c.Qualifier, HBaseColumnWebH5):
  98. stat.ArchiveSource.WebH5 = v
  99. case bytes.Equal(c.Qualifier, HBaseColumnOutsite):
  100. stat.ArchiveSource.Outsite = v
  101. case bytes.Equal(c.Qualifier, HBaseColumnIOS):
  102. stat.ArchiveSource.IOS = v
  103. case bytes.Equal(c.Qualifier, HBaseColumnAndroid):
  104. stat.ArchiveSource.Android = v
  105. case bytes.Equal(c.Qualifier, HBaseColumnElse):
  106. stat.ArchiveSource.Others = v
  107. case bytes.Equal(c.Qualifier, HBaseColumnFans):
  108. stat.ArchiveGroup.Fans = v
  109. case bytes.Equal(c.Qualifier, HBaseColumnGuest):
  110. stat.ArchiveGroup.Guest = v
  111. case bytes.Equal(c.Qualifier, HBaseColumnAll):
  112. stat.ArchiveStat.Play = v
  113. case bytes.Equal(c.Qualifier, HBaseColumnCoin):
  114. stat.ArchiveStat.Coin = v
  115. case bytes.Equal(c.Qualifier, HBaseColumnElec):
  116. stat.ArchiveStat.Elec = v
  117. case bytes.Equal(c.Qualifier, HBaseColumnFav):
  118. stat.ArchiveStat.Fav = v
  119. case bytes.Equal(c.Qualifier, HBaseColumnShare):
  120. stat.ArchiveStat.Share = v
  121. }
  122. }
  123. stat.ArchiveSource.Mainsite = stat.ArchiveSource.WebPC + stat.ArchiveSource.WebH5
  124. stat.ArchiveSource.Mobile = stat.ArchiveSource.Android + stat.ArchiveSource.IOS
  125. return
  126. }
  127. // ArchiveArea get the count of area.
  128. func (d *Dao) ArchiveArea(c context.Context, aid int64) (res []*data.ArchiveArea, err error) {
  129. var (
  130. ctx, cancel = context.WithTimeout(c, time.Duration(d.c.HBase.ReadTimeout))
  131. tableName = HBaseAreaTablePrefix + time.Now().AddDate(0, 0, -1).Add(-12*time.Hour).Format("20060102") // change table at 12:00am
  132. backupTableName = HBaseAreaTablePrefix + time.Now().AddDate(0, 0, -2).Add(-12*time.Hour).Format("20060102") // change table at 12:00am
  133. key = hbaseMd5Key(aid)
  134. )
  135. defer cancel()
  136. result, err := d.hbase.GetStr(ctx, tableName, key)
  137. if err != nil {
  138. if result, err = d.hbase.GetStr(ctx, backupTableName, key); err != nil {
  139. log.Error("ArchiveArea d.hbase.GetStr backupTableName(%s)|aid(%d)|key(%v)|error(%v)", backupTableName, aid, key, err)
  140. err = ecode.CreativeDataErr
  141. return
  142. }
  143. }
  144. if result == nil {
  145. return
  146. }
  147. var countArr []int
  148. countMap := make(map[int64][]*data.ArchiveArea)
  149. countSet := make(map[int]struct{}) // empty struct{} for saving memory
  150. for _, c := range result.Cells {
  151. if c != nil {
  152. area := &data.ArchiveArea{}
  153. area.Location = string(c.Qualifier[:])
  154. area.Count, _ = strconv.ParseInt(string(c.Value[:]), 10, 64)
  155. countMap[area.Count] = append(countMap[area.Count], area)
  156. countSet[int(area.Count)] = struct{}{}
  157. }
  158. }
  159. for key := range countSet {
  160. countArr = append(countArr, key)
  161. }
  162. sort.Sort(sort.Reverse(sort.IntSlice(countArr)))
  163. for _, c := range countArr {
  164. if _, ok := countMap[int64(c)]; ok {
  165. res = append(res, countMap[int64(c)]...)
  166. }
  167. if len(res) >= 10 {
  168. res = res[:10] // exact 10 item
  169. break
  170. }
  171. }
  172. return
  173. }
  174. // BaseUpStat get base up stat.
  175. func (d *Dao) BaseUpStat(c context.Context, mid int64, date string) (stat *data.UpBaseStat, err error) {
  176. var (
  177. result *hrpc.Result
  178. ctx, cancel = context.WithTimeout(c, d.hbaseTimeOut)
  179. tableName = HBaseUpStatTablePrefix + date // change table at 12:00am
  180. key = hbaseMd5Key(mid)
  181. )
  182. defer cancel()
  183. if result, err = d.hbase.GetStr(ctx, tableName, key); err != nil {
  184. log.Error("BaseUpStat d.hbase.GetStr tableName(%s)|mid(%d)|key(%v)|error(%v)", tableName, mid, key, err)
  185. err = ecode.CreativeDataErr
  186. return
  187. }
  188. if result == nil {
  189. return
  190. }
  191. stat = &data.UpBaseStat{}
  192. for _, c := range result.Cells {
  193. if c == nil {
  194. continue
  195. }
  196. v, _ := strconv.ParseInt(string(c.Value[:]), 10, 64)
  197. if !bytes.Equal(c.Family, []byte("u")) {
  198. continue
  199. }
  200. switch {
  201. case bytes.Equal(c.Qualifier, []byte("play")):
  202. stat.View = v
  203. case bytes.Equal(c.Qualifier, []byte("dm")):
  204. stat.Dm = v
  205. case bytes.Equal(c.Qualifier, []byte("reply")):
  206. stat.Reply = v
  207. case bytes.Equal(c.Qualifier, []byte("fans")):
  208. stat.Fans = v
  209. case bytes.Equal(c.Qualifier, []byte("fav")):
  210. stat.Fav = v
  211. case bytes.Equal(c.Qualifier, []byte("like")):
  212. stat.Like = v
  213. case bytes.Equal(c.Qualifier, []byte("sh")):
  214. stat.Share = v
  215. }
  216. }
  217. return
  218. }