feat(analytics): serve trending packages from persisted downloads

Trending read the in-memory analytics engine, which restarts empty. The
endpoint now ranks packages by the last 7 days of stored downloads and
reports the previous 7 days for the change. It counts a Go module once
and skips metadata entries. A daily time_bucket index serves the range
read. The Overview panel shows the change and no longer says the data
resets on restart.
This commit is contained in:
2026-11-24 02:33:21 +00:00
parent 6715cefd25
commit 3bfc367898
9 changed files with 444 additions and 10 deletions
+42 -1
View File
@@ -4,6 +4,7 @@ import (
"strconv"
"github.com/gofiber/fiber/v3"
"github.com/lukaszraczylo/gohoarder/pkg/metadata"
"github.com/rs/zerolog/log"
)
@@ -27,7 +28,18 @@ func (a *App) handleAnalyticsTopPackages(c fiber.Ctx) error {
})
}
// handleAnalyticsTrendingPackages returns trending packages
// trendingPackage keeps the Go field names the dashboard reads from the analytics engine.
type trendingPackage struct {
Registry string
Name string
Downloads int64
RecentDownloads int64
PreviousDownloads int64
Trend float64
}
// handleAnalyticsTrendingPackages ranks packages by downloads in the last 7 days. Persisted
// history serves it so a restart keeps the ranking; the in-memory engine covers stores without it.
func (a *App) handleAnalyticsTrendingPackages(c fiber.Ctx) error {
c.Set("Content-Type", "application/json")
@@ -39,6 +51,35 @@ func (a *App) handleAnalyticsTrendingPackages(c fiber.Ctx) error {
}
}
if source, ok := a.metadata.(metadata.TrendingSource); ok {
stored, err := source.GetTrendingPackages(c.Context(), limit)
if err != nil {
log.Error().Err(err).Msg("Failed to read trending packages")
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
"error": "failed to read trending packages",
})
}
packages := make([]trendingPackage, 0, len(stored))
for _, p := range stored {
trend := 0.0
if p.Previous > 0 {
trend = float64(p.Recent-p.Previous) / float64(p.Previous) * 100
}
packages = append(packages, trendingPackage{
Registry: p.Registry,
Name: p.Name,
Downloads: p.Recent,
RecentDownloads: p.Recent,
PreviousDownloads: p.Previous,
Trend: trend,
})
}
return c.Status(fiber.StatusOK).JSON(fiber.Map{
"packages": packages,
"total": len(packages),
})
}
packages := a.analyticsEngine.GetTrendingPackages(limit)
return c.Status(fiber.StatusOK).JSON(fiber.Map{
+84
View File
@@ -0,0 +1,84 @@
package app
import (
"context"
"encoding/json"
"fmt"
"net/http/httptest"
"testing"
"time"
"github.com/gofiber/fiber/v3"
"github.com/lukaszraczylo/gohoarder/pkg/analytics"
"github.com/lukaszraczylo/gohoarder/pkg/metadata"
"github.com/lukaszraczylo/gohoarder/pkg/metadata/gormstore"
"github.com/stretchr/testify/require"
)
type trendingResponse struct {
Packages []trendingPackage `json:"packages"`
Total int `json:"total"`
}
func getTrending(t *testing.T, a *App, query string) trendingResponse {
t.Helper()
f := fiber.New()
f.Get("/api/analytics/trending", a.handleAnalyticsTrendingPackages)
resp, err := f.Test(httptest.NewRequest("GET", "/api/analytics/trending"+query, nil))
require.NoError(t, err)
require.Equal(t, 200, resp.StatusCode)
var out trendingResponse
require.NoError(t, json.NewDecoder(resp.Body).Decode(&out))
return out
}
func seedDownloads(t *testing.T, store metadata.Store, registry, name, version string, n int) {
t.Helper()
ctx := context.Background()
now := time.Now()
require.NoError(t, store.SavePackage(ctx, &metadata.Package{
Registry: registry, Name: name, Version: version, StorageKey: registry + "/" + name + "/" + version,
Size: 1, CachedAt: now, LastAccessed: now,
}))
for i := 0; i < n; i++ {
require.NoError(t, store.UpdateDownloadCount(ctx, registry, name, version))
}
}
func TestTrendingServedFromPersistedDownloads(t *testing.T) {
store, err := gormstore.NewV2(gormstore.Config{
Driver: "sqlite", DSN: fmt.Sprintf("file:trend_%d?mode=memory&cache=shared", time.Now().UnixNano()),
MaxOpenConns: 1, MaxIdleConns: 1, LogLevel: "silent",
})
require.NoError(t, err)
t.Cleanup(func() { _ = store.Close() })
seedDownloads(t, store, "npm", "left-pad", "1.3.0", 3)
seedDownloads(t, store, "go", "github.com/google/uuid/@v/v1.6.0.zip", "v1.6.0", 2)
seedDownloads(t, store, "go", "github.com/google/uuid/@v/v1.6.0.mod", "v1.6.0", 2)
// A fresh engine is what a restarted server has: it must not decide the ranking.
engine := analytics.NewEngine(analytics.Config{MaxEvents: 10, FlushInterval: time.Minute})
t.Cleanup(engine.Close)
got := getTrending(t, &App{metadata: store, analyticsEngine: engine}, "")
require.Equal(t, 2, got.Total)
require.Equal(t, "npm", got.Packages[0].Registry)
require.Equal(t, "left-pad", got.Packages[0].Name)
require.Equal(t, int64(3), got.Packages[0].RecentDownloads)
require.Equal(t, int64(0), got.Packages[0].PreviousDownloads)
require.Equal(t, "github.com/google/uuid", got.Packages[1].Name)
require.Equal(t, int64(2), got.Packages[1].RecentDownloads)
limited := getTrending(t, &App{metadata: store, analyticsEngine: engine}, "?limit=1")
require.Len(t, limited.Packages, 1)
}
func TestTrendingFallsBackToEngineWithoutPersistedHistory(t *testing.T) {
engine := analytics.NewEngine(analytics.Config{MaxEvents: 10, FlushInterval: time.Minute})
t.Cleanup(engine.Close)
engine.TrackDownload(analytics.PackageDownload{Registry: "npm", Name: "lodash", Version: "4", Timestamp: time.Now()})
got := getTrending(t, &App{analyticsEngine: engine}, "")
require.Equal(t, 1, got.Total)
require.Equal(t, "lodash", got.Packages[0].Name)
}