-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanalytics.go
More file actions
132 lines (109 loc) · 3.46 KB
/
analytics.go
File metadata and controls
132 lines (109 loc) · 3.46 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
// SPDX-Licence-Identifier: EUPL-1.2
package session
import (
"fmt"
"maps"
"slices"
"strings"
"time"
)
// SessionAnalytics holds computed metrics for a parsed session.
type SessionAnalytics struct {
Duration time.Duration
ActiveTime time.Duration
EventCount int
ToolCounts map[string]int
ErrorCounts map[string]int
SuccessRate float64
AvgLatency map[string]time.Duration
MaxLatency map[string]time.Duration
EstimatedInputTokens int
EstimatedOutputTokens int
}
// Analyse iterates session events and computes analytics. Pure function, no I/O.
func Analyse(sess *Session) *SessionAnalytics {
a := &SessionAnalytics{
ToolCounts: make(map[string]int),
ErrorCounts: make(map[string]int),
AvgLatency: make(map[string]time.Duration),
MaxLatency: make(map[string]time.Duration),
}
if sess == nil {
return a
}
a.Duration = sess.EndTime.Sub(sess.StartTime)
a.EventCount = len(sess.Events)
// Track totals for latency averaging
type latencyAccum struct {
total time.Duration
count int
}
latencies := make(map[string]*latencyAccum)
var totalToolCalls int
var totalErrors int
for evt := range sess.EventsSeq() {
// Token estimation: ~4 chars per token
a.EstimatedInputTokens += len(evt.Input) / 4
a.EstimatedOutputTokens += len(evt.Output) / 4
if evt.Type != "tool_use" {
continue
}
totalToolCalls++
a.ToolCounts[evt.Tool]++
if !evt.Success {
totalErrors++
a.ErrorCounts[evt.Tool]++
}
// Active time: sum of tool call durations
a.ActiveTime += evt.Duration
// Latency tracking
if _, ok := latencies[evt.Tool]; !ok {
latencies[evt.Tool] = &latencyAccum{}
}
latencies[evt.Tool].total += evt.Duration
latencies[evt.Tool].count++
if evt.Duration > a.MaxLatency[evt.Tool] {
a.MaxLatency[evt.Tool] = evt.Duration
}
}
// Compute averages
for tool, acc := range latencies {
if acc.count > 0 {
a.AvgLatency[tool] = acc.total / time.Duration(acc.count)
}
}
// Success rate
if totalToolCalls > 0 {
a.SuccessRate = float64(totalToolCalls-totalErrors) / float64(totalToolCalls)
}
return a
}
// FormatAnalytics returns a tabular text summary suitable for CLI display.
func FormatAnalytics(a *SessionAnalytics) string {
var b strings.Builder
b.WriteString("Session Analytics\n")
b.WriteString(strings.Repeat("=", 50) + "\n\n")
b.WriteString(fmt.Sprintf(" Duration: %s\n", formatDuration(a.Duration)))
b.WriteString(fmt.Sprintf(" Active Time: %s\n", formatDuration(a.ActiveTime)))
b.WriteString(fmt.Sprintf(" Events: %d\n", a.EventCount))
b.WriteString(fmt.Sprintf(" Success Rate: %.1f%%\n", a.SuccessRate*100))
b.WriteString(fmt.Sprintf(" Est. Input Tk: %d\n", a.EstimatedInputTokens))
b.WriteString(fmt.Sprintf(" Est. Output Tk: %d\n", a.EstimatedOutputTokens))
if len(a.ToolCounts) > 0 {
b.WriteString("\n Tool Breakdown\n")
b.WriteString(" " + strings.Repeat("-", 48) + "\n")
b.WriteString(fmt.Sprintf(" %-14s %6s %6s %10s %10s\n",
"Tool", "Calls", "Errors", "Avg", "Max"))
b.WriteString(" " + strings.Repeat("-", 48) + "\n")
// Sort tools for deterministic output
for _, tool := range slices.Sorted(maps.Keys(a.ToolCounts)) {
errors := a.ErrorCounts[tool]
avg := a.AvgLatency[tool]
max := a.MaxLatency[tool]
b.WriteString(fmt.Sprintf(" %-14s %6d %6d %10s %10s\n",
tool, a.ToolCounts[tool], errors,
formatDuration(avg), formatDuration(max)))
}
}
return b.String()
}