-
Notifications
You must be signed in to change notification settings - Fork 4
/
redis.go
109 lines (93 loc) · 2.34 KB
/
redis.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
package main
import (
"math"
"strings"
"time"
"github.com/fatih/color"
redis "gopkg.in/redis.v5"
)
func opPopulateRedis() {
defer wg.Done()
s, err := r.Keys("ripple:leaderboard:*").Result()
if err != nil {
color.Red("> PopulateRedis: %v", err)
return
}
if len(s) > 0 {
err = r.Eval("return redis.call('del', unpack(redis.call('keys', 'ripple:leaderboard:*')))", nil).Err()
if err != nil {
color.Red("> PopulateRedis: %v", err)
return
}
}
r.Del("hanayo:country_list")
populateLeaderboard(false)
populateLeaderboard(true)
color.Green("> PopulateRedis: done!")
}
func populateLeaderboard(relax bool) {
var table string
var suffix string
if relax {
table = "users_stats_relax"
suffix = ":relax"
} else {
table = "users_stats"
suffix = ""
}
initQuery := `
SELECT
users_stats.id, full_stats.country, users_stats.pp_std,
users_stats.pp_taiko, users_stats.pp_ctb, users_stats.pp_mania,
users_stats.playcount_std, users_stats.playcount_taiko, users_stats.playcount_ctb, users_stats.playcount_mania,
users.latest_activity
FROM ` + table + ` AS users_stats JOIN users_stats AS full_stats USING(id) INNER JOIN users USING(id) WHERE is_public = 1`
rows, err := db.Query(initQuery)
if err != nil {
queryError(err, initQuery)
return
}
currentSeconds := time.Now().Unix()
var (
uid int
country string
pp [4]int64
playcount [4]int
latestActivity int64
)
for rows.Next() {
err = rows.Scan(
&uid, &country, &pp[0],
&pp[1], &pp[2], &pp[3],
&playcount[0], &playcount[1], &playcount[2], &playcount[3],
&latestActivity,
)
if err != nil {
queryError(err, initQuery)
continue
}
country = strings.ToLower(country)
if country != "xx" && country != "" {
r.ZIncrBy("hanayo:country_list", 1, country)
}
for k, v := range pp {
if isInactive(float64(currentSeconds-latestActivity), playcount[k]) {
continue
}
r.ZAdd("ripple:leaderboard:"+modes[k]+suffix, redis.Z{
Member: uid,
Score: float64(v),
})
if country != "xx" && country != "" {
r.ZAdd("ripple:leaderboard:"+modes[k]+":"+country+suffix, redis.Z{
Member: uid,
Score: float64(v),
})
}
}
}
}
func isInactive(secondsInactive float64, playcount int) bool {
daysInactive := secondsInactive / (60 * 60 * 24)
return daysInactive > (math.Log(float64(playcount)) * 16)
}