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
reorder_attention.py · 2.2 KB · 67 lines
| 1 | """system_reorder_attention -- how many shelves are waiting on a decision. |
| 2 | |
| 3 | COUNT {} -> {count, detail} |
| 4 | |
| 5 | The open reorder suggestions, and nothing else. `ordered` means somebody |
| 6 | went and bought some; `dismissed` means somebody looked and said no, which |
| 7 | is a real answer and has to be available -- a count that could not be |
| 8 | dismissed would stay permanently lit and teach people to stop reading the |
| 9 | whole band it sits in. |
| 10 | |
| 11 | Severity `warning` rather than `normal` in the manifest: unlike a receipt |
| 12 | sitting in a queue somebody put it in, nobody chose to be here, and the |
| 13 | consequence of ignoring it is running out of something a customer wants |
| 14 | to buy. It is not `urgent` -- that is reserved for the server reporting |
| 15 | that it stopped doing its own work, and a low shelf is a business problem |
| 16 | rather than a broken machine. |
| 17 | |
| 18 | The detail names the WORST one rather than a total, because "12 products |
| 19 | to reorder" is a number and "12 products to reorder, one is 6 below its |
| 20 | point" is a reason to open the page. |
| 21 | |
| 22 | Degrades to zero when reorder_suggestions is absent rather than raising, |
| 23 | the same posture system_shipment_attention and system_scan_attention |
| 24 | take: a rollup pass should not log an error every five minutes about a |
| 25 | collection nobody installed. |
| 26 | """ |
| 27 | |
| 28 | import os |
| 29 | |
| 30 | import object_records |
| 31 | |
| 32 | ACTOR = "system_reorder_attention" |
| 33 | |
| 34 | WAITING_STATUS = "open" |
| 35 | |
| 36 | |
| 37 | def _text(value): |
| 38 | return str(value if value is not None else "").strip() |
| 39 | |
| 40 | |
| 41 | def _number(value): |
| 42 | try: |
| 43 | return float(_text(value) or 0) |
| 44 | except (TypeError, ValueError): |
| 45 | return 0.0 |
| 46 | |
| 47 | |
| 48 | def COUNT(request): |
| 49 | base = os.environ.get("DBBASIC_DATA_DIR", "data") |
| 50 | try: |
| 51 | rows = object_records.read_collection_records("reorder_suggestions", |
| 52 | base_dir=base) |
| 53 | except Exception: |
| 54 | return {"count": 0} |
| 55 | |
| 56 | waiting = [row for row in rows |
| 57 | if _text(row.get("status")) == WAITING_STATUS] |
| 58 | if not waiting: |
| 59 | return {"count": 0} |
| 60 | |
| 61 | gaps = [_number(row.get("reorder_point")) - _number(row.get("on_hand")) |
| 62 | for row in waiting] |
| 63 | worst = max(gaps) |
| 64 | if worst <= 0: |
| 65 | return {"count": len(waiting)} |
| 66 | return {"count": len(waiting), |
| 67 | "detail": f"one is {worst:g} below its point"} |