-
Notifications
You must be signed in to change notification settings - Fork 0
/
lua_scripts.py
85 lines (80 loc) · 3.36 KB
/
lua_scripts.py
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
lua_1 = """
local k = 1/math.sqrt(0.05)
local val = tonumber(ARGV[1])
local old_vals = redis.call('get',KEYS[1])
local new_vals = {}
if (old_vals) then
old_vals = cjson.decode(old_vals)
new_vals["count_1"] = old_vals['count_1'] + 1
local delta = val - old_vals["mean_1"]
new_vals["mean_1"] = old_vals["mean_1"] + delta / new_vals["count_1"]
new_vals["M2_1"] = old_vals["M2_1"] + delta * (val - new_vals["mean_1"])
new_vals["variance_1"] = new_vals["M2_1"] / new_vals["count_1"]
local std = math.sqrt(new_vals["variance_1"])
new_vals["ODV1L"] = new_vals["mean_1"] - k * std
new_vals["ODV1U"] = new_vals["mean_1"] + k * std
if (val <= new_vals["ODV1U"] and val >= new_vals["ODV1L"]) then
new_vals["count_2"] = old_vals['count_2'] + 1
delta = val - old_vals["mean_2"]
new_vals["mean_2"] = old_vals["mean_2"] + delta / new_vals["count_2"]
new_vals["M2_2"] = old_vals["M2_2"] + delta * (val - new_vals["mean_2"])
new_vals["variance_2"] = new_vals["M2_2"] / new_vals["count_2"]
std = math.sqrt(new_vals["variance_2"])
new_vals["ODV2L"] = new_vals["mean_2"] - k * std
new_vals["ODV2U"] = new_vals["mean_2"] + k * std
else
new_vals["count_2"] = old_vals['count_2']
new_vals["mean_2"] = old_vals["mean_2"]
new_vals["M2_2"] = old_vals["M2_2"]
new_vals["variance_2"] = old_vals["variance_2"]
new_vals["ODV2L"] = old_vals["ODV2L"]
new_vals["ODV2U"] = old_vals["ODV2U"]
end
else
new_vals["count_1"] = 1
new_vals["mean_1"] = val
new_vals["M2_1"] = 0
new_vals["variance_1"] = 0
new_vals["ODV1L"] = val
new_vals["ODV1U"] = val
new_vals["count_2"] = 1
new_vals["mean_2"] = val
new_vals["M2_2"] = 0
new_vals["variance_2"] = 0
new_vals["ODV2L"] = val
new_vals["ODV2U"] = val
end
redis.call('set', KEYS[1], cjson.encode(new_vals))
"""
lua_2 = """
local val = tonumber(ARGV[1])
local dt = tostring(ARGV[2])
local month = tostring(ARGV[3])
local hour = tostring(ARGV[4])
local old_vals = redis.call('get',KEYS[1])
local new_vals = {}
if (old_vals) then
old_vals = cjson.decode(old_vals)
new_vals = old_vals
if(old_vals["count_" .. dt]) then
new_vals["count_" .. dt] = old_vals["count_" .. dt] + 1
new_vals["sum_" .. dt] = old_vals["sum_" .. dt] + val
else
new_vals["count_" .. dt] = 1
new_vals["sum_" .. dt] = val
end
if (old_vals["count_" .. dt .. '_' .. hour]) then
new_vals["count_" .. dt .. '_' .. hour] = old_vals["count_" .. dt .. '_' .. hour] + 1
new_vals["sum_" .. dt .. '_' .. hour] = old_vals["sum_" .. dt .. '_' .. hour] + val
else
new_vals["count_" .. dt .. '_' .. hour] = 1
new_vals["sum_" .. dt .. '_' .. hour] = val
end
else
new_vals["count_" .. dt .. '_' .. hour] = 1
new_vals["sum_" .. dt .. '_' .. hour] = val
new_vals["count_" .. dt] = 1
new_vals["sum_" .. dt] = val
end
redis.call('set', KEYS[1], cjson.encode(new_vals))
"""