Spaces:
Sleeping
Sleeping
File size: 9,178 Bytes
e704e6c 5b71488 e704e6c 5b71488 e704e6c a013512 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 | """Credit ledger.
Credits live in lots (monthly allowance, top-up pack, grant, refund).
A lot that expires earlier is used first.
reserve() debits the lots immediately and tracks origin lots.
settle() charges the price of completed work and returns the remainder to the same lots.
A reservation that is not settled expires and is returned in full when hold_seconds passes.
Every movement is logged in `entries`.
"""
from __future__ import annotations
import json
import uuid
from typing import Any
from .errors import ApiError
from .plans import Config
MAX_OPEN_HOLDS = 20 # unsettled reservations an account may have at once
def _new_lot(
conn: Any,
account_id: str,
kind: str,
amount: int,
now: int,
expires_at: int | None,
source: str | None,
) -> int | None:
cur = conn.execute(
"INSERT OR IGNORE INTO lots(account_id, kind, amount, remaining, expires_at, source,"
" created_at) VALUES (?,?,?,?,?,?,?)",
(account_id, kind, amount, amount, expires_at, source, now),
)
return cur.lastrowid if cur.rowcount else None
def _entry(conn: Any, account_id: str, now: int, kind: str, amount: int, ref: str):
conn.execute(
"INSERT INTO entries(account_id, ts, kind, amount, ref) VALUES (?,?,?,?,?)",
(account_id, now, kind, amount, ref),
)
def grant(
conn: Any,
account_id: str,
kind: str,
amount: int,
now: int,
expires_at: int | None = None,
source: str | None = None,
) -> bool:
"""Add credits. A source (e.g. 'plan:<licence>:<period>') makes the grant happen only once."""
if amount <= 0 or (expires_at is not None and expires_at <= now):
return False
lot = _new_lot(conn, account_id, kind, amount, now, expires_at, source)
if lot is None:
return False
_entry(conn, account_id, now, "grant", amount, f"lot:{lot}")
return True
def balance(conn: Any, account_id: str, now: int) -> int:
row = conn.execute(
"SELECT COALESCE(SUM(remaining), 0) AS total FROM lots"
" WHERE account_id=? AND (expires_at IS NULL OR expires_at>?)",
(account_id, now),
).fetchone()
if row is None:
return 0
return int(row["total"] if isinstance(row, dict) else row[0])
def _give_back(conn: Any, res: Any, amount: int, now: int, cfg: Config):
"""Return `amount` of a reservation to the lots it came from, the last one taken first."""
left = amount
alloc_list = json.loads(res["alloc"]) if isinstance(res["alloc"], str) else res["alloc"]
for lot_id, taken in reversed(alloc_list):
if left <= 0:
break
part = min(taken, left)
lot = conn.execute("SELECT expires_at FROM lots WHERE id=?", (lot_id,)).fetchone()
if lot and (lot["expires_at"] is None or lot["expires_at"] > now):
conn.execute("UPDATE lots SET remaining = remaining + ? WHERE id=?", (part, lot_id))
else:
# Lot expired in the meantime: customer gets credits back with refund shelf life
expires = now + cfg.refund_days * 86400
_new_lot(conn, res["account_id"], "refund", part, now, expires, None)
left -= part
_entry(conn, res["account_id"], now, "release", amount, f"res:{res['id']}")
def sweep(conn: Any, account_id: str, now: int, cfg: Config) -> None:
"""Return holds that timed out and drop lots that expired."""
stale = conn.execute(
"SELECT * FROM reservations WHERE account_id=? AND state='held' AND expires_at<=?",
(account_id, now),
).fetchall()
for res in stale:
_give_back(conn, res, res["cost"], now, cfg)
conn.execute(
"UPDATE reservations SET state='expired', settled_at=? WHERE id=?", (now, res["id"])
)
old = conn.execute(
"SELECT id, remaining FROM lots WHERE account_id=? AND remaining>0"
" AND expires_at IS NOT NULL AND expires_at<=?",
(account_id, now),
).fetchall()
for lot in old:
conn.execute("UPDATE lots SET remaining=0 WHERE id=?", (lot["id"],))
_entry(conn, account_id, now, "expire", -lot["remaining"], f"lot:{lot['id']}")
def _view(conn: Any, res: Any, now: int) -> dict:
out = {
"id": res["id"],
"state": res["state"],
"cost": res["cost"],
"balance": balance(conn, res["account_id"], now),
}
if res["state"] == "held":
out["expires_at"] = res["expires_at"]
if res["state"] == "settled":
out["charged"] = res["charged"]
out["refunded"] = res["cost"] - (res["charged"] or 0)
return out
def reserve(
conn: Any,
account_id: str,
ops: list[str],
idem: str,
now: int,
cfg: Config,
) -> dict:
prices = {}
for op in ops:
if op not in cfg.costs:
raise ApiError(400, "unknown_op", f"unknown operation '{op}'")
prices[op] = cfg.costs[op]
if not prices or len(prices) != len(ops):
raise ApiError(400, "bad_request", "ops must be a non-empty list of distinct names")
sweep(conn, account_id, now, cfg)
again = conn.execute(
"SELECT * FROM reservations WHERE account_id=? AND idem=?", (account_id, idem)
).fetchone()
if again is not None:
ops_recorded = json.loads(again["ops"]) if isinstance(again["ops"], str) else again["ops"]
if set(ops_recorded) != set(ops):
raise ApiError(409, "idempotency_conflict", "that key was used for other operations")
return _view(conn, again, now)
cost = sum(prices.values())
open_holds = conn.execute(
"SELECT COUNT(*) AS n FROM reservations WHERE account_id=? AND state='held'", (account_id,)
).fetchone()
if int(open_holds["n"] if isinstance(open_holds, dict) else open_holds[0]) >= MAX_OPEN_HOLDS:
raise ApiError(429, "too_many_holds", "settle or wait for open reservations before reserving more")
have = balance(conn, account_id, now)
if have < cost:
raise ApiError(
402,
"insufficient_credits",
f"needs {cost} credits, balance is {have}",
cost=cost,
balance=have,
)
alloc, left = [], cost
lots = conn.execute(
"SELECT id, remaining FROM lots WHERE account_id=? AND remaining>0"
" AND (expires_at IS NULL OR expires_at>?) ORDER BY expires_at IS NULL, expires_at, id",
(account_id, now),
).fetchall()
for lot in lots:
part = min(lot["remaining"], left)
conn.execute("UPDATE lots SET remaining = remaining - ? WHERE id=?", (part, lot["id"]))
alloc.append([lot["id"], part])
left -= part
if not left:
break
rid = uuid.uuid4().hex
conn.execute(
"INSERT INTO reservations(id, account_id, idem, state, ops, cost, alloc, created_at,"
" expires_at) VALUES (?,?,?,?,?,?,?,?,?)",
(
rid,
account_id,
idem,
"held",
json.dumps(prices),
cost,
json.dumps(alloc),
now,
now + cfg.hold_seconds,
),
)
_entry(conn, account_id, now, "hold", -cost, f"res:{rid}")
return {
"id": rid,
"state": "held",
"cost": cost,
"balance": have - cost,
"expires_at": now + cfg.hold_seconds,
}
def settle(
conn: Any, account_id: str, rid: str, done: list[str], now: int, cfg: Config
) -> dict:
"""Charge for the operations in `done` (those that were delivered) and return the rest."""
sweep(conn, account_id, now, cfg)
res = conn.execute(
"SELECT * FROM reservations WHERE id=? AND account_id=?", (rid, account_id)
).fetchone()
if res is None:
raise ApiError(404, "unknown_reservation", "no such reservation")
prices = json.loads(res["ops"]) if isinstance(res["ops"], str) else res["ops"]
if len(set(done)) != len(done) or not set(done) <= set(prices):
raise ApiError(400, "bad_request", "done must be distinct operations that were reserved")
if res["state"] == "expired":
raise ApiError(409, "reservation_expired", "the hold timed out; the credits came back")
if res["state"] == "settled":
done_recorded = json.loads(res["done"]) if isinstance(res["done"], str) else res["done"]
if sorted(done_recorded) != sorted(done):
raise ApiError(409, "already_settled", "this reservation was settled differently")
return _view(conn, res, now)
charge = sum(prices[op] for op in done)
if res["cost"] - charge:
_give_back(conn, res, res["cost"] - charge, now, cfg)
conn.execute(
"UPDATE reservations SET state='settled', done=?, charged=?, settled_at=? WHERE id=?",
(json.dumps(done), charge, now, rid),
)
res = conn.execute("SELECT * FROM reservations WHERE id=?", (rid,)).fetchone()
return _view(conn, res, now)
def history(conn: Any, account_id: str, limit: int = 20) -> list[dict]:
rows = conn.execute(
"SELECT ts, kind, amount, ref FROM entries WHERE account_id=? ORDER BY id DESC LIMIT ?",
(account_id, limit),
).fetchall()
return [dict(r) for r in rows]
|