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
dispute_attention.py · 2.7 KB · 77 lines
| 1 | """system_dispute_attention -- claims nobody has picked up. |
| 2 | |
| 3 | COUNT {} -> {count, detail} |
| 4 | |
| 5 | A dispute at `open` is a customer who has said something went wrong and |
| 6 | has heard nothing back. It is the only queue on this box where the thing |
| 7 | waiting is a person rather than a document: a short-received purchase |
| 8 | order waits patiently, a parcel on the returns bench waits patiently, and |
| 9 | a customer telling you their order never arrived is drafting a chargeback |
| 10 | while they wait. That is why the detail names the oldest one in days -- |
| 11 | three claims a day old and three claims a fortnight old are the same row |
| 12 | count and completely different mornings. |
| 13 | |
| 14 | `investigating` is deliberately NOT counted. Somebody has it; the number |
| 15 | exists to find the claims nobody has, and a count that also includes work |
| 16 | in progress can never reach zero, which is how a badge stops meaning |
| 17 | anything. The bench at /disputes shows the whole ladder, because the |
| 18 | person standing at it needs to see what is in flight as well as what is |
| 19 | untouched, and `status` is one click away in the filter bar. |
| 20 | |
| 21 | COUNT rather than GET or POST: the verb is the contract with the daemon's |
| 22 | attention pass. A missing collection reads as zero -- a server with no |
| 23 | disputes app has no disputes -- while a genuine failure raises, so the |
| 24 | rollup records it and keeps the last count rather than reporting a clear |
| 25 | queue nobody cleared. |
| 26 | """ |
| 27 | |
| 28 | import os |
| 29 | from datetime import datetime, timezone |
| 30 | |
| 31 | import object_records |
| 32 | |
| 33 | ACTOR = "system_dispute_attention" |
| 34 | |
| 35 | # Raised and untouched. `investigating` has a human on it, `resolved` and |
| 36 | # `withdrawn` are endings. |
| 37 | WAITING_STATUS = "open" |
| 38 | |
| 39 | |
| 40 | def _base_dir(): |
| 41 | return os.environ.get("DBBASIC_DATA_DIR", "data") |
| 42 | |
| 43 | |
| 44 | def _text(value): |
| 45 | return str(value if value is not None else "").strip() |
| 46 | |
| 47 | |
| 48 | def _age_days(value): |
| 49 | text = _text(value).replace("Z", "+00:00") |
| 50 | if not text: |
| 51 | return None |
| 52 | try: |
| 53 | stamp = datetime.fromisoformat(text) |
| 54 | except ValueError: |
| 55 | return None |
| 56 | if stamp.tzinfo is None: |
| 57 | stamp = stamp.replace(tzinfo=timezone.utc) |
| 58 | return (datetime.now(timezone.utc) - stamp).days |
| 59 | |
| 60 | |
| 61 | def COUNT(request): |
| 62 | try: |
| 63 | rows = object_records.read_collection_records("disputes", |
| 64 | base_dir=_base_dir()) |
| 65 | except Exception: |
| 66 | return {"count": 0} |
| 67 | |
| 68 | waiting = [row for row in rows |
| 69 | if _text(row.get("status")) == WAITING_STATUS] |
| 70 | if not waiting: |
| 71 | return {"count": 0} |
| 72 | |
| 73 | ages = [days for days in (_age_days(row.get("created_at")) |
| 74 | for row in waiting) if days is not None] |
| 75 | oldest = max(ages) if ages else 0 |
| 76 | detail = f"oldest waiting {oldest} days" if oldest else "" |
| 77 | return {"count": len(waiting), "detail": detail} |