forked from DFanso/commit-msg
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstats.go
More file actions
195 lines (160 loc) · 6.44 KB
/
stats.go
File metadata and controls
195 lines (160 loc) · 6.44 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
package cmd
import (
"fmt"
"sort"
"time"
"github.com/dfanso/commit-msg/cmd/cli/store"
"github.com/dfanso/commit-msg/pkg/types"
"github.com/pterm/pterm"
"github.com/spf13/cobra"
)
// statsCmd represents the statistics command
var statsCmd = &cobra.Command{
Use: "stats",
Short: "Display usage statistics",
Long: `Display comprehensive usage statistics including:
- Most used LLM provider
- Average generation time
- Success/failure rates
- Token usage per provider
- Cache hit rates
- Cost tracking`,
RunE: func(cmd *cobra.Command, args []string) error {
Store, err := store.NewStoreMethods()
if err != nil {
return fmt.Errorf("failed to initialize store: %w", err)
}
reset, _ := cmd.Flags().GetBool("reset")
if reset {
if err := resetStatistics(Store); err != nil {
return err
}
return nil
}
detailed, _ := cmd.Flags().GetBool("detailed")
return displayStatistics(Store, detailed)
},
}
func init() {
statsCmd.Flags().Bool("detailed", false, "Show detailed per-provider statistics")
statsCmd.Flags().Bool("reset", false, "Reset all usage statistics")
}
func displayStatistics(store *store.StoreMethods, detailed bool) error {
stats := store.GetUsageStats()
if stats.TotalGenerations == 0 {
pterm.Info.Println("No usage statistics available yet.")
pterm.Info.Println("Statistics will be collected as you use the commit message generator.")
return nil
}
// Header
pterm.DefaultHeader.WithFullWidth().
WithBackgroundStyle(pterm.NewStyle(pterm.BgBlue)).
WithTextStyle(pterm.NewStyle(pterm.FgWhite, pterm.Bold)).
Println("Usage Statistics")
pterm.Println()
// Overall Statistics
pterm.DefaultSection.WithLevel(2).Println("Overall Statistics")
overallData := [][]string{
{"Total Generations", fmt.Sprintf("%d", stats.TotalGenerations)},
{"Successful Generations", fmt.Sprintf("%d (%.1f%%)", stats.SuccessfulGenerations, store.GetOverallSuccessRate())},
{"Failed Generations", fmt.Sprintf("%d (%.1f%%)", stats.FailedGenerations, float64(stats.FailedGenerations)/float64(stats.TotalGenerations)*100)},
{"Average Generation Time", fmt.Sprintf("%.1f ms", stats.AverageGenerationTime)},
{"Total Cost", fmt.Sprintf("$%.4f", stats.TotalCost)},
{"Total Tokens Used", fmt.Sprintf("%d", stats.TotalTokensUsed)},
}
if stats.CacheHits > 0 || stats.CacheMisses > 0 {
cacheRate := store.GetCacheHitRate()
overallData = append(overallData, []string{"Cache Hit Rate", fmt.Sprintf("%.1f%% (%d hits, %d misses)", cacheRate, stats.CacheHits, stats.CacheMisses)})
}
if stats.FirstUse != "" {
if firstUse, err := time.Parse(time.RFC3339, stats.FirstUse); err == nil {
overallData = append(overallData, []string{"First Use", firstUse.Local().Format("Jan 2, 2006 15:04")})
}
}
if stats.LastUse != "" {
if lastUse, err := time.Parse(time.RFC3339, stats.LastUse); err == nil {
overallData = append(overallData, []string{"Last Use", lastUse.Local().Format("Jan 2, 2006 15:04")})
}
}
pterm.DefaultTable.WithHasHeader(false).WithData(overallData).Render()
pterm.Println()
// Provider Rankings
if len(stats.ProviderStats) > 0 {
pterm.DefaultSection.WithLevel(2).Println("Provider Rankings")
ranking := store.GetProviderRanking()
rankingData := [][]string{{"Rank", "Provider", "Uses", "Success Rate", "Avg Time (ms)", "Total Cost"}}
for i, provider := range ranking {
providerStats := stats.ProviderStats[provider]
rankingData = append(rankingData, []string{
fmt.Sprintf("#%d", i+1),
string(provider),
fmt.Sprintf("%d", providerStats.TotalUses),
fmt.Sprintf("%.1f%%", providerStats.SuccessRate),
fmt.Sprintf("%.1f", providerStats.AverageGenerationTime),
fmt.Sprintf("$%.4f", providerStats.TotalCost),
})
}
pterm.DefaultTable.WithHasHeader(true).WithData(rankingData).Render()
pterm.Println()
}
// Detailed Provider Statistics
if detailed && len(stats.ProviderStats) > 0 {
pterm.DefaultSection.WithLevel(2).Println("Detailed Provider Statistics")
// Sort providers alphabetically for consistent display
var providers []types.LLMProvider
for provider := range stats.ProviderStats {
providers = append(providers, provider)
}
sort.Slice(providers, func(i, j int) bool {
return string(providers[i]) < string(providers[j])
})
for _, provider := range providers {
providerStats := stats.ProviderStats[provider]
pterm.DefaultSection.WithLevel(3).Printf("%s Details", provider)
providerData := [][]string{
{"Total Uses", fmt.Sprintf("%d", providerStats.TotalUses)},
{"Successful Uses", fmt.Sprintf("%d", providerStats.SuccessfulUses)},
{"Failed Uses", fmt.Sprintf("%d", providerStats.FailedUses)},
{"Success Rate", fmt.Sprintf("%.1f%%", providerStats.SuccessRate)},
{"Average Generation Time", fmt.Sprintf("%.1f ms", providerStats.AverageGenerationTime)},
{"Total Cost", fmt.Sprintf("$%.4f", providerStats.TotalCost)},
{"Total Tokens Used", fmt.Sprintf("%d", providerStats.TotalTokensUsed)},
}
if providerStats.FirstUsed != "" {
if firstUsed, err := time.Parse(time.RFC3339, providerStats.FirstUsed); err == nil {
providerData = append(providerData, []string{"First Used", firstUsed.Local().Format("Jan 2, 2006 15:04")})
}
}
if providerStats.LastUsed != "" {
if lastUsed, err := time.Parse(time.RFC3339, providerStats.LastUsed); err == nil {
providerData = append(providerData, []string{"Last Used", lastUsed.Local().Format("Jan 2, 2006 15:04")})
}
}
pterm.DefaultTable.WithHasHeader(false).WithData(providerData).Render()
pterm.Println()
}
}
// Show tips
pterm.DefaultSection.WithLevel(2).Println("Tips")
pterm.Info.Println("• Use --detailed flag to see comprehensive per-provider statistics")
pterm.Info.Println("• Statistics help identify your most reliable and cost-effective providers")
pterm.Info.Println("• Cache hits save both time and API costs")
pterm.Info.Println("• Use --reset flag to clear all statistics (irreversible)")
return nil
}
func resetStatistics(store *store.StoreMethods) error {
pterm.Warning.Println("This will permanently delete all usage statistics.")
confirm, _ := pterm.DefaultInteractiveConfirm.
WithDefaultValue(false).
WithDefaultText("Are you sure you want to reset all statistics?").
Show()
if !confirm {
pterm.Info.Println("Statistics reset cancelled.")
return nil
}
if err := store.ResetUsageStats(); err != nil {
return fmt.Errorf("failed to reset statistics: %w", err)
}
pterm.Success.Println("All usage statistics have been reset.")
return nil
}