mirror of
https://github.com/cgrates/cgrates.git
synced 2026-02-11 18:16:24 +05:00
started to add apier methods
This commit is contained in:
@@ -18,32 +18,94 @@ along with this program. If not, see <http://www.gnu.org/licenses/>
|
||||
|
||||
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
|
||||
}
|
||||
*/
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
)
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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))
|
||||
}
|
||||
|
||||
@@ -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"
|
||||
)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user