diff --git a/push_to_db.py b/push_to_db.py index 6ae1f33777fe793feffc0a7e0a3346912910b5c7..3e40f8c6026b357e65ac9a83a9785afe11f15191 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)))