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
bank_attention.py · 2.7 KB · 79 lines
| 1 | """system_bank_attention -- how many bank lines nothing has matched. |
| 2 | |
| 3 | COUNT {} -> {count, detail} |
| 4 | |
| 5 | `match_status: unmatched` is the reconciliation queue in one field: money |
| 6 | moved in the real world and this server has no book record that explains |
| 7 | it. `system_bank_matcher` has already had its go and either found nothing |
| 8 | or found nothing it was confident enough to accept, so what is left is |
| 9 | precisely the set a person has to look at. |
| 10 | |
| 11 | `suggested` is deliberately NOT counted. A suggestion is the machine |
| 12 | having done its job; the line is answerable in one click from /reconcile |
| 13 | and does not need the front page to say so. Only `unmatched` -- nothing |
| 14 | proposed, nobody has looked -- is a queue in the sense this layer means. |
| 15 | |
| 16 | The detail names how many have been sitting there long enough that |
| 17 | `system_bank_escalation` would already have raised them, because a line |
| 18 | unmatched this morning and a line unmatched since March are the same row |
| 19 | count and completely different problems. |
| 20 | |
| 21 | COUNT rather than GET or POST: the verb is the contract with the daemon's |
| 22 | attention pass. Nothing here writes. A missing collection reads as zero; |
| 23 | a genuine failure raises, so the rollup records it and keeps the last |
| 24 | count rather than reporting a reconciled bank that nobody reconciled. |
| 25 | """ |
| 26 | |
| 27 | import os |
| 28 | from datetime import date |
| 29 | |
| 30 | import object_records |
| 31 | |
| 32 | ACTOR = "system_bank_attention" |
| 33 | |
| 34 | # Nothing proposed and nobody has looked. `suggested` has a candidate, |
| 35 | # `matched` and `resolved` have an answer. |
| 36 | WAITING_STATUS = "unmatched" |
| 37 | |
| 38 | # The same silence window system_bank_escalation defaults to, so the two |
| 39 | # surfaces cannot disagree about what "stale" means on one deployment. |
| 40 | STALE_DAYS = 7 |
| 41 | |
| 42 | |
| 43 | def _base_dir(): |
| 44 | return os.environ.get("DBBASIC_DATA_DIR", "data") |
| 45 | |
| 46 | |
| 47 | def _text(value): |
| 48 | return str(value if value is not None else "").strip() |
| 49 | |
| 50 | |
| 51 | def _day(value): |
| 52 | try: |
| 53 | year, month, day = (int(part) for part in _text(value).split("-")) |
| 54 | return date(year, month, day) |
| 55 | except (ValueError, AttributeError): |
| 56 | # A line with no posting date still counts; it just says nothing |
| 57 | # about age. |
| 58 | return None |
| 59 | |
| 60 | |
| 61 | def COUNT(request): |
| 62 | try: |
| 63 | rows = object_records.read_collection_records("bank_lines", base_dir=_base_dir()) |
| 64 | except Exception: |
| 65 | return {"count": 0} |
| 66 | |
| 67 | today = date.today() |
| 68 | waiting = [row for row in rows |
| 69 | if _text(row.get("match_status") or WAITING_STATUS) == WAITING_STATUS] |
| 70 | if not waiting: |
| 71 | return {"count": 0} |
| 72 | |
| 73 | stale = 0 |
| 74 | for row in waiting: |
| 75 | posted = _day(row.get("posted_on")) |
| 76 | if posted is not None and (today - posted).days >= STALE_DAYS: |
| 77 | stale += 1 |
| 78 | detail = f"{stale} older than {STALE_DAYS} days" if stale else "" |
| 79 | return {"count": len(waiting), "detail": detail} |