askrobots / dbbasic-object-server
Public
Runs live, versioned Python application objects on one VM, with a suite of apps: projects, notes, tasks, files and more.
danq9
signup: the person chooses the password and name on the confirm page
83c7aa6
2 weeks, 5 days ago
History
usage_rollup.py · 4.6 KB · 123 lines
| 1 | """system_usage_rollup -- fold events into the buckets rating reads. |
| 2 | |
| 3 | POST {today?} -- walks unrated usage_events, groups them by |
| 4 | (subscription, metric, the subscription's current period), and writes or |
| 5 | updates the matching usage_summaries row, marking the events rated. |
| 6 | |
| 7 | Why a fold at all, when rating could sum events directly: a month of |
| 8 | per-request rows must not be part of every invoice render, and pricing |
| 9 | must not be welded to the write side's shape. With this bucket in |
| 10 | between, metering can start batching later and no pricing code changes |
| 11 | (plan/billing-metering-spec.md section 2). |
| 12 | |
| 13 | Idempotent by construction rather than by marker: an event is folded once |
| 14 | because folding sets rated=true, and a summary is updated in place by its |
| 15 | (subscription, metric, period) identity, so a re-run after a crash adds |
| 16 | nothing twice. Costs are summed alongside quantities, which is what makes |
| 17 | margin per customer per metric a read. |
| 18 | """ |
| 19 | |
| 20 | import os |
| 21 | from datetime import date |
| 22 | from decimal import Decimal, InvalidOperation |
| 23 | |
| 24 | import object_ids |
| 25 | import object_records |
| 26 | |
| 27 | ACTOR = "system_usage_rollup" |
| 28 | |
| 29 | |
| 30 | def _base_dir(): |
| 31 | return os.environ.get("DBBASIC_DATA_DIR", "data") |
| 32 | |
| 33 | |
| 34 | def _dec(value): |
| 35 | try: |
| 36 | return Decimal(str(value or "0")) |
| 37 | except InvalidOperation: |
| 38 | return Decimal(0) |
| 39 | |
| 40 | |
| 41 | def _int(value): |
| 42 | try: |
| 43 | return int(str(value or "0").strip() or 0) |
| 44 | except (TypeError, ValueError): |
| 45 | return 0 |
| 46 | |
| 47 | |
| 48 | def _truthy(value): |
| 49 | return str(value or "").strip().lower() in ("true", "1", "yes", "on") |
| 50 | |
| 51 | |
| 52 | def POST(request): |
| 53 | base = _base_dir() |
| 54 | today = str(request.get("today") or date.today().isoformat()) |
| 55 | |
| 56 | try: |
| 57 | events = object_records.read_collection_records("usage_events", base_dir=base) |
| 58 | except Exception: |
| 59 | return {"ok": True, "skipped": "metering not installed (usage_events absent)"} |
| 60 | try: |
| 61 | subs = {s["id"]: s for s in |
| 62 | object_records.read_collection_records("subscriptions", base_dir=base)} |
| 63 | except Exception: |
| 64 | subs = {} |
| 65 | try: |
| 66 | summaries = object_records.read_collection_records("usage_summaries", base_dir=base) |
| 67 | except Exception: |
| 68 | summaries = [] |
| 69 | |
| 70 | existing = {(s.get("subscription_id"), s.get("metric"), |
| 71 | s.get("period_start"), s.get("period_end")): s for s in summaries} |
| 72 | |
| 73 | buckets = {} |
| 74 | folded = 0 |
| 75 | orphaned = 0 |
| 76 | for event in events: |
| 77 | if _truthy(event.get("rated")): |
| 78 | continue |
| 79 | sub = subs.get(event.get("subscription_id")) |
| 80 | if sub is None: |
| 81 | # Usage with no subscription cannot be priced; leave it |
| 82 | # UNRATED and visible rather than folding it somewhere wrong. |
| 83 | orphaned += 1 |
| 84 | continue |
| 85 | key = (sub["id"], str(event.get("metric") or ""), |
| 86 | str(sub.get("current_period_start") or "")[:10], |
| 87 | str(sub.get("current_period_end") or "")[:10]) |
| 88 | entry = buckets.setdefault(key, {"quantity": Decimal(0), "cost": 0, |
| 89 | "count": 0, "events": [], |
| 90 | "owner_id": sub.get("owner_id", "")}) |
| 91 | entry["quantity"] += _dec(event.get("quantity")) |
| 92 | entry["cost"] += _int(event.get("cost_minor")) |
| 93 | entry["count"] += 1 |
| 94 | entry["events"].append(event["id"]) |
| 95 | folded += 1 |
| 96 | |
| 97 | written = 0 |
| 98 | for (sub_id, metric, start, end), entry in sorted(buckets.items(), key=lambda kv: str(kv[0])): |
| 99 | row = existing.get((sub_id, metric, start, end)) |
| 100 | if row is not None: |
| 101 | object_records.update_collection_record( |
| 102 | "usage_summaries", row["id"], |
| 103 | {"quantity": str(_dec(row.get("quantity")) + entry["quantity"]), |
| 104 | "event_count": str(_int(row.get("event_count")) + entry["count"]), |
| 105 | "cost_minor": str(_int(row.get("cost_minor")) + entry["cost"])}, |
| 106 | base_dir=base, actor=ACTOR) |
| 107 | else: |
| 108 | object_records.create_collection_record( |
| 109 | "usage_summaries", |
| 110 | {"id": object_ids.new_uuid4(), "subscription_id": sub_id, |
| 111 | "metric": metric, "period_start": start, "period_end": end, |
| 112 | "quantity": str(entry["quantity"]), "event_count": str(entry["count"]), |
| 113 | "cost_minor": str(entry["cost"]), "invoiced": "false", |
| 114 | "owner_id": entry["owner_id"]}, |
| 115 | base_dir=base, actor=ACTOR) |
| 116 | written += 1 |
| 117 | for event_id in entry["events"]: |
| 118 | object_records.update_collection_record( |
| 119 | "usage_events", event_id, {"rated": "true"}, |
| 120 | base_dir=base, actor=ACTOR) |
| 121 | |
| 122 | return {"ok": True, "today": today, "events_folded": folded, |
| 123 | "summaries_written": written, "orphaned_events": orphaned} |