db migration done
This commit is contained in:
1 parent
c9c5ee9177
commit
3b4c154fb2
23 files changed
+622
-1049
No files matched your search
@@ -1,274 +0,0 @@
|
||||
import json
|
||||
import os
|
||||
import aiomysql
|
||||
from common.utils import get_env
|
||||
|
||||
_pool: aiomysql.Pool | None = None
|
||||
|
||||
|
||||
async def get_pool() -> aiomysql.Pool:
|
||||
global _pool
|
||||
if _pool is None:
|
||||
_pool = await aiomysql.create_pool(
|
||||
host=get_env("MYSQL_HOST"),
|
||||
port=int(os.getenv("MYSQL_PORT", "3306")),
|
||||
user=get_env("MYSQL_USER"),
|
||||
password=get_env("MYSQL_PASSWORD"),
|
||||
db=get_env("MYSQL_DB"),
|
||||
charset="utf8mb4",
|
||||
minsize=0,
|
||||
maxsize=30,
|
||||
connect_timeout=10,
|
||||
)
|
||||
return _pool
|
||||
|
||||
|
||||
# 쓰기 (INSERT/UPDATE/DELETE)
|
||||
async def execute(sql: str, args: tuple = ()) -> int:
|
||||
pool = await get_pool()
|
||||
async with pool.acquire() as conn:
|
||||
try:
|
||||
async with conn.cursor() as cur:
|
||||
await cur.execute(sql, args)
|
||||
await conn.commit()
|
||||
return cur.lastrowid
|
||||
finally:
|
||||
conn.close()
|
||||
|
||||
|
||||
# 읽기 (SELECT)
|
||||
async def fetchone(sql: str, args: tuple = ()) -> dict | None:
|
||||
pool = await get_pool()
|
||||
async with pool.acquire() as conn:
|
||||
try:
|
||||
async with conn.cursor(aiomysql.DictCursor) as cur:
|
||||
await cur.execute(sql, args)
|
||||
return await cur.fetchone()
|
||||
finally:
|
||||
conn.close()
|
||||
|
||||
|
||||
async def fetchall(sql: str, args: tuple = ()) -> list[dict]:
|
||||
pool = await get_pool()
|
||||
async with pool.acquire() as conn:
|
||||
try:
|
||||
async with conn.cursor(aiomysql.DictCursor) as cur:
|
||||
await cur.execute(sql, args)
|
||||
return await cur.fetchall()
|
||||
finally:
|
||||
conn.close()
|
||||
|
||||
async def insert_instagram_row(hospital_id: str, url: str) -> int:
|
||||
return await execute("INSERT INTO instagram_data (hospital_id, url) VALUES (%s, %s)", (hospital_id, url))
|
||||
|
||||
|
||||
async def insert_facebook_row(hospital_id: str, url: str) -> int:
|
||||
return await execute("INSERT INTO facebook_data (hospital_id, url) VALUES (%s, %s)", (hospital_id, url))
|
||||
|
||||
|
||||
async def insert_naver_blog_row(hospital_id: str, url: str) -> int:
|
||||
return await execute("INSERT INTO naver_blog_data (hospital_id, url) VALUES (%s, %s)", (hospital_id, url))
|
||||
|
||||
|
||||
async def insert_youtube_row(hospital_id: str, url: str) -> int:
|
||||
return await execute("INSERT INTO youtube_data (hospital_id, url) VALUES (%s, %s)", (hospital_id, url))
|
||||
|
||||
|
||||
async def insert_gangnam_unni_row(hospital_id: str, url: str) -> int:
|
||||
return await execute("INSERT INTO gangnam_unni_data (hospital_id, url) VALUES (%s, %s)", (hospital_id, url))
|
||||
|
||||
|
||||
async def insert_file_row(
|
||||
analysis_run_id: str,
|
||||
file_type: str,
|
||||
file_name: str,
|
||||
file_url: str,
|
||||
size_bytes: int | None = None,
|
||||
hospital_id: str | None = None,
|
||||
) -> int:
|
||||
return await execute(
|
||||
"INSERT INTO file_data (analysis_run_id, hospital_id, file_type, file_name, file_url, size_bytes)"
|
||||
" VALUES (%s, %s, %s, %s, %s, %s)",
|
||||
(analysis_run_id, hospital_id, file_type, file_name, file_url, size_bytes),
|
||||
)
|
||||
|
||||
|
||||
async def insert_analysis_run(
|
||||
analysis_run_id: str,
|
||||
hospital_id: str,
|
||||
owner_user_id: int,
|
||||
instagram_data_id: int | None,
|
||||
facebook_data_id: int | None,
|
||||
naver_blog_data_id: int | None,
|
||||
youtube_data_id: int | None,
|
||||
gangnam_unni_data_id: int | None,
|
||||
) -> str:
|
||||
await execute(
|
||||
"INSERT INTO analysis_runs"
|
||||
" (analysis_run_id, hospital_id, owner_user_id, instagram_data_id, facebook_data_id, naver_blog_data_id, youtube_data_id, gangnam_unni_data_id)"
|
||||
" VALUES (%s, %s, %s, %s, %s, %s, %s, %s)",
|
||||
(analysis_run_id, hospital_id, owner_user_id, instagram_data_id, facebook_data_id, naver_blog_data_id, youtube_data_id, gangnam_unni_data_id),
|
||||
)
|
||||
return analysis_run_id
|
||||
|
||||
|
||||
|
||||
async def save_analysis_report(analysis_run_id: str, data: dict) -> None:
|
||||
await execute(
|
||||
"UPDATE analysis_runs SET report_data = %s WHERE analysis_run_id = %s",
|
||||
(json.dumps(data, ensure_ascii=False), analysis_run_id),
|
||||
)
|
||||
|
||||
|
||||
async def is_done(table: str, row_id: int | None) -> bool:
|
||||
if row_id is None:
|
||||
return True
|
||||
r = await fetchone(f"SELECT status FROM {table} WHERE id = %s", (row_id,))
|
||||
return r["status"] == "done"
|
||||
|
||||
|
||||
async def fetch_raw(table: str, row_id: int | None) -> dict | None:
|
||||
if row_id is None:
|
||||
return None
|
||||
row = await fetchone(f"SELECT raw_data FROM {table} WHERE id = %s", (row_id,))
|
||||
if not row or not row["raw_data"]:
|
||||
return None
|
||||
return json.loads(row["raw_data"]) if isinstance(row["raw_data"], str) else row["raw_data"]
|
||||
|
||||
|
||||
async def get_analysis_raw_data(analysis_run_id: str) -> dict:
|
||||
run = await fetchone(
|
||||
"SELECT instagram_data_id, facebook_data_id, naver_blog_data_id, youtube_data_id, gangnam_unni_data_id"
|
||||
" FROM analysis_runs WHERE analysis_run_id = %s",
|
||||
(analysis_run_id,),
|
||||
)
|
||||
return {
|
||||
"instagram": await fetch_raw("instagram_data", run["instagram_data_id"]),
|
||||
"facebook": await fetch_raw("facebook_data", run["facebook_data_id"]),
|
||||
"naver_blog": await fetch_raw("naver_blog_data", run["naver_blog_data_id"]),
|
||||
"youtube": await fetch_raw("youtube_data", run["youtube_data_id"]),
|
||||
"gangnam_unni": await fetch_raw("gangnam_unni_data", run["gangnam_unni_data_id"]),
|
||||
}
|
||||
|
||||
|
||||
async def set_instagram_status(row_id: int, status: str) -> None:
|
||||
await execute("UPDATE instagram_data SET status = %s WHERE id = %s", (status, row_id))
|
||||
|
||||
|
||||
async def set_facebook_status(row_id: int, status: str) -> None:
|
||||
await execute("UPDATE facebook_data SET status = %s WHERE id = %s", (status, row_id))
|
||||
|
||||
|
||||
async def set_naver_blog_status(row_id: int, status: str) -> None:
|
||||
await execute("UPDATE naver_blog_data SET status = %s WHERE id = %s", (status, row_id))
|
||||
|
||||
|
||||
async def set_youtube_status(row_id: int, status: str) -> None:
|
||||
await execute("UPDATE youtube_data SET status = %s WHERE id = %s", (status, row_id))
|
||||
|
||||
|
||||
async def set_gangnam_unni_status(row_id: int, status: str) -> None:
|
||||
await execute("UPDATE gangnam_unni_data SET status = %s WHERE id = %s", (status, row_id))
|
||||
|
||||
|
||||
async def save_instagram_raw_data(row_id: int, data: dict) -> None:
|
||||
await execute("UPDATE instagram_data SET raw_data = %s, status = 'done' WHERE id = %s", (json.dumps(data, ensure_ascii=False), row_id))
|
||||
|
||||
|
||||
async def save_facebook_raw_data(row_id: int, data: dict) -> None:
|
||||
await execute("UPDATE facebook_data SET raw_data = %s, status = 'done' WHERE id = %s", (json.dumps(data, ensure_ascii=False), row_id))
|
||||
|
||||
|
||||
async def save_naver_blog_raw_data(row_id: int, data: dict) -> None:
|
||||
await execute("UPDATE naver_blog_data SET raw_data = %s, status = 'done' WHERE id = %s", (json.dumps(data, ensure_ascii=False), row_id))
|
||||
|
||||
|
||||
async def save_youtube_raw_data(row_id: int, data: dict) -> None:
|
||||
await execute("UPDATE youtube_data SET raw_data = %s, status = 'done' WHERE id = %s", (json.dumps(data, ensure_ascii=False), row_id))
|
||||
|
||||
|
||||
async def save_gangnam_unni_raw_data(row_id: int, data: dict) -> None:
|
||||
await execute("UPDATE gangnam_unni_data SET raw_data = %s, status = 'done' WHERE id = %s", (json.dumps(data, ensure_ascii=False), row_id))
|
||||
|
||||
|
||||
async def _insert_hospital_history(hospital_id: str, analysis_run_id: str | None) -> None:
|
||||
row = await fetchone(
|
||||
"SELECT owner_user_id, hospital_name, hospital_name_en, brn, road_address, site_address, url, status, raw_data"
|
||||
" FROM hospital_baseinfo WHERE hospital_id = %s",
|
||||
(hospital_id,),
|
||||
)
|
||||
if not row:
|
||||
return
|
||||
await execute(
|
||||
"INSERT INTO hospital_history"
|
||||
" (hospital_id, owner_user_id, hospital_name, hospital_name_en, brn, road_address, site_address, url, status, raw_data, analysis_run_id)"
|
||||
" VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)",
|
||||
(
|
||||
hospital_id,
|
||||
row["owner_user_id"],
|
||||
row["hospital_name"],
|
||||
row["hospital_name_en"],
|
||||
row["brn"],
|
||||
row["road_address"],
|
||||
row["site_address"],
|
||||
row["url"],
|
||||
row["status"],
|
||||
row["raw_data"] if isinstance(row["raw_data"], str) else json.dumps(row["raw_data"], ensure_ascii=False) if row["raw_data"] else None,
|
||||
analysis_run_id,
|
||||
),
|
||||
)
|
||||
|
||||
|
||||
async def insert_hospital(
|
||||
hospital_id: str,
|
||||
name: str,
|
||||
name_en: str | None = None,
|
||||
road_address: str | None = None,
|
||||
site_address: str | None = None,
|
||||
url: str | None = None,
|
||||
raw_data: dict | None = None,
|
||||
owner_user_id: int = 0,
|
||||
brn: str = "",
|
||||
) -> dict:
|
||||
await execute(
|
||||
"INSERT INTO hospital_baseinfo (hospital_id, hospital_name, hospital_name_en, road_address, site_address, url, raw_data, status, owner_user_id, brn)"
|
||||
" VALUES (%s, %s, %s, %s, %s, %s, %s, 'done', %s, %s)",
|
||||
(hospital_id, name, name_en, road_address, site_address, url,
|
||||
json.dumps(raw_data, ensure_ascii=False) if raw_data else None,
|
||||
owner_user_id, brn),
|
||||
)
|
||||
await _insert_hospital_history(hospital_id, analysis_run_id=None)
|
||||
return await fetchone(
|
||||
"SELECT created_at FROM hospital_baseinfo WHERE hospital_id = %s",
|
||||
(hospital_id,),
|
||||
)
|
||||
|
||||
|
||||
async def save_hospital_raw_data(hospital_id: str, data: dict, analysis_run_id: str | None = None) -> None:
|
||||
await execute(
|
||||
"UPDATE hospital_baseinfo"
|
||||
" SET raw_data = %s, status = 'done',"
|
||||
" hospital_name = COALESCE(%s, hospital_name),"
|
||||
" hospital_name_en = COALESCE(%s, hospital_name_en),"
|
||||
" road_address = COALESCE(%s, road_address)"
|
||||
" WHERE hospital_id = %s",
|
||||
(
|
||||
json.dumps(data, ensure_ascii=False),
|
||||
data.get("clinicName"),
|
||||
data.get("clinicNameEn"),
|
||||
data.get("address"),
|
||||
hospital_id,
|
||||
),
|
||||
)
|
||||
await _insert_hospital_history(hospital_id, analysis_run_id)
|
||||
|
||||
|
||||
async def get_market_analysis(analysis_run_id: str) -> dict:
|
||||
rows = await fetchall(
|
||||
"SELECT analysis_type, data FROM market_analysis WHERE analysis_run_id = %s AND status = 'done'",
|
||||
(analysis_run_id,),
|
||||
)
|
||||
return {
|
||||
row["analysis_type"]: json.loads(row["data"]) if isinstance(row["data"], str) else row["data"]
|
||||
for row in rows
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
from common.db.base import execute, fetchone, fetchall
|
||||
from common.db.hospital import select_hospital, update_hospital_status, insert_hospital, update_hospital
|
||||
from common.db.source import (
|
||||
insert_source, select_source_mainpage,
|
||||
insert_raw_info, update_raw_info_status, update_raw_info,
|
||||
select_raw_info_data,
|
||||
select_run_sources, select_run_raw_data, select_run_mainpage_url,
|
||||
)
|
||||
from common.db.run import (
|
||||
insert_run, select_run, select_run_status, update_run_status,
|
||||
update_run_report, update_run_plan, select_run_with_clinic,
|
||||
)
|
||||
from common.db.market import upsert_market_status, upsert_market_result, select_market
|
||||
from common.db.file_data import insert_file, select_run_files, select_file, delete_file
|
||||
@@ -0,0 +1,56 @@
|
||||
import os
|
||||
import aiomysql
|
||||
from common.utils import get_env
|
||||
|
||||
_pool: aiomysql.Pool | None = None
|
||||
|
||||
|
||||
async def get_pool() -> aiomysql.Pool:
|
||||
global _pool
|
||||
if _pool is None:
|
||||
_pool = await aiomysql.create_pool(
|
||||
host=get_env("MYSQL_HOST"),
|
||||
port=int(os.getenv("MYSQL_PORT", "3306")),
|
||||
user=get_env("MYSQL_USER"),
|
||||
password=get_env("MYSQL_PASSWORD"),
|
||||
db=get_env("MYSQL_DB"),
|
||||
charset="utf8mb4",
|
||||
minsize=0,
|
||||
maxsize=30,
|
||||
connect_timeout=10,
|
||||
)
|
||||
return _pool
|
||||
|
||||
|
||||
async def execute(sql: str, args: tuple = ()) -> int:
|
||||
pool = await get_pool()
|
||||
async with pool.acquire() as conn:
|
||||
try:
|
||||
async with conn.cursor() as cur:
|
||||
await cur.execute(sql, args)
|
||||
await conn.commit()
|
||||
return cur.lastrowid
|
||||
finally:
|
||||
conn.close()
|
||||
|
||||
|
||||
async def fetchone(sql: str, args: tuple = ()) -> dict | None:
|
||||
pool = await get_pool()
|
||||
async with pool.acquire() as conn:
|
||||
try:
|
||||
async with conn.cursor(aiomysql.DictCursor) as cur:
|
||||
await cur.execute(sql, args)
|
||||
return await cur.fetchone()
|
||||
finally:
|
||||
conn.close()
|
||||
|
||||
|
||||
async def fetchall(sql: str, args: tuple = ()) -> list[dict]:
|
||||
pool = await get_pool()
|
||||
async with pool.acquire() as conn:
|
||||
try:
|
||||
async with conn.cursor(aiomysql.DictCursor) as cur:
|
||||
await cur.execute(sql, args)
|
||||
return await cur.fetchall()
|
||||
finally:
|
||||
conn.close()
|
||||
@@ -0,0 +1,39 @@
|
||||
from common.db.base import execute, fetchone, fetchall
|
||||
|
||||
|
||||
async def insert_file(
|
||||
analysis_run_id: str,
|
||||
file_type: str,
|
||||
file_name: str,
|
||||
file_url: str,
|
||||
size_bytes: int | None = None,
|
||||
hospital_id: str | None = None,
|
||||
) -> int:
|
||||
return await execute(
|
||||
"INSERT INTO file_data (analysis_run_id, hospital_id, file_type, file_name, file_url, size_bytes)"
|
||||
" VALUES (%s, %s, %s, %s, %s, %s)",
|
||||
(analysis_run_id, hospital_id, file_type, file_name, file_url, size_bytes),
|
||||
)
|
||||
|
||||
|
||||
async def select_run_files(analysis_run_id: str) -> list[dict]:
|
||||
return await fetchall(
|
||||
"SELECT id, file_type, file_name, file_url, size_bytes, created_at"
|
||||
" FROM file_data WHERE analysis_run_id = %s AND is_deleted = FALSE"
|
||||
" ORDER BY created_at DESC",
|
||||
(analysis_run_id,),
|
||||
)
|
||||
|
||||
|
||||
async def select_file(file_id: int, analysis_run_id: str) -> dict | None:
|
||||
return await fetchone(
|
||||
"SELECT id FROM file_data WHERE id = %s AND analysis_run_id = %s",
|
||||
(file_id, analysis_run_id),
|
||||
)
|
||||
|
||||
|
||||
async def delete_file(file_id: int) -> None:
|
||||
await execute(
|
||||
"UPDATE file_data SET is_deleted = TRUE WHERE id = %s AND is_deleted = FALSE",
|
||||
(file_id,),
|
||||
)
|
||||
@@ -0,0 +1,78 @@
|
||||
from common.db.base import execute, fetchone
|
||||
|
||||
|
||||
async def select_hospital(hospital_id: str) -> dict | None:
|
||||
return await fetchone(
|
||||
"SELECT hospital_id, owner_user_id, hospital_name, hospital_name_en,"
|
||||
" brn, road_address, site_address, status, created_at, updated_at"
|
||||
" FROM hospital_baseinfo WHERE hospital_id = %s",
|
||||
(hospital_id,),
|
||||
)
|
||||
|
||||
|
||||
async def update_hospital_status(hospital_id: str, status: str) -> None:
|
||||
await execute(
|
||||
"UPDATE hospital_baseinfo SET status = %s WHERE hospital_id = %s",
|
||||
(status, hospital_id),
|
||||
)
|
||||
|
||||
|
||||
async def _insert_hospital_history(hospital_id: str, analysis_run_id: str | None) -> None:
|
||||
row = await fetchone(
|
||||
"SELECT owner_user_id, hospital_name, hospital_name_en, brn, road_address, site_address, status"
|
||||
" FROM hospital_baseinfo WHERE hospital_id = %s",
|
||||
(hospital_id,),
|
||||
)
|
||||
if not row:
|
||||
return
|
||||
await execute(
|
||||
"INSERT INTO hospital_history"
|
||||
" (hospital_id, owner_user_id, hospital_name, hospital_name_en, brn, road_address, site_address, status, analysis_run_id)"
|
||||
" VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s)",
|
||||
(
|
||||
hospital_id,
|
||||
row["owner_user_id"],
|
||||
row["hospital_name"],
|
||||
row["hospital_name_en"],
|
||||
row["brn"],
|
||||
row["road_address"],
|
||||
row["site_address"],
|
||||
row["status"],
|
||||
analysis_run_id,
|
||||
),
|
||||
)
|
||||
|
||||
|
||||
async def insert_hospital(
|
||||
hospital_id: str,
|
||||
name: str,
|
||||
name_en: str | None = None,
|
||||
road_address: str | None = None,
|
||||
site_address: str | None = None,
|
||||
owner_user_id: int = 0,
|
||||
brn: str = "",
|
||||
) -> dict:
|
||||
await execute(
|
||||
"INSERT INTO hospital_baseinfo"
|
||||
" (hospital_id, hospital_name, hospital_name_en, road_address, site_address, status, owner_user_id, brn)"
|
||||
" VALUES (%s, %s, %s, %s, %s, 'done', %s, %s)",
|
||||
(hospital_id, name, name_en, road_address, site_address, owner_user_id, brn),
|
||||
)
|
||||
await _insert_hospital_history(hospital_id, analysis_run_id=None)
|
||||
return await fetchone(
|
||||
"SELECT created_at FROM hospital_baseinfo WHERE hospital_id = %s",
|
||||
(hospital_id,),
|
||||
)
|
||||
|
||||
|
||||
async def update_hospital(hospital_id: str, data: dict, analysis_run_id: str | None = None) -> None:
|
||||
await execute(
|
||||
"UPDATE hospital_baseinfo"
|
||||
" SET status = 'done',"
|
||||
" hospital_name = COALESCE(%s, hospital_name),"
|
||||
" hospital_name_en = COALESCE(%s, hospital_name_en),"
|
||||
" road_address = COALESCE(%s, road_address)"
|
||||
" WHERE hospital_id = %s",
|
||||
(data.get("clinicName"), data.get("clinicNameEn"), data.get("address"), hospital_id),
|
||||
)
|
||||
await _insert_hospital_history(hospital_id, analysis_run_id)
|
||||
@@ -0,0 +1,31 @@
|
||||
import json
|
||||
from common.db.base import execute, fetchall
|
||||
|
||||
|
||||
async def upsert_market_status(analysis_run_id: str, analysis_type: str, status: str) -> None:
|
||||
await execute(
|
||||
"INSERT INTO market_analysis (analysis_run_id, analysis_type, status)"
|
||||
" VALUES (%s, %s, %s)"
|
||||
" ON DUPLICATE KEY UPDATE status = VALUES(status)",
|
||||
(analysis_run_id, analysis_type, status),
|
||||
)
|
||||
|
||||
|
||||
async def upsert_market_result(analysis_run_id: str, analysis_type: str, data: dict) -> None:
|
||||
await execute(
|
||||
"INSERT INTO market_analysis (analysis_run_id, analysis_type, status, data)"
|
||||
" VALUES (%s, %s, 'done', %s)"
|
||||
" ON DUPLICATE KEY UPDATE status = 'done', data = VALUES(data)",
|
||||
(analysis_run_id, analysis_type, json.dumps(data, ensure_ascii=False)),
|
||||
)
|
||||
|
||||
|
||||
async def select_market(analysis_run_id: str) -> dict:
|
||||
rows = await fetchall(
|
||||
"SELECT analysis_type, data FROM market_analysis WHERE analysis_run_id = %s AND status = 'done'",
|
||||
(analysis_run_id,),
|
||||
)
|
||||
return {
|
||||
row["analysis_type"]: json.loads(row["data"]) if isinstance(row["data"], str) else row["data"]
|
||||
for row in rows
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
import json
|
||||
from common.db.base import execute, fetchone
|
||||
|
||||
|
||||
async def insert_run(
|
||||
analysis_run_id: str,
|
||||
hospital_id: str,
|
||||
owner_user_id: int,
|
||||
) -> str:
|
||||
await execute(
|
||||
"INSERT INTO analysis_runs (analysis_run_id, hospital_id, owner_user_id) VALUES (%s, %s, %s)",
|
||||
(analysis_run_id, hospital_id, owner_user_id),
|
||||
)
|
||||
return analysis_run_id
|
||||
|
||||
|
||||
async def select_run(analysis_run_id: str) -> dict | None:
|
||||
return await fetchone(
|
||||
"SELECT analysis_run_id, hospital_id, owner_user_id, status, created_at, updated_at"
|
||||
" FROM analysis_runs WHERE analysis_run_id = %s",
|
||||
(analysis_run_id,),
|
||||
)
|
||||
|
||||
|
||||
async def select_run_status(analysis_run_id: str) -> str | None:
|
||||
row = await fetchone(
|
||||
"SELECT status FROM analysis_runs WHERE analysis_run_id = %s",
|
||||
(analysis_run_id,),
|
||||
)
|
||||
return row["status"] if row else None
|
||||
|
||||
|
||||
async def update_run_status(analysis_run_id: str, status: str) -> None:
|
||||
await execute(
|
||||
"UPDATE analysis_runs SET status = %s WHERE analysis_run_id = %s",
|
||||
(status, analysis_run_id),
|
||||
)
|
||||
|
||||
|
||||
async def update_run_report(analysis_run_id: str, data: dict) -> None:
|
||||
await execute(
|
||||
"UPDATE analysis_runs SET report_data = %s WHERE analysis_run_id = %s",
|
||||
(json.dumps(data, ensure_ascii=False), analysis_run_id),
|
||||
)
|
||||
|
||||
|
||||
async def update_run_plan(analysis_run_id: str, data: dict) -> None:
|
||||
await execute(
|
||||
"UPDATE analysis_runs SET plan_data = %s WHERE analysis_run_id = %s",
|
||||
(json.dumps(data, ensure_ascii=False), analysis_run_id),
|
||||
)
|
||||
|
||||
|
||||
async def select_run_with_clinic(analysis_run_id: str) -> dict | None:
|
||||
return await fetchone(
|
||||
"SELECT ar.report_data, ar.plan_data, ar.created_at,"
|
||||
" h.hospital_name, h.hospital_name_en,"
|
||||
" rs.url AS target_url"
|
||||
" FROM analysis_runs ar"
|
||||
" JOIN hospital_baseinfo h ON ar.hospital_id = h.hospital_id"
|
||||
" LEFT JOIN remote_source rs ON rs.hospital_id = h.hospital_id AND rs.source_type = 'mainpage'"
|
||||
" WHERE ar.analysis_run_id = %s",
|
||||
(analysis_run_id,),
|
||||
)
|
||||
@@ -0,0 +1,85 @@
|
||||
import json
|
||||
from common.db.base import execute, fetchone, fetchall
|
||||
from models.status import SourceType
|
||||
|
||||
|
||||
async def insert_source(
|
||||
hospital_id: str,
|
||||
source_type: SourceType,
|
||||
url: str,
|
||||
language: str | None = None,
|
||||
) -> int:
|
||||
return await execute(
|
||||
"INSERT INTO remote_source (hospital_id, source_type, language, url) VALUES (%s, %s, %s, %s)",
|
||||
(hospital_id, source_type, language, url),
|
||||
)
|
||||
|
||||
|
||||
async def select_source_mainpage(hospital_id: str) -> dict | None:
|
||||
return await fetchone(
|
||||
"SELECT source_id FROM remote_source WHERE hospital_id = %s AND source_type = 'mainpage'",
|
||||
(hospital_id,),
|
||||
)
|
||||
|
||||
|
||||
async def insert_raw_info(
|
||||
source_id: int,
|
||||
analysis_run_id: str,
|
||||
data_tag: SourceType,
|
||||
) -> int:
|
||||
return await execute(
|
||||
"INSERT INTO raw_info (source_id, analysis_run_id, data_tag) VALUES (%s, %s, %s)",
|
||||
(source_id, analysis_run_id, data_tag),
|
||||
)
|
||||
|
||||
|
||||
async def update_raw_info_status(info_id: int, status: str) -> None:
|
||||
await execute("UPDATE raw_info SET status = %s WHERE info_id = %s", (status, info_id))
|
||||
|
||||
|
||||
async def update_raw_info(info_id: int, data: dict) -> None:
|
||||
await execute(
|
||||
"UPDATE raw_info SET raw_data = %s, status = 'done' WHERE info_id = %s",
|
||||
(json.dumps(data, ensure_ascii=False), info_id),
|
||||
)
|
||||
|
||||
|
||||
async def select_raw_info_data(info_id: int | None) -> dict | None:
|
||||
if info_id is None:
|
||||
return None
|
||||
row = await fetchone("SELECT raw_data FROM raw_info WHERE info_id = %s", (info_id,))
|
||||
if not row or not row["raw_data"]:
|
||||
return None
|
||||
return json.loads(row["raw_data"]) if isinstance(row["raw_data"], str) else row["raw_data"]
|
||||
|
||||
|
||||
async def select_run_sources(analysis_run_id: str) -> list[dict]:
|
||||
return await fetchall(
|
||||
"SELECT ri.info_id, rs.source_type, rs.url"
|
||||
" FROM raw_info ri JOIN remote_source rs USING (source_id)"
|
||||
" WHERE ri.analysis_run_id = %s",
|
||||
(analysis_run_id,),
|
||||
)
|
||||
|
||||
|
||||
async def select_run_raw_data(analysis_run_id: str) -> dict:
|
||||
rows = await fetchall(
|
||||
"SELECT rs.source_type, ri.raw_data"
|
||||
" FROM raw_info ri JOIN remote_source rs USING (source_id)"
|
||||
" WHERE ri.analysis_run_id = %s",
|
||||
(analysis_run_id,),
|
||||
)
|
||||
result: dict = {}
|
||||
for row in rows:
|
||||
raw = row["raw_data"]
|
||||
result[row["source_type"]] = json.loads(raw) if isinstance(raw, str) else raw
|
||||
return result
|
||||
|
||||
|
||||
async def select_run_mainpage_url(analysis_run_id: str) -> str:
|
||||
row = await fetchone(
|
||||
"SELECT rs.url FROM raw_info ri JOIN remote_source rs USING (source_id)"
|
||||
" WHERE ri.analysis_run_id = %s AND rs.source_type = 'mainpage'",
|
||||
(analysis_run_id,),
|
||||
)
|
||||
return (row or {}).get("url") or ""
|
||||
@@ -1,236 +0,0 @@
|
||||
import json
|
||||
import os
|
||||
import aiomysql
|
||||
from common.utils import get_env
|
||||
|
||||
_pool: aiomysql.Pool | None = None
|
||||
|
||||
|
||||
async def get_pool() -> aiomysql.Pool:
|
||||
global _pool
|
||||
if _pool is None:
|
||||
_pool = await aiomysql.create_pool(
|
||||
host=get_env("MYSQL_HOST"),
|
||||
port=int(os.getenv("MYSQL_PORT", "3306")),
|
||||
user=get_env("MYSQL_USER"),
|
||||
password=get_env("MYSQL_PASSWORD"),
|
||||
db=get_env("MYSQL_DB"),
|
||||
charset="utf8mb4",
|
||||
minsize=0,
|
||||
maxsize=30,
|
||||
connect_timeout=10,
|
||||
)
|
||||
return _pool
|
||||
|
||||
|
||||
async def execute(sql: str, args: tuple = ()) -> int:
|
||||
pool = await get_pool()
|
||||
async with pool.acquire() as conn:
|
||||
try:
|
||||
async with conn.cursor() as cur:
|
||||
await cur.execute(sql, args)
|
||||
await conn.commit()
|
||||
return cur.lastrowid
|
||||
finally:
|
||||
conn.close()
|
||||
|
||||
|
||||
async def fetchone(sql: str, args: tuple = ()) -> dict | None:
|
||||
pool = await get_pool()
|
||||
async with pool.acquire() as conn:
|
||||
try:
|
||||
async with conn.cursor(aiomysql.DictCursor) as cur:
|
||||
await cur.execute(sql, args)
|
||||
return await cur.fetchone()
|
||||
finally:
|
||||
conn.close()
|
||||
|
||||
|
||||
async def fetchall(sql: str, args: tuple = ()) -> list[dict]:
|
||||
pool = await get_pool()
|
||||
async with pool.acquire() as conn:
|
||||
try:
|
||||
async with conn.cursor(aiomysql.DictCursor) as cur:
|
||||
await cur.execute(sql, args)
|
||||
return await cur.fetchall()
|
||||
finally:
|
||||
conn.close()
|
||||
|
||||
|
||||
# ── remote_source ─────────────────────────────────────────────────────────────
|
||||
|
||||
async def insert_source(hospital_id: str, source_type: str, url: str) -> int:
|
||||
return await execute(
|
||||
"INSERT INTO remote_source (hospital_id, source_type, url) VALUES (%s, %s, %s)",
|
||||
(hospital_id, source_type, url),
|
||||
)
|
||||
|
||||
|
||||
# ── raw_info ──────────────────────────────────────────────────────────────────
|
||||
|
||||
async def insert_raw_info(source_id: int, analysis_run_id: str, data_tag: str = "default") -> int:
|
||||
return await execute(
|
||||
"INSERT INTO raw_info (source_id, analysis_run_id, data_tag) VALUES (%s, %s, %s)",
|
||||
(source_id, analysis_run_id, data_tag),
|
||||
)
|
||||
|
||||
|
||||
async def set_raw_info_status(info_id: int, status: str) -> None:
|
||||
await execute("UPDATE raw_info SET status = %s WHERE info_id = %s", (status, info_id))
|
||||
|
||||
|
||||
async def save_raw_info(info_id: int, data: dict) -> None:
|
||||
await execute(
|
||||
"UPDATE raw_info SET raw_data = %s, status = 'done' WHERE info_id = %s",
|
||||
(json.dumps(data, ensure_ascii=False), info_id),
|
||||
)
|
||||
|
||||
|
||||
async def fetch_raw(info_id: int | None) -> dict | None:
|
||||
if info_id is None:
|
||||
return None
|
||||
row = await fetchone("SELECT raw_data FROM raw_info WHERE info_id = %s", (info_id,))
|
||||
if not row or not row["raw_data"]:
|
||||
return None
|
||||
return json.loads(row["raw_data"]) if isinstance(row["raw_data"], str) else row["raw_data"]
|
||||
|
||||
|
||||
async def is_done(info_id: int | None) -> bool:
|
||||
if info_id is None:
|
||||
return True
|
||||
r = await fetchone("SELECT status FROM raw_info WHERE info_id = %s", (info_id,))
|
||||
return r["status"] == "done"
|
||||
|
||||
|
||||
async def get_analysis_raw_data(analysis_run_id: str) -> dict:
|
||||
rows = await fetchall(
|
||||
"SELECT rs.source_type, ri.raw_data"
|
||||
" FROM raw_info ri JOIN remote_source rs USING (source_id)"
|
||||
" WHERE ri.analysis_run_id = %s",
|
||||
(analysis_run_id,),
|
||||
)
|
||||
result: dict = {}
|
||||
for row in rows:
|
||||
raw = row["raw_data"]
|
||||
result[row["source_type"]] = json.loads(raw) if isinstance(raw, str) else raw
|
||||
return result
|
||||
|
||||
|
||||
# ── analysis_runs ─────────────────────────────────────────────────────────────
|
||||
|
||||
async def insert_analysis_run(
|
||||
analysis_run_id: str,
|
||||
hospital_id: str,
|
||||
owner_user_id: int,
|
||||
) -> str:
|
||||
await execute(
|
||||
"INSERT INTO analysis_runs (analysis_run_id, hospital_id, owner_user_id)"
|
||||
" VALUES (%s, %s, %s)",
|
||||
(analysis_run_id, hospital_id, owner_user_id),
|
||||
)
|
||||
return analysis_run_id
|
||||
|
||||
|
||||
async def save_analysis_report(analysis_run_id: str, data: dict) -> None:
|
||||
await execute(
|
||||
"UPDATE analysis_runs SET report_data = %s WHERE analysis_run_id = %s",
|
||||
(json.dumps(data, ensure_ascii=False), analysis_run_id),
|
||||
)
|
||||
|
||||
|
||||
# ── hospital_baseinfo ─────────────────────────────────────────────────────────
|
||||
|
||||
async def _insert_hospital_history(hospital_id: str, analysis_run_id: str | None) -> None:
|
||||
row = await fetchone(
|
||||
"SELECT owner_user_id, hospital_name, hospital_name_en, brn, road_address, site_address, status"
|
||||
" FROM hospital_baseinfo WHERE hospital_id = %s",
|
||||
(hospital_id,),
|
||||
)
|
||||
if not row:
|
||||
return
|
||||
await execute(
|
||||
"INSERT INTO hospital_history"
|
||||
" (hospital_id, owner_user_id, hospital_name, hospital_name_en, brn, road_address, site_address, status, analysis_run_id)"
|
||||
" VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s)",
|
||||
(
|
||||
hospital_id,
|
||||
row["owner_user_id"],
|
||||
row["hospital_name"],
|
||||
row["hospital_name_en"],
|
||||
row["brn"],
|
||||
row["road_address"],
|
||||
row["site_address"],
|
||||
row["status"],
|
||||
analysis_run_id,
|
||||
),
|
||||
)
|
||||
|
||||
|
||||
async def insert_hospital(
|
||||
hospital_id: str,
|
||||
name: str,
|
||||
name_en: str | None = None,
|
||||
road_address: str | None = None,
|
||||
site_address: str | None = None,
|
||||
owner_user_id: int = 0,
|
||||
brn: str = "",
|
||||
) -> dict:
|
||||
await execute(
|
||||
"INSERT INTO hospital_baseinfo (hospital_id, hospital_name, hospital_name_en, road_address, site_address, status, owner_user_id, brn)"
|
||||
" VALUES (%s, %s, %s, %s, %s, 'done', %s, %s)",
|
||||
(hospital_id, name, name_en, road_address, site_address, owner_user_id, brn),
|
||||
)
|
||||
await _insert_hospital_history(hospital_id, analysis_run_id=None)
|
||||
return await fetchone(
|
||||
"SELECT created_at FROM hospital_baseinfo WHERE hospital_id = %s",
|
||||
(hospital_id,),
|
||||
)
|
||||
|
||||
|
||||
async def update_hospital_info(hospital_id: str, data: dict, analysis_run_id: str | None = None) -> None:
|
||||
"""clinic 스크래핑 후 hospital_baseinfo의 기본 필드 업데이트."""
|
||||
await execute(
|
||||
"UPDATE hospital_baseinfo"
|
||||
" SET status = 'done',"
|
||||
" hospital_name = COALESCE(%s, hospital_name),"
|
||||
" hospital_name_en = COALESCE(%s, hospital_name_en),"
|
||||
" road_address = COALESCE(%s, road_address)"
|
||||
" WHERE hospital_id = %s",
|
||||
(
|
||||
data.get("clinicName"),
|
||||
data.get("clinicNameEn"),
|
||||
data.get("address"),
|
||||
hospital_id,
|
||||
),
|
||||
)
|
||||
await _insert_hospital_history(hospital_id, analysis_run_id)
|
||||
|
||||
|
||||
# ── file_data ─────────────────────────────────────────────────────────────────
|
||||
|
||||
async def insert_file_row(
|
||||
analysis_run_id: str,
|
||||
file_type: str,
|
||||
file_name: str,
|
||||
file_url: str,
|
||||
size_bytes: int | None = None,
|
||||
hospital_id: str | None = None,
|
||||
) -> int:
|
||||
return await execute(
|
||||
"INSERT INTO file_data (analysis_run_id, hospital_id, file_type, file_name, file_url, size_bytes)"
|
||||
" VALUES (%s, %s, %s, %s, %s, %s)",
|
||||
(analysis_run_id, hospital_id, file_type, file_name, file_url, size_bytes),
|
||||
)
|
||||
|
||||
|
||||
# ── market_analysis ───────────────────────────────────────────────────────────
|
||||
|
||||
async def get_market_analysis(analysis_run_id: str) -> dict:
|
||||
rows = await fetchall(
|
||||
"SELECT analysis_type, data FROM market_analysis WHERE analysis_run_id = %s AND status = 'done'",
|
||||
(analysis_run_id,),
|
||||
)
|
||||
return {
|
||||
row["analysis_type"]: json.loads(row["data"]) if isinstance(row["data"], str) else row["data"]
|
||||
for row in rows
|
||||
}
|
||||
Reference in new issue
Block a user