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
time_attention.py · 2.0 KB · 61 lines
| 1 | """system_time_attention -- how much logged time is waiting for an approver. |
| 2 | |
| 3 | COUNT {} -> {count, detail} |
| 4 | |
| 5 | `submitted` on a time log is a gate with a second person in it: the |
| 6 | approver is deliberately not the person who logged the hours, so this |
| 7 | queue can only ever be cleared by somebody who has no reason to know it |
| 8 | exists. That is exactly the shape of queue that quietly grows for a month |
| 9 | and then turns into an invoicing argument, and exactly what an attention |
| 10 | count is for. |
| 11 | |
| 12 | The detail carries hours rather than a second count, because "9 entries" |
| 13 | and "62 hours" are different amounts of urgency and only the second one |
| 14 | tells an approver how much of their afternoon this is. |
| 15 | |
| 16 | COUNT rather than GET or POST: the verb is the contract with the daemon's |
| 17 | attention pass. Nothing here writes. A missing collection reads as zero |
| 18 | (app-timers not installed here); a genuine failure raises, so the rollup |
| 19 | records an error and keeps the last count instead of reporting calm. |
| 20 | """ |
| 21 | |
| 22 | import os |
| 23 | |
| 24 | import object_records |
| 25 | |
| 26 | ACTOR = "system_time_attention" |
| 27 | |
| 28 | # draft is still being worked on, approved/rejected have had a decision, |
| 29 | # billed is on an invoice. `submitted` is the one that is waiting. |
| 30 | WAITING_STATUS = "submitted" |
| 31 | |
| 32 | |
| 33 | def _base_dir(): |
| 34 | return os.environ.get("DBBASIC_DATA_DIR", "data") |
| 35 | |
| 36 | |
| 37 | def _text(value): |
| 38 | return str(value if value is not None else "").strip() |
| 39 | |
| 40 | |
| 41 | def _seconds(value): |
| 42 | try: |
| 43 | return int(_text(value) or 0) |
| 44 | except ValueError: |
| 45 | # A hand-edited duration must not take the count down with it. |
| 46 | return 0 |
| 47 | |
| 48 | |
| 49 | def COUNT(request): |
| 50 | try: |
| 51 | rows = object_records.read_collection_records("time_logs", base_dir=_base_dir()) |
| 52 | except Exception: |
| 53 | return {"count": 0} |
| 54 | |
| 55 | waiting = [row for row in rows if _text(row.get("status")) == WAITING_STATUS] |
| 56 | if not waiting: |
| 57 | return {"count": 0} |
| 58 | |
| 59 | hours = sum(_seconds(row.get("duration_seconds")) for row in waiting) / 3600.0 |
| 60 | detail = f"{hours:.1f} hours awaiting approval" if hours else "" |
| 61 | return {"count": len(waiting), "detail": detail} |