mirror of
https://github.com/lukaszraczylo/gohoarder.git
synced 2026-11-26 06:37:07 +00:00
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:
@@ -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{
|
||||
|
||||
Reference in New Issue
Block a user