mirror of
https://github.com/anatolykopyl/DINS-test-assignment.git
synced 2026-03-26 12:55:05 +00:00
replaced a dictionary with a list to increace performance
This commit is contained in:
@@ -2,10 +2,10 @@ import numpy as np
|
|||||||
|
|
||||||
|
|
||||||
def is_anomaly(result):
|
def is_anomaly(result):
|
||||||
data = [item["value"] for name in result for item in result[name]]
|
data = [item[1] for name in result for item in result[name]]
|
||||||
|
|
||||||
x = np.array(data)
|
x = np.array(data)
|
||||||
|
|
||||||
for name in result:
|
for name in result:
|
||||||
for item in result[name]:
|
for item in result[name]:
|
||||||
item["anomaly"] = item["value"] > x.mean() + 3 * x.std()
|
item[2] = item[1] > x.mean() + 3 * x.std()
|
||||||
16
main.py
16
main.py
@@ -15,19 +15,17 @@ with open('raw_data.csv', 'rt', encoding="UTF-8") as csvfile:
|
|||||||
name = row[1] + "*" + row[2]
|
name = row[1] + "*" + row[2]
|
||||||
if name not in result:
|
if name not in result:
|
||||||
result[name] = []
|
result[name] = []
|
||||||
result[name].append({"time": timestamp, "value": 1})
|
result[name].append([timestamp, 1, 0])
|
||||||
else:
|
else:
|
||||||
for item in result[name]:
|
for item in result[name]:
|
||||||
if abs(timestamp - item["time"]) < 7.5 * 60:
|
if abs(timestamp - item[0]) < 7.5 * 60:
|
||||||
item["value"] += 1
|
item[1] += 1
|
||||||
break
|
break
|
||||||
else:
|
else:
|
||||||
result[name].append({"time": timestamp, "value": 1})
|
result[name].append([timestamp, 1, 0])
|
||||||
|
|
||||||
is_anomaly(result)
|
is_anomaly(result)
|
||||||
|
|
||||||
print(result)
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
connection = MySQLdb.connect(host="127.0.0.1", user="user1", passwd="testserver", db="mydb")
|
connection = MySQLdb.connect(host="127.0.0.1", user="user1", passwd="testserver", db="mydb")
|
||||||
cursor = connection.cursor()
|
cursor = connection.cursor()
|
||||||
@@ -38,13 +36,11 @@ try:
|
|||||||
for item in result[name]:
|
for item in result[name]:
|
||||||
cursor.execute("INSERT INTO result (timeframe_start, api_name, http_method, count_http_code_5xx, is_anomaly)"
|
cursor.execute("INSERT INTO result (timeframe_start, api_name, http_method, count_http_code_5xx, is_anomaly)"
|
||||||
"VALUES (\"{0}\", \"{1}\", \"{2}\", {3}, {4})"
|
"VALUES (\"{0}\", \"{1}\", \"{2}\", {3}, {4})"
|
||||||
.format(str(datetime.datetime.fromtimestamp(item["time"]).strftime("%Y-%m-%d %H:%M:%S")),
|
.format(str(datetime.datetime.fromtimestamp(item[0]).strftime("%Y-%m-%d %H:%M:%S")),
|
||||||
name.split("*")[0], name[(name.find("*")+1):], item["value"], item["anomaly"]))
|
name.split("*")[0], name[(name.find("*")+1):], item[1], item[2]))
|
||||||
|
|
||||||
connection.commit()
|
connection.commit()
|
||||||
cursor.close()
|
cursor.close()
|
||||||
connection.close()
|
connection.close()
|
||||||
except MySQLdb.Error as err:
|
except MySQLdb.Error as err:
|
||||||
print("Connection error: {}".format(err))
|
print("Connection error: {}".format(err))
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user