mirror of
https://github.com/lukaszraczylo/gohoarder.git
synced 2026-11-26 06:37:07 +00:00
fix(stats): include unaggregated downloads in the timeseries
The timeseries read only the aggregated tables. The hourly worker skips the current hour and the daily rollup skips the current day, so fresh downloads showed as zero. The query now merges raw download events, hourly rows and daily rollups, with all buckets in UTC. The change in gormstore is limited to GetTimeSeriesStats.
This commit is contained in:
@@ -0,0 +1,52 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"github.com/lukaszraczylo/gohoarder/pkg/config"
|
||||
)
|
||||
|
||||
func TestTimeSeriesCountsCacheHitDownloads(t *testing.T) {
|
||||
upstream := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.URL.Path == "/left-pad/-/left-pad-1.3.0.tgz" {
|
||||
_, _ = w.Write([]byte("TARBALL"))
|
||||
return
|
||||
}
|
||||
http.NotFound(w, r)
|
||||
}))
|
||||
t.Cleanup(upstream.Close)
|
||||
a := newTestApp(t, func(c *config.Config) { c.Handlers.NPM.UpstreamRegistry = upstream.URL })
|
||||
|
||||
// The npm handler tracks every tarball request, the first miss included.
|
||||
const downloads = 4
|
||||
for i := 0; i < downloads; i++ {
|
||||
if code, _, body := doRequest(t, a, "/npm/left-pad/-/left-pad-1.3.0.tgz", nil); code != http.StatusOK {
|
||||
t.Fatalf("download %d: status %d, body %q", i, code, body)
|
||||
}
|
||||
}
|
||||
|
||||
for _, period := range []string{"1h", "1day", "7day", "30day"} {
|
||||
code, _, body := doRequest(t, a, "/api/stats/timeseries?period="+period, nil)
|
||||
if code != http.StatusOK {
|
||||
t.Fatalf("%s: status %d, body %q", period, code, body)
|
||||
}
|
||||
var ts struct {
|
||||
DataPoints []struct {
|
||||
Value int64 `json:"value"`
|
||||
} `json:"data_points"`
|
||||
}
|
||||
if err := json.Unmarshal([]byte(body), &ts); err != nil {
|
||||
t.Fatalf("%s: decode %q: %v", period, body, err)
|
||||
}
|
||||
var total int64
|
||||
for _, p := range ts.DataPoints {
|
||||
total += p.Value
|
||||
}
|
||||
if total != downloads {
|
||||
t.Errorf("%s: timeseries total = %d, want %d (body %s)", period, total, downloads, body)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user