From 8ac006fc1a596701b71896984c8079a61f492b79 Mon Sep 17 00:00:00 2001
From: Eunhak Lee <lee@enak.kr>
Date: Tue, 3 Dec 2024 17:52:33 +0900
Subject: [PATCH] =?UTF-8?q?fix:=20=EC=83=88=EB=A1=9C=EC=9A=B4=20=EB=B6=80?=
 =?UTF-8?q?=ED=92=88=EC=9D=84=20=EB=A7=8C=EB=93=A4=20=EB=95=8C=20=EC=9E=98?=
 =?UTF-8?q?=EB=AA=BB=EB=90=9C=20=ED=85=8C=EC=9D=B4=EB=B8=94=EC=9D=98=20?=
 =?UTF-8?q?=EC=BB=AC=EB=9F=BC=EC=9D=84=20=EC=B0=B8=EC=A1=B0=ED=95=98?=
 =?UTF-8?q?=EB=8A=94=20=EB=AC=B8=EC=A0=9C?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 push_to_db.py | 53 +++++++++++++++++++++------------------------------
 1 file changed, 22 insertions(+), 31 deletions(-)

diff --git a/push_to_db.py b/push_to_db.py
index 6ae1f33..3e40f8c 100644
--- a/push_to_db.py
+++ b/push_to_db.py
@@ -5,23 +5,20 @@ import os
 import re
 import sys
 import time
-import parse
 import redis
 import psycopg2
-
 import traceback
 
-from datetime import datetime, timedelta
-
+import parse
+import utils
 
-def conn():
-    return psycopg2.connect(host="hub.enak.kr", user="meanspec", password="foopassword123", dbname="meanspec")
+from datetime import datetime, timedelta
 
 
 def check_similarity(_conn, name, part_type):
     _cursor = _conn.cursor()
     where_clause = "WHERE type = '{}'".format(part_type.upper()) if part_type else ""
-    _auto_threshold = 1.0 if part_type == 'RAM' else 0.13
+    _auto_threshold = 1.0 if part_type in ('RAM', 'SSD', 'HDD',) else 0.13
 
     _cursor.execute("WITH comp AS (SELECT id, name, similarity(name::text, %s::text) as sim FROM parts " + where_clause + ") SELECT * FROM comp WHERE sim >=%s ORDER BY sim DESC LIMIT 5;", (name, _auto_threshold,))
 
@@ -65,7 +62,7 @@ def create_new_one(_dict, part_type, _cursor):
     values = [value for _, value in _obj]
 
     sql = """WITH rows AS (INSERT INTO parts (name, type) VALUES (%s, %s) RETURNING id)
-INSERT INTO part_info_{type} (part_id, {columns}) (SELECT rows.id, {value_entries} FROM rows) RETURNING rows.id;""".format(type=_part_type_enum, columns=columns, value_entries=", ".join(["%s"] * len(values)))
+INSERT INTO part_info_{type} (part_id, {columns}) (SELECT rows.id, {value_entries} FROM rows) RETURNING part_id;""".format(type=_part_type_enum, columns=columns, value_entries=", ".join(["%s"] * len(values)))
     _cursor.execute(sql, (name, _part_type_enum, *values))
     data = _cursor.fetchall()
 
@@ -145,6 +142,10 @@ def func():
     _user_id = R.get(f"mypc:code:{code}:user_id")
     _transaction_id = R.get(f"mypc:code:{code}:transaction_id")
     _xml_document = R.get(f"mypc:code:{code}:document")
+    print("(User ID (raw):", _user_id)
+    print("Transaction ID: (raw)", _transaction_id)
+    print("XML starts with (raw):", _xml_document)
+
     assert _user_id is not None, "userId must not be null"
     assert _transaction_id is not None, "transactionId must not be null"
     assert _xml_document is not None, "document must not be null"
@@ -163,10 +164,16 @@ def func():
     print("Matching parts")
 
     for part_type, parts in _parts.items():  # 부품 유형별 iterate
-        if part_type == "DISK":
-            continue
+        for part in parts:  # 부품별 iterat
+            if part_type == "DISK":
+                disk_part_type = part.pop("type", "not_set").upper()
+
+                if disk_part_type not in ("SSD", "HDD",):
+                    print("[WARNING] not supported disk type", disk_part_type, file=sys.stderr)
+                    continue
+
+                part_type = disk_part_type
 
-        for part in parts:  # 부품별 iterate
             _matched_id = match_part_obj_into_db(part, part_type)
 
             if not _matched_id:  # DB에 없는 부품이라면
@@ -203,30 +210,14 @@ def func():
 if __name__ == "__main__":
     _first_run = True
 
-    _redis_url = os.getenv("REDIS_URL", "redis://localhost:6379/")
-    print("Connecting Redis to", _redis_url)
     global R
-    R = redis.Redis.from_url(_redis_url)
-
-    try:
-        _ = R.keys()
-        print("Successfully connected Redis.")
-    except:
-        print("Failed to connect Redis.", traceback.format_exc(), file=sys.stderr)
+    R = utils.env_connect_redis()
+    if not R:
         exit(1)
 
-    _db_host = os.getenv("DB_HOST", "localhost")
-    _db_port = int(os.getenv("DB_PORT", "5432"))
-    _db_name = os.getenv("DB_NAME", "meanspec")
-    _db_user = os.getenv("DB_USER", "meanspec")
-    _db_pass = os.getenv("DB_PASS", "foopassword123")
-    print("Connecting PostgreSQL to", _db_host, _db_port, _db_name, _db_user)
     global db_conn
-
-    try:
-        db_conn = psycopg2.connect(host=_db_host, port=_db_port, dbname=_db_name, user=_db_user, password=_db_pass)
-    except:
-        print("Failed to connect PostgreSQL.", traceback.format_exc(), file=sys.stderr)
+    db_conn = utils.env_connect_postgresql()
+    if not db_conn:
         exit(1)
 
     _next_run_delta = timedelta(seconds=int(os.getenv("PARSE_DAEMON_REFRESH_INTERVAL", 5)))
-- 
GitLab