81 lines
2.8 KiB
Python
81 lines
2.8 KiB
Python
#!/usr/bin/env python3
|
|
import json
|
|
import os
|
|
import sqlite3
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
DB = Path("/root/aza-app/data/stripe_webhook.sqlite")
|
|
PRACTICE = "prac_883ddc21fb6a"
|
|
SUB = "wc_sub_1502"
|
|
sys.path.insert(0, "/root/aza-app")
|
|
from aza_ai_budget import LicenseBudgetRow, check_allows_openai_call, compute_budget_snapshot
|
|
|
|
con = sqlite3.connect(str(DB))
|
|
row = con.execute(
|
|
"SELECT auto_topup_enabled, stripe_customer_id, default_payment_method_id, "
|
|
"trigger_below_percent, monthly_limit_chf, topup_amount_chf, internal_credit_usd "
|
|
"FROM ai_topup_settings WHERE practice_id=?",
|
|
(PRACTICE,),
|
|
).fetchone()
|
|
print("=== 1. ai_topup_settings ===")
|
|
print(
|
|
"enabled=", row[0],
|
|
"cus_ok=", str(row[1] or "").startswith("cus_"),
|
|
"pm_ok=", str(row[2] or "").startswith("pm_"),
|
|
"trigger=", row[3], "limit=", row[4], "chf=", row[5], "usd=", row[6],
|
|
)
|
|
print("\n=== 3. ledger ===")
|
|
for r in con.execute(
|
|
"SELECT event_type, status, COUNT(*), COALESCE(SUM(amount_paid_chf),0) "
|
|
"FROM ai_credit_ledger WHERE practice_id=? GROUP BY event_type, status",
|
|
(PRACTICE,),
|
|
):
|
|
print(" ", r)
|
|
auto24 = con.execute(
|
|
"SELECT COUNT(*) FROM ai_credit_ledger WHERE practice_id=? "
|
|
"AND event_type='auto_topup_purchase' AND status='succeeded' "
|
|
"AND created_at >= strftime('%s','now')-86400",
|
|
(PRACTICE,),
|
|
).fetchone()[0]
|
|
print("auto_topup_succeeded_last_24h=", auto24)
|
|
lic_row = con.execute(
|
|
"SELECT subscription_id, customer_email, customer_id, practice_id, lookup_key, status, "
|
|
"current_period_start, current_period_end FROM licenses WHERE subscription_id=? AND practice_id=?",
|
|
(SUB, PRACTICE),
|
|
).fetchone()
|
|
lic = LicenseBudgetRow(
|
|
subscription_id=lic_row[0],
|
|
customer_email=lic_row[1] or "",
|
|
customer_id=lic_row[2],
|
|
practice_id=lic_row[3],
|
|
lookup_key=lic_row[4],
|
|
status=lic_row[5] or "",
|
|
period_start=int(lic_row[6]),
|
|
period_end=int(lic_row[7]),
|
|
)
|
|
snap = compute_budget_snapshot(con, lic)
|
|
ok, _ = check_allows_openai_call(con, lic)
|
|
print("\n=== 2. budget ===")
|
|
print(json.dumps({
|
|
"monthly_budget": snap.get("budget_usd"),
|
|
"used_usd": snap.get("used_usd"),
|
|
"monthly_remaining": snap.get("remaining_usd"),
|
|
"extra_credit_remaining": snap.get("extra_credit_remaining_usd"),
|
|
"total_available": snap.get("total_available_usd"),
|
|
"available_percent": snap.get("available_percent"),
|
|
"allows_call": ok,
|
|
}, ensure_ascii=False))
|
|
print("\n=== 4. ENV ===")
|
|
for k in ["AZA_AI_TOPUP_DRY_RUN", "AZA_AI_TOPUP_ALLOW_LIVE", "AZA_AI_AUTO_TOPUP_ENABLED"]:
|
|
v = os.environ.get(k)
|
|
if v is None:
|
|
try:
|
|
with open("/root/aza-app/.env", encoding="utf-8") as f:
|
|
for line in f:
|
|
if line.startswith(k + "="):
|
|
v = line.strip().split("=", 1)[1].strip().strip('"').strip("'")
|
|
except OSError:
|
|
v = "?"
|
|
print(k, "=", v)
|