service.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  1. package show
  2. import (
  3. "strconv"
  4. "time"
  5. clive "go-common/app/interface/main/app-card/model/card/live"
  6. "go-common/app/interface/main/app-card/model/card/operate"
  7. "go-common/app/interface/main/app-card/model/card/rank"
  8. "go-common/app/interface/main/app-show/conf"
  9. accdao "go-common/app/interface/main/app-show/dao/account"
  10. actdao "go-common/app/interface/main/app-show/dao/activity"
  11. addao "go-common/app/interface/main/app-show/dao/ad"
  12. arcdao "go-common/app/interface/main/app-show/dao/archive"
  13. adtdao "go-common/app/interface/main/app-show/dao/audit"
  14. bgmdao "go-common/app/interface/main/app-show/dao/bangumi"
  15. carddao "go-common/app/interface/main/app-show/dao/card"
  16. dbusdao "go-common/app/interface/main/app-show/dao/databus"
  17. dyndao "go-common/app/interface/main/app-show/dao/dynamic"
  18. livedao "go-common/app/interface/main/app-show/dao/live"
  19. locdao "go-common/app/interface/main/app-show/dao/location"
  20. rcmmdao "go-common/app/interface/main/app-show/dao/recommend"
  21. regiondao "go-common/app/interface/main/app-show/dao/region"
  22. reldao "go-common/app/interface/main/app-show/dao/relation"
  23. resdao "go-common/app/interface/main/app-show/dao/resource"
  24. showdao "go-common/app/interface/main/app-show/dao/show"
  25. tagdao "go-common/app/interface/main/app-show/dao/tag"
  26. "go-common/app/interface/main/app-show/model/card"
  27. recmod "go-common/app/interface/main/app-show/model/recommend"
  28. "go-common/app/interface/main/app-show/model/region"
  29. "go-common/app/interface/main/app-show/model/show"
  30. creativeAPI "go-common/app/interface/main/creative/api"
  31. "go-common/app/service/main/archive/api"
  32. resource "go-common/app/service/main/resource/model"
  33. httpx "go-common/library/net/http/blademaster"
  34. "go-common/library/stat/prom"
  35. )
  36. type recommend struct {
  37. key string
  38. aids []int64
  39. }
  40. type rcmmndCfg struct {
  41. Aid int64 `json:"aid"`
  42. Goto string `json:"goto"`
  43. Title string `json:"title"`
  44. Cover string `json:"cover"`
  45. }
  46. // Service is show service.
  47. type Service struct {
  48. c *conf.Config
  49. creativeClient creativeAPI.CreativeClient
  50. dao *showdao.Dao
  51. rcmmnd *rcmmdao.Dao
  52. ad *addao.Dao // cptbanner
  53. bgm *bgmdao.Dao
  54. lv *livedao.Dao
  55. // bnnr *bnnrdao.Dao
  56. adt *adtdao.Dao
  57. tag *tagdao.Dao
  58. arc *arcdao.Dao
  59. dbus *dbusdao.Dao
  60. dyn *dyndao.Dao
  61. res *resdao.Dao
  62. // artic *articledao.Dao
  63. client *httpx.Client
  64. rg *regiondao.Dao
  65. cdao *carddao.Dao
  66. act *actdao.Dao
  67. acc *accdao.Dao
  68. // relation
  69. reldao *reldao.Dao
  70. loc *locdao.Dao
  71. tick time.Duration
  72. rcmmndCache []*show.Item
  73. rcmmndOseaCache []*show.Item
  74. regionCache map[string][]*show.Item
  75. regionOseaCache map[string][]*show.Item
  76. regionBgCache map[string][]*show.Item
  77. regionBgOseaCache map[string][]*show.Item
  78. regionBgEpCache map[string][]*show.Item
  79. regionBgEpOseaCache map[string][]*show.Item
  80. bgmCache map[int8][]*show.Item
  81. liveCount int
  82. liveMoeCache []*show.Item // TODO change to liveMoeCache
  83. liveHotCache []*show.Item // TODO change to liveHotCache
  84. bannerCache map[int8]map[int][]*resource.Banner
  85. cache map[string][]*show.Show
  86. cacheBg map[string][]*show.Show
  87. cacheBgEp map[string][]*show.Show
  88. tempCache map[string][]*show.Show
  89. auditCache map[string]map[int]struct{} // audit mobi_app builds
  90. blackCache map[int64]struct{} // black aids
  91. logCh chan infoc
  92. logFeedCh chan interface{}
  93. rcmmndCh chan recommend
  94. logPath string
  95. // loadfile
  96. jsonOn bool
  97. // cpm percentage 0~100
  98. cpmNum int
  99. cpmMid map[int64]struct{}
  100. cpmAll bool
  101. cpmRcmmndNum int
  102. cpmRcmmndMid map[int64]struct{}
  103. cpmRcmmndAll bool
  104. adIsPost bool
  105. // recommend api
  106. rcmmndOn bool
  107. rcmmndGroup map[int64]int // mid -> group
  108. rcmmndHosts map[int][]string // group -> hosts
  109. // region
  110. reRegionCache map[int]*region.Region
  111. // ranking
  112. rankCache []*show.Item
  113. rankOseaCache []*show.Item
  114. // card
  115. cardCache map[string][]*show.Show
  116. columnListCache map[int]*card.ColumnList
  117. cardSetCache map[int64]*operate.CardSet
  118. eventTopicCache map[int64]*operate.EventTopic
  119. // hot card
  120. hotTenTabCardCache map[int][]*recmod.CardList
  121. rankAidsCache []int64
  122. rankScoreCache map[int64]int64
  123. rankArchivesCache map[int64]*api.Arc
  124. // hotCache []*card.PopularCard
  125. rcmdCache []*card.PopularCard
  126. hottopicsCache []*clive.TopicHot
  127. rankCache2 []*rank.Rank
  128. dynamicHotCache []*clive.DynamicHot
  129. // prom
  130. pHit *prom.Prom
  131. pMiss *prom.Prom
  132. }
  133. // New new a show service.
  134. func New(c *conf.Config) (s *Service) {
  135. rcmmndHosts := make(map[int][]string, len(c.Recommend.Host))
  136. for k, v := range c.Recommend.Host {
  137. key, err := strconv.Atoi(k)
  138. if err != nil {
  139. continue
  140. }
  141. rcmmndHosts[key] = v
  142. }
  143. rcmmndGroup := make(map[int64]int, len(c.Recommend.Group))
  144. for k, v := range c.Recommend.Group {
  145. key, err := strconv.Atoi(k)
  146. if err != nil {
  147. continue
  148. }
  149. rcmmndGroup[int64(key)] = v
  150. }
  151. s = &Service{
  152. c: c,
  153. dao: showdao.New(c),
  154. rcmmnd: rcmmdao.New(c),
  155. ad: addao.New(c),
  156. bgm: bgmdao.New(c),
  157. lv: livedao.New(c),
  158. // bnnr: bnnrdao.New(c),
  159. adt: adtdao.New(c),
  160. tag: tagdao.New(c),
  161. arc: arcdao.New(c),
  162. dbus: dbusdao.New(c),
  163. dyn: dyndao.New(c),
  164. res: resdao.New(c),
  165. // artic: articledao.New(c),
  166. rg: regiondao.New(c),
  167. cdao: carddao.New(c),
  168. act: actdao.New(c),
  169. acc: accdao.New(c),
  170. // relation
  171. reldao: reldao.New(c),
  172. loc: locdao.New(c),
  173. client: httpx.NewClient(c.HTTPData),
  174. tick: time.Duration(c.Tick),
  175. jsonOn: false,
  176. logCh: make(chan infoc, 1024),
  177. logFeedCh: make(chan interface{}, 1024),
  178. rcmmndCh: make(chan recommend, 1024),
  179. logPath: c.ShowLog,
  180. rcmmndOn: false,
  181. rcmmndGroup: rcmmndGroup,
  182. rcmmndHosts: rcmmndHosts,
  183. // cpm percentage 0~100
  184. cpmNum: 0,
  185. cpmMid: map[int64]struct{}{},
  186. cpmAll: true,
  187. cpmRcmmndNum: 0,
  188. cpmRcmmndMid: map[int64]struct{}{},
  189. cpmRcmmndAll: true,
  190. adIsPost: false,
  191. // region
  192. reRegionCache: map[int]*region.Region{},
  193. // ranking
  194. rankCache: []*show.Item{},
  195. rankOseaCache: []*show.Item{},
  196. //card
  197. cardCache: map[string][]*show.Show{},
  198. columnListCache: map[int]*card.ColumnList{},
  199. cardSetCache: map[int64]*operate.CardSet{},
  200. eventTopicCache: map[int64]*operate.EventTopic{},
  201. // hot card
  202. hotTenTabCardCache: make(map[int][]*recmod.CardList),
  203. rankAidsCache: []int64{},
  204. rankScoreCache: map[int64]int64{},
  205. rankArchivesCache: map[int64]*api.Arc{},
  206. // hotCache: []*card.PopularCard{},
  207. rcmdCache: []*card.PopularCard{},
  208. hottopicsCache: []*clive.TopicHot{},
  209. rankCache2: []*rank.Rank{},
  210. dynamicHotCache: []*clive.DynamicHot{},
  211. // prom
  212. pHit: prom.CacheHit,
  213. pMiss: prom.CacheMiss,
  214. }
  215. var err error
  216. if s.creativeClient, err = creativeAPI.NewClient(nil); err != nil {
  217. panic("creativeGRPC not found!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!")
  218. }
  219. now := time.Now()
  220. s.loadCache(now)
  221. go s.cacheproc()
  222. go s.infocproc()
  223. go s.rcmmndproc()
  224. go s.infocfeedproc()
  225. return s
  226. }
  227. // cacheproc load all cache.
  228. func (s *Service) cacheproc() {
  229. for {
  230. time.Sleep(s.tick)
  231. now := time.Now()
  232. s.loadCache(now)
  233. }
  234. }
  235. func (s *Service) loadCache(now time.Time) {
  236. s.loadRcmmndCache(now)
  237. s.loadRegionCache(now)
  238. s.loadBgmCache(now)
  239. s.loadLiveCache(now)
  240. s.loadBannerCahce()
  241. s.loadShowCache()
  242. s.loadShowTempCache()
  243. s.loadBlackCache()
  244. s.loadAuditCache()
  245. s.loadRegionListCache()
  246. s.loadRankAllCache()
  247. s.loadColumnListCache(now)
  248. s.loadCardCache(now)
  249. s.loadCardSetCache()
  250. // hot
  251. // s.loadPopularCard(now)
  252. s.loadHotTenTabAids()
  253. s.loadHotTopicCache()
  254. for i := 0; i < 10; i++ {
  255. s.loadHotTenMergeRcmdCache(i)
  256. }
  257. s.loadDynamicHotCache()
  258. s.loadEventTopicCache()
  259. }
  260. // SetRcmmndOn
  261. func (s *Service) SetRcmmndOn(on bool) {
  262. s.rcmmndOn = on
  263. }
  264. // GetRcmmndOn
  265. func (s *Service) RcmmndOn() bool {
  266. return s.rcmmndOn
  267. }
  268. // Close dao
  269. func (s *Service) Close() error {
  270. return s.dao.Close()
  271. }
  272. // SetRcmmndGroup set rcmmnd group data.
  273. func (s *Service) SetRcmmndGroup(m int64, g int) {
  274. tmp := map[int64]int{}
  275. tmp[m] = g
  276. for k, v := range s.rcmmndGroup {
  277. if k != m {
  278. tmp[k] = v
  279. }
  280. }
  281. s.rcmmndGroup = tmp
  282. }
  283. // GetRcmmndGroup get rcmmnd group data.
  284. func (s *Service) GetRcmmndGroup() map[string]int {
  285. tmp := map[string]int{}
  286. for k, v := range s.rcmmndGroup {
  287. tmp[strconv.FormatInt(k, 10)] = v
  288. }
  289. return tmp
  290. }
  291. // SetRcmmndHost set rcmmnd host data.
  292. func (s *Service) SetRcmmndHost(g int, hosts []string) {
  293. tmp := map[int][]string{}
  294. tmp[g] = hosts
  295. for k, v := range s.rcmmndHosts {
  296. if k != g {
  297. tmp[k] = v
  298. }
  299. }
  300. s.rcmmndHosts = tmp
  301. }
  302. // GetRcmmndHost get rcmmnd host data.
  303. func (s *Service) GetRcmmndHost() map[string][]string {
  304. tmp := map[string][]string{}
  305. for k, v := range s.rcmmndHosts {
  306. tmp[strconv.Itoa(k)] = v
  307. }
  308. return tmp
  309. }
  310. // SetCpm percentage 0~100
  311. func (s *Service) SetCpmNum(num int) {
  312. s.cpmNum = num
  313. if s.cpmNum < 0 {
  314. s.cpmNum = 0
  315. } else if s.cpmNum > 100 {
  316. s.cpmNum = 100
  317. }
  318. }
  319. // GetCpm percentage
  320. func (s *Service) CpmNum() int {
  321. return s.cpmNum
  322. }
  323. // SetCpm percentage 0~100
  324. func (s *Service) SetCpmMid(mid int64) {
  325. var mids = map[int64]struct{}{}
  326. mids[mid] = struct{}{}
  327. for mid, _ := range s.cpmMid {
  328. if _, ok := mids[mid]; !ok {
  329. mids[mid] = struct{}{}
  330. }
  331. }
  332. s.cpmMid = mids
  333. }
  334. // GetCpm percentage
  335. func (s *Service) CpmMid() []int {
  336. var mids []int
  337. for mid, _ := range s.cpmMid {
  338. mids = append(mids, int(mid))
  339. }
  340. return mids
  341. }
  342. // SetCpm All
  343. func (s *Service) SetCpmAll(isAll bool) {
  344. s.cpmAll = isAll
  345. }
  346. // GetCpm All
  347. func (s *Service) CpmAll() int {
  348. if s.cpmAll {
  349. return 1
  350. }
  351. return 0
  352. }
  353. // RcmmndNum percentage
  354. func (s *Service) RcmmndNum() int {
  355. return s.cpmRcmmndNum
  356. }
  357. // SetRcmmndNum percentage 0~100
  358. func (s *Service) SetRcmmndNum(num int) {
  359. s.cpmRcmmndNum = num
  360. if s.cpmRcmmndNum < 0 {
  361. s.cpmRcmmndNum = 0
  362. } else if s.cpmRcmmndNum > 100 {
  363. s.cpmRcmmndNum = 100
  364. }
  365. }
  366. // CpmRcmmndMid Mid
  367. func (s *Service) CpmRcmmndMid() []int {
  368. var mids []int
  369. for mid, _ := range s.cpmRcmmndMid {
  370. mids = append(mids, int(mid))
  371. }
  372. return mids
  373. }
  374. // SetCpmRcmmndMid Mid
  375. func (s *Service) SetCpmRcmmndMid(mid int64) {
  376. var mids = map[int64]struct{}{}
  377. mids[mid] = struct{}{}
  378. for mid, _ := range s.cpmRcmmndMid {
  379. if _, ok := mids[mid]; !ok {
  380. mids[mid] = struct{}{}
  381. }
  382. }
  383. s.cpmRcmmndMid = mids
  384. }
  385. // CpmRcmmnd All
  386. func (s *Service) CpmRcmmndAll() int {
  387. if s.cpmRcmmndAll {
  388. return 1
  389. }
  390. return 0
  391. }
  392. // SetCpmRcmmnd All
  393. func (s *Service) SetCpmRcmmndAll(isAll bool) {
  394. s.cpmRcmmndAll = isAll
  395. }
  396. // SetIsPost Get or Post
  397. func (s *Service) SetAdIsPost(isPost bool) {
  398. s.adIsPost = isPost
  399. }
  400. // IsPost Get or Post
  401. func (s *Service) AdIsPost() int {
  402. if s.adIsPost {
  403. return 1
  404. }
  405. return 0
  406. }