mirror of
https://github.com/anatolykopyl/DINS-test-assignment.git
synced 2026-03-26 12:55:05 +00:00
Commented and removed some useless code
This commit is contained in:
@@ -2,10 +2,8 @@ import numpy as np
|
|||||||
|
|
||||||
|
|
||||||
def is_anomaly(result):
|
def is_anomaly(result):
|
||||||
data = [item[1] for name in result for item in result[name]]
|
data = np.array([item[1] for name in result for item in result[name]])
|
||||||
|
|
||||||
x = np.array(data)
|
|
||||||
|
|
||||||
for name in result:
|
for name in result:
|
||||||
for item in result[name]:
|
for item in result[name]:
|
||||||
item[2] = item[1] > x.mean() + 3 * x.std()
|
item[2] = item[1] > data.mean() + 3 * data.std()
|
||||||
22
main.py
22
main.py
@@ -5,33 +5,31 @@ import MySQLdb
|
|||||||
|
|
||||||
from anomaly import is_anomaly
|
from anomaly import is_anomaly
|
||||||
|
|
||||||
TIME_SPAN = 900
|
TIME_SPAN = 900 # 15 minutes
|
||||||
result = {}
|
result = {}
|
||||||
with open('raw_data.csv', 'rt', encoding="UTF-8") as csvfile:
|
with open('raw_data2.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[0] != "ts":
|
if row[0] != "ts":
|
||||||
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()) # generating a unix timestamp
|
||||||
|
|
||||||
name = row[1] + "*" + row[2]
|
name = row[1] + "*" + row[2] # combining api_name and http_method into a pair name
|
||||||
|
|
||||||
if 'start_time' not in locals():
|
if 'start_time' not in locals():
|
||||||
start_time = timestamp
|
start_time = timestamp
|
||||||
start_time = int(min(start_time, timestamp))
|
start_time = int(min(start_time, timestamp)) # earliest request
|
||||||
if 'end_time' not in locals():
|
if 'end_time' not in locals():
|
||||||
end_time = timestamp
|
end_time = timestamp
|
||||||
end_time = int(max(end_time, timestamp))
|
end_time = int(max(end_time, timestamp)) # latest request
|
||||||
|
|
||||||
if name not in result:
|
if name not in result:
|
||||||
result[name] = []
|
result[name] = []
|
||||||
|
|
||||||
with open('raw_data.csv', 'rt', encoding="UTF-8") as csvfile:
|
|
||||||
reader = csv.reader(csvfile, quotechar='"')
|
|
||||||
|
|
||||||
name = row[1] + "*" + row[2]
|
|
||||||
|
|
||||||
for t in range(start_time, end_time, TIME_SPAN):
|
for t in range(start_time, end_time, TIME_SPAN):
|
||||||
result[name].append([t, 0, 0])
|
result[name].append([t, 0, 0]) # [timestamp, num of requests, is anomaly]
|
||||||
|
|
||||||
|
csvfile.seek(0)
|
||||||
|
|
||||||
for row in reader:
|
for row in reader:
|
||||||
if (row[3])[:1] == "5":
|
if (row[3])[:1] == "5":
|
||||||
|
|||||||
Reference in New Issue
Block a user