forked from mailru/tntlua
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmstat.lua
205 lines (194 loc) · 6.61 KB
/
mstat.lua
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
local dkim_space = 0
local from_space = 1
local envfrom_space = 2
local sender_ip_space = 3
local dkim_msgtype_ts_space = 4
local dkim_senderip_space = 5
local field_last = 4
local field_count = 10
local timeout = 0.006
local max_attempts = 5
local function increment_stat3(space, key, subject, timestamp)
retry = true
count = 0
while retry do
status, result = pcall(box.update, space, key, '=p', field_last, timestamp)
if status then
--success update or tuple is not exist
retry = false
if result == nil then
--insert new tuple
tuple = {}
for i = 2, field_last do tuple[i] = 0 end
tuple[1] = string.sub(key, -8)
tuple[2] = subject
tuple[3] = timestamp
tuple[4] = timestamp
box.insert(space, key, unpack(tuple))
end
else
--exception
count = count + 1
if count == max_attempts then
print("max attempts reached for space="..space.." key="..key)
break
end
box.fiber.sleep(timeout)
end
end
end
-- BEG deprecated interface
function increment_or_insert(space, key, field)
retry = true
count = 0
while retry do
status, result = pcall(box.update, space, key, '+p', field, 1)
if status then
--success update or tuple is not exist
retry = false
if result == nil then
--insert new tuple
tuple = {}
for i = 2, field_count do tuple[i] = 0 end
tuple[1] = string.sub(key, -8)
tuple[tonumber(field)] = 1
box.insert(space, key, unpack(tuple))
end
else
--exception
count = count + 1
if count == max_attempts then
print("max attempts reached for space="..space.." key="..key.." field="..field)
break
end
box.fiber.sleep(timeout)
end
end
end
function increment_or_insert_2(space, key, field, element1, element2)
retry = true
count = 0
while retry do
status, result = pcall(box.update, space, key, '+p', field, 1)
if status then
--success update or tuple is not exist
retry = false
if result == nil then
--insert new tuple
tuple = {}
for i = 2, field_count + 2 do tuple[i] = 0 end
tuple[1] = string.sub(key, -8)
tuple[tonumber(field)] = 1
tuple[field_count + 1] = element1
tuple[field_count + 2] = element2
box.insert(space, key, unpack(tuple))
end
else
--exception
count = count + 1
if count == max_attempts then
print("max attempts reached for space="..space.." key="..key.." field="..field)
break
end
box.fiber.sleep(timeout)
end
end
end
function update_or_insert(space, key, subject, timestamp)
increment_stat3(space, key, subject, timestamp)
end
-- END deprecated interface
local function increment_stat(space, key, users, spam_users, prob_spam_users, inv_users)
retry = true
count = 0
while retry do
status, result = pcall(box.update, space, key, '+p+p+p+p', 2, users, 3, spam_users, 4, prob_spam_users, 5, inv_users)
if status then
--success update or tuple is not exist
retry = false
if result == nil then
--insert new tuple
tuple = {}
for i = 2, field_count do tuple[i] = 0 end
tuple[1] = string.sub(key, -8)
tuple[2] = users
tuple[3] = spam_users
tuple[4] = prob_spam_users
tuple[5] = inv_users
box.insert(space, key, unpack(tuple))
end
else
--exception
count = count + 1
if count == max_attempts then
print("max attempts reached for space="..space.." key="..key)
break
end
box.fiber.sleep(timeout)
end
end
end
local function increment_stat2(space, key, element1, element2, users, spam_users, prob_spam, inv_users)
retry = true
count = 0
while retry do
status, result = pcall(box.update, space, key, '+p+p+p+p', 2, users, 3, spam_users, 4, prob_spam_users, 5, inv_users)
if status then
--success update or tuple is not exist
retry = false
if result == nil then
--insert new tuple
tuple = {}
for i = 2, field_count + 2 do tuple[i] = 0 end
tuple[1] = string.sub(key, -8)
tuple[2] = users
tuple[3] = spam_users
tuple[4] = prob_spam_users
tuple[5] = inv_users
tuple[field_count + 1] = element1
tuple[field_count + 2] = element2
box.insert(space, key, unpack(tuple))
end
else
--exception
count = count + 1
if count == max_attempts then
print("max attempts reached for space="..space.." key="..key)
break
end
box.fiber.sleep(timeout)
end
end
end
function mstat_add(
msgtype, sender_ip, from_domain, envfrom_domain, dkim_domain, subject,
users, spam_users, prob_spam_users, inv_users,
timestamp)
users = box.unpack('i', users)
spam_users = box.unpack('i', spam_users)
prob_spam_users = box.unpack('i', prob_spam_users)
inv_users = box.unpack('i', inv_users)
timestamp = box.unpack('i', timestamp)
local time_str = os.date("_%d_%m_%y", timestamp)
if envfrom_domain ~= "" then
increment_stat(envfrom_space, envfrom_domain..time_str, users, spam_users, prob_spam_users, inv_users)
end
if from_domain ~= "" then
increment_stat(from_space, from_domain..time_str, users, spam_users, prob_spam_users, inv_users)
end
if dkim_domain ~= "" then
increment_stat(dkim_space, dkim_domain..time_str, users, spam_users, prob_spam_users, inv_users)
end
if sender_ip ~= "" then
increment_stat(sender_ip_space, sender_ip..time_str, users, spam_users, prob_spam, inv_users)
end
if dkim_domain ~= "" and sender_ip ~= "" then
increment_stat2(dkim_senderip_space, dkim_domain.."|"..sender_ip..time_str, dkim_domain, sender_ip, users, spam_users, prob_spam_users, inv_users)
end
if dkim_domain ~= "" and msgtype ~= "" then
local element = dkim_domain..":"..msgtype..time_str
increment_stat(dkim_space, element, users, spam_users, prob_spam_users, inv_users)
if subject == "" then subject = " " end
increment_stat3(dkim_msgtype_ts_space, element, subject, timestamp)
end
end