From a53c8e1d5ccfa4ed9b547c36bb664f5b7946a1de Mon Sep 17 00:00:00 2001 From: Anatoly Kopyl Date: Sun, 12 Nov 2017 01:18:45 +0300 Subject: [PATCH] replaced a dictionary with a list to increace performance --- anomaly.py | 4 ++-- main.py | 16 ++++++---------- 2 files changed, 8 insertions(+), 12 deletions(-) diff --git a/anomaly.py b/anomaly.py index 706044e..c9fdf87 100644 --- a/anomaly.py +++ b/anomaly.py @@ -2,10 +2,10 @@ import numpy as np 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) for name in result: for item in result[name]: - item["anomaly"] = item["value"] > x.mean() + 3 * x.std() \ No newline at end of file + item[2] = item[1] > x.mean() + 3 * x.std() \ No newline at end of file diff --git a/main.py b/main.py index 906c209..ec0a9a3 100644 --- a/main.py +++ b/main.py @@ -15,19 +15,17 @@ with open('raw_data.csv', 'rt', encoding="UTF-8") as csvfile: name = row[1] + "*" + row[2] if name not in result: result[name] = [] - result[name].append({"time": timestamp, "value": 1}) + result[name].append([timestamp, 1, 0]) else: for item in result[name]: - if abs(timestamp - item["time"]) < 7.5 * 60: - item["value"] += 1 + if abs(timestamp - item[0]) < 7.5 * 60: + item[1] += 1 break else: - result[name].append({"time": timestamp, "value": 1}) + result[name].append([timestamp, 1, 0]) is_anomaly(result) -print(result) - try: connection = MySQLdb.connect(host="127.0.0.1", user="user1", passwd="testserver", db="mydb") cursor = connection.cursor() @@ -38,13 +36,11 @@ try: for item in result[name]: cursor.execute("INSERT INTO result (timeframe_start, api_name, http_method, count_http_code_5xx, is_anomaly)" "VALUES (\"{0}\", \"{1}\", \"{2}\", {3}, {4})" - .format(str(datetime.datetime.fromtimestamp(item["time"]).strftime("%Y-%m-%d %H:%M:%S")), - name.split("*")[0], name[(name.find("*")+1):], item["value"], item["anomaly"])) + .format(str(datetime.datetime.fromtimestamp(item[0]).strftime("%Y-%m-%d %H:%M:%S")), + name.split("*")[0], name[(name.find("*")+1):], item[1], item[2])) connection.commit() cursor.close() connection.close() except MySQLdb.Error as err: print("Connection error: {}".format(err)) - -