Rewrote main.py so it no longer ignores 15 minute timespans without 5xx responces

This commit is contained in:
2017-11-12 14:19:02 +03:00
parent 0fbb998404
commit 5960bf0e6f

35
main.py
View File

@@ -5,24 +5,41 @@ import MySQLdb
from anomaly import is_anomaly from anomaly import is_anomaly
TIME_SPAN = 900
result = {} result = {}
with open('raw_data.csv', 'rt', encoding="UTF-8") as csvfile:
reader = csv.reader(csvfile, quotechar='"')
for row in reader:
if row[0] != "ts":
timestamp = time.mktime(datetime.datetime.strptime(row[0], "%Y-%m-%d %H:%M:%S,%f").timetuple())
name = row[1] + "*" + row[2]
if 'start_time' not in locals():
start_time = timestamp
start_time = int(min(start_time, timestamp))
if 'end_time' not in locals():
end_time = timestamp
end_time = int(max(end_time, timestamp))
if name not in result:
result[name] = []
for name in result:
for t in range(start_time, end_time, TIME_SPAN):
result[name].append([t, 0, 0])
with open('raw_data.csv', 'rt', encoding="UTF-8") as csvfile: with open('raw_data.csv', 'rt', encoding="UTF-8") as csvfile:
reader = csv.reader(csvfile, quotechar='"') reader = csv.reader(csvfile, quotechar='"')
for row in reader: for row in reader:
if (row[3])[:1] == "5": if (row[3])[:1] == "5":
timestamp = time.mktime(datetime.datetime.strptime(row[0], "%Y-%m-%d %H:%M:%S,%f").timetuple()) timestamp = time.mktime(datetime.datetime.strptime(row[0], "%Y-%m-%d %H:%M:%S,%f").timetuple())
name = row[1] + "*" + row[2] name = row[1] + "*" + row[2]
if name not in result:
result[name] = [] for name in result:
result[name].append([timestamp, 1, 0])
else:
for item in result[name]: for item in result[name]:
if abs(timestamp - item[0]) < 7.5 * 60: if item[0] <= timestamp < item[0] + TIME_SPAN:
item[1] += 1 item[1] += 1
break
else:
result[name].append([timestamp, 1, 0])
is_anomaly(result) is_anomaly(result)