-
Notifications
You must be signed in to change notification settings - Fork 0
/
rank.go
314 lines (261 loc) · 7.99 KB
/
rank.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
// Copyright 2017 The StudyGolang Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// http://studygolang.com
// Author:polaris polaris@studygolang.com
package logic
import (
"context"
"fmt"
"time"
. "github.com/ue4-community/learnue/db"
"github.com/ue4-community/learnue/model"
"github.com/garyburd/redigo/redis"
"github.com/ue4-community/learnue/modules/logger"
"github.com/ue4-community/learnue/modules/nosql"
"github.com/ue4-community/learnue/modules/times"
)
type RankLogic struct{}
var DefaultRank = RankLogic{}
func (self RankLogic) GenDayRank(objtype, objid, num int) {
if objtype == model.TypeTopic {
topic := &model.Topic{}
_, err := MasterDB.Where("tid=?", objid).Get(topic)
if err != nil {
return
}
topicNode := &model.TopicNode{}
_, err = MasterDB.Where("nid=?", topic.Nid).Get(topicNode)
if err != nil {
return
}
if !topicNode.ShowIndex {
return
}
}
redisClient := nosql.NewRedisClient()
defer redisClient.Close()
key := self.getDayRankKey(objtype, times.Format("ymd"))
err := redisClient.ZINCRBY(key, num, objid)
if err != nil {
logger.Errorln("view redis ZINCRBY error:", err)
}
redisClient.EXPIRE(key, 2*30*86400)
}
// GenWeekRank 过去 7 天排行榜
func (self RankLogic) GenWeekRank(objtype int) {
redisClient := nosql.NewRedisClient()
defer redisClient.Close()
dest := self.getWeekRankKey(objtype)
keys := self.getMultiKey(objtype, 7)
err := redisClient.ZUNIONSTORE(dest, 7, keys, nil)
if err != nil {
logger.Errorln("GenWeekRank ZUNIONSTORE error:", err)
}
}
// GenMonthRank 过去 30 天排行榜
func (self RankLogic) GenMonthRank(objtype int) {
redisClient := nosql.NewRedisClient()
defer redisClient.Close()
dest := self.getMonthRankKey(objtype)
keys := self.getMultiKey(objtype, 30)
err := redisClient.ZUNIONSTORE(dest, 30, keys, nil)
if err != nil {
logger.Errorln("GenMonthRank ZUNIONSTORE error:", err)
}
}
// GenDAURank 生成日活跃用户排行
func (self RankLogic) GenDAURank(uid, weight int) {
redisClient := nosql.NewRedisClient()
defer redisClient.Close()
key := self.getDAURankKey(times.Format("ymd"))
err := redisClient.ZINCRBY(key, weight, uid)
if err != nil {
logger.Errorln("dau redis ZINCRBY error:", err)
}
redisClient.EXPIRE(key, 2*30*86400)
}
// FindDayRank needExt 是否需要扩展数据
func (self RankLogic) FindDayRank(ctx context.Context, objtype int, ymd string, num int, needExt ...bool) (result interface{}) {
objLog := GetLogger(ctx)
redisClient := nosql.NewRedisClient()
key := self.getDayRankKey(objtype, ymd)
resultSlice, err := redisClient.ZREVRANGE(key, 0, num-1, true)
redisClient.Close()
if err != nil {
objLog.Errorln("FindDayRank ZREVRANGE error:", err)
return nil
}
return self.findModelsByRank(resultSlice, objtype, num, needExt...)
}
func (self RankLogic) FindWeekRank(ctx context.Context, objtype, num int, needExt ...bool) (result interface{}) {
objLog := GetLogger(ctx)
redisClient := nosql.NewRedisClient()
key := self.getWeekRankKey(objtype)
resultSlice, err := redisClient.ZREVRANGE(key, 0, num-1, true)
redisClient.Close()
if err != nil {
objLog.Errorln("FindWeekRank ZREVRANGE error:", err)
return nil
}
return self.findModelsByRank(resultSlice, objtype, num, needExt...)
}
func (self RankLogic) FindMonthRank(ctx context.Context, objtype, num int, needExt ...bool) (result interface{}) {
objLog := GetLogger(ctx)
redisClient := nosql.NewRedisClient()
key := self.getMonthRankKey(objtype)
resultSlice, err := redisClient.ZREVRANGE(key, 0, num-1, true)
redisClient.Close()
if err != nil {
objLog.Errorln("FindMonthRank ZREVRANGE error:", err)
return nil
}
return self.findModelsByRank(resultSlice, objtype, num, needExt...)
}
// FindDAURank DAU 排名,默认获取当天的
func (self RankLogic) FindDAURank(ctx context.Context, num int, ymds ...string) []*model.User {
objLog := GetLogger(ctx)
ymd := times.Format("ymd")
if len(ymds) > 0 {
ymd = ymds[0]
}
redisClient := nosql.NewRedisClient()
key := self.getDAURankKey(ymd)
resultSlice, err := redisClient.ZREVRANGE(key, 0, num-1, true)
redisClient.Close()
if err != nil {
objLog.Errorln("FindDAURank ZREVRANGE error:", err)
return nil
}
uids := make([]int, 0, num)
weights := make([]int, 0, num)
for len(resultSlice) > 0 {
var (
uid, weight int
err error
)
resultSlice, err = redis.Scan(resultSlice, &uid, &weight)
if err != nil {
logger.Errorln("FindDAURank redis Scan error:", err)
return nil
}
uids = append(uids, uid)
weights = append(weights, weight)
}
if len(uids) == 0 {
return nil
}
userMap := DefaultUser.FindDAUUsers(ctx, uids)
users := make([]*model.User, len(userMap))
for i, uid := range uids {
if val,ok := userMap[uid];ok { // 多一层检测,确保缓存中的用户还存在,但用户却在数据库被删了时不报错
user := val
user.Weight = weights[i]
users[i] = user
}
}
return users
}
// TotalDAUUser 今日活跃用户数
func (self RankLogic) TotalDAUUser(ctx context.Context) int {
redisClient := nosql.NewRedisClient()
defer redisClient.Close()
key := self.getDAURankKey(times.Format("ymd"))
return redisClient.ZCARD(key)
}
func (self RankLogic) UserDAURank(ctx context.Context, uid int) int {
redisClient := nosql.NewRedisClient()
defer redisClient.Close()
key := self.getDAURankKey(times.Format("ymd"))
return redisClient.ZREVRANK(key, uid)
}
// FindRichRank 社区财富排行榜
func (self RankLogic) FindRichRank(ctx context.Context) []*model.User {
objLog := GetLogger(ctx)
userList := make([]*model.User, 0)
err := MasterDB.Where("balance>?", 0).Desc("balance").Limit(25).Find(&userList)
if err != nil {
objLog.Errorln("find rich rank error:", err)
return nil
}
return userList
}
func (RankLogic) findModelsByRank(resultSlice []interface{}, objtype, num int, needExt ...bool) (result interface{}) {
objids := make([]int, 0, num)
viewNums := make([]int, 0, num)
for len(resultSlice) > 0 {
var (
objid, viewNum int
err error
)
resultSlice, err = redis.Scan(resultSlice, &objid, &viewNum)
if err != nil {
logger.Errorln("findModelsByRank redis Scan error:", err)
return nil
}
objids = append(objids, objid)
viewNums = append(viewNums, viewNum)
}
switch objtype {
case model.TypeTopic:
if len(needExt) > 0 && needExt[0] {
topics := DefaultTopic.FindFullinfoByTids(objids)
for i, topic := range topics {
topic["rank_view"] = viewNums[i]
}
result = topics
} else {
topics := DefaultTopic.FindByTids(objids)
for i, topic := range topics {
topic.RankView = viewNums[i]
}
result = topics
}
case model.TypeResource:
resources := DefaultResource.FindByIds(objids)
for i, resource := range resources {
resource.RankView = viewNums[i]
}
result = resources
case model.TypeArticle:
articles := DefaultArticle.FindByIds(objids)
for i, article := range articles {
article.RankView = viewNums[i]
}
result = articles
case model.TypeProject:
projects := DefaultProject.FindByIds(objids)
for i, project := range projects {
project.RankView = viewNums[i]
}
result = projects
case model.TypeBook:
books := DefaultGoBook.FindByIds(objids)
for i, book := range books {
book.RankView = viewNums[i]
}
result = books
}
return
}
func (self RankLogic) getMultiKey(objtype, num int) []string {
today := time.Now()
keys := make([]string, num)
for i := 0; i < num; i++ {
ymd := times.Format("ymd", today.Add(time.Duration(-(i+1)*86400)*time.Second))
keys[i] = self.getDayRankKey(objtype, ymd)
}
return keys
}
func (RankLogic) getDayRankKey(objtype int, ymd string) string {
return fmt.Sprintf("view:type-%d:rank:%s", objtype, ymd)
}
func (RankLogic) getWeekRankKey(objtype int) string {
return fmt.Sprintf("view:type-%d:rank:last-week", objtype)
}
func (RankLogic) getMonthRankKey(objtype int) string {
return fmt.Sprintf("view:type-%d:rank:last-month", objtype)
}
func (RankLogic) getDAURankKey(ymd string) string {
return fmt.Sprintf("dau:rank:%s", ymd)
}