diff --git a/backend-recommend/__pycache__/recommend.cpython-311.pyc b/backend-recommend/__pycache__/recommend.cpython-311.pyc index 5f8599a..18dff80 100644 Binary files a/backend-recommend/__pycache__/recommend.cpython-311.pyc and b/backend-recommend/__pycache__/recommend.cpython-311.pyc differ diff --git a/backend-recommend/__pycache__/redisConnection.cpython-311.pyc b/backend-recommend/__pycache__/redisConnection.cpython-311.pyc new file mode 100644 index 0000000..7be633f Binary files /dev/null and b/backend-recommend/__pycache__/redisConnection.cpython-311.pyc differ diff --git a/backend-recommend/recommend.py b/backend-recommend/recommend.py index 3bd8f7c..9456d78 100644 --- a/backend-recommend/recommend.py +++ b/backend-recommend/recommend.py @@ -4,6 +4,7 @@ from mlxtend.frequent_patterns import association_rules import pymysql import time +import redisConnection import os from dotenv import load_dotenv from flask import jsonify @@ -13,7 +14,7 @@ def mysql_create_session(): conn = pymysql.connect(host=os.getenv('host'), user=os.getenv('user'), password=os.getenv('password'), db=os.getenv('db')) cur = conn.cursor() - return conn, cur + return conn, cur def recByApriori(body): start = time.time() @@ -32,26 +33,32 @@ def recByApriori(body): input = set(input) dataset = [] - conn, cur = mysql_create_session() - try: - cur.execute('SELECT hm.history_id, sh.sold_count, sh.sold_options_id FROM SalesHistory sh INNER JOIN HistoryModelMapper hm ON sh.history_id = hm.history_id WHERE sh.car_id = %s AND hm.model_id IN (%s, %s, %s) GROUP BY hm.history_id HAVING COUNT(DISTINCT hm.model_id) = 3;', (carId, powerTrainId, bodyTypeId, operationId)) - dbRow = cur.fetchall() - finally: - conn.close() - - for j in range(len(dbRow)): - oneRow = dbRow[j][2] - if(oneRow == ''): - continue - options = oneRow.split(",") - for i in range(int(dbRow[j][1])): - dataset.append(options) + df = redisConnection.redis_getData() + if df is None: + conn, cur = mysql_create_session() + try: + cur.execute('SELECT hm.history_id, sh.sold_count, sh.sold_options_id FROM SalesHistory sh INNER JOIN HistoryModelMapper hm ON sh.history_id = hm.history_id WHERE sh.car_id = %s AND hm.model_id IN (%s, %s, %s) GROUP BY hm.history_id HAVING COUNT(DISTINCT hm.model_id) = 3;', (carId, powerTrainId, bodyTypeId, operationId)) + dbRow = cur.fetchall() + finally: + conn.close() + + for j in range(len(dbRow)): + oneRow = dbRow[j][2] + if(oneRow == ''): + continue + options = oneRow.split(",") + for i in range(int(dbRow[j][1])): + dataset.append(options) + + start = time.time() + te = TransactionEncoder() + te_ary = te.fit(dataset).transform(dataset) + df = pd.DataFrame(te_ary, columns=te.columns_) + redisConnection.redis_setData(df) - start = time.time() - te = TransactionEncoder() - te_ary = te.fit(dataset).transform(dataset) - df = pd.DataFrame(te_ary, columns=te.columns_) df = df.iloc[:100000] + + frequent_itemsets = apriori(df, min_support=0.01, use_colnames=True) result_itemsets = association_rules(frequent_itemsets, metric="confidence", min_threshold=0.1) diff --git a/backend-recommend/redisConnection.py b/backend-recommend/redisConnection.py new file mode 100644 index 0000000..321afbb --- /dev/null +++ b/backend-recommend/redisConnection.py @@ -0,0 +1,22 @@ +import io +import redis +import pandas as pd +import os + +redis_pool = redis.ConnectionPool(host=os.getenv('redis_host'), port=6379, password=os.getenv('redis_password'), db=0) + +def redis_getData(): + with redis.StrictRedis(connection_pool=redis_pool) as conn: + bytes_object = conn.get("rec_dataframe") + if bytes_object is None: + return None + buffer = io.BytesIO(bytes_object) + return pd.read_pickle(buffer) + +def redis_setData(df): + with redis.StrictRedis(connection_pool=redis_pool) as conn: + buffer = io.BytesIO() + df.to_pickle(buffer) + buffer.seek(0) + bytes_object = buffer.read() + conn.set('rec_dataframe', bytes_object, 60*60*24*7)