bigdata.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. package dao
  2. import (
  3. "context"
  4. "fmt"
  5. "net/url"
  6. "go-common/app/interface/main/web/model"
  7. "go-common/library/ecode"
  8. "go-common/library/log"
  9. "go-common/library/net/metadata"
  10. )
  11. const (
  12. _rankURI = "%s.json"
  13. _rankAllURI = "all-%d"
  14. _rankAllRidURI = "all_region-%d-%d"
  15. _rankAllRecURI = "recent_all-%d"
  16. _rankAllRecRidURI = "recent_region-%d-%d"
  17. _rankOriAllURI = "all_origin-%d"
  18. _rankOriAllRidURI = "all_region_origin-%d-%d"
  19. _rankOriRecURI = "recent_origin-%d"
  20. _rankOriRecRidURI = "recent_region_origin-%d-%d"
  21. _rankAllNewURI = "all_rookie-%d"
  22. _rankAllNewRidURI = "all_region_rookie-%d-%d"
  23. _rankRegionURI = "recent_region%s-%d-%d.json"
  24. _rankRecURI = "reco_region-%d.json"
  25. _rankTagURI = "/tag/hot/web/%d/%d.json"
  26. _rankIndexURI = "reco-%d.json"
  27. _customURI = "game_custom_2.json"
  28. )
  29. // Ranking get ranking data from new api
  30. func (d *Dao) Ranking(c context.Context, rid int16, rankType, day, arcType int) (res *model.RankNew, err error) {
  31. var (
  32. params = url.Values{}
  33. ip = metadata.String(c, metadata.RemoteIP)
  34. )
  35. suffix := rankURI(rid, model.RankType[rankType], day, arcType)
  36. var rs struct {
  37. Code int `json:"code"`
  38. Note string `json:"note"`
  39. List []*model.RankNewArchive `json:"list"`
  40. }
  41. if err = d.httpBigData.RESTfulGet(c, d.rankURL, ip, params, &rs, suffix); err != nil {
  42. log.Error("d.httpBigData.RESTfulGet(%s) error(%v)", suffix, err)
  43. return
  44. }
  45. if rs.Code != ecode.OK.Code() {
  46. log.Error("d.httpBigData.RESTfulGet(%s) error code(%d)", suffix, rs.Code)
  47. err = ecode.Int(rs.Code)
  48. return
  49. }
  50. res = &model.RankNew{Note: rs.Note, List: rs.List}
  51. return
  52. }
  53. // RankingIndex get rank index data from bigdata
  54. func (d *Dao) RankingIndex(c context.Context, day int) (res []*model.NewArchive, err error) {
  55. var (
  56. params = url.Values{}
  57. remoteIP = metadata.String(c, metadata.RemoteIP)
  58. )
  59. var rs struct {
  60. Code int `json:"code"`
  61. Num int `json:"num"`
  62. List []*model.NewArchive `json:"list"`
  63. }
  64. if err = d.httpBigData.RESTfulGet(c, d.rankIndexURL, remoteIP, params, &rs, day); err != nil {
  65. log.Error("d.httpBigData.RESTfulGet(%d) error(%v)", day, err)
  66. return
  67. }
  68. if rs.Code != ecode.OK.Code() {
  69. log.Error("d.httpBigData.RESTfulGet(%d) error(%v)", day, rs.Code)
  70. err = ecode.Int(rs.Code)
  71. return
  72. }
  73. res = rs.List
  74. return
  75. }
  76. // RankingRegion get rank region data from bigdata
  77. func (d *Dao) RankingRegion(c context.Context, rid int16, day, original int) (res []*model.NewArchive, err error) {
  78. var (
  79. params = url.Values{}
  80. remoteIP = metadata.String(c, metadata.RemoteIP)
  81. )
  82. var rs struct {
  83. Code int `json:"code"`
  84. List []*model.NewArchive `json:"list"`
  85. }
  86. if err = d.httpBigData.RESTfulGet(c, d.rankRegionURL, remoteIP, params, &rs, model.OriType[original], rid, day); err != nil {
  87. log.Error("d.httpBigData.RESTfulGet(%d,%d,%d) error(%v)", original, rid, day, err)
  88. return
  89. }
  90. if rs.Code != ecode.OK.Code() {
  91. log.Error("d.httpBigData.RESTfulGet(%d,%d,%d) error code(%d)", original, rid, day, rs.Code)
  92. err = ecode.Int(rs.Code)
  93. return
  94. }
  95. res = rs.List
  96. return
  97. }
  98. // RankingRecommend get rank recommend data from bigdata.
  99. func (d *Dao) RankingRecommend(c context.Context, rid int16) (res []*model.NewArchive, err error) {
  100. var (
  101. params = url.Values{}
  102. remoteIP = metadata.String(c, metadata.RemoteIP)
  103. )
  104. var rs struct {
  105. Code int `json:"code"`
  106. Num int `json:"num"`
  107. List []*model.NewArchive `json:"list"`
  108. }
  109. if err = d.httpBigData.RESTfulGet(c, d.rankRecURL, remoteIP, params, &rs, rid); err != nil {
  110. log.Error("d.httpBigData.RESTfulGet(%d) error(%v)", rid, err)
  111. return
  112. }
  113. if rs.Code != ecode.OK.Code() {
  114. log.Error("d.httpBigData.RESTfulGet(%d) error(%v)", rid, rs.Code)
  115. err = ecode.Int(rs.Code)
  116. return
  117. }
  118. res = rs.List
  119. return
  120. }
  121. // RankingTag get rank tag data from bigdata
  122. func (d *Dao) RankingTag(c context.Context, rid int16, tagID int64) (rs []*model.NewArchive, err error) {
  123. var (
  124. params = url.Values{}
  125. remoteIP = metadata.String(c, metadata.RemoteIP)
  126. )
  127. var res struct {
  128. Code int `json:"code"`
  129. List []*model.NewArchive `json:"list"`
  130. }
  131. if err = d.httpBigData.RESTfulGet(c, d.rankTagURL, remoteIP, params, &res, rid, tagID); err != nil {
  132. log.Error("d.httpBigData.RESTfulGet(%d,%d) error(%v)", rid, tagID, err)
  133. return
  134. }
  135. if res.Code != ecode.OK.Code() {
  136. log.Error("d.httpBigData.RESTfulGet(%d,%d) code(%d)", rid, tagID, res.Code)
  137. err = ecode.Int(res.Code)
  138. return
  139. }
  140. rs = res.List
  141. return
  142. }
  143. // RegionCustom get region(game) custom data from big data
  144. func (d *Dao) RegionCustom(c context.Context) (rs []*model.Custom, err error) {
  145. var (
  146. params = url.Values{}
  147. ip = metadata.String(c, metadata.RemoteIP)
  148. )
  149. var res struct {
  150. Code int
  151. List []*model.Custom
  152. }
  153. if err = d.httpBigData.Get(c, d.customURL, ip, params, &res); err != nil {
  154. log.Error("d.httpBigData.Get(%s) error(%v)", d.customURL, err)
  155. return
  156. }
  157. if res.Code != ecode.OK.Code() {
  158. log.Error("d.httpBigData.Get(%s) error(%v)", d.customURL, err)
  159. err = ecode.Int(res.Code)
  160. return
  161. }
  162. rs = res.List
  163. return
  164. }
  165. func rankURI(rid int16, rankType string, day, arcType int) string {
  166. if rankType == model.RankType[1] {
  167. if arcType == 1 {
  168. if rid > 0 {
  169. return fmt.Sprintf(_rankAllRecRidURI, rid, day)
  170. }
  171. return fmt.Sprintf(_rankAllRecURI, day)
  172. }
  173. if rid > 0 {
  174. return fmt.Sprintf(_rankAllRidURI, rid, day)
  175. }
  176. return fmt.Sprintf(_rankAllURI, day)
  177. } else if rankType == model.RankType[2] {
  178. if arcType == 1 {
  179. if rid > 0 {
  180. return fmt.Sprintf(_rankOriRecRidURI, rid, day)
  181. }
  182. return fmt.Sprintf(_rankOriRecURI, day)
  183. }
  184. if rid > 0 {
  185. return fmt.Sprintf(_rankOriAllRidURI, rid, day)
  186. }
  187. return fmt.Sprintf(_rankOriAllURI, day)
  188. }
  189. if rid > 0 {
  190. return fmt.Sprintf(_rankAllNewRidURI, rid, day)
  191. }
  192. return fmt.Sprintf(_rankAllNewURI, day)
  193. }