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
pickup_attention.py · 3.7 KB · 100 lines
| 1 | """system_pickup_attention -- orders past the time we promised them. |
| 2 | |
| 3 | COUNT {} -> {count, detail} |
| 4 | |
| 5 | The one queue a counter business cannot afford to discover late. Every |
| 6 | other number on the attention band is work waiting; this one is a |
| 7 | promise already broken, with somebody standing in a shop holding a phone |
| 8 | that says their order was ready ten minutes ago. Severity `warning` |
| 9 | rather than `normal` for exactly that: nobody chose to put these here and |
| 10 | the clock is running in front of a customer. |
| 11 | |
| 12 | **Keyed on promised_at, not on fulfillment_method.** An order is late |
| 13 | because a time passed, and the field that holds that time is the only |
| 14 | thing this needs to read. Keying on the method instead would mean |
| 15 | deciding here which methods make promises -- and the moment a delivery |
| 16 | order or a counter order acquires a promised_at, a method-keyed count |
| 17 | would silently ignore it while the customer waited. A shipping order |
| 18 | never has a promised_at at all, so it can never appear in this count, |
| 19 | which is the regression that matters and it holds by construction rather |
| 20 | than by a filter somebody has to remember. |
| 21 | |
| 22 | Not-ready is the other half, and `ready` is where the clock stops rather |
| 23 | than `collected`: the shop's obligation is to HAVE IT READY at the |
| 24 | promised time. An order sitting on the shelf that the customer has not |
| 25 | walked in for yet is not the shop's failure, and counting it would fill |
| 26 | this queue with orders nobody can do anything about -- which is how a |
| 27 | band stops being read. |
| 28 | |
| 29 | COUNT rather than GET or POST: the verb is the contract with the daemon's |
| 30 | attention pass. A missing orders collection reads as zero; a genuine |
| 31 | failure raises, so the rollup records it and keeps the last count rather |
| 32 | than reporting a kitchen that is quietly on fire. |
| 33 | """ |
| 34 | |
| 35 | import os |
| 36 | from datetime import datetime |
| 37 | |
| 38 | import object_records |
| 39 | |
| 40 | ACTOR = "system_pickup_attention" |
| 41 | |
| 42 | # The promise is discharged at `ready` -- see the module docstring. The |
| 43 | # other three are orders that will never be ready and must not be counted |
| 44 | # as late: two because they already happened, one because it was called |
| 45 | # off. |
| 46 | SETTLED_STATUSES = {"ready", "collected", "cancelled"} |
| 47 | |
| 48 | |
| 49 | def _text(value): |
| 50 | return str(value if value is not None else "").strip() |
| 51 | |
| 52 | |
| 53 | def _moment(value): |
| 54 | """An ISO datetime as NAIVE local wall-clock, or None. |
| 55 | |
| 56 | Times in this repo are the shop's own clock (order_date, shipped_on, |
| 57 | pickup_slots.starts_at), but a hand-typed or imported value may |
| 58 | carry an offset -- and comparing an aware datetime to a naive one |
| 59 | raises, which would turn one badly-typed order into a permanently |
| 60 | errored attention count for the whole shop. Converting to local and |
| 61 | dropping the offset keeps that one row honest and the queue alive. |
| 62 | """ |
| 63 | text = _text(value) |
| 64 | if not text: |
| 65 | return None |
| 66 | try: |
| 67 | moment = datetime.fromisoformat(text.replace("Z", "+00:00")) |
| 68 | except ValueError: |
| 69 | return None |
| 70 | if moment.tzinfo is not None: |
| 71 | moment = moment.astimezone().replace(tzinfo=None) |
| 72 | return moment |
| 73 | |
| 74 | |
| 75 | def _minutes(delta): |
| 76 | return int(delta.total_seconds() // 60) |
| 77 | |
| 78 | |
| 79 | def COUNT(request): |
| 80 | base = os.environ.get("DBBASIC_DATA_DIR", "data") |
| 81 | try: |
| 82 | orders = object_records.read_collection_records("orders", base_dir=base) |
| 83 | except Exception: |
| 84 | return {"count": 0} |
| 85 | |
| 86 | now = datetime.now() |
| 87 | late = [] |
| 88 | for order in orders: |
| 89 | if _text(order.get("status")) in SETTLED_STATUSES: |
| 90 | continue |
| 91 | promised = _moment(order.get("promised_at")) |
| 92 | if promised is None or promised >= now: |
| 93 | continue |
| 94 | late.append(promised) |
| 95 | |
| 96 | if not late: |
| 97 | return {"count": 0} |
| 98 | worst = _minutes(now - min(late)) |
| 99 | return {"count": len(late), |
| 100 | "detail": f"oldest {worst} minutes past its promised time"} |