-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathdaily_digest.py
More file actions
195 lines (167 loc) · 6.33 KB
/
daily_digest.py
File metadata and controls
195 lines (167 loc) · 6.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#!/usr/bin/env python3
"""daily_digest.py — Standalone task digest for sqlite-memory-mcp.
Reads tasks from the SQLite memory DB and prints a markdown digest to stdout.
Usage:
python3 daily_digest.py
python3 daily_digest.py --db ~/.claude/memory/memory.db
python3 daily_digest.py --sections today,inbox,next,waiting
python3 daily_digest.py --no-overdue --limit 30
"""
import argparse
import os
import sys
from db_utils import DB_PATH as DEFAULT_DB
from db_utils import TASK_ACTIVE_EXCLUSIONS, build_priority_order_sql, get_conn, now_iso
# Pre-built SQL fragment for active-task exclusion filter
_EXCL_PH = ",".join("?" for _ in TASK_ACTIVE_EXCLUSIONS)
def run_digest(
db_path: str,
sections: list[str],
include_overdue: bool,
limit: int,
include_notes: bool = False,
) -> str:
"""Query the DB and return a markdown digest string."""
with get_conn(db_path) as conn:
# Active tasks by section — mirrors server.py task_digest SQL exactly
ph = ",".join("?" * len(sections))
active = conn.execute(
f"SELECT id, title, status, priority, section, due_date, project "
f"FROM tasks "
f"WHERE section IN ({ph}) AND status IN ('not_started', 'in_progress') AND type = 'task' "
f"ORDER BY "
f" CASE section WHEN 'today' THEN 0 WHEN 'inbox' THEN 1 "
f" WHEN 'next' THEN 2 WHEN 'waiting' THEN 3 WHEN 'someday' THEN 4 END, "
f" {build_priority_order_sql()} "
f"LIMIT ?",
sections + [limit],
).fetchall()
# Overdue tasks
overdue: list = []
if include_overdue:
overdue = conn.execute(
"SELECT id, title, status, priority, section, due_date, project "
"FROM tasks "
f"WHERE due_date < date('now') AND status NOT IN ({_EXCL_PH}) AND type = 'task' "
"ORDER BY due_date ASC LIMIT 10",
list(TASK_ACTIVE_EXCLUSIONS),
).fetchall()
# Status counts (active + done, excluding archived/cancelled)
counts = conn.execute(
"SELECT status, COUNT(*) as cnt FROM tasks "
"WHERE status NOT IN ('archived', 'cancelled') AND type = 'task' GROUP BY status"
).fetchall()
note_rows: list = []
if include_notes:
note_rows = conn.execute(
"SELECT id, title, priority, updated_at FROM tasks "
"WHERE type = 'note' AND status NOT IN ('archived', 'cancelled') "
f"ORDER BY {build_priority_order_sql()}, updated_at DESC "
"LIMIT ?",
(limit,),
).fetchall()
# ── Format markdown ───────────────────────────────────────────────────────
now_utc = now_iso()
lines = [
"## Task Digest",
f"*Generated: {now_utc}*",
"",
]
if counts:
stats = {r["status"]: r["cnt"] for r in counts}
total = sum(stats.values())
lines.append(
f"**Total active:** {total} | "
f"Not started: {stats.get('not_started', 0)} | "
f"In progress: {stats.get('in_progress', 0)} | "
f"Done: {stats.get('done', 0)}"
)
lines.append("")
if overdue:
lines.append(f"### OVERDUE ({len(overdue)})")
for t in overdue:
priority = t["priority"] or "medium"
lines.append(f"- [{priority.upper()}] {t['title']} (due: {t['due_date']})")
lines.append("")
# Group active tasks by section
by_section: dict[str, list] = {}
for t in active:
by_section.setdefault(t["section"], []).append(t)
for sec in sections:
tasks = by_section.get(sec, [])
if tasks:
lines.append(f"### {sec.upper()} ({len(tasks)})")
for t in tasks:
due = f" [due: {t['due_date']}]" if t["due_date"] else ""
priority = t["priority"] or "medium"
prio = f"[{priority.upper()}] " if priority != "medium" else ""
lines.append(f"- {prio}{t['title']}{due}")
lines.append("")
if note_rows:
lines.append(f"### NOTES ({len(note_rows)})")
for n in note_rows:
prio = f"[{n['priority'].upper()}] " if n["priority"] != "medium" else ""
lines.append(f"- {prio}{n['title']}")
lines.append("")
if not active and not overdue:
lines.append("*No tasks found for the selected sections.*")
lines.append("")
return "\n".join(lines)
def main() -> int:
parser = argparse.ArgumentParser(
description="Print a markdown task digest from the sqlite-memory-mcp database.",
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog=__doc__,
)
parser.add_argument(
"--db",
default=DEFAULT_DB,
metavar="PATH",
help=f"Path to SQLite memory DB (default: {DEFAULT_DB})",
)
parser.add_argument(
"--sections",
default="today,inbox,next",
metavar="SECTIONS",
help="Comma-separated section names to include (default: today,inbox,next)",
)
parser.add_argument(
"--no-overdue",
dest="include_overdue",
action="store_false",
default=True,
help="Skip overdue tasks section",
)
parser.add_argument(
"--limit",
type=int,
default=20,
metavar="N",
help="Max tasks to fetch per query (default: 20)",
)
parser.add_argument(
"--include-notes",
action="store_true",
default=False,
help="Include notes in the digest output",
)
args = parser.parse_args()
db_path = os.path.expanduser(args.db)
if not os.path.exists(db_path):
print(f"Error: Database not found: {db_path}", file=sys.stderr)
return 1
sections = [s.strip() for s in args.sections.split(",") if s.strip()]
if not sections:
print("Error: --sections cannot be empty", file=sys.stderr)
return 1
digest = run_digest(
db_path=db_path,
sections=sections,
include_overdue=args.include_overdue,
limit=args.limit,
include_notes=args.include_notes,
)
print(digest)
return 0
if __name__ == "__main__":
sys.exit(main())