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
scan_attention.py · 3.0 KB · 84 lines
| 1 | """system_scan_attention -- how many receipts are waiting for a person. |
| 2 | |
| 3 | COUNT {} -> {count, detail} |
| 4 | |
| 5 | A scan at `extracted` is the whole intake pipeline's honest stopping |
| 6 | point: the OCR ran, the extractor produced fields, and the machine |
| 7 | declined to turn somebody's money into an expense record on its own. That |
| 8 | is a waiting room with a person's name on it, and until now nothing said |
| 9 | so out loud -- `system_scan_processor` has been returning a key literally |
| 10 | called `needing_a_human` since the day it was written, and no surface has |
| 11 | ever read it. This object is that number, declared in app-intake's |
| 12 | manifest so the home page can fold it without knowing anything about |
| 13 | scans. |
| 14 | |
| 15 | COUNT rather than GET or POST because the verb is the contract: the |
| 16 | daemon's attention pass calls COUNT, and a provider stays free to expose |
| 17 | the other methods to a human without either use accidentally answering |
| 18 | the other. Nothing here writes. |
| 19 | |
| 20 | Degrades to zero when the collection is absent (app-intake schema not |
| 21 | installed on this deployment) rather than raising, because the pass |
| 22 | should not log an error every five minutes about an app nobody installed. |
| 23 | A provider that genuinely FAILS is a different thing and does raise -- |
| 24 | the rollup records that as an error and keeps the last count, so a broken |
| 25 | counter looks broken instead of looking calm. |
| 26 | """ |
| 27 | |
| 28 | import os |
| 29 | from datetime import datetime, timezone |
| 30 | |
| 31 | import object_records |
| 32 | |
| 33 | ACTOR = "system_scan_attention" |
| 34 | |
| 35 | # The one status that means "a machine finished and a human has not". |
| 36 | # pending/processing are the pipeline's own business; confirmed, ignored |
| 37 | # and error have all had a decision made about them. |
| 38 | WAITING_STATUS = "extracted" |
| 39 | |
| 40 | # A receipt nobody has confirmed within a week is not just queued, it is |
| 41 | # aging -- and the aging is the sentence a bare number cannot say. |
| 42 | STALE_DAYS = 7 |
| 43 | |
| 44 | |
| 45 | def _base_dir(): |
| 46 | return os.environ.get("DBBASIC_DATA_DIR", "data") |
| 47 | |
| 48 | |
| 49 | def _text(value): |
| 50 | return str(value if value is not None else "").strip() |
| 51 | |
| 52 | |
| 53 | def _age_days(value): |
| 54 | """Days since an ISO timestamp, or None when it will not parse. |
| 55 | |
| 56 | A hand-edited or empty created_at must not take the count down with |
| 57 | it: the row still counts, it just contributes nothing to the detail. |
| 58 | """ |
| 59 | text = _text(value).replace("Z", "+00:00") |
| 60 | if not text: |
| 61 | return None |
| 62 | try: |
| 63 | stamp = datetime.fromisoformat(text) |
| 64 | except ValueError: |
| 65 | return None |
| 66 | if stamp.tzinfo is None: |
| 67 | stamp = stamp.replace(tzinfo=timezone.utc) |
| 68 | return (datetime.now(timezone.utc) - stamp).days |
| 69 | |
| 70 | |
| 71 | def COUNT(request): |
| 72 | try: |
| 73 | rows = object_records.read_collection_records("scans", base_dir=_base_dir()) |
| 74 | except Exception: |
| 75 | return {"count": 0} |
| 76 | |
| 77 | waiting = [row for row in rows if _text(row.get("status")) == WAITING_STATUS] |
| 78 | if not waiting: |
| 79 | return {"count": 0} |
| 80 | |
| 81 | stale = sum(1 for row in waiting |
| 82 | if (_age_days(row.get("created_at")) or 0) >= STALE_DAYS) |
| 83 | detail = f"{stale} over a week old" if stale else "" |
| 84 | return {"count": len(waiting), "detail": detail} |