mirror of
https://github.com/lukaszraczylo/gohoarder.git
synced 2026-11-26 06:37:07 +00:00
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.
113 lines
3.4 KiB
Go
113 lines
3.4 KiB
Go
package gormstore
|
|
|
|
import (
|
|
"context"
|
|
"sort"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/lukaszraczylo/gohoarder/pkg/errors"
|
|
"github.com/lukaszraczylo/gohoarder/pkg/metadata"
|
|
)
|
|
|
|
const trendingWindow = 7 * 24 * time.Hour
|
|
|
|
type trendingKey struct{ registry, name string }
|
|
|
|
type trendingRow struct {
|
|
Registry string
|
|
Name string
|
|
Version string
|
|
Count int64
|
|
}
|
|
|
|
// GetTrendingPackages ranks packages by downloads in the last 7 days, with the 7 days before
|
|
// for the change. Downloads sit in raw events (current hour), hourly rows (last 7 days) and
|
|
// daily rollups (older), the same layering GetTimeSeriesStats merges.
|
|
func (s *GORMStoreV2) GetTrendingPackages(ctx context.Context, limit int) ([]*metadata.TrendingPackage, error) {
|
|
now := s.clock().UTC()
|
|
since := now.Add(-trendingWindow)
|
|
// Daily rows hold whole days, so the previous window ends at the day boundary at or before since.
|
|
// The 12h shift keeps midnight rows on the right side whether the driver stores a date or a timestamp.
|
|
prevEnd := since.Truncate(24 * time.Hour).Add(-12 * time.Hour)
|
|
prevStart := prevEnd.Add(-trendingWindow)
|
|
|
|
hourly, err := s.packageDownloads(ctx, "download_stats_hourly s", "SUM(s.download_count)", "s.time_bucket >= ?", since)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
events, err := s.packageDownloads(ctx, "download_events s", "COUNT(*)", "s.downloaded_at >= ?", since)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
daily, err := s.packageDownloads(ctx, "download_stats_daily s", "SUM(s.download_count)", "s.time_bucket >= ? AND s.time_bucket < ?", prevStart, prevEnd)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
totals := make(map[trendingKey]*metadata.TrendingPackage)
|
|
add := func(rows []trendingRow, recent bool) {
|
|
for _, r := range rows {
|
|
if metadata.IsMetadataEntry(r.Registry, r.Name, r.Version) {
|
|
continue
|
|
}
|
|
name := r.Name
|
|
if r.Registry == metadata.GoRegistry {
|
|
name, _, _ = strings.Cut(name, "/@v/")
|
|
}
|
|
key := trendingKey{r.Registry, name}
|
|
pkg := totals[key]
|
|
if pkg == nil {
|
|
pkg = &metadata.TrendingPackage{Registry: r.Registry, Name: name}
|
|
totals[key] = pkg
|
|
}
|
|
if recent {
|
|
pkg.Recent += r.Count
|
|
} else {
|
|
pkg.Previous += r.Count
|
|
}
|
|
}
|
|
}
|
|
add(hourly, true)
|
|
add(events, true)
|
|
add(daily, false)
|
|
|
|
ranked := make([]*metadata.TrendingPackage, 0, len(totals))
|
|
for _, pkg := range totals {
|
|
if pkg.Recent > 0 {
|
|
ranked = append(ranked, pkg)
|
|
}
|
|
}
|
|
sort.Slice(ranked, func(i, j int) bool {
|
|
if ranked[i].Recent != ranked[j].Recent {
|
|
return ranked[i].Recent > ranked[j].Recent
|
|
}
|
|
if ranked[i].Registry != ranked[j].Registry {
|
|
return ranked[i].Registry < ranked[j].Registry
|
|
}
|
|
return ranked[i].Name < ranked[j].Name
|
|
})
|
|
if limit > 0 && limit < len(ranked) {
|
|
ranked = ranked[:limit]
|
|
}
|
|
return ranked, nil
|
|
}
|
|
|
|
// packageDownloads sums one download table per package row; table carries the alias "s".
|
|
func (s *GORMStoreV2) packageDownloads(ctx context.Context, table, sum, cond string, args ...interface{}) ([]trendingRow, error) {
|
|
var rows []trendingRow
|
|
err := s.db.WithContext(ctx).
|
|
Table(table).
|
|
Select("r.name AS registry, p.name AS name, p.version AS version, "+sum+" AS count").
|
|
Joins("JOIN packages p ON p.id = s.package_id").
|
|
Joins("JOIN registries r ON r.id = p.registry_id").
|
|
Where("s.package_id IS NOT NULL").
|
|
Where(cond, args...).
|
|
Group("r.name, p.name, p.version").
|
|
Scan(&rows).Error
|
|
if err != nil {
|
|
return nil, errors.Wrap(err, errors.ErrCodeStorageFailure, "failed to read trending downloads")
|
|
}
|
|
return rows, nil
|
|
}
|