diff --git a/apier/tpdestinations.go b/apier/tpdestinations.go index 1efbfd6c7..115e02834 100644 --- a/apier/tpdestinations.go +++ b/apier/tpdestinations.go @@ -18,32 +18,94 @@ along with this program. If not, see package apier - import ( - "fmt" "errors" - "github.com/cgrates/cgrates/utils" + "fmt" "github.com/cgrates/cgrates/rater" + "github.com/cgrates/cgrates/utils" ) type AttrGetTPDestinations struct { - TPid string - DestinationsTag string + TPid string + DestinationsTag string } - // Return destinations profile for a destination tag received as parameter -func (self *Apier) GetTPDestinations( attrs AttrGetTPDestinations, reply *rater.Destination) error { - if missing := utils.MissingStructFields(&attrs, []string{"TPid","DestinationsTag"}); len(missing) != 0 { //Params missing - return fmt.Errorf("%s:%v", utils.ERR_MANDATORY_IE_MISSING, missing) - } - if dst, err := self.StorDb.GetTPDestination( attrs.TPid, attrs.DestinationsTag ); err!= nil { +func (self *Apier) GetTPDestinations(attrs AttrGetTPDestinations, reply *rater.Destination) error { + if missing := utils.MissingStructFields(&attrs, []string{"TPid", "DestinationsTag"}); len(missing) != 0 { //Params missing + return fmt.Errorf("%s:%v", utils.ERR_MANDATORY_IE_MISSING, missing) + } + if dst, err := self.StorDb.GetTPDestination(attrs.TPid, attrs.DestinationsTag); err != nil { return fmt.Errorf("%s:%s", utils.ERR_SERVER_ERROR, err.Error()) } else if dst == nil { - return errors.New(utils.ERR_DST_NOT_FOUND) + return errors.New(utils.ERR_NOT_FOUND) } else { - reply = dst + *reply = *dst } return nil } +func (self *Apier) GetDestination(tag string, reply *rater.Destination) error { + if dst, err := self.StorDb.GetDestination(tag); err != nil { + return errors.New(utils.ERR_NOT_FOUND) + } else { + *reply = *dst + } + return nil +} + +func (self *Apier) SetDestination(dest *rater.Destination, reply *rater.Destination) error { + if err := self.StorDb.SetDestination(dest); err != nil { + return err + } + return nil +} + +/* +func (self *Apier) GetMoneyBalance(arg CallDescriptor, reply *CallCost) (err error) { + err = rs.getBalance(&arg, CREDIT, reply) + return err +} + +func (self *Apier) GetSMSBalance(arg CallDescriptor, reply *CallCost) (err error) { + err = rs.getBalance(&arg, SMS, reply) + return err +} + +func (self *Apier) GetInternetBalance(arg CallDescriptor, reply *CallCost) (err error) { + err = rs.getBalance(&arg, TRAFFIC, reply) + return err +} + +func (self *Apier) GetInternetTimeBalance(arg CallDescriptor, reply *CallCost) (err error) { + err = rs.getBalance(&arg, TRAFFIC_TIME, reply) + return err +} + +func (self *Apier) GetMinutesBalance(arg CallDescriptor, reply *CallCost) (err error) { + err = rs.getBalance(&arg, MINUTES, reply) + return err +} + +// Get balance +func (rs *Responder) getBalance(arg *CallDescriptor, balanceId string, reply *CallCost) (err error) { + if rs.Bal != nil { + return errors.New("No balancer supported for this command right now") + } + ubKey := arg.Direction + ":" + arg.Tenant + ":" + arg.Account + userBalance, err := storageGetter.GetUserBalance(ubKey) + if err != nil { + return err + } + if balance, balExists := userBalance.BalanceMap[balanceId+arg.Direction]; !balExists { + // No match, balanceId not found + return errors.New("-BALANCE_NOT_FOUND") + } else { + reply.Tenant = arg.Tenant + reply.Account = arg.Account + reply.Direction = arg.Direction + reply.Cost = balance + } + return nil +} +*/ \ No newline at end of file diff --git a/rater/actions_test.go b/rater/actions_test.go index 18d986ec6..d0477f4e3 100644 --- a/rater/actions_test.go +++ b/rater/actions_test.go @@ -708,7 +708,7 @@ func TestActionTriggerLogging(t *testing.T) { storageGetter.LogActionTrigger("rif", RATER_SOURCE, at, as) //expected := "rif*some_uuid;MONETARY;OUT;NAT;TEST_ACTIONS;100;10;false*|TOPUP|MONETARY|OUT|10|0" var key string - atMap, _ := storageGetter.GetAllActionTimings("") + atMap, _ := storageGetter.GetAllActionTimings() for k, v := range atMap { _ = k _ = v @@ -750,7 +750,7 @@ func TestActionTimingLogging(t *testing.T) { storageGetter.LogActionTiming(SCHED_SOURCE, at, as) //expected := "some uuid|test|one,two,three|;1,2,3,4,5,6,7,8,9,10,11,12;1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31;1,2,3,4,5;18:00:00;00:00:00;10;0;1;60;1|10|TEST_ACTIONS*|TOPUP|MONETARY|OUT|10|0" var key string - atMap, _ := storageGetter.GetAllActionTimings("") + atMap, _ := storageGetter.GetAllActionTimings() for k, v := range atMap { _ = k _ = v diff --git a/rater/calldesc.go b/rater/calldesc.go index ca5c2711a..18ce8e995 100644 --- a/rater/calldesc.go +++ b/rater/calldesc.go @@ -50,8 +50,6 @@ var ( storageGetter, _ = NewMapStorage() //storageGetter, _ = NewMongoStorage(db_server, "27017", "cgrates_test", "", "") //storageGetter, _ = NewRedisStorage(db_server+":6379", 11, "") - //storageGetter, _ = NewRedigoStorage(db_server+":6379", 11, "") - //storageGetter, _ = NewGosexyStorage(db_server+":6379", 11, "") storageLogger = storageGetter debitPeriod = 10 * time.Second ) diff --git a/rater/storage_interface.go b/rater/storage_interface.go index e1ec5c5a3..74f576b41 100644 --- a/rater/storage_interface.go +++ b/rater/storage_interface.go @@ -64,7 +64,7 @@ type DataStorage interface { SetUserBalance(*UserBalance) error GetActionTimings(string) (ActionTimings, error) SetActionTimings(string, ActionTimings) error - GetAllActionTimings(string) (map[string]ActionTimings, error) + GetAllActionTimings() (map[string]ActionTimings, error) SetCdr(utils.CDR) error SetRatedCdr(utils.CDR, *CallCost) error //GetAllActionTimingsLogs() (map[string]ActionsTimings, error) diff --git a/rater/storage_map.go b/rater/storage_map.go index 8ebc00da0..7dd7afd7b 100644 --- a/rater/storage_map.go +++ b/rater/storage_map.go @@ -130,15 +130,15 @@ func (ms *MapStorage) SetActionTimings(key string, ats ActionTimings) (err error return } -func (ms *MapStorage) GetAllActionTimings(tpid string) (ats map[string]ActionTimings, err error) { +func (ms *MapStorage) GetAllActionTimings() (ats map[string]ActionTimings, err error) { ats = make(map[string]ActionTimings) for key, value := range ms.dict { - if !strings.Contains(key, ACTION_TIMING_PREFIX+tpid) { + if !strings.Contains(key, ACTION_TIMING_PREFIX) { continue } var tempAts ActionTimings err = ms.ms.Unmarshal(value, &tempAts) - ats[key[len(ACTION_TIMING_PREFIX+tpid):]] = tempAts + ats[key[len(ACTION_TIMING_PREFIX):]] = tempAts } return diff --git a/rater/storage_mongo.go b/rater/storage_mongo.go index d056f8b06..4a3bd3f04 100644 --- a/rater/storage_mongo.go +++ b/rater/storage_mongo.go @@ -182,7 +182,7 @@ func (ms *MongoStorage) SetActionTimings(key string, ats ActionTimings) error { return ms.db.C("actiontimings").Insert(&AtKeyValue{key, ats}) } -func (ms *MongoStorage) GetAllActionTimings(tpid string) (ats map[string]ActionTimings, err error) { +func (ms *MongoStorage) GetAllActionTimings() (ats map[string]ActionTimings, err error) { result := AtKeyValue{} iter := ms.db.C("actiontimings").Find(nil).Iter() ats = make(map[string]ActionTimings) diff --git a/rater/storage_mysql.go b/rater/storage_mysql.go index 602a65796..46a0ccd8e 100644 --- a/rater/storage_mysql.go +++ b/rater/storage_mysql.go @@ -101,7 +101,7 @@ func (mys *MySQLStorage) GetActionTimings(key string) (ats ActionTimings, err er func (mys *MySQLStorage) SetActionTimings(key string, ats ActionTimings) (err error) { return } -func (mys *MySQLStorage) GetAllActionTimings(tpid string) (ats map[string]ActionTimings, err error) { +func (mys *MySQLStorage) GetAllActionTimings() (ats map[string]ActionTimings, err error) { return } diff --git a/rater/storage_postgres.go b/rater/storage_postgres.go index 11ff35062..b80aa090e 100644 --- a/rater/storage_postgres.go +++ b/rater/storage_postgres.go @@ -82,7 +82,7 @@ func (psl *PostgresStorage) GetActionTimings(key string) (ats ActionTimings, err func (psl *PostgresStorage) SetActionTimings(key string, ats ActionTimings) (err error) { return } -func (psl *PostgresStorage) GetAllActionTimings(tpid string) (ats map[string]ActionTimings, err error) { return } +func (psl *PostgresStorage) GetAllActionTimings() (ats map[string]ActionTimings, err error) { return } func (psl *PostgresStorage) LogCallCost(uuid, source string, cc *CallCost) (err error) { if psl.Db == nil { diff --git a/rater/storage_redis.go b/rater/storage_redis.go index 3ad4884cb..f04e6da9e 100644 --- a/rater/storage_redis.go +++ b/rater/storage_redis.go @@ -156,8 +156,8 @@ func (rs *RedisStorage) SetActionTimings(key string, ats ActionTimings) (err err return } -func (rs *RedisStorage) GetAllActionTimings(tpid string) (ats map[string]ActionTimings, err error) { - keys, err := rs.db.Keys(ACTION_TIMING_PREFIX + tpid + "*") +func (rs *RedisStorage) GetAllActionTimings() (ats map[string]ActionTimings, err error) { + keys, err := rs.db.Keys(ACTION_TIMING_PREFIX + "*") if err != nil { return nil, err } @@ -169,7 +169,7 @@ func (rs *RedisStorage) GetAllActionTimings(tpid string) (ats map[string]ActionT } var tempAts ActionTimings err = rs.ms.Unmarshal([]byte(values), &tempAts) - ats[key[len(ACTION_TIMING_PREFIX+tpid):]] = tempAts + ats[key[len(ACTION_TIMING_PREFIX):]] = tempAts } return diff --git a/scheduler/scheduler.go b/scheduler/scheduler.go index 23865143f..492a28f9a 100644 --- a/scheduler/scheduler.go +++ b/scheduler/scheduler.go @@ -64,7 +64,7 @@ func (s *Scheduler) Loop() { } func (s *Scheduler) LoadActionTimings(storage rater.DataStorage) { - actionTimings, err := storage.GetAllActionTimings("") + actionTimings, err := storage.GetAllActionTimings() if err != nil { rater.Logger.Warning(fmt.Sprintf("Cannot get action timings: %v", err)) } diff --git a/utils/consts.go b/utils/consts.go index 98bbaf627..5ffd2d0a1 100644 --- a/utils/consts.go +++ b/utils/consts.go @@ -11,7 +11,7 @@ const ( RATED = "rated" ERR_NOT_IMPLEMENTED = "NOT_IMPLEMENTED" ERR_SERVER_ERROR = "SERVER_ERROR" - ERR_DST_NOT_FOUND = "DESTINATION_NOT_FOUND" + ERR_NOT_FOUND = "NOT_FOUND" ERR_MANDATORY_IE_MISSING = "MANDATORY_IE_MISSING" )