@@ -2,44 +2,26 @@ package stats
22
33import (
44 "context"
5- "errors"
65 "fmt"
76 "io"
87 "os"
9- "sync"
108 "time"
119
12- "github.com/vanadium23/kompanion/internal/storage"
1310 "github.com/vanadium23/kompanion/pkg/postgres"
1411)
1512
1613const KOReaderFile = "statistics.sqlite3"
1714
18- // KOReaderStats implements ReadingStats interface
19- type KOReaderStats struct {
20- rw sync.RWMutex
21- st storage.Storage
15+ // KOReaderPGStats implements ReadingStats interface
16+ type KOReaderPGStats struct {
2217 pg * postgres.Postgres
2318}
2419
25- func NewKOReaderStats ( st storage. Storage , pg * postgres.Postgres ) * KOReaderStats {
26- return & KOReaderStats { st : st , rw : sync. RWMutex {}, pg : pg }
20+ func NewKOReaderPGStats ( pg * postgres.Postgres ) * KOReaderPGStats {
21+ return & KOReaderPGStats { pg : pg }
2722}
2823
29- func (s * KOReaderStats ) Read (ctx context.Context ) (* os.File , error ) {
30- s .rw .RLock ()
31- stats , err := s .st .Read (ctx , KOReaderFile )
32- s .rw .RUnlock ()
33- if errors .Is (err , storage .ErrNotFound ) {
34- return nil , ErrEmptyStats
35- }
36- if err != nil {
37- return nil , err
38- }
39- return stats , nil
40- }
41-
42- func (s * KOReaderStats ) Write (ctx context.Context , r io.ReadCloser , deviceName string ) error {
24+ func (s * KOReaderPGStats ) Write (ctx context.Context , r io.ReadCloser , deviceName string ) error {
4325 // make by temp files
4426 tempFile , err := os .CreateTemp ("" , fmt .Sprintf ("%s-" , deviceName ))
4527 if err != nil {
@@ -53,12 +35,6 @@ func (s *KOReaderStats) Write(ctx context.Context, r io.ReadCloser, deviceName s
5335 return err
5436 }
5537
56- s .rw .Lock ()
57- err = s .st .Write (ctx , tempFile .Name (), KOReaderFile )
58- s .rw .Unlock ()
59- if err != nil {
60- return err
61- }
6238 go SyncDatabases (filepath , s .pg , deviceName )
6339 return nil
6440}
@@ -70,7 +46,7 @@ type BookStats struct {
7046 TotalReadDays int
7147}
7248
73- func (s * KOReaderStats ) GetBookStats (ctx context.Context , fileHash string ) (* BookStats , error ) {
49+ func (s * KOReaderPGStats ) GetBookStats (ctx context.Context , fileHash string ) (* BookStats , error ) {
7450 query := `
7551 WITH daily_reads AS (
7652 SELECT DISTINCT DATE(start_time) as read_date
@@ -102,7 +78,7 @@ func (s *KOReaderStats) GetBookStats(ctx context.Context, fileHash string) (*Boo
10278 return & stats , nil
10379}
10480
105- func (s * KOReaderStats ) GetGeneralStats (ctx context.Context , from , to time.Time ) (* GeneralStats , error ) {
81+ func (s * KOReaderPGStats ) GetGeneralStats (ctx context.Context , from , to time.Time ) (* GeneralStats , error ) {
10682 var stats GeneralStats
10783
10884 // Get per book statistics
@@ -163,7 +139,7 @@ type DailyStats struct {
163139 AvgDurationPerPage float64
164140}
165141
166- func (s * KOReaderStats ) GetDailyStats (ctx context.Context , from , to time.Time ) ([]DailyStats , error ) {
142+ func (s * KOReaderPGStats ) GetDailyStats (ctx context.Context , from , to time.Time ) ([]DailyStats , error ) {
167143 query := `
168144 WITH RECURSIVE dates AS (
169145 SELECT date_trunc('day', $1::timestamp)::date as date
0 commit comments