From 38edd735b645b97e5195a3e980c9b7b2cf8ceac7 Mon Sep 17 00:00:00 2001 From: Lukasz Raczylo Date: Sun, 4 Jan 2026 13:44:36 +0000 Subject: [PATCH] fix: improve download statistics tracking resilience Problem: - Download counts were not incrementing when packages existed in storage but not in the database (e.g., after database migration/reset) - UpdateDownloadCount() was failing silently when package didn't exist in DB - Error was completely ignored despite comment claiming "error logged" Changes: 1. Log errors when UpdateDownloadCount() fails instead of silently ignoring 2. Auto-save package to database if UpdateDownloadCount() fails 3. Retry download count update after saving package 4. Provide detailed error logging for troubleshooting This fixes the issue where: - Database is migrated but storage still has cached packages - Cache hits occur but download events aren't recorded - Statistics show zero downloads despite actual usage Resolves user report: "I cleaned go mod which redownloaded the modules through the proxy but counters did not increased" --- pkg/cache/cache.go | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/pkg/cache/cache.go b/pkg/cache/cache.go index 4c32ab2..33968af 100644 --- a/pkg/cache/cache.go +++ b/pkg/cache/cache.go @@ -145,7 +145,37 @@ func (m *Manager) getOrFetch(ctx context.Context, registry, name, version string if err == nil { // Cache hit! metrics.RecordCacheHit(registry) - _ = m.metadata.UpdateDownloadCount(ctx, registry, name, version) // #nosec G104 -- Async update, error logged + + // Update download count (log errors for debugging) + if err := m.metadata.UpdateDownloadCount(ctx, registry, name, version); err != nil { + log.Warn(). + Err(err). + Str("registry", registry). + Str("package", name). + Str("version", version). + Msg("Failed to update download count - package may not exist in database") + + // Try to save package to database if it doesn't exist + // This handles the case where storage has files but database was migrated/reset + if saveErr := m.metadata.SavePackage(ctx, pkg); saveErr != nil { + log.Error(). + Err(saveErr). + Str("registry", registry). + Str("package", name). + Str("version", version). + Msg("Failed to save package to database") + } else { + // Retry download count update after saving package + if retryErr := m.metadata.UpdateDownloadCount(ctx, registry, name, version); retryErr != nil { + log.Error(). + Err(retryErr). + Str("registry", registry). + Str("package", name). + Str("version", version). + Msg("Failed to update download count even after saving package") + } + } + } // Track download in analytics if enabled if m.analytics != nil {