|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "time" |
| 6 | + |
| 7 | + "github.com/dfanso/commit-msg/cmd/cli/store" |
| 8 | + "github.com/pterm/pterm" |
| 9 | +) |
| 10 | + |
| 11 | +// ShowCacheStats displays cache statistics. |
| 12 | +func ShowCacheStats(Store *store.StoreMethods) error { |
| 13 | + stats := Store.GetCacheStats() |
| 14 | + |
| 15 | + pterm.DefaultHeader.WithFullWidth(). |
| 16 | + WithBackgroundStyle(pterm.NewStyle(pterm.BgBlue)). |
| 17 | + WithTextStyle(pterm.NewStyle(pterm.FgWhite, pterm.Bold)). |
| 18 | + Println("Commit Message Cache Statistics") |
| 19 | + |
| 20 | + pterm.Println() |
| 21 | + |
| 22 | + // Create statistics table |
| 23 | + statsData := [][]string{ |
| 24 | + {"Total Entries", fmt.Sprintf("%d", stats.TotalEntries)}, |
| 25 | + {"Cache Hits", fmt.Sprintf("%d", stats.TotalHits)}, |
| 26 | + {"Cache Misses", fmt.Sprintf("%d", stats.TotalMisses)}, |
| 27 | + {"Hit Rate", fmt.Sprintf("%.2f%%", stats.HitRate*100)}, |
| 28 | + {"Total Cost Saved", fmt.Sprintf("$%.4f", stats.TotalCostSaved)}, |
| 29 | + {"Cache Size", formatBytes(stats.CacheSizeBytes)}, |
| 30 | + } |
| 31 | + |
| 32 | + if stats.OldestEntry != "" { |
| 33 | + statsData = append(statsData, []string{"Oldest Entry", formatTime(stats.OldestEntry)}) |
| 34 | + } |
| 35 | + |
| 36 | + if stats.NewestEntry != "" { |
| 37 | + statsData = append(statsData, []string{"Newest Entry", formatTime(stats.NewestEntry)}) |
| 38 | + } |
| 39 | + |
| 40 | + pterm.DefaultTable.WithHasHeader(false).WithData(statsData).Render() |
| 41 | + |
| 42 | + pterm.Println() |
| 43 | + |
| 44 | + // Show cache status |
| 45 | + if stats.TotalEntries == 0 { |
| 46 | + pterm.Info.Println("Cache is empty. Generate some commit messages to start building the cache.") |
| 47 | + } else { |
| 48 | + pterm.Success.Printf("Cache is active with %d entries\n", stats.TotalEntries) |
| 49 | + |
| 50 | + if stats.HitRate > 0 { |
| 51 | + pterm.Info.Printf("Cache hit rate: %.2f%% (%.0f%% of requests served from cache)\n", |
| 52 | + stats.HitRate*100, stats.HitRate*100) |
| 53 | + } |
| 54 | + |
| 55 | + if stats.TotalCostSaved > 0 { |
| 56 | + pterm.Success.Printf("Total cost saved: $%.4f\n", stats.TotalCostSaved) |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + return nil |
| 61 | +} |
| 62 | + |
| 63 | +// ClearCache removes all cached messages. |
| 64 | +func ClearCache(Store *store.StoreMethods) error { |
| 65 | + pterm.DefaultHeader.WithFullWidth(). |
| 66 | + WithBackgroundStyle(pterm.NewStyle(pterm.BgRed)). |
| 67 | + WithTextStyle(pterm.NewStyle(pterm.FgWhite, pterm.Bold)). |
| 68 | + Println("Clear Cache") |
| 69 | + |
| 70 | + pterm.Println() |
| 71 | + |
| 72 | + // Get current stats before clearing |
| 73 | + stats := Store.GetCacheStats() |
| 74 | + |
| 75 | + if stats.TotalEntries == 0 { |
| 76 | + pterm.Info.Println("Cache is already empty.") |
| 77 | + return nil |
| 78 | + } |
| 79 | + |
| 80 | + // Confirm before clearing |
| 81 | + confirm, err := pterm.DefaultInteractiveConfirm. |
| 82 | + WithDefaultValue(false). |
| 83 | + Show(fmt.Sprintf("Are you sure you want to clear %d cached entries? This action cannot be undone.", stats.TotalEntries)) |
| 84 | + |
| 85 | + if err != nil { |
| 86 | + return fmt.Errorf("failed to get confirmation: %w", err) |
| 87 | + } |
| 88 | + |
| 89 | + if !confirm { |
| 90 | + pterm.Info.Println("Cache clear cancelled.") |
| 91 | + return nil |
| 92 | + } |
| 93 | + |
| 94 | + // Clear the cache |
| 95 | + if err := Store.ClearCache(); err != nil { |
| 96 | + return fmt.Errorf("failed to clear cache: %w", err) |
| 97 | + } |
| 98 | + |
| 99 | + pterm.Success.Println("Cache cleared successfully!") |
| 100 | + pterm.Info.Printf("Removed %d entries and saved $%.4f in future API costs\n", |
| 101 | + stats.TotalEntries, stats.TotalCostSaved) |
| 102 | + |
| 103 | + return nil |
| 104 | +} |
| 105 | + |
| 106 | +// CleanupCache removes old cached messages. |
| 107 | +func CleanupCache(Store *store.StoreMethods) error { |
| 108 | + pterm.DefaultHeader.WithFullWidth(). |
| 109 | + WithBackgroundStyle(pterm.NewStyle(pterm.BgYellow)). |
| 110 | + WithTextStyle(pterm.NewStyle(pterm.FgBlack, pterm.Bold)). |
| 111 | + Println("Cleanup Cache") |
| 112 | + |
| 113 | + pterm.Println() |
| 114 | + |
| 115 | + // Get stats before cleanup |
| 116 | + statsBefore := Store.GetCacheStats() |
| 117 | + |
| 118 | + if statsBefore.TotalEntries == 0 { |
| 119 | + pterm.Info.Println("Cache is empty. Nothing to cleanup.") |
| 120 | + return nil |
| 121 | + } |
| 122 | + |
| 123 | + pterm.Info.Println("Removing old and unused cached entries...") |
| 124 | + |
| 125 | + // Perform cleanup |
| 126 | + if err := Store.CleanupCache(); err != nil { |
| 127 | + return fmt.Errorf("failed to cleanup cache: %w", err) |
| 128 | + } |
| 129 | + |
| 130 | + // Get stats after cleanup |
| 131 | + statsAfter := Store.GetCacheStats() |
| 132 | + removed := statsBefore.TotalEntries - statsAfter.TotalEntries |
| 133 | + |
| 134 | + if removed > 0 { |
| 135 | + pterm.Success.Printf("Cleanup completed! Removed %d old entries.\n", removed) |
| 136 | + pterm.Info.Printf("Cache now contains %d entries\n", statsAfter.TotalEntries) |
| 137 | + } else { |
| 138 | + pterm.Info.Println("No old entries found to remove.") |
| 139 | + } |
| 140 | + |
| 141 | + return nil |
| 142 | +} |
| 143 | + |
| 144 | +// Helper functions |
| 145 | + |
| 146 | +// formatBytes formats bytes into human-readable format. |
| 147 | +func formatBytes(bytes int64) string { |
| 148 | + const unit = 1024 |
| 149 | + if bytes < unit { |
| 150 | + return fmt.Sprintf("%d B", bytes) |
| 151 | + } |
| 152 | + div, exp := int64(unit), 0 |
| 153 | + for n := bytes / unit; n >= unit; n /= unit { |
| 154 | + div *= unit |
| 155 | + exp++ |
| 156 | + } |
| 157 | + return fmt.Sprintf("%.1f %cB", float64(bytes)/float64(div), "KMGTPE"[exp]) |
| 158 | +} |
| 159 | + |
| 160 | +// formatTime formats a timestamp string for display. |
| 161 | +func formatTime(timestamp string) string { |
| 162 | + t, err := time.Parse(time.RFC3339, timestamp) |
| 163 | + if err != nil { |
| 164 | + return timestamp |
| 165 | + } |
| 166 | + return t.Format("2006-01-02 15:04:05") |
| 167 | +} |
0 commit comments