-
Notifications
You must be signed in to change notification settings - Fork 1
/
init.redis.rb
86 lines (69 loc) · 1.78 KB
/
init.redis.rb
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
require 'redis'
require 'mysql2-cs-bind'
def db
@db = Mysql2::Client.new(
host: 'localhost',
port: nil,
username: 'root',
password: nil,
database: 'isu4_qualifier',
reconnect: true,
)
end
Thread.new {
Redis.current.keys('isu4:*').each_slice(1000) { |keys| Redis.current.del(keys) }
}.join
fork {
@db = nil
userfails = Hash.new(0)
ips = Hash.new(0)
last_logins = {}
ths = []
puts "=> SELECT * FROM login_log"
db.xquery('SELECT * FROM login_log').each do |log|
user_id, login, ip, ok, at = log.values_at('user_id', 'login', 'ip', 'succeeded', 'created_at')
if ok == 1
ips[ip] = 0
userfails[login] = 0
last_logins[login] = {ip: ip, at: at.to_i}
else
ips[ip] += 1
userfails[login] += 1 if user_id
end
end
ths << Thread.new do
last_logins.each do |k, vs|
Redis.current.hmset("isu4:last:#{k}", vs.to_a.flatten)
end
puts " * last"
end
ths << Thread.new do
userfails.each_slice(1000) do |pairs|
Redis.current.mset(*pairs.flat_map {|v, c| ["isu4:userfail:#{v}", c] })
end
puts " * userfail"
end
ths << Thread.new do
ips.each_slice(1000) do |pairs|
Redis.current.mset(*pairs.flat_map {|v, c| ["isu4:ip:#{v}", c] })
end
puts " * ip"
end
ths.each(&:join)
}
puts "=> SELECT * FROM users"
db.xquery('SELECT * FROM users').each_slice(10000).with_index do |slice, idx|
puts "==> users:#{idx}"
fork {
users = {}
slice.each do |user|
login, password_hash, salt = user.values_at('login', 'password_hash', 'salt')
users[login] = {password: password_hash, salt: salt, login: login}
end
users.each do |k, vs|
Redis.current.hmset("isu4:user:#{k}", vs.to_a.flatten)
end
puts " * users:#{idx}"
}
end
p Process.waitall