-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathview_logs.py
More file actions
316 lines (249 loc) · 10.5 KB
/
view_logs.py
File metadata and controls
316 lines (249 loc) · 10.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
#!/usr/bin/env python3
"""
Log Viewer and Analyzer
View and analyze audit logs from agent testing sessions.
"""
import json
import sys
from pathlib import Path
from datetime import datetime
from collections import defaultdict
try:
from rich.console import Console
from rich.table import Table
from rich.panel import Panel
from rich.syntax import Syntax
from rich.prompt import Prompt
from rich import print as rprint
except ImportError:
print("Please install rich: pip install rich")
sys.exit(1)
console = Console()
# Logs directory
LOGS_DIR = Path(__file__).parent.parent / "logs" / "audit"
def list_log_files():
"""List all available log files."""
if not LOGS_DIR.exists():
console.print("[yellow]No logs directory found[/yellow]")
return []
log_files = sorted(LOGS_DIR.glob("*.jsonl"), key=lambda x: x.stat().st_mtime, reverse=True)
return log_files
def display_log_list():
"""Display list of log files."""
log_files = list_log_files()
if not log_files:
console.print("[yellow]No log files found[/yellow]")
return None
table = Table(title="Available Audit Logs", show_header=True)
table.add_column("#", style="cyan", width=4)
table.add_column("Timestamp", style="yellow")
table.add_column("Agent", style="green")
table.add_column("Session ID", style="blue")
table.add_column("Size", style="magenta")
table.add_column("Path", style="dim")
for i, log_file in enumerate(log_files[:20], 1): # Show last 20
parts = log_file.stem.split("_")
if len(parts) >= 3:
agent_name = parts[0]
session_id = parts[1]
timestamp_str = "_".join(parts[2:])
size = log_file.stat().st_size
# Parse timestamp
try:
timestamp = datetime.strptime(timestamp_str, "%Y%m%d_%H%M%S")
time_display = timestamp.strftime("%Y-%m-%d %H:%M:%S")
except:
time_display = timestamp_str
table.add_row(
str(i),
time_display,
agent_name,
session_id,
f"{size:,}B",
str(log_file.relative_to(LOGS_DIR.parent))
)
console.print(table)
return log_files[:20]
def load_log_file(log_file: Path):
"""Load and parse a JSONL log file."""
entries = []
with open(log_file, "r") as f:
for line in f:
try:
entries.append(json.loads(line))
except json.JSONDecodeError:
continue
return entries
def analyze_log(entries: list):
"""Analyze log entries and generate statistics."""
stats = {
"total_entries": len(entries),
"events": defaultdict(int),
"errors": [],
"metrics": {},
"timeline": []
}
start_time = None
end_time = None
total_agent_duration = 0
agent_calls = 0
for entry in entries:
event = entry.get("event", "unknown")
stats["events"][event] += 1
timestamp = entry.get("timestamp")
if timestamp:
dt = datetime.fromisoformat(timestamp)
if start_time is None:
start_time = dt
end_time = dt
if event == "error":
stats["errors"].append({
"timestamp": timestamp,
"type": entry.get("error_type"),
"message": entry.get("error_message")
})
elif event == "agent_output":
duration = entry.get("duration_seconds", 0)
total_agent_duration += duration
agent_calls += 1
elif event == "metric":
metric_name = entry.get("metric_name")
stats["metrics"][metric_name] = entry.get("value")
stats["timeline"].append({
"timestamp": timestamp,
"event": event
})
# Calculate session duration
if start_time and end_time:
stats["session_duration"] = (end_time - start_time).total_seconds()
stats["start_time"] = start_time.isoformat()
stats["end_time"] = end_time.isoformat()
# Calculate averages
if agent_calls > 0:
stats["avg_agent_duration"] = total_agent_duration / agent_calls
stats["total_agent_duration"] = total_agent_duration
stats["agent_calls"] = agent_calls
stats["success_rate"] = (agent_calls / max(stats["events"].get("agent_input", 1), 1)) * 100
return stats
def display_log_analysis(stats: dict):
"""Display log analysis."""
# Session summary
console.print("\n[bold cyan]Session Summary[/bold cyan]")
summary_table = Table(show_header=False, box=None)
summary_table.add_column("Metric", style="yellow")
summary_table.add_column("Value", style="green")
if "start_time" in stats:
summary_table.add_row("Start Time", stats["start_time"])
summary_table.add_row("End Time", stats["end_time"])
summary_table.add_row("Duration", f"{stats.get('session_duration', 0):.2f}s")
summary_table.add_row("Total Entries", str(stats["total_entries"]))
summary_table.add_row("Agent Calls", str(stats.get("agent_calls", 0)))
summary_table.add_row("Success Rate", f"{stats.get('success_rate', 0):.1f}%")
if "avg_agent_duration" in stats:
summary_table.add_row("Avg Call Duration", f"{stats['avg_agent_duration']:.2f}s")
console.print(summary_table)
# Event breakdown
console.print("\n[bold cyan]Event Breakdown[/bold cyan]")
events_table = Table(show_header=True)
events_table.add_column("Event Type", style="yellow")
events_table.add_column("Count", style="green", justify="right")
for event, count in sorted(stats["events"].items(), key=lambda x: x[1], reverse=True):
events_table.add_row(event, str(count))
console.print(events_table)
# Errors
if stats["errors"]:
console.print("\n[bold red]Errors[/bold red]")
for error in stats["errors"]:
console.print(Panel(
f"[red]{error['type']}:[/red] {error['message']}",
title=error['timestamp'],
border_style="red"
))
# Metrics
if stats["metrics"]:
console.print("\n[bold cyan]Custom Metrics[/bold cyan]")
metrics_table = Table(show_header=False, box=None)
metrics_table.add_column("Metric", style="yellow")
metrics_table.add_column("Value", style="green")
for metric, value in stats["metrics"].items():
metrics_table.add_row(metric, str(value))
console.print(metrics_table)
def display_log_entries(entries: list, event_filter: str = None):
"""Display log entries."""
filtered = entries if not event_filter else [e for e in entries if e.get("event") == event_filter]
for i, entry in enumerate(filtered, 1):
event = entry.get("event", "unknown")
timestamp = entry.get("timestamp", "")
if event == "agent_input":
console.print(f"\n[yellow]{i}. AGENT INPUT[/yellow] [{timestamp}]")
console.print(f"Function: {entry.get('function')}")
console.print(Syntax(json.dumps(entry.get('parameters', {}), indent=2), "json"))
elif event == "agent_output":
console.print(f"\n[green]{i}. AGENT OUTPUT[/green] [{timestamp}]")
console.print(f"Duration: {entry.get('duration_seconds', 0):.2f}s")
console.print(Syntax(json.dumps(entry.get('result', {}), indent=2), "json"))
elif event == "error":
console.print(f"\n[red]{i}. ERROR[/red] [{timestamp}]")
console.print(f"Type: {entry.get('error_type')}")
console.print(f"Message: {entry.get('error_message')}")
elif event == "conversation_turn":
console.print(f"\n[cyan]{i}. CONVERSATION TURN {entry.get('turn_number')}[/cyan] [{timestamp}]")
console.print(f"[blue]User:[/blue] {entry.get('user_input')}")
console.print(f"[green]Agent:[/green] {entry.get('agent_response')[:200]}...")
elif event == "session_start":
console.print(f"\n[bold cyan]{i}. SESSION START[/bold cyan] [{timestamp}]")
console.print(f"Agent: {entry.get('agent_name')}")
console.print(f"Session ID: {entry.get('session_id')}")
elif event == "session_end":
console.print(f"\n[bold cyan]{i}. SESSION END[/bold cyan] [{timestamp}]")
console.print(f"Duration: {entry.get('duration_seconds', 0):.2f}s")
console.print(f"Total Entries: {entry.get('total_entries', 0)}")
def main():
"""Main entry point."""
console.print(Panel.fit(
"[bold cyan]Sygaldry Log Viewer & Analyzer[/bold cyan]\n\n"
"View and analyze audit logs from agent testing sessions",
border_style="cyan"
))
while True:
log_files = display_log_list()
if not log_files:
break
console.print("\n[cyan]Options:[/cyan]")
console.print(" Enter log number to view details")
console.print(" Enter 'q' to quit")
choice = Prompt.ask("\nYour choice").strip()
if choice.lower() == 'q':
break
if choice.isdigit() and 1 <= int(choice) <= len(log_files):
log_file = log_files[int(choice) - 1]
console.print(f"\n[cyan]Loading: {log_file.name}[/cyan]\n")
entries = load_log_file(log_file)
stats = analyze_log(entries)
# Display analysis
display_log_analysis(stats)
# Ask what to do next
console.print("\n[cyan]View options:[/cyan]")
console.print(" 1. View all entries")
console.print(" 2. View inputs only")
console.print(" 3. View outputs only")
console.print(" 4. View errors only")
console.print(" 5. View conversation turns")
console.print(" 6. Back to log list")
view_choice = Prompt.ask("\nView option", default="6")
if view_choice == "1":
display_log_entries(entries)
elif view_choice == "2":
display_log_entries(entries, "agent_input")
elif view_choice == "3":
display_log_entries(entries, "agent_output")
elif view_choice == "4":
display_log_entries(entries, "error")
elif view_choice == "5":
display_log_entries(entries, "conversation_turn")
input("\nPress Enter to continue...")
if __name__ == "__main__":
try:
main()
except KeyboardInterrupt:
console.print("\n[cyan]Interrupted by user[/cyan]")