Skip to content

Commit ac09467

Browse files
authored
ref: completely remove storage for stats (#25)
Previously, KOmpanion returns last saved statistics files. But it was deleted due to messing up with primary keys. Closes #22
1 parent 741f040 commit ac09467

File tree

8 files changed

+25
-74
lines changed

8 files changed

+25
-74
lines changed

README.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,6 @@ Features, that can buy you in:
4545
- `KOMPANION_PG_URL` - postgresql link
4646
- `KOMPANION_BSTORAGE_TYPE` - type of storage for books: postgres, memory, filesystem (default: postgres)
4747
- `KOMPANION_BSTORAGE_PATH` - path in case of filesystem
48-
- `KOMPANION_STATS_TYPE` - type of temporary storage for uploaded sqlite3 stats files: postgres, memory, filesystem (default: memory)
49-
- `KOMPANION_STATS_PATH` - path in case of filesystem
5048

5149
## Usage
5250

config/config.go

Lines changed: 5 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ type (
1616
Log
1717
PG
1818
BookStorage
19-
StatsStorage
2019
}
2120

2221
// App -.
@@ -52,12 +51,6 @@ type (
5251
Type string
5352
Path string
5453
}
55-
56-
// StatsStorage -.
57-
StatsStorage struct {
58-
Type string
59-
Path string
60-
}
6154
)
6255

6356
// NewConfig - reads from env, validates and returns the config.
@@ -87,22 +80,16 @@ func NewConfig(version string) (*Config, error) {
8780
return nil, err
8881
}
8982

90-
statsStorage, err := readStatsStorageConfig()
91-
if err != nil {
92-
return nil, err
93-
}
94-
9583
return &Config{
9684
App: App{
9785
Name: "kompanion",
9886
Version: version,
9987
},
100-
Auth: auth,
101-
HTTP: http,
102-
Log: log,
103-
PG: postgres,
104-
BookStorage: bookStorage,
105-
StatsStorage: statsStorage,
88+
Auth: auth,
89+
HTTP: http,
90+
Log: log,
91+
PG: postgres,
92+
BookStorage: bookStorage,
10693
}, nil
10794
}
10895

@@ -184,18 +171,6 @@ func readBookStorageConfig() (BookStorage, error) {
184171
}, nil
185172
}
186173

187-
func readStatsStorageConfig() (StatsStorage, error) {
188-
stats_type := readPrefixedEnv("STATS_TYPE")
189-
if stats_type == "" {
190-
stats_type = "postgres"
191-
}
192-
stats_path := readPrefixedEnv("STATS_PATH")
193-
return StatsStorage{
194-
Type: stats_type,
195-
Path: stats_path,
196-
}, nil
197-
}
198-
199174
func readPrefixedEnv(key string) string {
200175
envKey := fmt.Sprintf("KOMPANION_%s", strings.ToUpper(key))
201176
return os.Getenv(envKey)

docker-compose.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ services:
1919
- ./data:/data
2020
environment:
2121
KOMPANION_PG_URL: 'postgres://user:pass@postgres:5432/postgres'
22-
KOMPANION_STATS_PATH: '/data/stats/'
2322
KOMPANION_BSTORAGE_PATH: '/data/books/'
2423
KOMPANION_AUTH_USERNAME: 'user'
2524
KOMPANION_AUTH_PASSWORD: 'password'

integration-test/integration_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,16 @@ func TestWebStats(t *testing.T) {
387387
Expect().Status().Equal(http.StatusOK),
388388
Expect().Body().String().Contains("Crime and Punishment"),
389389
)
390+
391+
// regress for uploading same file
392+
// https://github.com/vanadium23/kompanion/issues/22
393+
Test(t,
394+
Description("Kompanion Upload Stats via WebDAV"),
395+
Put(basePath+"/webdav/statistics.sqlite3"),
396+
Send().Headers("Authorization").Add(basicAuth),
397+
Send().Body().Bytes(statsContent),
398+
Expect().Status().Equal(http.StatusCreated),
399+
)
390400
}
391401

392402
// HTTP test kompanion shelf feature

internal/app/app.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,6 @@ func Run(cfg *config.Config) {
3939
if err != nil {
4040
l.Fatal(fmt.Errorf("app - Run - storage.NewStorage: %w", err))
4141
}
42-
statsStorage, err := storage.NewStorage(cfg.StatsStorage.Type, cfg.StatsStorage.Path, pg)
43-
if err != nil {
44-
l.Fatal(fmt.Errorf("app - Run - storage.NewStorage: %w", err))
45-
}
4642

4743
// Use case
4844
var repo auth.UserRepo
@@ -61,14 +57,14 @@ func Run(cfg *config.Config) {
6157
)
6258
progress := sync.NewProgressSync(sync.NewProgressDatabaseRepo(pg))
6359
shelf := library.NewBookShelf(bookStorage, library.NewBookDatabaseRepo(pg), l)
64-
rs := stats.NewKOReaderStats(statsStorage, pg)
60+
rs := stats.NewKOReaderPGStats(pg)
6561

6662
// HTTP Server
6763
handler := gin.New()
6864
web.NewRouter(handler, l, authService, progress, shelf, rs, cfg.Version)
6965
v1.NewRouter(handler, l, authService, progress, shelf)
7066
opds.NewRouter(handler, l, authService, progress, shelf)
71-
webdav.NewRouter(handler, authService, l, rs, cfg.StatsStorage.Path)
67+
webdav.NewRouter(handler, authService, l, rs)
7268
httpServer := httpserver.New(handler, httpserver.Port(cfg.HTTP.Port))
7369

7470
// Waiting signal

internal/controller/http/webdav/router.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ func NewRouter(
1414
a auth.AuthInterface,
1515
l logger.Interface,
1616
rs stats.ReadingStats,
17-
dirPath string,
1817
) {
1918
// Options
2019
handler.Use(gin.Logger())

internal/stats/interface.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"context"
55
"errors"
66
"io"
7-
"os"
87
"time"
98
)
109

@@ -28,5 +27,4 @@ type ReadingStats interface {
2827
GetGeneralStats(ctx context.Context, from, to time.Time) (*GeneralStats, error)
2928
GetDailyStats(ctx context.Context, from, to time.Time) ([]DailyStats, error)
3029
Write(ctx context.Context, r io.ReadCloser, deviceName string) error
31-
Read(ctx context.Context) (*os.File, error)
3230
}

internal/stats/stats.go

Lines changed: 8 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2,44 +2,26 @@ package stats
22

33
import (
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

1613
const 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

Comments
 (0)