-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathopencode_log_analyzer.py
More file actions
342 lines (293 loc) · 11.2 KB
/
Copy pathopencode_log_analyzer.py
File metadata and controls
342 lines (293 loc) · 11.2 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
#!/usr/bin/env python3
"""Analyze an opencode session export JSON and print timing/token statistics.
Recursively includes tool usage from child sessions (subagents).
Usage:
opencode export <sessionID> | python3 opencode_log_analyzer.py
python3 opencode_log_analyzer.py < export.json
python3 opencode_log_analyzer.py <sessionID>
"""
import json
import sqlite3
import subprocess
import sys
import tempfile
from pathlib import Path
DB_PATH = Path.home() / ".local" / "share" / "opencode" / "opencode.db"
OPENCODE_BIN = Path.home() / ".opencode" / "bin" / "opencode"
def find_db() -> Path:
"""Find the opencode session database."""
candidates = [
Path.home() / ".local" / "share" / "opencode" / "opencode.db",
Path.home() / ".opencode" / "data" / "opencode.db",
]
for c in candidates:
if c.exists():
return c
return DB_PATH
def get_child_ids(session_id: str) -> list[str]:
"""Query the session DB for direct child sessions."""
db = find_db()
if not db.exists():
return []
try:
conn = sqlite3.connect(str(db))
rows = conn.execute(
"SELECT id FROM session WHERE parent_id = ?", (session_id,)
).fetchall()
conn.close()
return [r[0] for r in rows]
except Exception:
return []
def get_session_info(session_id: str) -> dict:
"""Get token counts from DB for a session."""
db = find_db()
if not db.exists():
return {}
try:
conn = sqlite3.connect(str(db))
row = conn.execute(
"SELECT tokens_input, tokens_output, tokens_reasoning, "
"tokens_cache_read, tokens_cache_write, "
"time_created, time_updated "
"FROM session WHERE id = ?",
(session_id,),
).fetchone()
conn.close()
if row:
return {
"tokens": {
"input": row[0] or 0,
"output": row[1] or 0,
"reasoning": row[2] or 0,
"cache": {
"read": row[3] or 0,
"write": row[4] or 0,
},
},
"time": {
"created": row[5] or 0,
"updated": row[6] or 0,
},
}
except Exception:
pass
return {}
def export_session(session_id: str) -> dict:
"""Export a session via the opencode CLI into a temp file (avoids pipe buffer limits)."""
tmp = ""
try:
with tempfile.NamedTemporaryFile(suffix=".json", delete=False, mode="w") as f:
tmp = f.name
subprocess.run(
[str(OPENCODE_BIN), "export", session_id, "--sanitize"],
stdout=open(tmp, "w"), stderr=subprocess.DEVNULL, timeout=60,
)
with open(tmp) as f:
data = json.load(f)
return data
except Exception:
return {}
finally:
if tmp:
Path(tmp).unlink(missing_ok=True)
def parse_session(data: dict) -> dict:
"""Extract tool times, counts, thinking time, and token info from session data."""
info = data.get("info", {})
session_tokens = info.get("tokens", {})
tool_times: dict[str, float] = {}
tool_counts: dict[str, int] = {}
thinking_time = 0.0
first_time = None
last_time = None
for msg in data.get("messages", []):
msg_info = msg.get("info", {})
msg_time = msg_info.get("time", {})
mc = msg_time.get("created")
if mc and (first_time is None or mc < first_time):
first_time = mc
mcomp = msg_time.get("completed")
if mcomp and (last_time is None or mcomp > last_time):
last_time = mcomp
for part in msg.get("parts", []):
ptype = part.get("type")
if ptype == "tool":
tool_name = part.get("tool", "unknown")
state = part.get("state", {})
t = state.get("time", {})
start = t.get("start")
end = t.get("end")
if start and end:
dur = end - start
tool_times[tool_name] = tool_times.get(tool_name, 0) + dur
tool_counts[tool_name] = tool_counts.get(tool_name, 0) + 1
elif ptype == "reasoning":
t = part.get("time", {})
start = t.get("start")
end = t.get("end")
if start and end:
thinking_time += end - start
return {
"tool_times": tool_times,
"tool_counts": tool_counts,
"thinking_time": thinking_time,
"session_tokens": session_tokens,
"first_time": first_time,
"last_time": last_time,
"info_time": {
"created": info.get("time", {}).get("created"),
"updated": info.get("time", {}).get("updated"),
},
}
def merge_results(results: list[dict]) -> dict:
"""Merge multiple parse results together."""
merged = {
"tool_times": {},
"tool_counts": {},
"thinking_time": 0.0,
"session_tokens": {
"input": 0, "output": 0, "reasoning": 0,
"cache": {"read": 0, "write": 0},
},
"first_time": None,
"last_time": None,
"info_time": {"created": None, "updated": None},
}
for r in results:
for tool, t in r["tool_times"].items():
merged["tool_times"][tool] = merged["tool_times"].get(tool, 0) + t
for tool, c in r["tool_counts"].items():
merged["tool_counts"][tool] = merged["tool_counts"].get(tool, 0) + c
merged["thinking_time"] += r["thinking_time"]
st = r.get("session_tokens", {})
if st:
merged["session_tokens"]["input"] += st.get("input", 0)
merged["session_tokens"]["output"] += st.get("output", 0)
merged["session_tokens"]["reasoning"] += st.get("reasoning", 0)
cache = st.get("cache", {})
if cache:
merged["session_tokens"]["cache"]["read"] += cache.get("read", 0)
merged["session_tokens"]["cache"]["write"] += cache.get("write", 0)
ft = r.get("first_time")
if ft and (merged["first_time"] is None or ft < merged["first_time"]):
merged["first_time"] = ft
lt = r.get("last_time")
if lt and (merged["last_time"] is None or lt > merged["last_time"]):
merged["last_time"] = lt
it = r.get("info_time", {})
ic = it.get("created")
if ic and (merged["info_time"]["created"] is None or ic < merged["info_time"]["created"]):
merged["info_time"]["created"] = ic
iu = it.get("updated")
if iu and (merged["info_time"]["updated"] is None or iu > merged["info_time"]["updated"]):
merged["info_time"]["updated"] = iu
return merged
def print_results(merged: dict, label: str = ""):
"""Print formatted results."""
tool_times = merged["tool_times"]
tool_counts = merged["tool_counts"]
thinking_time = merged["thinking_time"]
session_tokens = merged["session_tokens"]
first_time = merged["first_time"]
last_time = merged["last_time"]
info_time = merged["info_time"]
all_tools = sum(tool_times.values())
if label:
print(f"=== {label} ===")
print()
total_time_ms = 0
created = info_time.get("created")
updated = info_time.get("updated")
if created and updated:
total_time_ms = updated - created
print(f"{'Metric':<30} {'Value':>15}")
print("-" * 47)
if total_time_ms > 0:
print(f"{'Total session time':<30} {total_time_ms / 1000:>15.3f}s")
elif first_time and last_time:
print(f"{'Total session time':<30} {(last_time - first_time) / 1000:>15.3f}s")
else:
print(f"{'Total session time':<30} {'N/A':>15}")
print(f"{'Grep tool total':<30} {tool_times.get('grep', 0) / 1000:>15.3f}s")
print(f"{'LSP tool total':<30} {tool_times.get('lsp', 0) / 1000:>15.3f}s")
print(f"{'All tools total':<30} {all_tools / 1000:>15.3f}s")
print(f"{'Thinking total':<30} {thinking_time / 1000:>15.3f}s")
if session_tokens:
inp = session_tokens.get("input", 0)
out = session_tokens.get("output", 0)
reas = session_tokens.get("reasoning", 0)
cache_read = session_tokens.get("cache", {}).get("read", 0)
cache_write = session_tokens.get("cache", {}).get("write", 0)
total_tok = inp + out + reas
print()
print(f"{'Token':<30} {'Count':>15}")
print("-" * 47)
print(f"{'Input':<30} {inp:>15,}")
print(f"{'Output':<30} {out:>15,}")
print(f"{'Reasoning':<30} {reas:>15,}")
print(f"{'Total (input+output+reasoning)':<30} {total_tok:>15,}")
if cache_read:
print(f"{'Cache read':<30} {cache_read:>15,}")
if cache_write:
print(f"{'Cache write':<30} {cache_write:>15,}")
print()
print(f"{'Tool':<20} {'Calls':>7} {'Total Time':>12}")
print("-" * 40)
for tool_name in sorted(tool_times, key=lambda t: tool_times[t], reverse=True):
print(f"{tool_name:<20} {tool_counts.get(tool_name, 0):>7} {tool_times[tool_name] / 1000:>12.3f}s")
def process_session(session_id: str, depth: int = 0) -> list[dict]:
"""Recursively process a session and all its children."""
data = export_session(session_id)
if not data:
return []
result = parse_session(data)
child_ids = get_child_ids(session_id)
all_results = [result]
for cid in child_ids:
child_results = process_session(cid, depth + 1)
all_results.extend(child_results)
return all_results
def main():
args = sys.argv[1:]
# Pipe mode: opencode export <id> | python3 script.py
if not sys.stdin.isatty() and not args:
tmp = ""
try:
with tempfile.NamedTemporaryFile(suffix=".json", delete=False, mode="wb") as f:
tmp = f.name
f.write(sys.stdin.buffer.read())
with open(tmp) as f:
data = json.load(f)
except json.JSONDecodeError as e:
if tmp:
sys.stderr.write(
f"Error: JSON parse failed. Piped data might be truncated "
f"(pipe buffer limit on macOS is 128KB). "
f"Raw data saved to {tmp} for inspection.\n"
f"Try: python3 script.py <sessionID> instead.\n"
)
sys.exit(1)
finally:
if tmp:
Path(tmp).unlink(missing_ok=True)
result = parse_session(data)
print_results(result)
return
# Session ID mode: python3 script.py <sessionID>
if args:
session_id = args[0]
all_results = process_session(session_id)
if not all_results:
print(f"Error: could not export session '{session_id}'", file=sys.stderr)
sys.exit(1)
nchild = len(all_results) - 1
label = f"Session {session_id}"
if nchild > 0:
label += f" (including {nchild} child session{'s' if nchild > 1 else ''})"
merged = merge_results(all_results)
print_results(merged, label=label)
return
# No input
print(__doc__, file=sys.stderr)
sys.exit(1)
if __name__ == "__main__":
main()