From a096d97d94814271b2c2e285571f0782ebb06cb3 Mon Sep 17 00:00:00 2001 From: Radu Ioan Fericean Date: Fri, 1 Aug 2014 17:49:18 +0300 Subject: [PATCH] added GetQueueIds and expire at GetValues --- engine/stats.go | 30 +++++++++++++++++++++++------- engine/stats_metrics.go | 15 +++++++++++---- engine/stats_queue.go | 7 ++++--- 3 files changed, 38 insertions(+), 14 deletions(-) diff --git a/engine/stats.go b/engine/stats.go index b37291d08..d7e72cc59 100644 --- a/engine/stats.go +++ b/engine/stats.go @@ -30,6 +30,7 @@ import ( type StatsInterface interface { AddQueue(*CdrStats, *int) error GetValues(string, *map[string]float64) error + GetQueueIds(int, *[]string) error AppendCDR(*utils.StoredCdr, *int) error } @@ -38,6 +39,16 @@ type Stats struct { mux sync.RWMutex } +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) AddQueue(cs *CdrStats, out *int) error { s.mux.Lock() defer s.mux.Unlock() @@ -52,14 +63,15 @@ func (s *Stats) AddQueue(cs *CdrStats, out *int) error { 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)) +func (s *Stats) GetQueueIds(in int, ids *[]string) error { + s.mux.Lock() + defer s.mux.Unlock() + var result []string + for id, _ := range s.queues { + result = append(result, id) } - return cdrStats + *ids = result + return nil } func (s *Stats) GetValues(sqID string, values *map[string]float64) error { @@ -129,3 +141,7 @@ 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) GetQueueIds(in int, ids *[]string) error { + return ps.Client.Call("Stats.GetQueueIds", in, ids) +} diff --git a/engine/stats_metrics.go b/engine/stats_metrics.go index c05ea1f86..9aecc93bb 100644 --- a/engine/stats_metrics.go +++ b/engine/stats_metrics.go @@ -18,7 +18,11 @@ along with this program. If not, see package engine -import "time" +import ( + "time" + + "github.com/cgrates/cgrates/utils" +) type Metric interface { AddCdr(*QCdr) @@ -67,7 +71,8 @@ func (asr *ASRMetric) GetValue() float64 { if asr.total == 0 { return 0 } - return asr.answered / asr.total * 100 + val := asr.answered / asr.total * 100 + return utils.Round(val, globalRoundingDecimals, utils.ROUNDING_MIDDLE) } // ACD – Average Call Duration @@ -95,7 +100,8 @@ func (acd *ACDMetric) GetValue() float64 { if acd.count == 0 { return 0 } - return acd.sum.Seconds() / acd.count + val := acd.sum.Seconds() / acd.count + return utils.Round(val, globalRoundingDecimals, utils.ROUNDING_MIDDLE) } // ACC – Average Call Cost @@ -123,5 +129,6 @@ func (acc *ACCMetric) GetValue() float64 { if acc.count == 0 { return 0 } - return acc.sum / acc.count + val := acc.sum / acc.count + return utils.Round(val, globalRoundingDecimals, utils.ROUNDING_MIDDLE) } diff --git a/engine/stats_queue.go b/engine/stats_queue.go index 4f900ffa2..9907c397e 100644 --- a/engine/stats_queue.go +++ b/engine/stats_queue.go @@ -30,7 +30,7 @@ type StatsQueue struct { cdrs []*QCdr conf *CdrStats metrics map[string]Metric - mux sync.RWMutex + mux sync.Mutex } // Simplified cdr structure containing only the necessary info @@ -141,8 +141,9 @@ func (sq *StatsQueue) purgeObsoleteCdrs() { } func (sq *StatsQueue) GetStats() map[string]float64 { - sq.mux.RLock() - defer sq.mux.RUnlock() + sq.mux.Lock() + defer sq.mux.Unlock() + sq.purgeObsoleteCdrs() return sq.getStats() }