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
scan_attention.py · 3.0 KB · 84 lines
1"""system_scan_attention -- how many receipts are waiting for a person.
2
3COUNT {} -> {count, detail}
4
5A scan at `extracted` is the whole intake pipeline's honest stopping
6point: the OCR ran, the extractor produced fields, and the machine
7declined to turn somebody's money into an expense record on its own. That
8is a waiting room with a person's name on it, and until now nothing said
9so out loud -- `system_scan_processor` has been returning a key literally
10called `needing_a_human` since the day it was written, and no surface has
11ever read it. This object is that number, declared in app-intake's
12manifest so the home page can fold it without knowing anything about
13scans.
14
15COUNT rather than GET or POST because the verb is the contract: the
16daemon's attention pass calls COUNT, and a provider stays free to expose
17the other methods to a human without either use accidentally answering
18the other. Nothing here writes.
19
20Degrades to zero when the collection is absent (app-intake schema not
21installed on this deployment) rather than raising, because the pass
22should not log an error every five minutes about an app nobody installed.
23A provider that genuinely FAILS is a different thing and does raise --
24the rollup records that as an error and keeps the last count, so a broken
25counter looks broken instead of looking calm.
26"""
27
28import os
29from datetime import datetime, timezone
30
31import object_records
32
33ACTOR = "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.
38WAITING_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.
42STALE_DAYS = 7
43
44
45def _base_dir():
46 return os.environ.get("DBBASIC_DATA_DIR", "data")
47
48
49def _text(value):
50 return str(value if value is not None else "").strip()
51
52
53def _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
71def 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}