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
412 lines (348 loc) · 15.7 KB
/
Copy pathscanner.py
File metadata and controls
412 lines (348 loc) · 15.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
"""
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"
DB_PATH = Path.home() / ".claude" / "usage.db"
def get_db(db_path=DB_PATH):
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
);
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);
""")
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 yield (session_data, turns) tuples."""
turns = []
session_meta = {} # session_id -> dict
try:
with open(filepath, encoding="utf-8", errors="replace") as f:
for line in f:
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", "")
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
turns.append({
"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,
})
except Exception as e:
print(f" Warning: error reading {filepath}: {e}")
return list(session_meta.values()), turns
def aggregate_sessions(session_metas, turns):
"""Aggregate turn data back into session-level stats."""
from collections import defaultdict
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,
})
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"]:
s["model"] = t["model"]
# 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)
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 = COALESCE(?, 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"], s["model"],
s["session_id"]
))
def insert_turns(conn, turns):
conn.executemany("""
INSERT INTO turns
(session_id, timestamp, model, input_tokens, output_tokens,
cache_read_tokens, cache_creation_tokens, tool_name, cwd)
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"])
for t in turns
])
def scan(projects_dir=PROJECTS_DIR, db_path=DB_PATH, verbose=True):
conn = get_db(db_path)
init_db(conn)
jsonl_files = glob.glob(str(projects_dir / "**" / "*.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}] {os.path.relpath(filepath, projects_dir)}")
session_metas, turns = parse_jsonl_file(filepath)
if turns or session_metas:
sessions = aggregate_sessions(session_metas, turns)
# For incremental updates: only insert turns not already in DB
if not is_new:
# Get existing turns count to detect which are new
# Simple approach: delete old turns for these sessions and re-insert
# More correct: track file line count and only process new lines
old_lines = row["lines"] if row else 0
# Re-parse only if file grew
current_lines = sum(1 for _ in open(filepath, encoding="utf-8", errors="replace"))
if current_lines <= old_lines:
conn.execute("UPDATE processed_files SET mtime = ? WHERE path = ?",
(mtime, filepath))
conn.commit()
skipped_files += 1
continue
# Only process the new lines
new_turns = []
new_metas = {}
try:
with open(filepath, encoding="utf-8", errors="replace") as f:
for i, line in enumerate(f):
if i < 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 != "assistant":
continue
session_id = record.get("sessionId")
if not session_id:
continue
msg = record.get("message", {})
usage = msg.get("usage", {})
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
new_turns.append({
"session_id": session_id,
"timestamp": record.get("timestamp", ""),
"model": msg.get("model", ""),
"input_tokens": input_tokens,
"output_tokens": output_tokens,
"cache_read_tokens": cache_read,
"cache_creation_tokens": cache_creation,
"tool_name": tool_name,
"cwd": record.get("cwd", ""),
})
except Exception as e:
print(f" Warning: {e}")
turns = new_turns
sessions = aggregate_sessions(list(new_metas.values()) or [], turns)
# Update session timestamps from full parse
for meta in session_metas:
sessions_to_update = [s for s in sessions if s["session_id"] == meta["session_id"]]
if not sessions_to_update:
# Session exists but no new turns -- still update timestamps
sessions.append({**meta,
"total_input_tokens": 0,
"total_output_tokens": 0,
"total_cache_read": 0,
"total_cache_creation": 0,
"turn_count": 0,
"model": meta.get("model")})
updated_files += 1
else:
new_files += 1
upsert_sessions(conn, sessions)
insert_turns(conn, turns)
for s in sessions:
total_sessions.add(s["session_id"])
total_turns += len(turns)
# Record file as processed
line_count = sum(1 for _ in open(filepath, encoding="utf-8", errors="replace"))
conn.execute("""
INSERT OR REPLACE INTO processed_files (path, mtime, lines)
VALUES (?, ?, ?)
""", (filepath, mtime, line_count))
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__":
print(f"Scanning {PROJECTS_DIR} ...")
scan()