-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathwhatidid.py
More file actions
577 lines (506 loc) · 22.5 KB
/
whatidid.py
File metadata and controls
577 lines (506 loc) · 22.5 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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
#!/usr/bin/env python3
"""
whatidid.py — Daily GitHub Copilot activity analytics.
Usage:
python whatidid.py # Today
python whatidid.py --date 2026-03-30 # Specific date
python whatidid.py --from 2026-03-09 --to 2026-03-30 # Date range
python whatidid.py --from 2026-03-09 # From date to today
python whatidid.py --date 7D # Last 7 days
python whatidid.py --date 30D # Last 30 days
python whatidid.py --refresh # Force re-analysis
python whatidid.py --from 2026-03-01 --to 2026-03-31 --lock # Freeze estimates
Date formats accepted: YYYY-MM-DD, MM-DD-YYYY, MM/DD/YYYY, DD-Mon-YYYY
Lookback shortcuts: 7D, 14D, 30D, 60D, 90D (days back from today)
Triggered as a Copilot skill via /whatididghcp
"""
import argparse
import io
import json as _json
import re as _re
import subprocess
import sys
from datetime import date, timedelta
from pathlib import Path
# Force UTF-8 output on Windows to avoid cp1252 UnicodeEncodeError on emoji/symbols
def _ensure_utf8_stream(stream):
encoding = getattr(stream, "encoding", None)
if encoding and encoding.lower() == "utf-8":
return stream
if hasattr(stream, "reconfigure"):
stream.reconfigure(encoding="utf-8", errors="replace")
return stream
if hasattr(stream, "buffer"):
return io.TextIOWrapper(stream.buffer, encoding="utf-8", errors="replace")
return stream
sys.stdout = _ensure_utf8_stream(sys.stdout)
sys.stderr = _ensure_utf8_stream(sys.stderr)
sys.path.insert(0, str(Path(__file__).parent))
DEFAULT_EMAIL = "" # Auto-detected from GitHub API or git config
# Lookback pattern: e.g. 7D, 30d, 14D
_LOOKBACK_RE = _re.compile(r'^(\d+)[dD]$')
def _parse_date(s: str) -> str:
"""Parse flexible date formats into YYYY-MM-DD.
Accepts: YYYY-MM-DD, MM-DD-YYYY, MM/DD/YYYY, DD-Mon-YYYY, 'today'
"""
if not s or s.lower() == "today":
return date.today().isoformat()
# Lookback shortcut (7D, 30D, etc.)
m = _LOOKBACK_RE.match(s.strip())
if m:
days = int(m.group(1))
return (date.today() - timedelta(days=days)).isoformat()
cleaned = s.strip().replace("/", "-")
# Already YYYY-MM-DD
if _re.match(r'^\d{4}-\d{1,2}-\d{1,2}$', cleaned):
parts = cleaned.split("-")
return f"{parts[0]}-{int(parts[1]):02d}-{int(parts[2]):02d}"
# MM-DD-YYYY
if _re.match(r'^\d{1,2}-\d{1,2}-\d{4}$', cleaned):
parts = cleaned.split("-")
return f"{parts[2]}-{int(parts[0]):02d}-{int(parts[1]):02d}"
# DD-Mon-YYYY (e.g., 15-Mar-2026)
m = _re.match(r'^(\d{1,2})-([A-Za-z]{3})-(\d{4})$', cleaned)
if m:
from datetime import datetime
dt = datetime.strptime(cleaned, "%d-%b-%Y")
return dt.strftime("%Y-%m-%d")
# Last resort — try fromisoformat
try:
return date.fromisoformat(cleaned).isoformat()
except ValueError:
print(f" WARNING: Could not parse date '{s}'. Expected YYYY-MM-DD, MM-DD-YYYY, MM/DD/YYYY, or 7D/30D.")
sys.exit(1)
def _date_range(from_str: str, to_str: str) -> list:
"""Return list of YYYY-MM-DD strings for every day in [from, to]."""
d0 = date.fromisoformat(_parse_date(from_str))
d1 = date.fromisoformat(_parse_date(to_str))
days, cur = [], d0
while cur <= d1:
days.append(cur.isoformat())
cur += timedelta(days=1)
return days
def _normalize_project(name: str) -> str:
"""Normalize project name for grouping (lowercase, strip path separators)."""
return name.replace("\\", "/").split("/")[-1].lower().strip().replace(" ", "-")
def _merge_related_goals(goals: list) -> list:
"""Group goals from the same project across different days into single entries.
Goals are considered related when they share the same normalized project name.
Merged goals combine all tasks, sum hours, and show the date range.
"""
from collections import OrderedDict
groups: OrderedDict = OrderedDict()
for g in goals:
proj = g.get("project", "")
key = _normalize_project(proj) if proj else f"_unnamed_{id(g)}"
if key in groups:
merged = groups[key]
merged["tasks"].extend(g.get("tasks", []))
merged["human_hours"] += g.get("human_hours", 0)
merged["_dates"].add(g.get("date", ""))
# Keep the longer/better title
if len(g.get("title", "")) > len(merged.get("title", "")):
merged["title"] = g["title"]
# Merge docs
for d in g.get("docs_referenced", []):
if d not in merged.get("docs_referenced", []):
merged.setdefault("docs_referenced", []).append(d)
else:
groups[key] = {
**g,
"tasks": list(g.get("tasks", [])),
"human_hours": g.get("human_hours", 0),
"_dates": {g.get("date", "")},
}
# Finalize: set date field to earliest date, add date range info
result = []
for merged in groups.values():
dates = sorted(merged.pop("_dates", set()))
merged["_all_dates"] = dates # Keep all dates for metrics aggregation
if len(dates) > 1:
merged["date"] = dates[0]
d0 = dates[0][5:] # MM-DD
d1 = dates[-1][5:]
merged["summary"] = (merged.get("summary", "") or "") + f" ({len(dates)} days: {d0} to {d1})"
elif dates:
merged["date"] = dates[0]
# Round hours
merged["human_hours"] = round(merged["human_hours"] * 4) / 4
result.append(merged)
return result
def _merge_analyses(day_analyses: list) -> dict:
"""Combine per-day analysis dicts into one, tagging each goal with its date."""
all_goals = []
all_sessions = []
total_tokens = {"input": 0, "output": 0, "cache_read": 0, "cache_creation": 0, "total": 0}
total_premium = 0
total_api_ms = 0
total_lines_added = 0
total_lines_removed = 0
all_files = []
all_projects = set()
merged_session_metrics = {}
heuristic_dates = []
for target_date, analysis, sessions in day_analyses:
for g in analysis.get("goals", []):
g["date"] = target_date
all_goals.append(g)
for k in total_tokens:
total_tokens[k] += analysis.get("tokens", {}).get(k, 0)
total_premium += analysis.get("premium_requests", 0)
total_api_ms += analysis.get("total_api_ms", 0)
total_lines_added += analysis.get("lines_added", 0)
total_lines_removed += analysis.get("lines_removed", 0)
for f in analysis.get("files_modified", []):
if f not in all_files:
all_files.append(f)
all_sessions.extend(sessions)
all_projects.update(analysis.get("projects", []))
if analysis.get("analysis_method") == "heuristic":
heuristic_dates.append(target_date)
# Merge per-project session metrics across days (keyed by date|project)
for proj, metrics in analysis.get("session_metrics", {}).items():
dated_key = target_date + "|" + proj
merged_session_metrics[dated_key] = dict(metrics)
# Also store under normalized key for cross-day matching
norm_key = target_date + "|" + _normalize_project(proj)
merged_session_metrics.setdefault(norm_key, dict(metrics))
active_dates = sorted({d for d, _, _ in day_analyses})
# Merge goals from the same project across days into single entries
if len(active_dates) > 1:
all_goals = _merge_related_goals(all_goals)
# Create aggregated session_metrics for merged goals that span multiple days
for g in all_goals:
all_dates = g.get("_all_dates", [g.get("date", "")])
if len(all_dates) <= 1:
continue
proj = g.get("project", "")
norm = _normalize_project(proj)
# Sum metrics across all dates for this project
agg = {"tokens": 0, "tool_invocations": 0, "premium_requests": 0,
"lines_added": 0, "lines_removed": 0, "active_minutes": 0,
"wall_clock_minutes": 0, "sessions": 0}
for d in all_dates:
for try_key in [d + "|" + proj, d + "|" + norm]:
m = merged_session_metrics.get(try_key, {})
if m:
for k in agg:
agg[k] += m.get(k, 0)
break
# Store aggregated metrics under the earliest date key
merged_session_metrics[all_dates[0] + "|" + proj] = agg
merged_session_metrics[all_dates[0] + "|" + norm] = agg
if len(active_dates) == 1:
headline = day_analyses[0][1].get("headline", f"Activity on {active_dates[0]}")
narrative = day_analyses[0][1].get("day_narrative", "")
else:
d0 = active_dates[0][5:]
d1 = active_dates[-1][5:]
n = len(all_goals)
headline = (f"{len(active_dates)} active days ({d0} – {d1}): "
f"{n} project{'s' if n != 1 else ''} delivered")
narrative = (f"Across {len(active_dates)} active days from "
f"{active_dates[0]} to {active_dates[-1]}, Copilot assisted with "
f"{n} distinct project{'s' if n != 1 else ''} across "
f"{len(all_projects)} workspace{'s' if len(all_projects) != 1 else ''}. "
f"Related work across days has been grouped.")
return {
"headline": headline,
"primary_focus": day_analyses[0][1].get("primary_focus", ""),
"day_narrative": narrative,
"goals": all_goals,
"tokens": total_tokens,
"premium_requests": total_premium,
"total_api_ms": total_api_ms,
"lines_added": total_lines_added,
"lines_removed": total_lines_removed,
"files_modified": all_files,
"session_metrics": merged_session_metrics,
"sessions_count": len(all_sessions),
"projects": list(all_projects),
"active_dates": active_dates,
"heuristic_dates": heuristic_dates,
"analysis_method": "heuristic" if heuristic_dates else "ai",
}
def _print_summary(analysis: dict):
goals = analysis.get("goals", [])
total_t = sum(len(g.get("tasks", [])) for g in goals)
total_h = sum(g.get("human_hours", 0) for g in goals)
print(f"Identified {len(goals)} goal(s), {total_t} task(s):")
for g in goals:
date_tag = f" [{g['date']}]" if "date" in g else ""
print(f" [GOAL]{date_tag} {g.get('title', '')[:65]} ({g.get('human_hours', 0):.1f}h)")
for t in g.get("tasks", []):
domain = ", ".join(t.get("domain_skills", []))
tech = ", ".join(t.get("tech_skills", []))
skills = " | ".join(filter(None, [domain, tech]))
print(f" - {t.get('title', '')[:55]} ({t.get('human_hours', 0):.1f}h | {skills})")
print(f"\n Total human effort estimate: {total_h:.1f} hours")
print(f" Premium requests: {analysis.get('premium_requests', 0)}")
lines_added = analysis.get("lines_added", 0)
lines_removed = analysis.get("lines_removed", 0)
if lines_added or lines_removed:
print(f" Code impact: +{lines_added} / -{lines_removed} lines")
def _save_and_open(html: str, label: str) -> Path:
output_path = Path(__file__).parent / f"report_{label}.html"
output_path.write_text(html, encoding="utf-8")
print(f"\nHTML report saved: {output_path}")
try:
subprocess.run(["cmd", "/c", "start", "", str(output_path)], check=False)
except Exception:
pass
return output_path
def _detect_email() -> str:
"""Detect the user's email address.
Priority order:
1. GitHub API /user/emails (primary verified) via `gh auth token`
2. git config user.email
3. DEFAULT_EMAIL constant
"""
# 1. Try GitHub API
try:
token_result = subprocess.run(
["gh", "auth", "token"], capture_output=True, text=True, timeout=5
)
token = token_result.stdout.strip()
if token:
import urllib.request
req = urllib.request.Request(
"https://api.github.com/user/emails",
headers={"Authorization": f"token {token}", "Accept": "application/vnd.github+json"},
)
with urllib.request.urlopen(req, timeout=5) as resp:
emails = _json.loads(resp.read().decode())
# Prefer primary+verified, then primary, then first verified
for e in emails:
if e.get("primary") and e.get("verified"):
return e["email"]
for e in emails:
if e.get("primary"):
return e["email"]
for e in emails:
if e.get("verified"):
return e["email"]
except Exception:
pass
# 2. git config
try:
result = subprocess.run(
["git", "config", "user.email"], capture_output=True, text=True, timeout=5
)
email = result.stdout.strip()
if email:
return email
except Exception:
pass
# 3. Fallback
return DEFAULT_EMAIL
def _send_outlook_email(subject: str, html: str, to_email: str) -> bool:
"""Send an email via Outlook COM automation with the full HTML report as the body.
Returns True on success, False if Outlook is unavailable or the send fails."""
import tempfile, os
tmp = tempfile.NamedTemporaryFile(
mode="w", suffix=".html", encoding="utf-8", delete=False
)
try:
tmp.write(html)
tmp.close()
# Single-quoted PowerShell strings are literal — no backslash escaping needed,
# only single quotes need doubling.
ps_path = tmp.name.replace("'", "''")
escaped_subject = subject.replace("'", "''")
escaped_to = to_email.replace("'", "''")
ps = (
f"$html = Get-Content -Path '{ps_path}' -Raw -Encoding UTF8;"
f"$ol = New-Object -ComObject Outlook.Application;"
f"$mail = $ol.CreateItem(0);"
f"$mail.Subject = '{escaped_subject}';"
f"$mail.To = '{escaped_to}';"
f"$mail.HTMLBody = $html;"
f"$mail.Send();"
f"$ol.Session.SendAndReceive($true);"
f"Start-Sleep -Seconds 5"
)
result = subprocess.run(
["powershell", "-NoProfile", "-NonInteractive", "-WindowStyle", "Hidden", "-Command", ps],
timeout=60,
capture_output=True,
text=True,
)
if result.returncode != 0:
print(f"\n [email error] {result.stderr.strip() or result.stdout.strip()}")
return False
return True
except Exception as exc:
print(f"\n [email error] {exc}")
return False
finally:
try:
os.unlink(tmp.name)
except Exception:
pass
def _preprocess_argv(argv: list) -> list:
"""Rewrite --ND shorthand (e.g. --14D) to --date ND before argparse sees it."""
out = []
for arg in argv:
m = _re.match(r'^--(\d+[dD])$', arg)
if m:
out += ["--date", m.group(1).upper()]
else:
out.append(arg)
return out
def main():
parser = argparse.ArgumentParser(
description="Generate a digest of what GitHub Copilot helped you accomplish."
)
parser.add_argument("--date", default="7D",
help="Single date or lookback: YYYY-MM-DD, MM-DD-YYYY, 7D, 30D, 'today' (default: 7D)")
parser.add_argument("--from", dest="date_from", default=None,
help="Start of date range (any format)")
parser.add_argument("--to", dest="date_to", default=None,
help="End of date range (any format, default: today)")
parser.add_argument("--refresh", action="store_true",
help="Re-run semantic analysis even if cached")
parser.add_argument("--lock", action="store_true",
help="Freeze estimates after this run — future --refresh calls will be ignored")
parser.add_argument("--email", nargs="?", const=True, default=False,
help="Send report via Outlook (auto-detects email, or pass an explicit address)")
args = parser.parse_args(_preprocess_argv(sys.argv[1:]))
today = date.today().isoformat()
if args.date_from:
from_date = _parse_date(args.date_from)
to_date = _parse_date(args.date_to) if args.date_to else today
dates = _date_range(from_date, to_date)
report_label = f"{from_date}_to_{to_date}"
elif _LOOKBACK_RE.match(args.date.strip()):
# Lookback shortcut: 7D, 30D, etc. → date range
from_date = _parse_date(args.date)
dates = _date_range(from_date, today)
report_label = f"{from_date}_to_{today}"
else:
target = _parse_date(args.date)
dates = [target]
report_label = target
from harvest import get_sessions_for_date
from analyze import analyze_day, check_api_health
print(f"\nwhatididghcp -- {report_label}")
print("-" * 40)
# Pre-flight: check if AI analysis API is reachable
import time
MAX_RETRIES = 5
RETRY_WAIT = 60 # 1 minute
api_ok = False
print(" Checking AI analysis API... ", end="", flush=True)
status, msg = check_api_health()
if status == "ok":
print("[OK] connected.")
api_ok = True
elif status == "auth":
print(f"[FAIL] {msg}")
print(f"\n WARNING: This is an authentication issue -- retrying won't help.")
print(f" Fix: run `gh auth login` in your terminal, then re-run.\n")
print(f" Proceeding with heuristic fallback.\n")
else:
print(f"[FAIL] {msg}\n")
print(f" The AI analysis API is currently unreachable.")
print(f" Without it, estimates will use a less accurate heuristic approach.\n")
print(f" Options:")
print(f" 1. Retry automatically (up to {MAX_RETRIES}× at 1-min intervals)")
print(f" 2. Continue now with heuristic fallback\n")
try:
choice = input(" Enter choice [1]: ").strip()
except (EOFError, KeyboardInterrupt):
choice = "2"
if choice == "2":
print("\n Proceeding with heuristic fallback.\n")
else:
for attempt in range(1, MAX_RETRIES + 1):
print(f"\n Retry {attempt}/{MAX_RETRIES} — waiting {RETRY_WAIT}s... ", end="", flush=True)
try:
time.sleep(RETRY_WAIT)
except KeyboardInterrupt:
print("\n Skipped. Proceeding with heuristic fallback.\n")
break
status, msg = check_api_health()
if status == "ok":
print("[OK] connected!")
api_ok = True
break
elif status == "auth":
print(f"[FAIL] {msg}")
print(f" Authentication issue detected. Run `gh auth login` to fix.\n")
break
else:
print(f"[FAIL] {msg}")
else:
print(f"\n WARNING: API unreachable after {MAX_RETRIES} attempts.")
print(f" Proceeding with heuristic fallback.\n")
day_analyses = []
all_sessions = []
for d in dates:
sessions = get_sessions_for_date(d)
if not sessions:
continue
premium = sum(s.get("premium_requests", 0) for s in sessions)
print(f" {d}: {len(sessions)} session(s), {premium} premium requests")
analysis = analyze_day(d, sessions, refresh=args.refresh, use_api=api_ok)
day_analyses.append((d, analysis, sessions))
all_sessions.extend(sessions)
if not day_analyses:
print(f"\nNo Copilot sessions found for {report_label}.")
print(" (Sessions are stored in ~/.copilot/session-state/)")
sys.exit(0)
print()
analysis = _merge_analyses(day_analyses)
_print_summary(analysis)
if args.lock:
from analyze import _cache_path
locked_count = 0
for d in dates:
cf = _cache_path(d)
if cf.exists():
try:
data = _json.loads(cf.read_text(encoding="utf-8"))
if not data.get("locked"):
data["locked"] = True
cf.write_text(_json.dumps(data, indent=2), encoding="utf-8")
locked_count += 1
except Exception:
pass
if locked_count:
print(f"\n Locked {locked_count} cache file(s). These estimates are now frozen.")
print(" To unlock: delete the cache file(s) in cache/ and re-run.")
heuristic_dates = analysis.get("heuristic_dates", [])
if heuristic_dates:
n = len(heuristic_dates)
total = len(analysis.get("active_dates", []))
print(f"\n WARNING: {n}/{total} day(s) used heuristic fallback (API unavailable).")
print(f" Estimates for those days are approximate and likely inflated.")
print(f" Re-run with --refresh when the GitHub Models API is available for accurate results.")
from report import generate_html
html = generate_html(report_label, analysis, all_sessions, max_width=960)
_save_and_open(html, report_label)
if args.email is not False:
# Resolve recipient email
if args.email is True or args.email is None:
to_email = _detect_email()
if to_email:
print(f" Detected email: {to_email}")
else:
print(" Could not detect email. Use --email you@company.com to specify.")
else:
to_email = args.email
if to_email:
# Generate a narrower version for email clients (Outlook, Gmail)
email_html = generate_html(report_label, analysis, all_sessions, max_width=700)
subject = f"My GitHub Copilot Impact | {report_label.replace('_', ' ')}"
print(f" Sending email to: {to_email} ...", end="", flush=True)
ok = _send_outlook_email(subject, email_html, to_email)
print(" sent." if ok else " failed.")
print("\nDone.")
if today in [d for d in dates]:
print(" Note: Active sessions (still open) may show incomplete metrics.")
print(" Close your Copilot session and re-run for full code/token data.")
print()
if __name__ == "__main__":
main()