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
backorder_attention.py · 2.3 KB · 70 lines
| 1 | """system_backorder_attention -- how many people are waiting on stock. |
| 2 | |
| 3 | COUNT {} -> {count, detail} |
| 4 | |
| 5 | The open backorder rows: goods a customer has already been sold and not |
| 6 | yet been sent, plus the interest recorded for people the shop refused and |
| 7 | promised to tell. `filled` and `cancelled` have both had an answer. |
| 8 | |
| 9 | Severity `warning` rather than `normal` in the manifest, and the argument |
| 10 | is the one system_shipment_attention makes about a stuck parcel: nobody |
| 11 | chose to be in this queue, and the clock is running somewhere else -- in |
| 12 | this case in the inbox of somebody who has already paid. It is not |
| 13 | `urgent`, which is reserved for the server saying it stopped doing its |
| 14 | own work. |
| 15 | |
| 16 | The detail names the OLDEST wait rather than a total, because that is the |
| 17 | row somebody should ring first: "9 waiting" is a number, "9 waiting, the |
| 18 | oldest 21 days" is a reason to open the page. |
| 19 | |
| 20 | A row whose requested_on will not parse is counted but contributes no |
| 21 | age. An unreadable date must not silently become evidence that somebody |
| 22 | has been waiting no time at all, and must not take the whole count down |
| 23 | with it either. |
| 24 | |
| 25 | Degrades to zero when backorders is absent, the same posture every other |
| 26 | provider on this box takes: a rollup pass should not log an error every |
| 27 | five minutes about a collection nobody installed. |
| 28 | """ |
| 29 | |
| 30 | import os |
| 31 | from datetime import date |
| 32 | |
| 33 | import object_records |
| 34 | |
| 35 | ACTOR = "system_backorder_attention" |
| 36 | |
| 37 | WAITING_STATUS = "open" |
| 38 | |
| 39 | |
| 40 | def _text(value): |
| 41 | return str(value if value is not None else "").strip() |
| 42 | |
| 43 | |
| 44 | def _days_since(value, today): |
| 45 | try: |
| 46 | return (today - date.fromisoformat(_text(value))).days |
| 47 | except ValueError: |
| 48 | return None |
| 49 | |
| 50 | |
| 51 | def COUNT(request): |
| 52 | base = os.environ.get("DBBASIC_DATA_DIR", "data") |
| 53 | try: |
| 54 | rows = object_records.read_collection_records("backorders", |
| 55 | base_dir=base) |
| 56 | except Exception: |
| 57 | return {"count": 0} |
| 58 | |
| 59 | waiting = [row for row in rows |
| 60 | if _text(row.get("status")) == WAITING_STATUS] |
| 61 | if not waiting: |
| 62 | return {"count": 0} |
| 63 | |
| 64 | today = date.today() |
| 65 | ages = [age for age in (_days_since(row.get("requested_on"), today) |
| 66 | for row in waiting) |
| 67 | if age is not None and age >= 0] |
| 68 | if not ages: |
| 69 | return {"count": len(waiting)} |
| 70 | return {"count": len(waiting), "detail": f"oldest waiting {max(ages)} days"} |