add statistics storage
This commit is contained in:
@@ -21,4 +21,9 @@ type Statistics struct {
|
||||
|
||||
InterfaceType InterfaceType `json:"interface_type"`
|
||||
TasksCompleted int `json:"tasks_completed"`
|
||||
ButtonClicks ButtonClicks `json:"button_clicks"`
|
||||
}
|
||||
|
||||
type ButtonClicks struct {
|
||||
Clicks int `json:"clicks"`
|
||||
}
|
||||
|
||||
@@ -1,13 +1,27 @@
|
||||
package service
|
||||
|
||||
import "beadstest/internal/storage"
|
||||
import (
|
||||
"beadstest/internal/models"
|
||||
"beadstest/internal/storage"
|
||||
"errors"
|
||||
)
|
||||
|
||||
type StatisticsService struct {
|
||||
storage *storage.StatisticsStorage
|
||||
storage *storage.Storage[models.Statistics]
|
||||
}
|
||||
|
||||
func NewStatisticsService(storage *storage.StatisticsStorage) *StatisticsService {
|
||||
func NewStatisticsService(storage *storage.Storage[models.Statistics]) (*StatisticsService, error) {
|
||||
if err := storage.Init(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &StatisticsService{
|
||||
storage: storage,
|
||||
}
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s *StatisticsService) GetStatistics() (models.Statistics, error) {
|
||||
if s.storage.Data == nil {
|
||||
return models.Statistics{}, errors.New("statistics data not initialized")
|
||||
}
|
||||
return *s.storage.Data, nil
|
||||
}
|
||||
|
||||
@@ -1,17 +1,54 @@
|
||||
package storage
|
||||
|
||||
type StatisticsStorage struct {
|
||||
Path string
|
||||
import (
|
||||
"encoding/json"
|
||||
"os"
|
||||
"sync"
|
||||
)
|
||||
|
||||
type Storage[T any] struct {
|
||||
path string
|
||||
mu sync.RWMutex
|
||||
Data *T
|
||||
}
|
||||
|
||||
func NewStatisticsStorage(path string) *StatisticsStorage {
|
||||
return &StatisticsStorage{Path: path}
|
||||
func NewJsonStorage[T any](path string, defaultData *T) *Storage[T] {
|
||||
return &Storage[T]{
|
||||
path: path,
|
||||
Data: defaultData,
|
||||
}
|
||||
}
|
||||
|
||||
func (s *StatisticsStorage) Save(stats any) error {
|
||||
return nil
|
||||
func (s *Storage[T]) Save() error {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
|
||||
data, err := json.MarshalIndent(s.Data, "", " ")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return os.WriteFile(s.path, data, 0644)
|
||||
}
|
||||
|
||||
func (s *StatisticsStorage) Load() (any, error) {
|
||||
return nil, nil
|
||||
func (s *Storage[T]) Load() error {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
|
||||
bytes, err := os.ReadFile(s.path)
|
||||
if err != nil {
|
||||
if os.IsNotExist(err) {
|
||||
return nil
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
return json.Unmarshal(bytes, s.Data)
|
||||
}
|
||||
|
||||
func (s *Storage[T]) Init() error {
|
||||
if _, err := os.Stat(s.path); os.IsNotExist(err) {
|
||||
return s.Save()
|
||||
}
|
||||
return s.Load()
|
||||
}
|
||||
Reference in New Issue
Block a user