Files
cgrates/engine/stats_queue.go
Radu Ioan Fericean 3d0553ac37 started stats saver
2015-06-18 23:26:19 +03:00

204 lines
4.7 KiB
Go

/*
Rating system designed to be used in VoIP Carriers World
Copyright (C) 2012-2015 ITsysCOM
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>
*/
package engine
import (
"strings"
"sync"
"time"
)
type StatsQueue struct {
Cdrs []*QCdr
Conf *CdrStats
Metrics map[string]Metric
mux sync.Mutex
dirty bool
}
var METRIC_TRIGGER_MAP = map[string]string{
"*min_asr": ASR,
"*max_asr": ASR,
"*min_pdd": PDD,
"*max_pdd": PDD,
"*min_acd": ACD,
"*max_acd": ACD,
"*min_tcd": TCD,
"*max_tcd": TCD,
"*min_acc": ACC,
"*max_acc": ACC,
"*min_tcc": TCC,
"*max_tcc": TCC,
}
// Simplified cdr structure containing only the necessary info
type QCdr struct {
SetupTime time.Time
AnswerTime time.Time
Pdd time.Duration
Usage time.Duration
Cost float64
}
func NewStatsQueue(conf *CdrStats) *StatsQueue {
if conf == nil {
return &StatsQueue{Metrics: make(map[string]Metric)}
}
sq := &StatsQueue{}
sq.UpdateConf(conf)
return sq
}
func (sq *StatsQueue) UpdateConf(conf *CdrStats) {
sq.mux.Lock()
defer sq.mux.Unlock()
sq.Conf = conf
sq.Cdrs = make([]*QCdr, 0)
sq.Metrics = make(map[string]Metric, len(conf.Metrics))
sq.dirty = true
for _, m := range conf.Metrics {
if metric := CreateMetric(m); metric != nil {
sq.Metrics[m] = metric
}
}
}
func (sq *StatsQueue) IsDirty() bool {
sq.mux.Lock()
defer sq.mux.Unlock()
v := sq.dirty
// take advantage of the locking to set it to flip it
sq.dirty = false
return v
}
func (sq *StatsQueue) AppendCDR(cdr *StoredCdr) {
sq.mux.Lock()
defer sq.mux.Unlock()
if sq.Conf.AcceptCdr(cdr) {
qcdr := sq.simplifyCdr(cdr)
sq.Cdrs = append(sq.Cdrs, qcdr)
sq.addToMetrics(qcdr)
sq.purgeObsoleteCdrs()
sq.dirty = true
// check for trigger
stats := sq.getStats()
sq.Conf.Triggers.Sort()
for _, at := range sq.Conf.Triggers {
if at.MinQueuedItems > 0 && len(sq.Cdrs) < at.MinQueuedItems {
continue
}
if strings.HasPrefix(at.ThresholdType, "*min_") {
if value, ok := stats[METRIC_TRIGGER_MAP[at.ThresholdType]]; ok {
if value > STATS_NA && value <= at.ThresholdValue {
at.Execute(nil, sq.Triggered(at))
}
}
}
if strings.HasPrefix(at.ThresholdType, "*max_") {
if value, ok := stats[METRIC_TRIGGER_MAP[at.ThresholdType]]; ok {
if value > STATS_NA && value >= at.ThresholdValue {
at.Execute(nil, sq.Triggered(at))
}
}
}
}
}
}
func (sq *StatsQueue) addToMetrics(cdr *QCdr) {
sq.dirty = true
for _, metric := range sq.Metrics {
metric.AddCdr(cdr)
}
}
func (sq *StatsQueue) removeFromMetrics(cdr *QCdr) {
sq.dirty = true
for _, metric := range sq.Metrics {
metric.RemoveCdr(cdr)
}
}
func (sq *StatsQueue) simplifyCdr(cdr *StoredCdr) *QCdr {
return &QCdr{
SetupTime: cdr.SetupTime,
AnswerTime: cdr.AnswerTime,
Pdd: cdr.Pdd,
Usage: cdr.Usage,
Cost: cdr.Cost,
}
}
func (sq *StatsQueue) purgeObsoleteCdrs() {
if sq.Conf.QueueLength > 0 {
currentLength := len(sq.Cdrs)
if currentLength > sq.Conf.QueueLength {
for _, cdr := range sq.Cdrs[:currentLength-sq.Conf.QueueLength] {
sq.removeFromMetrics(cdr)
}
sq.Cdrs = sq.Cdrs[currentLength-sq.Conf.QueueLength:]
}
}
if sq.Conf.TimeWindow > 0 {
for i, cdr := range sq.Cdrs {
if time.Now().Sub(cdr.SetupTime) > sq.Conf.TimeWindow {
sq.removeFromMetrics(cdr)
continue
} else {
if i > 0 {
sq.Cdrs = sq.Cdrs[i:]
}
break
}
}
}
}
func (sq *StatsQueue) GetStats() map[string]float64 {
sq.mux.Lock()
defer sq.mux.Unlock()
sq.purgeObsoleteCdrs()
return sq.getStats()
}
func (sq *StatsQueue) getStats() map[string]float64 {
stat := make(map[string]float64, len(sq.Metrics))
for key, metric := range sq.Metrics {
stat[key] = metric.GetValue()
}
return stat
}
func (sq *StatsQueue) GetId() string {
return sq.Conf.Id
}
// Convert data into a struct which can be used in actions based on triggers hit
func (sq *StatsQueue) Triggered(at *ActionTrigger) *StatsQueueTriggered {
return &StatsQueueTriggered{Id: sq.Conf.Id, Metrics: sq.getStats(), Trigger: at}
}
// Struct to be passed to triggered actions
type StatsQueueTriggered struct {
Id string // StatsQueueId
Metrics map[string]float64
Trigger *ActionTrigger
}