Skip to content

Commit

Permalink
Fine tune summary
Browse files Browse the repository at this point in the history
  • Loading branch information
deluan committed Jan 10, 2025
1 parent fa6e6fd commit e73f56b
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 12 deletions.
3 changes: 2 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ import (

func startTasks(ctx context.Context, db *sql.DB) error {
c := cron.New()
_, err := c.AddFunc("1 * * * *", summarize(ctx, db))
// Run summarize every two hours
_, err := c.AddFunc("0 */2 * * *", summarize(ctx, db))
if err != nil {
return err
}
Expand Down
34 changes: 26 additions & 8 deletions summary.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,11 @@ import (
)

type Summary struct {
NumInstances int64 `json:"numInstances,omitempty"`
NumActiveUsers int64 `json:"numActiveUsers,omitempty"`
Versions map[string]uint64 `json:"versions,omitempty"`
OS map[string]uint64 `json:"OS,omitempty"`
OS map[string]uint64 `json:"os,omitempty"`
Distros map[string]uint64 `json:"distros,omitempty"`
PlayerTypes map[string]uint64 `json:"playerTypes,omitempty"`
Players map[string]uint64 `json:"players,omitempty"`
Users map[string]uint64 `json:"users,omitempty"`
Expand All @@ -38,6 +41,7 @@ func summarizeData(db *sql.DB, date time.Time) error {
summary := Summary{
Versions: make(map[string]uint64),
OS: make(map[string]uint64),
Distros: make(map[string]uint64),
PlayerTypes: make(map[string]uint64),
Players: make(map[string]uint64),
Users: make(map[string]uint64),
Expand All @@ -50,8 +54,13 @@ func summarizeData(db *sql.DB, date time.Time) error {
var sumTracksSquared int64
for data := range rows {
// Summarize data here
summary.NumInstances++
summary.NumActiveUsers += data.Library.ActiveUsers
summary.Versions[mapVersion(data)]++
summary.OS[mapOS(data)]++
if data.OS.Type == "linux" && !data.OS.Containerized {
summary.Distros[data.OS.Distro]++
}
summary.Users[fmt.Sprintf("%d", data.Library.ActiveUsers)]++
summary.MusicFS[mapFS(data.FS.Music)]++
summary.DataFS[mapFS(data.FS.Data)]++
Expand All @@ -64,12 +73,15 @@ func summarizeData(db *sql.DB, date time.Time) error {
numInstances++
}
}
if numInstances > 0 {
summary.LibSizeAverage = sumTracks / numInstances
mean := float64(sumTracks) / float64(numInstances)
variance := float64(sumTracksSquared)/float64(numInstances) - mean*mean
summary.LibSizeStdDev = math.Sqrt(variance)
if numInstances == 0 {
log.Printf("No data to summarize for %s", date.Format("2006-01-02"))
return nil
}
summary.LibSizeAverage = sumTracks / numInstances
mean := float64(sumTracks) / float64(numInstances)
variance := float64(sumTracksSquared)/float64(numInstances) - mean*mean
summary.LibSizeStdDev = math.Sqrt(variance)

// Save summary to database
err = saveSummary(db, summary, date)
if err != nil {
Expand All @@ -79,7 +91,12 @@ func summarizeData(db *sql.DB, date time.Time) error {
return err
}

func mapVersion(data insights.Data) string { return data.Version }
// Match the first 8 characters of a git sha
var versionRegex = regexp.MustCompile(`\(([0-9a-fA-F]{8})[0-9a-fA-F]*\)`)

func mapVersion(data insights.Data) string {
return versionRegex.ReplaceAllString(data.Version, "($1)")
}

var trackBins = []int64{0, 1, 100, 500, 1000, 5000, 10000, 20000, 50000, 100000, 500000, 1000000}

Expand Down Expand Up @@ -118,6 +135,7 @@ var playersTypes = map[*regexp.Regexp]string{
regexp.MustCompile("supersonic"): "Supersonic",
regexp.MustCompile("feishin"): "", // Discard (old version reporting multiple times)
regexp.MustCompile("audioling"): "Audioling",
regexp.MustCompile("^AginMusic.*"): "AginMusic",
regexp.MustCompile("playSub.*"): "play:Sub",
regexp.MustCompile("eu.callcc.audrey"): "audrey",
regexp.MustCompile("DSubCC"): "", // Discard (chromecast)
Expand Down Expand Up @@ -167,7 +185,7 @@ func mapFS(fs *insights.FSInfo) string {
if t, ok := fsMappings[fs.Type]; ok {
return t
}
return fs.Type
return strings.ToLower(fs.Type)
}

func selectData(db *sql.DB, date time.Time) (iter.Seq[insights.Data], error) {
Expand Down
11 changes: 11 additions & 0 deletions summary_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,17 @@ var _ = Describe("Summary", func() {
})
})

DescribeTable("mapVersion",
func(expected string, data insights.Data) {
Expect(mapVersion(data)).To(Equal(expected))
},
Entry("should map version", "0.54.2 (0b184893)", insights.Data{Version: "0.54.2 (0b184893)"}),
Entry("should map version with long hash", "0.54.2 (0b184893)", insights.Data{Version: "0.54.2 (0b184893278620bb421a85c8b47df36900cd4df7)"}),
Entry("should map version with no hash", "dev", insights.Data{Version: "dev"}),
Entry("should map version with other values", "0.54.3 (source_archive)", insights.Data{Version: "0.54.3 (source_archive)"}),
Entry("should map any version with a hash", "0.54.3-SNAPSHOT (734eb30a)", insights.Data{Version: "0.54.3-SNAPSHOT (734eb30a)"}),
)

DescribeTable("mapOS",
func(expected string, data insights.Data) {
Expect(mapOS(data)).To(Equal(expected))
Expand Down
8 changes: 5 additions & 3 deletions tasks.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@ func cleanup(_ context.Context, db *sql.DB) func() {

func summarize(_ context.Context, db *sql.DB) func() {
return func() {
log.Print("Summarizing data for the last week")
log.Print("Summarizing data")
now := time.Now().Truncate(24 * time.Hour).UTC()
for d := 0; d < 15; d++ {
_ = summarizeData(db, now.Add(-time.Duration(d)*24*time.Hour))
for d := 0; d < 45; d++ {
date := now.Add(-time.Duration(d) * 24 * time.Hour)
log.Print("Summarizing data for ", date.Format("2006-01-02"))
_ = summarizeData(db, date)
}
}
}

0 comments on commit e73f56b

Please sign in to comment.