Files
kis_bot/scratch/check_db_counts.py

77 lines
2.6 KiB
Python

import sys
import os
import datetime
# PYTHONPATH=. setting
sys.path.insert(0, "/home/hoon/kis_bot")
from database import TradeDB
def main():
db = TradeDB()
today_str = "2026-08-07"
today_ts = datetime.datetime.strptime(today_str, "%Y-%m-%d").timestamp()
tables_to_check = [
"ws_price_validation", # 키움/KIS 체결가 검증
"ls_ws_ticks", # LS 체결 틱
"ws_orderbook", # 키움 실시간 호가
"kis_ws_orderbook", # KIS 실시간 호가
"ls_ws_orderbook" # LS 실시간 호가
]
results = {}
try:
for table in tables_to_check:
# 테이블 및 컬럼 존재 여부 확인 (SHOW COLUMNS)
try:
cols_raw = db.conn.execute(f"SHOW COLUMNS FROM {table}").fetchall()
cols = [dict(r)["Field"] for r in cols_raw]
except Exception as e:
results[table] = {"error": f"테이블/컬럼 조회 실패: {e}"}
continue
time_col = None
if "created_at" in cols:
time_col = "created_at"
elif "recv_ts" in cols:
time_col = "recv_ts"
elif "timestamp" in cols:
time_col = "timestamp"
elif "ts" in cols:
time_col = "ts"
if not time_col:
results[table] = {"error": f"시간 필드(created_at/recv_ts 등) 없음. (존재하는 컬럼: {cols})"}
continue
# PyMySQL 안전한 파라미터 바인딩 (%s) 사용
try:
if time_col == "created_at":
q = f"SELECT COUNT(*) as c FROM {table} WHERE {time_col} >= %s"
param = (f"{today_str} 00:00:00",)
else:
q = f"SELECT COUNT(*) as c FROM {table} WHERE {time_col} >= %s"
param = (today_ts,)
res = db.conn.execute(q, param).fetchone()
count = dict(res)["c"] if res else 0
results[table] = {"count": count, "time_col": time_col}
except Exception as e:
results[table] = {"error": f"조회 쿼리 실패: {e}"}
print("=== RESULT_START ===")
for table, data in results.items():
if "error" in data:
print(f"{table}|ERROR|{data['error']}")
else:
print(f"{table}|{data['count']}|{data['time_col']}")
print("=== RESULT_END ===")
finally:
db.close()
if __name__ == "__main__":
main()