Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add summary #3

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,7 @@ linters:
- unconvert
- unused
- whitespace
linters-settings:
gosec:
excludes:
- G115 # Can't check context, where the warning is clearly a false positive. See discussion in https://github.com/securego/gosec/pull/1149
30 changes: 24 additions & 6 deletions db.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,21 @@ func openDB(fileName string) (*sql.DB, error) {
return nil, err
}

// Create table if not exists
// Create schema if not exists
createTableQuery := `
CREATE TABLE IF NOT EXISTS insights (
id VARCHAR NOT NULL,
time DATETIME default CURRENT_TIMESTAMP,
data JSONB,
PRIMARY KEY (id, time)
);`
);
CREATE TABLE IF NOT EXISTS summary (
id INTEGER PRIMARY KEY AUTOINCREMENT,
time DATETIME UNIQUE,
data JSONB
);
CREATE INDEX IF NOT EXISTS idx_summary_time ON summary (time);
`
_, err = db.Exec(createTableQuery)
if err != nil {
return nil, err
Expand All @@ -44,21 +51,32 @@ CREATE TABLE IF NOT EXISTS insights (
return db, nil
}

func saveToDB(db *sql.DB, data insights.Data) error {
func saveReport(db *sql.DB, data insights.Data, t time.Time) error {
dataJSON, err := json.Marshal(data)
if err != nil {
return err
}

query := `INSERT INTO insights (id, data) VALUES (?, ?)`
_, err = db.Exec(query, data.InsightsID, dataJSON)
query := `INSERT INTO insights (id, data, time) VALUES (?, ?, ?)`
_, err = db.Exec(query, data.InsightsID, dataJSON, t.Format("2006-01-02 15:04:05"))
return err
}

func saveSummary(db *sql.DB, summary Summary, t time.Time) error {
summaryJSON, err := json.Marshal(summary)
if err != nil {
return err
}

query := `INSERT INTO summary (time, data) VALUES (?, ?) ON CONFLICT(time) DO UPDATE SET data=?`
_, err = db.Exec(query, t.Format("2006-01-02 15:04:05"), summaryJSON, summaryJSON)
return err
}

func purgeOldEntries(db *sql.DB) error {
// Delete entries older than 30 days
query := `DELETE FROM insights WHERE time < ?`
cnt, err := db.Exec(query, time.Now().Add(-30*24*time.Hour))
cnt, err := db.Exec(query, time.Now().Add(-90*24*time.Hour))
if err != nil {
return err
}
Expand Down
20 changes: 17 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,12 +1,26 @@
module github.com/navidrome/insights

go 1.23.3
go 1.23.4

require (
github.com/go-chi/chi/v5 v5.2.0
github.com/go-chi/httprate v0.14.1
github.com/mattn/go-sqlite3 v1.14.24
github.com/navidrome/navidrome v0.53.4-0.20241219230533-906ac635c296
github.com/navidrome/navidrome v0.54.2
github.com/onsi/ginkgo/v2 v2.22.0
github.com/onsi/gomega v1.36.1
github.com/robfig/cron/v3 v3.0.1
)

require github.com/cespare/xxhash/v2 v2.3.0 // indirect
require (
github.com/cespare/xxhash/v2 v2.3.0 // indirect
github.com/go-logr/logr v1.4.2 // indirect
github.com/go-task/slim-sprig/v3 v3.0.0 // indirect
github.com/google/go-cmp v0.6.0 // indirect
github.com/google/pprof v0.0.0-20241029153458-d1b30febd7db // indirect
golang.org/x/net v0.32.0 // indirect
golang.org/x/sys v0.28.0 // indirect
golang.org/x/text v0.21.0 // indirect
golang.org/x/tools v0.27.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
38 changes: 36 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,12 +1,46 @@
github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs=
github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM=
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/go-chi/chi/v5 v5.2.0 h1:Aj1EtB0qR2Rdo2dG4O94RIU35w2lvQSj6BRA4+qwFL0=
github.com/go-chi/chi/v5 v5.2.0/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8=
github.com/go-chi/httprate v0.14.1 h1:EKZHYEZ58Cg6hWcYzoZILsv7ppb46Wt4uQ738IRtpZs=
github.com/go-chi/httprate v0.14.1/go.mod h1:TUepLXaz/pCjmCtf/obgOQJ2Sz6rC8fSf5cAt5cnTt0=
github.com/go-logr/logr v1.4.2 h1:6pFjapn8bFcIbiKo3XT4j/BhANplGihG6tvd+8rYgrY=
github.com/go-logr/logr v1.4.2/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY=
github.com/go-task/slim-sprig/v3 v3.0.0 h1:sUs3vkvUymDpBKi3qH1YSqBQk9+9D/8M2mN1vB6EwHI=
github.com/go-task/slim-sprig/v3 v3.0.0/go.mod h1:W848ghGpv3Qj3dhTPRyJypKRiqCdHZiAzKg9hl15HA8=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/pprof v0.0.0-20241029153458-d1b30febd7db h1:097atOisP2aRj7vFgYQBbFN4U4JNXUNYpxael3UzMyo=
github.com/google/pprof v0.0.0-20241029153458-d1b30febd7db/go.mod h1:vavhavw2zAxS5dIdcRluK6cSGGPlZynqzFM8NdvU144=
github.com/mattn/go-sqlite3 v1.14.24 h1:tpSp2G2KyMnnQu99ngJ47EIkWVmliIizyZBfPrBWDRM=
github.com/mattn/go-sqlite3 v1.14.24/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y=
github.com/navidrome/navidrome v0.53.4-0.20241219230533-906ac635c296 h1:xVDyE7lmkEUSNfz9vWHYueY35Ao/QxJEAFnGghOWtLA=
github.com/navidrome/navidrome v0.53.4-0.20241219230533-906ac635c296/go.mod h1:4uSM4pHPlsvdjVkjb9C98nLK38t3ph+zz1zjOCSKd4I=
github.com/navidrome/navidrome v0.54.2 h1:tDeiV6MHW/GuVh2Vyk/kQ8E24AIcwJZhjmeNLfXCZhM=
github.com/navidrome/navidrome v0.54.2/go.mod h1:4uSM4pHPlsvdjVkjb9C98nLK38t3ph+zz1zjOCSKd4I=
github.com/onsi/ginkgo/v2 v2.22.0 h1:Yed107/8DjTr0lKCNt7Dn8yQ6ybuDRQoMGrNFKzMfHg=
github.com/onsi/ginkgo/v2 v2.22.0/go.mod h1:7Du3c42kxCUegi0IImZ1wUQzMBVecgIHjR1C+NkhLQo=
github.com/onsi/gomega v1.36.1 h1:bJDPBO7ibjxcbHMgSCoo4Yj18UWbKDlLwX1x9sybDcw=
github.com/onsi/gomega v1.36.1/go.mod h1:PvZbdDc8J6XJEpDK4HCuRBm8a6Fzp9/DmhC9C7yFlog=
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U=
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/robfig/cron/v3 v3.0.1 h1:WdRxkvbJztn8LMz/QEvLN5sBU+xKpSqwwUO1Pjr4qDs=
github.com/robfig/cron/v3 v3.0.1/go.mod h1:eQICP3HwyT7UooqI/z+Ov+PtYAWygg1TEWWzGIFLtro=
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
golang.org/x/net v0.32.0 h1:ZqPmj8Kzc+Y6e0+skZsuACbx+wzMgo5MQsJh9Qd6aYI=
golang.org/x/net v0.32.0/go.mod h1:CwU0IoeOlnQQWJ6ioyFrfRuomB8GKF6KbYXZVyeXNfs=
golang.org/x/sync v0.10.0 h1:3NQrjDixjgGwUOCaF8w2+VYHv0Ve/vGYSbdkTa98gmQ=
golang.org/x/sync v0.10.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sys v0.28.0 h1:Fksou7UEQUWlKvIdsqzJmUmCX3cZuD2+P3XyyzwMhlA=
golang.org/x/sys v0.28.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/text v0.21.0 h1:zyQAAkrwaneQ066sspRyJaG9VNi/YJ1NfzcGB3hZ/qo=
golang.org/x/text v0.21.0/go.mod h1:4IBbMaMmOPCJ8SecivzSH54+73PCFmPWxNTLm+vZkEQ=
golang.org/x/tools v0.27.0 h1:qEKojBykQkQ4EynWy4S8Weg69NumxKdn40Fce3uc/8o=
golang.org/x/tools v0.27.0/go.mod h1:sUi0ZgbwW9ZPAq26Ekut+weQPR5eIM6GQLQ1Yjm1H0Q=
google.golang.org/protobuf v1.35.1 h1:m3LfL6/Ca+fqnjnlqQXNpFPABW1UD7mjh8KO2mKFytA=
google.golang.org/protobuf v1.35.1/go.mod h1:9fA7Ob0pmnwhb644+1+CVWFRbNajQ6iRojtC/QF5bRE=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
3 changes: 2 additions & 1 deletion handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"log"
"net/http"
"time"

"github.com/navidrome/navidrome/core/metrics/insights"
)
Expand All @@ -25,7 +26,7 @@ func handler(db *sql.DB) http.HandlerFunc {
return
}

err = saveToDB(db, data)
err = saveReport(db, data, time.Now())
if err != nil {
log.Printf("Error handling request: %s", err.Error())
w.WriteHeader(http.StatusInternalServerError)
Expand Down
26 changes: 20 additions & 6 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"context"
"database/sql"
"log"
"net/http"
Expand All @@ -10,23 +11,36 @@ import (
"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
"github.com/go-chi/httprate"
"github.com/robfig/cron/v3"
)

func dataCleaner(db *sql.DB) {
for {
log.Print("Cleaning old data")
_ = purgeOldEntries(db)
time.Sleep(24 * time.Hour)
func startTasks(ctx context.Context, db *sql.DB) error {
c := cron.New()
// Run summarize every two hours
_, err := c.AddFunc("0 */2 * * *", summarize(ctx, db))
if err != nil {
return err
}
_, err = c.AddFunc("5 0 * * *", cleanup(ctx, db))
if err != nil {
return err
}
c.Start()
return nil
}

func main() {
ctx := context.Background()
db, err := openDB("insights.db")
if err != nil {
log.Fatal(err)
}

go dataCleaner(db)
if err := startTasks(ctx, db); err != nil {
log.Fatal(err)
}

go summarize(ctx, db)()

r := chi.NewRouter()
r.Use(middleware.RealIP)
Expand Down
Loading
Loading