From f2f46f80549590cbd7b69424fc2857dec80252cd Mon Sep 17 00:00:00 2001 From: DanB Date: Sun, 16 Mar 2014 19:51:30 +0100 Subject: [PATCH] Adding GetCallCostLog API and console command --- apier/apier.go | 1 + cmd/cgr-engine/cgr-engine.go | 2 +- console/get_callcost.go | 76 ++++++++++++++++++++++++++++++++++++ engine/storage_sql.go | 10 ++++- 4 files changed, 87 insertions(+), 2 deletions(-) create mode 100644 console/get_callcost.go diff --git a/apier/apier.go b/apier/apier.go index 48259cf64..5bc3b4381 100644 --- a/apier/apier.go +++ b/apier/apier.go @@ -40,6 +40,7 @@ type ApierV1 struct { RatingDb engine.RatingStorage AccountDb engine.AccountingStorage CdrDb engine.CdrStorage + LogDb engine.LogStorage Sched *scheduler.Scheduler Config *config.CGRConfig } diff --git a/cmd/cgr-engine/cgr-engine.go b/cmd/cgr-engine/cgr-engine.go index e368d000c..5d82fbc2d 100644 --- a/cmd/cgr-engine/cgr-engine.go +++ b/cmd/cgr-engine/cgr-engine.go @@ -383,7 +383,7 @@ func main() { } responder := &engine.Responder{ExitChan: exitChan} - apier := &apier.ApierV1{StorDb: loadDb, RatingDb: ratingDb, AccountDb: accountDb, CdrDb: cdrDb, Config: cfg} + apier := &apier.ApierV1{StorDb: loadDb, RatingDb: ratingDb, AccountDb: accountDb, CdrDb: cdrDb, LogDb: logDb, Config: cfg} if cfg.RaterEnabled && !cfg.BalancerEnabled && cfg.RaterBalancer != utils.INTERNAL { engine.Logger.Info("Registering Rater service") diff --git a/console/get_callcost.go b/console/get_callcost.go new file mode 100644 index 000000000..d12eb7c03 --- /dev/null +++ b/console/get_callcost.go @@ -0,0 +1,76 @@ +/* +Rating system designed to be used in VoIP Carriers World +Copyright (C) 2013 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 +*/ + +package console + +import ( + "fmt" + + "github.com/cgrates/cgrates/apier" + "github.com/cgrates/cgrates/engine" + "github.com/cgrates/cgrates/utils" +) + +func init() { + commands["get_callcost"] = &CmdGetCallCost{} +} + +// Commander implementation +type CmdGetCallCost struct { + rpcMethod string + rpcParams *apier.AttrGetCallCost + rpcResult *engine.CallCost +} + +// name should be exec's name +func (self *CmdGetCallCost) Usage(name string) string { + return fmt.Sprintf("\n\tUsage: cgr-console [cfg_opts...{-h}] get_callcost []") +} + +// set param defaults +func (self *CmdGetCallCost) defaults() error { + self.rpcMethod = "ApierV1.GetCallCostLog" + self.rpcParams = &apier.AttrGetCallCost{RunId: utils.DEFAULT_RUNID} + return nil +} + +// Parses command line args and builds CmdBalance value +func (self *CmdGetCallCost) FromArgs(args []string) error { + if len(args) < 3 { + return fmt.Errorf(self.Usage("")) + } + // Args look OK, set defaults before going further + self.defaults() + self.rpcParams.CgrId = args[2] + if len(args) == 4 { + self.rpcParams.RunId = args[3] + } + return nil +} + +func (self *CmdGetCallCost) RpcMethod() string { + return self.rpcMethod +} + +func (self *CmdGetCallCost) RpcParams() interface{} { + return self.rpcParams +} + +func (self *CmdGetCallCost) RpcResult() interface{} { + return &self.rpcResult +} diff --git a/engine/storage_sql.go b/engine/storage_sql.go index e19f9532c..839f714df 100644 --- a/engine/storage_sql.go +++ b/engine/storage_sql.go @@ -492,12 +492,20 @@ func (self *SQLStorage) LogCallCost(uuid, source, runid string, cc *CallCost) (e } func (self *SQLStorage) GetCallCostLog(cgrid, source, runid string) (cc *CallCost, err error) { - row := self.Db.QueryRow(fmt.Sprintf("SELECT cgrid, accid, direction, tenant, tor, account, subject, destination, cost, timespans, source FROM %s WHERE cgrid='%s' AND source='%s' AND runid='%s'", utils.TBL_COST_DETAILS, cgrid, source, runid)) + qry := fmt.Sprintf("SELECT cgrid, accid, direction, tenant, tor, account, subject, destination, cost, timespans, source FROM %s WHERE cgrid='%s' AND runid='%s'", + utils.TBL_COST_DETAILS, cgrid, runid) + if len(source) != 0 { + qry += fmt.Sprintf(" AND source='%s'", source) + } + row := self.Db.QueryRow(qry) var accid, src string var timespansJson string cc = &CallCost{Cost: -1} err = row.Scan(&cgrid, &accid, &cc.Direction, &cc.Tenant, &cc.TOR, &cc.Account, &cc.Subject, &cc.Destination, &cc.Cost, ×pansJson, &src) + if len(timespansJson) == 0 { // No costs returned + return nil, nil + } if err = json.Unmarshal([]byte(timespansJson), &cc.Timespans); err != nil { return nil, err }