Runs live, versioned Python application objects on one VM, with a suite of apps: projects, notes, tasks, files and more.

0 downloads

Download zip
Clone
Clone with git
git clone https://gitstable.com/git/askrobots/dbbasic-object-server.git
HTTPS
https://gitstable.com/git/askrobots/dbbasic-object-server.git
SSH
ssh://git@gitstable.com:2222/askrobots/dbbasic-object-server.git

Cloning needs no sign-in. Pushing over HTTPS asks for your username and an access token. SSH needs your key under SSH keys.

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
3COUNT {} -> {count, detail}
4
5The one queue a counter business cannot afford to discover late. Every
6other number on the attention band is work waiting; this one is a
7promise already broken, with somebody standing in a shop holding a phone
8that says their order was ready ten minutes ago. Severity `warning`
9rather than `normal` for exactly that: nobody chose to put these here and
10the clock is running in front of a customer.
11
12**Keyed on promised_at, not on fulfillment_method.** An order is late
13because a time passed, and the field that holds that time is the only
14thing this needs to read. Keying on the method instead would mean
15deciding here which methods make promises -- and the moment a delivery
16order or a counter order acquires a promised_at, a method-keyed count
17would silently ignore it while the customer waited. A shipping order
18never has a promised_at at all, so it can never appear in this count,
19which is the regression that matters and it holds by construction rather
20than by a filter somebody has to remember.
21
22Not-ready is the other half, and `ready` is where the clock stops rather
23than `collected`: the shop's obligation is to HAVE IT READY at the
24promised time. An order sitting on the shelf that the customer has not
25walked in for yet is not the shop's failure, and counting it would fill
26this queue with orders nobody can do anything about -- which is how a
27band stops being read.
28
29COUNT rather than GET or POST: the verb is the contract with the daemon's
30attention pass. A missing orders collection reads as zero; a genuine
31failure raises, so the rollup records it and keeps the last count rather
32than reporting a kitchen that is quietly on fire.
33"""
34
35import os
36from datetime import datetime
37
38import object_records
39
40ACTOR = "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.
46SETTLED_STATUSES = {"ready", "collected", "cancelled"}
47
48
49def _text(value):
50 return str(value if value is not None else "").strip()
51
52
53def _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
75def _minutes(delta):
76 return int(delta.total_seconds() // 60)
77
78
79def 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"}