-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdb.go
68 lines (60 loc) · 1.5 KB
/
db.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package main
import (
"database/sql"
"encoding/json"
"fmt"
"log"
"net/url"
"time"
_ "github.com/mattn/go-sqlite3"
"github.com/navidrome/navidrome/core/metrics/insights"
)
func openDB(fileName string) (*sql.DB, error) {
params := url.Values{
"_journal_mode": []string{"WAL"},
"_synchronous": []string{"NORMAL"},
"cache_size": []string{"1000000000"},
"cache": []string{"shared"},
"_busy_timeout": []string{"5000"},
"_txlock": []string{"immediate"},
}
dataSourceName := fmt.Sprintf("file:%s?%s", fileName, params.Encode())
db, err := sql.Open("sqlite3", dataSourceName)
if err != nil {
return nil, err
}
// Create table 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)
);`
_, err = db.Exec(createTableQuery)
if err != nil {
return nil, err
}
db.SetMaxOpenConns(1)
return db, nil
}
func saveToDB(db *sql.DB, data insights.Data) 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)
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))
if err != nil {
return err
}
deleted, _ := cnt.RowsAffected()
log.Printf("Deleted %d old entries\n", deleted)
return nil
}