-
Notifications
You must be signed in to change notification settings - Fork 1
/
init.py
252 lines (225 loc) · 8.67 KB
/
init.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
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
import json
import datetime
import numpy as np
def millis(start_time):
dt = datetime.datetime.now() - start_time
ms = (dt.days * 24 * 60 * 60 +
dt.seconds) * 1000 + dt.microseconds / 1000.0
return ms
def calculate_logs(filename):
level_dict = {
"empty": 0,
"info": 1,
"warning": 2,
"WARNING": 2,
"error": 3,
"notice": 4
}
code_dict = {
"empty": 0,
"H10": 1, # app crashed
"H12": 2, # request timeout
"H13": 3, # connection close without response
"R14": 4, # Memory quota exceeded
"R15": 5 # Memory quota vastly exceeded
}
method_dict = {"empty": 0, "GET": 1, "POST": 2, "PUT": 3, "DELETE": 4}
logs_arr = []
prediction_arr = []
rowcount = 0
zamanlar = []
# 0 level_dict, 1 timestamp, 2 method_dict, 3 code_dict, 4 log_status, 5 log_service, 6 diff
with open(filename) as f:
for line in f:
output = json.loads(line)
jdate = output['timestamp'].split('+')[0]
timestamp = datetime.datetime.strptime(jdate, '%Y-%m-%dT%H:%M:%S.%f')
log_arr = []
try:
log_level = output['level']
if log_level is not None:
log_arr.append(level_dict[log_level])
else:
log_arr.append(level_dict['empty'])
except:
log_arr.append(level_dict['empty'])
log_arr.append(millis(timestamp))
try:
log_method = output['method']
log_arr.append(method_dict[log_method])
except:
log_arr.append(method_dict['empty'])
try:
log_code = output['code']
log_arr.append(code_dict[log_code])
except:
log_arr.append(code_dict['empty'])
try:
log_status = output['status']
log_arr.append(log_status)
except:
log_arr.append(0)
try:
log_service = output['service'].split('ms')[0]
log_arr.append(log_service)
except:
log_arr.append(0)
if rowcount - 1 >= 0:
log = logs_arr[rowcount - 1]
log_timestamp = log[1]
current_timestamp = millis(timestamp)
if log_timestamp >= current_timestamp:
diff = log_timestamp - current_timestamp
log_arr.append(diff)
elif current_timestamp > log_timestamp:
diff = current_timestamp - log_timestamp
log_arr.append(diff)
else:
log_arr.append(0)
if log_arr[4] is not 0:
#print(log_arr)
logs_arr.append(log_arr)
rowcount += 1
zamanlar.append(timestamp)
print(len(logs_arr))
ratio = 0.0
timeout_count = 0
success_response_count = 0
connection_closed_count = 0
memory_quota_exceeded_count = 0
error_count = 0
for i in range(len(logs_arr)):
tempLog = logs_arr[i]
if tempLog[3] == 0: # 😎
success_response_count += 1
if success_response_count > 20:
timeout_count = 0
connection_closed_count = 0
memory_quota_exceeded_count = 0
error_count = 0
ratio = 0
elif success_response_count > 10:
ratio = 0.0
if timeout_count > 2:
timeout_count -= 3
if connection_closed_count > 2:
connection_closed_count -= 3
if memory_quota_exceeded_count > 2:
memory_quota_exceeded_count -= 3
if error_count > 3:
error_count -= 3
elif success_response_count > 5:
ratio /= 2
if timeout_count > 1:
timeout_count -= 2
if connection_closed_count > 1:
connection_closed_count -= 2
if memory_quota_exceeded_count > 1:
memory_quota_exceeded_count -= 2
if error_count > 1:
error_count -= 2
else:
if timeout_count > 0:
timeout_count -= 1
if connection_closed_count > 0:
connection_closed_count -= 1
if memory_quota_exceeded_count > 0:
memory_quota_exceeded_count -= 1
if error_count > 0:
error_count -= 1
if ratio > 0:
ratio -= 0.1
prediction_arr.append(ratio)
elif tempLog[3] == 1: # app crashed :( ⚰️
timeout_count = 0
connection_closed_count = 0
success_response_count = 0
memory_quota_exceeded_count = 0
error_count = 0
ratio = 0.0
#print(1)
prediction_arr.append(1.0)
else:
if tempLog[3] == 2: # request timeout :| 🥴
timeout_count +=1
if timeout_count > 10:
ratio = ratio + 1.0
success_response_count = 0
elif timeout_count > 7:
ratio = ratio + 0.8
success_response_count = 0
elif timeout_count > 5:
success_response_count = 0
ratio = ratio + 0.5
elif timeout_count > 3:
ratio = ratio + 0.3
if success_response_count > 2:
success_response_count -= 3
elif timeout_count > 1:
ratio = ratio + 0.1
if success_response_count > 0:
success_response_count -= 1
elif tempLog[3] == 3: # connection close without response 🤯
connection_closed_count +=1
if connection_closed_count > 10:
ratio = ratio + 1.0
success_response_count = 0
elif connection_closed_count > 7:
ratio = ratio + 0.9
success_response_count = 0
elif connection_closed_count > 5:
ratio = ratio + 0.7
success_response_count = 0
elif connection_closed_count > 3:
ratio = ratio + 0.4
success_response_count = 0
elif connection_closed_count >= 1:
ratio = ratio + 0.1
if success_response_count > 1:
success_response_count -= 2
elif success_response_count == 1:
success_response_count = 0
elif tempLog[3] == 4 or tempLog[3] == 5: # memory quota exceeded 🤮
memory_quota_exceeded_count +=1
if memory_quota_exceeded_count > 8:
ratio = ratio + 1.0
success_response_count = 0
elif memory_quota_exceeded_count > 5:
ratio = ratio + 0.8
success_response_count = 0
elif memory_quota_exceeded_count > 3:
ratio = ratio + 0.5
success_response_count = 0
elif memory_quota_exceeded_count >= 1:
ratio = ratio + 0.2
if success_response_count > 0:
success_response_count -= 1
else:
if tempLog[0] == 3: #😡
error_count += 1
if error_count > 20:
ratio = ratio + 0.7
success_response_count = 0
elif error_count > 10:
ratio = ratio + 0.4
if success_response_count > 2:
success_response_count -= 3
elif error_count > 5:
ratio = ratio + 0.1
if success_response_count > 0:
success_response_count -= 1
prediction_arr.append(ratio)
print(prediction_arr[i])
print('prediction_arr length ',len(prediction_arr))
print(max(prediction_arr))
def sigmoid(x):
output = 1/(1+np.exp(-x))
return output
for i in range(0, len(logs_arr)):
if prediction_arr[i] == 0:
logs_arr[i].append(0.0)
else:
logs_arr[i].append(sigmoid(prediction_arr[i]))
logs_arr[i][1] = 0
print(logs_arr[i])
return logs_arr