41 lines
714 B
Go
41 lines
714 B
Go
package memory
|
|
|
|
import (
|
|
"context"
|
|
"sync"
|
|
|
|
"git.loyso.art/frx/devsim/internal/entities"
|
|
)
|
|
|
|
type store struct {
|
|
stats map[entities.DeviceID]entities.DeviceStatistics
|
|
mu sync.Mutex
|
|
}
|
|
|
|
func NewStore() *store {
|
|
return &store{
|
|
stats: make(map[entities.DeviceID]entities.DeviceStatistics),
|
|
}
|
|
}
|
|
|
|
func (s *store) List(ctx context.Context) ([]entities.DeviceStatistics, error) {
|
|
s.mu.Lock()
|
|
defer s.mu.Unlock()
|
|
|
|
out := make([]entities.DeviceStatistics, 0, len(s.stats))
|
|
for _, s := range s.stats {
|
|
out = append(out, s)
|
|
}
|
|
|
|
return out, nil
|
|
}
|
|
|
|
func (s *store) Upsert(ctx context.Context, stats entities.DeviceStatistics) error {
|
|
s.mu.Lock()
|
|
defer s.mu.Unlock()
|
|
|
|
s.stats[stats.ID] = stats
|
|
|
|
return nil
|
|
}
|