forked from phuryn/claude-usage
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscanner.py
More file actions
526 lines (450 loc) · 20.7 KB
/
scanner.py
File metadata and controls
526 lines (450 loc) · 20.7 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
"""
scanner.py - Scans Claude Code JSONL transcript files and stores data in SQLite.
"""
import json
import os
import glob
import sqlite3
from pathlib import Path
from datetime import datetime, timezone
PROJECTS_DIR = Path.home() / ".claude" / "projects"
XCODE_PROJECTS_DIR = Path.home() / "Library" / "Developer" / "Xcode" / "CodingAssistant" / "ClaudeAgentConfig" / "projects"
DB_PATH = Path.home() / ".claude" / "usage.db"
DEFAULT_PROJECTS_DIRS = [PROJECTS_DIR, XCODE_PROJECTS_DIR]
# Higher number = higher priority when choosing a session's primary model
MODEL_PRIORITY = {"opus": 3, "sonnet": 2, "haiku": 1}
def _model_priority(model):
"""Return a priority score for a model name (higher = more capable)."""
if not model:
return 0
m = model.lower()
for keyword, priority in MODEL_PRIORITY.items():
if keyword in m:
return priority
return 0
def get_db(db_path=DB_PATH):
# Ensure the parent directory exists — on a fresh install or CI runner
# ~/.claude may not yet exist, and sqlite3.connect needs the parent dir.
Path(db_path).parent.mkdir(parents=True, exist_ok=True)
conn = sqlite3.connect(db_path)
conn.row_factory = sqlite3.Row
return conn
def init_db(conn):
conn.executescript("""
CREATE TABLE IF NOT EXISTS sessions (
session_id TEXT PRIMARY KEY,
project_name TEXT,
first_timestamp TEXT,
last_timestamp TEXT,
git_branch TEXT,
total_input_tokens INTEGER DEFAULT 0,
total_output_tokens INTEGER DEFAULT 0,
total_cache_read INTEGER DEFAULT 0,
total_cache_creation INTEGER DEFAULT 0,
model TEXT,
turn_count INTEGER DEFAULT 0
);
CREATE TABLE IF NOT EXISTS turns (
id INTEGER PRIMARY KEY AUTOINCREMENT,
session_id TEXT,
timestamp TEXT,
model TEXT,
input_tokens INTEGER DEFAULT 0,
output_tokens INTEGER DEFAULT 0,
cache_read_tokens INTEGER DEFAULT 0,
cache_creation_tokens INTEGER DEFAULT 0,
tool_name TEXT,
cwd TEXT,
message_id TEXT
);
CREATE TABLE IF NOT EXISTS processed_files (
path TEXT PRIMARY KEY,
mtime REAL,
lines INTEGER
);
CREATE INDEX IF NOT EXISTS idx_turns_session ON turns(session_id);
CREATE INDEX IF NOT EXISTS idx_turns_timestamp ON turns(timestamp);
CREATE INDEX IF NOT EXISTS idx_sessions_first ON sessions(first_timestamp);
""")
# Add message_id column if upgrading from older schema
try:
conn.execute("SELECT message_id FROM turns LIMIT 1")
except sqlite3.OperationalError:
conn.execute("ALTER TABLE turns ADD COLUMN message_id TEXT")
# Conditional unique index: only dedup non-null message IDs
conn.execute("""
CREATE UNIQUE INDEX IF NOT EXISTS idx_turns_message_id
ON turns(message_id) WHERE message_id IS NOT NULL AND message_id != ''
""")
conn.commit()
def project_name_from_cwd(cwd):
"""Derive a friendly project name from cwd path."""
if not cwd:
return "unknown"
# Normalize to forward slashes, take last 2 components
parts = cwd.replace("\\", "/").rstrip("/").split("/")
if len(parts) >= 2:
return "/".join(parts[-2:])
return parts[-1] if parts else "unknown"
def parse_jsonl_file(filepath):
"""Parse a JSONL file and return (session_metas, turns, line_count).
Deduplicates streaming events by message.id — Claude Code logs multiple
JSONL records per API response, all sharing the same message.id. Only the
last record per message_id is kept (it has the final usage tallies).
"""
seen_messages = {} # message_id -> turn dict (dedup streaming records)
turns_no_id = [] # turns without a message_id (kept as-is)
session_meta = {} # session_id -> dict
line_count = 0
try:
with open(filepath, encoding="utf-8", errors="replace") as f:
for line_count, line in enumerate(f, 1):
line = line.strip()
if not line:
continue
try:
record = json.loads(line)
except json.JSONDecodeError:
continue
rtype = record.get("type")
if rtype not in ("assistant", "user"):
continue
session_id = record.get("sessionId")
if not session_id:
continue
timestamp = record.get("timestamp", "")
cwd = record.get("cwd", "")
git_branch = record.get("gitBranch", "")
# Update session metadata from any record
if session_id not in session_meta:
session_meta[session_id] = {
"session_id": session_id,
"project_name": project_name_from_cwd(cwd),
"first_timestamp": timestamp,
"last_timestamp": timestamp,
"git_branch": git_branch,
"model": None,
}
else:
meta = session_meta[session_id]
if timestamp and (not meta["first_timestamp"] or timestamp < meta["first_timestamp"]):
meta["first_timestamp"] = timestamp
if timestamp and (not meta["last_timestamp"] or timestamp > meta["last_timestamp"]):
meta["last_timestamp"] = timestamp
if git_branch and not meta["git_branch"]:
meta["git_branch"] = git_branch
if rtype == "assistant":
msg = record.get("message", {})
usage = msg.get("usage", {})
model = msg.get("model", "")
message_id = msg.get("id", "")
input_tokens = usage.get("input_tokens", 0) or 0
output_tokens = usage.get("output_tokens", 0) or 0
cache_read = usage.get("cache_read_input_tokens", 0) or 0
cache_creation = usage.get("cache_creation_input_tokens", 0) or 0
# Only record turns that have actual token usage
if input_tokens + output_tokens + cache_read + cache_creation == 0:
continue
# Extract tool name from content if present
tool_name = None
for item in msg.get("content", []):
if isinstance(item, dict) and item.get("type") == "tool_use":
tool_name = item.get("name")
break
if model:
session_meta[session_id]["model"] = model
turn = {
"session_id": session_id,
"timestamp": timestamp,
"model": model,
"input_tokens": input_tokens,
"output_tokens": output_tokens,
"cache_read_tokens": cache_read,
"cache_creation_tokens": cache_creation,
"tool_name": tool_name,
"cwd": cwd,
"message_id": message_id,
}
# Dedup: last record per message_id wins (final usage tallies)
if message_id:
seen_messages[message_id] = turn
else:
turns_no_id.append(turn)
except Exception as e:
print(f" Warning: error reading {filepath}: {e}")
turns = turns_no_id + list(seen_messages.values())
return list(session_meta.values()), turns, line_count
def aggregate_sessions(session_metas, turns):
"""Aggregate turn data back into session-level stats."""
from collections import defaultdict, Counter
session_stats = defaultdict(lambda: {
"total_input_tokens": 0,
"total_output_tokens": 0,
"total_cache_read": 0,
"total_cache_creation": 0,
"turn_count": 0,
"model": None,
})
session_model_counts = defaultdict(Counter)
for t in turns:
s = session_stats[t["session_id"]]
s["total_input_tokens"] += t["input_tokens"]
s["total_output_tokens"] += t["output_tokens"]
s["total_cache_read"] += t["cache_read_tokens"]
s["total_cache_creation"] += t["cache_creation_tokens"]
s["turn_count"] += 1
if t["model"]:
session_model_counts[t["session_id"]][t["model"]] += 1
for sid, counts in session_model_counts.items():
if counts:
session_stats[sid]["model"] = counts.most_common(1)[0][0]
# Merge into session_metas
result = []
for meta in session_metas:
sid = meta["session_id"]
stats = session_stats[sid]
result.append({**meta, **stats})
return result
def upsert_sessions(conn, sessions):
for s in sessions:
# Check if session exists
existing = conn.execute(
"SELECT total_input_tokens, total_output_tokens, total_cache_read, "
"total_cache_creation, turn_count FROM sessions WHERE session_id = ?",
(s["session_id"],)
).fetchone()
if existing is None:
conn.execute("""
INSERT INTO sessions
(session_id, project_name, first_timestamp, last_timestamp,
git_branch, total_input_tokens, total_output_tokens,
total_cache_read, total_cache_creation, model, turn_count)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
""", (
s["session_id"], s["project_name"], s["first_timestamp"],
s["last_timestamp"], s["git_branch"],
s["total_input_tokens"], s["total_output_tokens"],
s["total_cache_read"], s["total_cache_creation"],
s["model"], s["turn_count"]
))
else:
# Update: add new tokens on top of existing (since we only insert new turns)
# Keep the highest-priority model (e.g. opus over haiku from subagents)
existing_model = conn.execute(
"SELECT model FROM sessions WHERE session_id = ?",
(s["session_id"],)
).fetchone()["model"]
new_model = s["model"]
if _model_priority(new_model) > _model_priority(existing_model):
model_to_set = new_model
else:
model_to_set = existing_model
conn.execute("""
UPDATE sessions SET
last_timestamp = MAX(last_timestamp, ?),
total_input_tokens = total_input_tokens + ?,
total_output_tokens = total_output_tokens + ?,
total_cache_read = total_cache_read + ?,
total_cache_creation = total_cache_creation + ?,
turn_count = turn_count + ?,
model = ?
WHERE session_id = ?
""", (
s["last_timestamp"],
s["total_input_tokens"], s["total_output_tokens"],
s["total_cache_read"], s["total_cache_creation"],
s["turn_count"], model_to_set,
s["session_id"]
))
def insert_turns(conn, turns):
conn.executemany("""
INSERT OR IGNORE INTO turns
(session_id, timestamp, model, input_tokens, output_tokens,
cache_read_tokens, cache_creation_tokens, tool_name, cwd, message_id)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
""", [
(t["session_id"], t["timestamp"], t["model"],
t["input_tokens"], t["output_tokens"],
t["cache_read_tokens"], t["cache_creation_tokens"],
t["tool_name"], t["cwd"], t.get("message_id", ""))
for t in turns
])
def scan(projects_dir=None, projects_dirs=None, db_path=DB_PATH, verbose=True):
conn = get_db(db_path)
init_db(conn)
if projects_dirs:
dirs_to_scan = [Path(d) for d in projects_dirs]
elif projects_dir:
dirs_to_scan = [Path(projects_dir)]
else:
dirs_to_scan = DEFAULT_PROJECTS_DIRS
jsonl_files = []
for d in dirs_to_scan:
if not d.exists():
continue
if verbose:
print(f"Scanning {d} ...")
jsonl_files.extend(glob.glob(str(d / "**" / "*.jsonl"), recursive=True))
jsonl_files.sort()
new_files = 0
updated_files = 0
skipped_files = 0
total_turns = 0
total_sessions = set()
for filepath in jsonl_files:
try:
mtime = os.path.getmtime(filepath)
except OSError:
continue
row = conn.execute(
"SELECT mtime, lines FROM processed_files WHERE path = ?",
(filepath,)
).fetchone()
if row and abs(row["mtime"] - mtime) < 0.01:
skipped_files += 1
continue
is_new = row is None
if verbose:
status = "NEW" if is_new else "UPD"
print(f" [{status}] {filepath}")
if is_new:
# New file: full parse (single read, returns line count)
session_metas, turns, line_count = parse_jsonl_file(filepath)
if turns or session_metas:
sessions = aggregate_sessions(session_metas, turns)
upsert_sessions(conn, sessions)
insert_turns(conn, turns)
for s in sessions:
total_sessions.add(s["session_id"])
total_turns += len(turns)
new_files += 1
else:
# Updated file: read once, process only new lines
old_lines = row["lines"] if row else 0
seen_messages = {} # message_id -> turn (dedup streaming)
turns_no_id = []
new_session_metas = {}
line_count = 0
try:
with open(filepath, encoding="utf-8", errors="replace") as f:
for line_count, line in enumerate(f, 1):
if line_count <= old_lines:
continue
line = line.strip()
if not line:
continue
try:
record = json.loads(line)
except json.JSONDecodeError:
continue
rtype = record.get("type")
if rtype not in ("assistant", "user"):
continue
session_id = record.get("sessionId")
if not session_id:
continue
timestamp = record.get("timestamp", "")
cwd = record.get("cwd", "")
# Track session metadata from new lines
if session_id not in new_session_metas:
new_session_metas[session_id] = {
"session_id": session_id,
"project_name": project_name_from_cwd(cwd),
"first_timestamp": timestamp,
"last_timestamp": timestamp,
"git_branch": record.get("gitBranch", ""),
"model": None,
}
else:
meta = new_session_metas[session_id]
if timestamp and (not meta["last_timestamp"] or timestamp > meta["last_timestamp"]):
meta["last_timestamp"] = timestamp
if rtype == "assistant":
msg = record.get("message", {})
usage = msg.get("usage", {})
model = msg.get("model", "")
message_id = msg.get("id", "")
input_tokens = usage.get("input_tokens", 0) or 0
output_tokens = usage.get("output_tokens", 0) or 0
cache_read = usage.get("cache_read_input_tokens", 0) or 0
cache_creation = usage.get("cache_creation_input_tokens", 0) or 0
if input_tokens + output_tokens + cache_read + cache_creation == 0:
continue
tool_name = None
for item in msg.get("content", []):
if isinstance(item, dict) and item.get("type") == "tool_use":
tool_name = item.get("name")
break
if model:
new_session_metas[session_id]["model"] = model
turn = {
"session_id": session_id,
"timestamp": timestamp,
"model": model,
"input_tokens": input_tokens,
"output_tokens": output_tokens,
"cache_read_tokens": cache_read,
"cache_creation_tokens": cache_creation,
"tool_name": tool_name,
"cwd": cwd,
"message_id": message_id,
}
if message_id:
seen_messages[message_id] = turn
else:
turns_no_id.append(turn)
except Exception as e:
print(f" Warning: {e}")
if line_count <= old_lines:
# File didn't grow (mtime changed but no new content)
conn.execute("UPDATE processed_files SET mtime = ? WHERE path = ?",
(mtime, filepath))
conn.commit()
skipped_files += 1
continue
new_turns = turns_no_id + list(seen_messages.values())
if new_turns or new_session_metas:
sessions = aggregate_sessions(list(new_session_metas.values()), new_turns)
upsert_sessions(conn, sessions)
insert_turns(conn, new_turns)
for s in sessions:
total_sessions.add(s["session_id"])
total_turns += len(new_turns)
updated_files += 1
# Record file as processed (line_count already known from the single read)
conn.execute("""
INSERT OR REPLACE INTO processed_files (path, mtime, lines)
VALUES (?, ?, ?)
""", (filepath, mtime, line_count))
conn.commit()
# Recompute session totals from actual turns in DB.
# This ensures correctness when INSERT OR IGNORE skips duplicate turns
# but upsert_sessions had already added their tokens additively.
if new_files or updated_files:
conn.execute("""
UPDATE sessions SET
total_input_tokens = COALESCE((SELECT SUM(input_tokens) FROM turns WHERE turns.session_id = sessions.session_id), 0),
total_output_tokens = COALESCE((SELECT SUM(output_tokens) FROM turns WHERE turns.session_id = sessions.session_id), 0),
total_cache_read = COALESCE((SELECT SUM(cache_read_tokens) FROM turns WHERE turns.session_id = sessions.session_id), 0),
total_cache_creation = COALESCE((SELECT SUM(cache_creation_tokens) FROM turns WHERE turns.session_id = sessions.session_id), 0),
turn_count = COALESCE((SELECT COUNT(*) FROM turns WHERE turns.session_id = sessions.session_id), 0)
""")
conn.commit()
if verbose:
print(f"\nScan complete:")
print(f" New files: {new_files}")
print(f" Updated files: {updated_files}")
print(f" Skipped files: {skipped_files}")
print(f" Turns added: {total_turns}")
print(f" Sessions seen: {len(total_sessions)}")
conn.close()
return {"new": new_files, "updated": updated_files, "skipped": skipped_files,
"turns": total_turns, "sessions": len(total_sessions)}
if __name__ == "__main__":
import sys
projects_dir = None
for i, arg in enumerate(sys.argv[1:]):
if arg == "--projects-dir" and i + 1 < len(sys.argv[1:]):
projects_dir = Path(sys.argv[i + 2])
break
scan(projects_dir=projects_dir)