mirror of
https://github.com/cgrates/cgrates.git
synced 2026-02-22 15:48:44 +05:00
better stats methods
This commit is contained in:
@@ -463,12 +463,7 @@ func main() {
|
||||
}
|
||||
|
||||
if cfg.CDRStatsEnabled {
|
||||
cdrStats = &engine.Stats{}
|
||||
if css, err := accountDb.GetAllCdrStats(); err == nil {
|
||||
cdrStats.UpdateQueues(css, nil)
|
||||
} else {
|
||||
engine.Logger.Err(fmt.Sprintf("Cannot load cdr stats: %v", err))
|
||||
}
|
||||
cdrStats = NewStats(accountDb)
|
||||
server.RpcRegister(cdrStats)
|
||||
}
|
||||
|
||||
|
||||
@@ -20,6 +20,7 @@ package engine
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/rpc"
|
||||
"sync"
|
||||
|
||||
@@ -27,7 +28,7 @@ import (
|
||||
)
|
||||
|
||||
type StatsInterface interface {
|
||||
AddQueue(*StatsQueue, *int) error
|
||||
AddQueue(*CdrStats, *int) error
|
||||
GetValues(string, *map[string]float64) error
|
||||
AppendCDR(*utils.StoredCdr, *int) error
|
||||
}
|
||||
@@ -37,16 +38,30 @@ type Stats struct {
|
||||
mux sync.RWMutex
|
||||
}
|
||||
|
||||
func (s *Stats) AddQueue(sq *StatsQueue, out *int) error {
|
||||
func (s *Stats) AddQueue(cs *CdrStats, out *int) error {
|
||||
s.mux.Lock()
|
||||
defer s.mux.Unlock()
|
||||
if s.queues == nil {
|
||||
s.queues = make(map[string]*StatsQueue)
|
||||
}
|
||||
s.queues[sq.conf.Id] = sq
|
||||
if sq, exists := s.queues[cs.Id]; exists {
|
||||
sq.UpdateConf(cs)
|
||||
} else {
|
||||
s.queues[cs.Id] = NewStatsQueue(cs)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func NewStats(accountDb AccountingStorage) *Stats {
|
||||
cdrStats := &Stats{}
|
||||
if css, err := accountDb.GetAllCdrStats(); err == nil {
|
||||
cdrStats.UpdateQueues(css, nil)
|
||||
} else {
|
||||
Logger.Err(fmt.Sprintf("Cannot load cdr stats: %v", err))
|
||||
}
|
||||
return cdrStats
|
||||
}
|
||||
|
||||
func (s *Stats) GetValues(sqID string, values *map[string]float64) error {
|
||||
s.mux.RLock()
|
||||
defer s.mux.RUnlock()
|
||||
@@ -100,8 +115,8 @@ func NewProxyStats(addr string) (*ProxyStats, error) {
|
||||
return &ProxyStats{Client: client}, nil
|
||||
}
|
||||
|
||||
func (ps *ProxyStats) AddQueue(sq *StatsQueue, out *int) error {
|
||||
return ps.Client.Call("Stats.AddQueue", sq, out)
|
||||
func (ps *ProxyStats) AddQueue(cs *CdrStats, out *int) error {
|
||||
return ps.Client.Call("Stats.AddQueue", cs, out)
|
||||
}
|
||||
|
||||
func (ps *ProxyStats) GetValues(sqID string, values *map[string]float64) error {
|
||||
@@ -111,7 +126,3 @@ func (ps *ProxyStats) GetValues(sqID string, values *map[string]float64) error {
|
||||
func (ps *ProxyStats) AppendCDR(cdr *utils.StoredCdr, out *int) error {
|
||||
return ps.Client.Call("Stats.AppendCDR", cdr, out)
|
||||
}
|
||||
|
||||
/*func (ps *ProxyStats) UpdateQueues(css []*CdrStats, out *int) error {
|
||||
return ps.Client.Call("Stats.UpdateQueues", css, out)
|
||||
}*/
|
||||
|
||||
@@ -45,17 +45,20 @@ func NewStatsQueue(conf *CdrStats) *StatsQueue {
|
||||
if conf == nil {
|
||||
return &StatsQueue{metrics: make(map[string]Metric)}
|
||||
}
|
||||
sq := &StatsQueue{
|
||||
conf: conf,
|
||||
metrics: make(map[string]Metric, len(conf.Metrics)),
|
||||
}
|
||||
sq := &StatsQueue{}
|
||||
sq.UpdateConf(conf)
|
||||
return sq
|
||||
}
|
||||
|
||||
func (sq *StatsQueue) UpdateConf(conf *CdrStats) {
|
||||
sq.conf = conf
|
||||
sq.metrics = make(map[string]Metric, len(conf.Metrics))
|
||||
for _, m := range conf.Metrics {
|
||||
metric := CreateMetric(m)
|
||||
if metric != nil {
|
||||
sq.metrics[m] = metric
|
||||
}
|
||||
}
|
||||
return sq
|
||||
}
|
||||
|
||||
func (sq *StatsQueue) AppendCDR(cdr *utils.StoredCdr) {
|
||||
|
||||
Reference in New Issue
Block a user