mirror of
https://github.com/grassrootseconomics/eth-tracker.git
synced 2026-05-17 10:15:21 +02:00
release: v1.0.0-rc
This commit is contained in:
88
internal/stats/stats.go
Normal file
88
internal/stats/stats.go
Normal file
@@ -0,0 +1,88 @@
|
||||
package stats
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log/slog"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
|
||||
"github.com/grassrootseconomics/celo-tracker/internal/cache"
|
||||
"github.com/grassrootseconomics/celo-tracker/internal/pool"
|
||||
)
|
||||
|
||||
type (
|
||||
StatsOpts struct {
|
||||
Cache cache.Cache
|
||||
Logg *slog.Logger
|
||||
Pool *pool.Pool
|
||||
}
|
||||
|
||||
Stats struct {
|
||||
cache cache.Cache
|
||||
logg *slog.Logger
|
||||
pool *pool.Pool
|
||||
stopCh chan struct{}
|
||||
latestBlock atomic.Uint64
|
||||
}
|
||||
)
|
||||
|
||||
const statsPrinterInterval = 15 * time.Second
|
||||
|
||||
func New(o StatsOpts) *Stats {
|
||||
return &Stats{
|
||||
cache: o.Cache,
|
||||
logg: o.Logg,
|
||||
pool: o.Pool,
|
||||
stopCh: make(chan struct{}),
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Stats) SetLatestBlock(v uint64) {
|
||||
s.latestBlock.Store(v)
|
||||
}
|
||||
|
||||
func (s *Stats) GetLatestBlock() uint64 {
|
||||
return s.latestBlock.Load()
|
||||
}
|
||||
|
||||
func (s *Stats) Stop() {
|
||||
s.stopCh <- struct{}{}
|
||||
}
|
||||
|
||||
func (s *Stats) APIStatsResponse(ctx context.Context) (map[string]interface{}, error) {
|
||||
cacheSize, err := s.cache.Size(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return map[string]interface{}{
|
||||
"latestBlock": s.GetLatestBlock(),
|
||||
"poolQueueSize": s.pool.Size(),
|
||||
"poolActiveWorkers": s.pool.ActiveWorkers(),
|
||||
"cacheSize": cacheSize,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s *Stats) StartStatsPrinter() {
|
||||
ticker := time.NewTicker(statsPrinterInterval)
|
||||
|
||||
for {
|
||||
select {
|
||||
case <-s.stopCh:
|
||||
s.logg.Debug("stats shutting down")
|
||||
return
|
||||
case <-ticker.C:
|
||||
cacheSize, err := s.cache.Size(context.Background())
|
||||
if err != nil {
|
||||
s.logg.Error("stats printer could not fetch cache size", "error", err)
|
||||
}
|
||||
|
||||
s.logg.Info("block stats",
|
||||
"latest_block", s.GetLatestBlock(),
|
||||
"pool_queue_size", s.pool.Size(),
|
||||
"pool_active_workers", s.pool.ActiveWorkers(),
|
||||
"cache_size", cacheSize,
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user