feat(performance): 실적(쇼핑몰 통합 매출) 모듈 1단계 — 대시보드·설정·보안·어댑터 구조
- 네 영역 분리: ① 수집(adapters/ 쇼핑몰 10곳 + sync.py) ② 보안(security/ + credentials.py) ③ 계산·저장(calc.py + db.py) ④ 화면(dashboard.py, router.py, templates, performance.css/js) - 대시보드: 기간(오늘/이번주/이번달/올해/직접선택, 이전·다음), 쇼핑몰 필터, KPI 5개(진행 중 기간은 직전 기간 같은 경과일 대비), 일자×쇼핑몰 표(주간합계, 총매출/수수료/차감매출 전환), 일별 추이· 쇼핑몰 비중(SVG), 월 누적(비중)·연간 누적, 금액 클릭 → 주문 상세 - 설정(관리자): 사용/매출 집계, 수수료율 이력(적용 시작일), 쇼핑몰별 API 인증정보 입력·연결 테스트· 연결 상태, 마스터 키 상태·재암호화, 테스트(모의) 데이터 생성/삭제 - 인증정보: 설정 화면 입력 → AES-256-GCM(키 버전, AAD=쇼핑몰+항목) 암호화 저장, secret 원문은 화면으로 다시 보내지 않음, 오류·로그·raw 는 비밀값 가린 뒤 저장. 마스터 키는 APP_ENCRYPTION_KEY(.env) - 매출 기준일: 결제=결제일, 취소·반품=발생일. 금액 원 단위 정수, 수수료 반올림은 calc.commission 한 곳 - 동기화: 같은 쇼핑몰 동시 실행 차단(running 부분 유니크), 쇼핑몰별 실패 격리, 중복 방지 UNIQUE+UPSERT - 카페24 어댑터는 기존 app/integrations/cafe24 OAuth 토큰 재사용, 나머지는 입력칸만(수집은 다음 단계) - performance_db 초기화 스크립트, 권한키 performance(설정은 is_admin), 문서·.env.example Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,332 @@
|
||||
"""performance_db PostgreSQL 저장소.
|
||||
|
||||
- 드라이버: psycopg 3 (`psycopg[binary,pool]`) — 다른 모듈과 동일 패턴.
|
||||
- 연결 정보: 환경변수 `PERFORMANCE_DB_URL`
|
||||
(예: postgresql://performance_app:<pwd>@postgres-db:5432/performance_db)
|
||||
- 스키마는 앱이 만들지 않는다. `scripts/sql/performance_db_init.sql` 을 superuser 가 사전 적용.
|
||||
- 연결 풀은 lazy open — 부팅 시 DB 가 잠시 끊겨도 컨테이너가 죽지 않게.
|
||||
|
||||
이 저장소는 인증정보를 **암호문 그대로만** 읽고 쓴다. 암·복호화는 credentials.py 의 몫.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from datetime import date, datetime, timedelta
|
||||
from decimal import Decimal
|
||||
from typing import Any, Iterable
|
||||
|
||||
from psycopg.rows import dict_row
|
||||
from psycopg.types.json import Jsonb
|
||||
from psycopg_pool import ConnectionPool
|
||||
|
||||
from app.timezone import to_kst_iso
|
||||
|
||||
# running 상태로 이 시간 넘게 남은 동기화는 죽은 것으로 보고 정리한다(서버 재시작 등).
|
||||
STALE_RUN_AFTER = timedelta(minutes=30)
|
||||
|
||||
|
||||
def _iso(v: Any) -> Any:
|
||||
if isinstance(v, datetime):
|
||||
return to_kst_iso(v)
|
||||
if isinstance(v, date):
|
||||
return v.isoformat()
|
||||
return v
|
||||
|
||||
|
||||
class PerformanceDBStore:
|
||||
def __init__(self, dsn: str, *, min_size: int = 1, max_size: int = 5):
|
||||
self._pool = ConnectionPool(
|
||||
conninfo=dsn,
|
||||
min_size=min_size,
|
||||
max_size=max_size,
|
||||
kwargs={"row_factory": dict_row, "autocommit": True},
|
||||
open=False,
|
||||
)
|
||||
self._pool.open(wait=False)
|
||||
|
||||
def close(self) -> None:
|
||||
self._pool.close()
|
||||
|
||||
# ════════════════════════════════════════════════════════════
|
||||
# 쇼핑몰 기본 설정
|
||||
# ════════════════════════════════════════════════════════════
|
||||
def list_marketplaces(self) -> list[dict[str, Any]]:
|
||||
with self._pool.connection() as conn:
|
||||
rows = conn.execute("SELECT * FROM perf_marketplaces ORDER BY sort_order, code").fetchall()
|
||||
return [{k: _iso(v) for k, v in r.items()} for r in rows]
|
||||
|
||||
def update_marketplace(self, code: str, *, enabled: bool | None = None,
|
||||
include_in_sales: bool | None = None) -> dict[str, Any] | None:
|
||||
sets, params = [], []
|
||||
if enabled is not None:
|
||||
sets.append("enabled = %s")
|
||||
params.append(bool(enabled))
|
||||
if include_in_sales is not None:
|
||||
sets.append("include_in_sales = %s")
|
||||
params.append(bool(include_in_sales))
|
||||
if not sets:
|
||||
return None
|
||||
params.append(code)
|
||||
with self._pool.connection() as conn:
|
||||
row = conn.execute(
|
||||
f"UPDATE perf_marketplaces SET {', '.join(sets)} WHERE code = %s RETURNING *", params
|
||||
).fetchone()
|
||||
return {k: _iso(v) for k, v in row.items()} if row else None
|
||||
|
||||
# ════════════════════════════════════════════════════════════
|
||||
# 수수료율 이력
|
||||
# ════════════════════════════════════════════════════════════
|
||||
def rate_table(self) -> dict[str, list[tuple[date, Decimal]]]:
|
||||
with self._pool.connection() as conn:
|
||||
rows = conn.execute(
|
||||
"SELECT marketplace_code, effective_from, rate_percent FROM perf_commission_rates "
|
||||
"WHERE rule_type = 'marketplace' ORDER BY marketplace_code, effective_from"
|
||||
).fetchall()
|
||||
out: dict[str, list[tuple[date, Decimal]]] = {}
|
||||
for r in rows:
|
||||
out.setdefault(r["marketplace_code"], []).append((r["effective_from"], Decimal(r["rate_percent"])))
|
||||
return out
|
||||
|
||||
def list_rates(self, code: str) -> list[dict[str, Any]]:
|
||||
with self._pool.connection() as conn:
|
||||
rows = conn.execute(
|
||||
"SELECT id, rate_percent, effective_from, memo, created_by, created_at "
|
||||
"FROM perf_commission_rates WHERE marketplace_code = %s AND rule_type = 'marketplace' "
|
||||
"ORDER BY effective_from DESC",
|
||||
(code,),
|
||||
).fetchall()
|
||||
return [{k: (str(v) if isinstance(v, Decimal) else _iso(v)) for k, v in r.items()} for r in rows]
|
||||
|
||||
def set_rate(self, code: str, *, rate_percent: Decimal, effective_from: date,
|
||||
memo: str = "", user: str = "") -> None:
|
||||
"""같은 적용 시작일이면 덮어쓴다(오타 수정), 다르면 이력으로 추가."""
|
||||
with self._pool.connection() as conn:
|
||||
conn.execute(
|
||||
"""
|
||||
INSERT INTO perf_commission_rates
|
||||
(marketplace_code, rule_type, rate_percent, effective_from, memo, created_by)
|
||||
VALUES (%s, 'marketplace', %s, %s, %s, %s)
|
||||
ON CONFLICT (marketplace_code, rule_type, effective_from) DO UPDATE
|
||||
SET rate_percent = EXCLUDED.rate_percent, memo = EXCLUDED.memo,
|
||||
created_by = EXCLUDED.created_by, created_at = now()
|
||||
""",
|
||||
(code, rate_percent, effective_from, memo, user),
|
||||
)
|
||||
|
||||
def delete_rate(self, code: str, rate_id: int) -> bool:
|
||||
with self._pool.connection() as conn:
|
||||
cur = conn.execute(
|
||||
"DELETE FROM perf_commission_rates WHERE id = %s AND marketplace_code = %s", (rate_id, code)
|
||||
)
|
||||
return cur.rowcount > 0
|
||||
|
||||
# ════════════════════════════════════════════════════════════
|
||||
# 인증정보(암호문)
|
||||
# ════════════════════════════════════════════════════════════
|
||||
def credential_rows(self, code: str) -> dict[str, dict[str, Any]]:
|
||||
with self._pool.connection() as conn:
|
||||
rows = conn.execute(
|
||||
"SELECT credential_key, encrypted_value, key_version, updated_at, updated_by "
|
||||
"FROM perf_credentials WHERE marketplace_code = %s",
|
||||
(code,),
|
||||
).fetchall()
|
||||
return {r["credential_key"]: r for r in rows}
|
||||
|
||||
def credential_keys_by_code(self) -> dict[str, set[str]]:
|
||||
with self._pool.connection() as conn:
|
||||
rows = conn.execute("SELECT marketplace_code, credential_key FROM perf_credentials").fetchall()
|
||||
out: dict[str, set[str]] = {}
|
||||
for r in rows:
|
||||
out.setdefault(r["marketplace_code"], set()).add(r["credential_key"])
|
||||
return out
|
||||
|
||||
def upsert_credential(self, code: str, key: str, *, encrypted_value: str, key_version: int,
|
||||
user: str = "") -> None:
|
||||
with self._pool.connection() as conn:
|
||||
conn.execute(
|
||||
"""
|
||||
INSERT INTO perf_credentials (marketplace_code, credential_key, encrypted_value, key_version, updated_by)
|
||||
VALUES (%s, %s, %s, %s, %s)
|
||||
ON CONFLICT (marketplace_code, credential_key) DO UPDATE
|
||||
SET encrypted_value = EXCLUDED.encrypted_value, key_version = EXCLUDED.key_version,
|
||||
updated_by = EXCLUDED.updated_by
|
||||
""",
|
||||
(code, key, encrypted_value, key_version, user),
|
||||
)
|
||||
|
||||
def delete_credential(self, code: str, key: str) -> None:
|
||||
with self._pool.connection() as conn:
|
||||
conn.execute(
|
||||
"DELETE FROM perf_credentials WHERE marketplace_code = %s AND credential_key = %s", (code, key)
|
||||
)
|
||||
|
||||
def all_credential_rows(self) -> list[dict[str, Any]]:
|
||||
with self._pool.connection() as conn:
|
||||
return conn.execute(
|
||||
"SELECT marketplace_code, credential_key, encrypted_value, key_version FROM perf_credentials"
|
||||
).fetchall()
|
||||
|
||||
# ════════════════════════════════════════════════════════════
|
||||
# 연결 상태
|
||||
# ════════════════════════════════════════════════════════════
|
||||
def statuses(self) -> dict[str, dict[str, Any]]:
|
||||
with self._pool.connection() as conn:
|
||||
rows = conn.execute("SELECT * FROM perf_connection_status").fetchall()
|
||||
return {r["marketplace_code"]: {k: _iso(v) for k, v in r.items()} for r in rows}
|
||||
|
||||
def record_test(self, code: str, *, status: str, ok: bool, error: str = "") -> None:
|
||||
with self._pool.connection() as conn:
|
||||
conn.execute(
|
||||
"""
|
||||
INSERT INTO perf_connection_status (marketplace_code, status, last_test_at, last_test_ok,
|
||||
last_error, last_error_at)
|
||||
VALUES (%s, %s, now(), %s, %s, CASE WHEN %s = '' THEN NULL ELSE now() END)
|
||||
ON CONFLICT (marketplace_code) DO UPDATE
|
||||
SET status = EXCLUDED.status, last_test_at = now(), last_test_ok = EXCLUDED.last_test_ok,
|
||||
last_error = CASE WHEN EXCLUDED.last_error = '' THEN perf_connection_status.last_error
|
||||
ELSE EXCLUDED.last_error END,
|
||||
last_error_at = CASE WHEN EXCLUDED.last_error = '' THEN perf_connection_status.last_error_at
|
||||
ELSE now() END
|
||||
""",
|
||||
(code, status, ok, error, error),
|
||||
)
|
||||
|
||||
def record_sync(self, code: str, *, status: str, ok: bool, error: str = "") -> None:
|
||||
with self._pool.connection() as conn:
|
||||
conn.execute(
|
||||
"""
|
||||
INSERT INTO perf_connection_status (marketplace_code, status, last_sync_at,
|
||||
last_success_sync_at, last_error, last_error_at)
|
||||
VALUES (%s, %s, now(), CASE WHEN %s THEN now() END, %s, CASE WHEN %s = '' THEN NULL ELSE now() END)
|
||||
ON CONFLICT (marketplace_code) DO UPDATE
|
||||
SET status = EXCLUDED.status, last_sync_at = now(),
|
||||
last_success_sync_at = CASE WHEN %s THEN now()
|
||||
ELSE perf_connection_status.last_success_sync_at END,
|
||||
last_error = CASE WHEN EXCLUDED.last_error = '' THEN perf_connection_status.last_error
|
||||
ELSE EXCLUDED.last_error END,
|
||||
last_error_at = CASE WHEN EXCLUDED.last_error = '' THEN perf_connection_status.last_error_at
|
||||
ELSE now() END
|
||||
""",
|
||||
(code, status, ok, error, error, ok),
|
||||
)
|
||||
|
||||
# ════════════════════════════════════════════════════════════
|
||||
# 매출 사건
|
||||
# ════════════════════════════════════════════════════════════
|
||||
def upsert_events(self, code: str, events: Iterable[dict[str, Any]], *, is_sample: bool = False) -> int:
|
||||
"""같은 (쇼핑몰, 사건 종류, 외부 사건 ID)는 덮어쓴다 — 여러 번 동기화해도 중복 없음."""
|
||||
sql = """
|
||||
INSERT INTO perf_sales_events
|
||||
(marketplace_code, event_type, external_event_id, external_order_id, external_line_id,
|
||||
order_date, payment_date, event_date, sales_date, product_name, option_name, quantity,
|
||||
amount, discount_amount, order_status, raw_data, is_sample, synced_at)
|
||||
VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, now())
|
||||
ON CONFLICT (marketplace_code, event_type, external_event_id) DO UPDATE
|
||||
SET external_order_id = EXCLUDED.external_order_id, external_line_id = EXCLUDED.external_line_id,
|
||||
order_date = EXCLUDED.order_date, payment_date = EXCLUDED.payment_date,
|
||||
event_date = EXCLUDED.event_date, sales_date = EXCLUDED.sales_date,
|
||||
product_name = EXCLUDED.product_name, option_name = EXCLUDED.option_name,
|
||||
quantity = EXCLUDED.quantity, amount = EXCLUDED.amount,
|
||||
discount_amount = EXCLUDED.discount_amount, order_status = EXCLUDED.order_status,
|
||||
raw_data = EXCLUDED.raw_data, is_sample = EXCLUDED.is_sample, synced_at = now()
|
||||
"""
|
||||
rows = [
|
||||
(code, e["event_type"], e["external_event_id"], e["external_order_id"], e.get("external_line_id", ""),
|
||||
e.get("order_date"), e.get("payment_date"), e["event_date"], e["sales_date"],
|
||||
e.get("product_name", ""), e.get("option_name", ""), int(e.get("quantity") or 0),
|
||||
int(e["amount"]), int(e.get("discount_amount") or 0), e.get("order_status", ""),
|
||||
Jsonb(e.get("raw") or {}), is_sample)
|
||||
for e in events
|
||||
]
|
||||
if not rows:
|
||||
return 0
|
||||
with self._pool.connection() as conn:
|
||||
with conn.transaction():
|
||||
with conn.cursor() as cur:
|
||||
cur.executemany(sql, rows)
|
||||
return len(rows)
|
||||
|
||||
def daily_amounts(self, start: date, end: date, codes: list[str]) -> dict[tuple[date, str], int]:
|
||||
if not codes:
|
||||
return {}
|
||||
with self._pool.connection() as conn:
|
||||
rows = conn.execute(
|
||||
"SELECT sales_date, marketplace_code, SUM(amount)::BIGINT AS amount "
|
||||
"FROM perf_sales_events WHERE sales_date BETWEEN %s AND %s AND marketplace_code = ANY(%s) "
|
||||
"GROUP BY sales_date, marketplace_code",
|
||||
(start, end, codes),
|
||||
).fetchall()
|
||||
return {(r["sales_date"], r["marketplace_code"]): int(r["amount"] or 0) for r in rows}
|
||||
|
||||
def list_events(self, code: str, start: date, end: date, *, limit: int = 500) -> list[dict[str, Any]]:
|
||||
"""금액 클릭 상세(Drill-down). raw_data 는 보내지 않는다."""
|
||||
with self._pool.connection() as conn:
|
||||
rows = conn.execute(
|
||||
"SELECT id, event_type, external_order_id, external_line_id, order_date, payment_date, "
|
||||
" event_date, sales_date, product_name, option_name, quantity, amount, "
|
||||
" discount_amount, order_status, is_sample "
|
||||
"FROM perf_sales_events WHERE marketplace_code = %s AND sales_date BETWEEN %s AND %s "
|
||||
"ORDER BY sales_date, external_order_id, event_type LIMIT %s",
|
||||
(code, start, end, limit),
|
||||
).fetchall()
|
||||
return [{k: _iso(v) for k, v in r.items()} for r in rows]
|
||||
|
||||
def sample_count(self) -> int:
|
||||
with self._pool.connection() as conn:
|
||||
row = conn.execute("SELECT COUNT(*) AS n FROM perf_sales_events WHERE is_sample").fetchone()
|
||||
return int(row["n"] or 0)
|
||||
|
||||
def delete_sample_events(self) -> int:
|
||||
"""테스트 데이터만(is_sample) 지운다 — 조건이 있는 삭제."""
|
||||
with self._pool.connection() as conn:
|
||||
cur = conn.execute("DELETE FROM perf_sales_events WHERE is_sample = TRUE")
|
||||
return cur.rowcount
|
||||
|
||||
# ════════════════════════════════════════════════════════════
|
||||
# 동기화 실행(중복 실행 방지)
|
||||
# ════════════════════════════════════════════════════════════
|
||||
def start_run(self, code: str, *, trigger: str, range_from: date, range_to: date, user: str) -> int | None:
|
||||
"""running 행을 만든다. 이미 돌고 있으면 None(부분 유니크 인덱스로 막힘)."""
|
||||
import psycopg # noqa: WPS433
|
||||
|
||||
with self._pool.connection() as conn:
|
||||
conn.execute(
|
||||
"UPDATE perf_sync_runs SET status = 'failed', finished_at = now(), "
|
||||
"error = '중단됨(시간 초과로 정리)' "
|
||||
"WHERE marketplace_code = %s AND status = 'running' AND started_at < now() - %s",
|
||||
(code, STALE_RUN_AFTER),
|
||||
)
|
||||
try:
|
||||
row = conn.execute(
|
||||
"INSERT INTO perf_sync_runs (marketplace_code, trigger, range_from, range_to, started_by) "
|
||||
"VALUES (%s, %s, %s, %s, %s) RETURNING id",
|
||||
(code, trigger, range_from, range_to, user),
|
||||
).fetchone()
|
||||
except psycopg.errors.UniqueViolation:
|
||||
return None
|
||||
return int(row["id"])
|
||||
|
||||
def finish_run(self, run_id: int, *, ok: bool, fetched: int = 0, upserted: int = 0, error: str = "") -> None:
|
||||
with self._pool.connection() as conn:
|
||||
conn.execute(
|
||||
"UPDATE perf_sync_runs SET status = %s, fetched_count = %s, upserted_count = %s, "
|
||||
"error = %s, finished_at = now() WHERE id = %s",
|
||||
("success" if ok else "failed", fetched, upserted, error, run_id),
|
||||
)
|
||||
|
||||
def last_update_at(self) -> str | None:
|
||||
"""대시보드 "마지막 업데이트" — 성공한 동기화 중 가장 최근."""
|
||||
with self._pool.connection() as conn:
|
||||
row = conn.execute(
|
||||
"SELECT MAX(finished_at) AS t FROM perf_sync_runs WHERE status = 'success'"
|
||||
).fetchone()
|
||||
return _iso(row["t"]) if row and row["t"] else None
|
||||
|
||||
def running_codes(self) -> list[str]:
|
||||
with self._pool.connection() as conn:
|
||||
rows = conn.execute(
|
||||
"SELECT marketplace_code FROM perf_sync_runs WHERE status = 'running' "
|
||||
"AND started_at >= now() - %s",
|
||||
(STALE_RUN_AFTER,),
|
||||
).fetchall()
|
||||
return [r["marketplace_code"] for r in rows]
|
||||
Reference in New Issue
Block a user