From ac5fddd8ca83d80e0a0e1b2b8a5fdd8437e1e39c Mon Sep 17 00:00:00 2001 From: Radu Ioan Fericean Date: Fri, 29 Apr 2016 21:08:10 +0300 Subject: [PATCH] improved GetActions in ApierV2 (params changes!) --- apier/v1/apier.go | 2 +- apier/v2/accounts.go | 2 +- apier/v2/apier.go | 55 +++++++++++++++++++++++++++++ console/actions.go | 11 +++--- data/tariffplans/testtp/Actions.csv | 2 ++ engine/storage_interface.go | 1 + engine/tp_reader.go | 6 ++-- 7 files changed, 70 insertions(+), 9 deletions(-) diff --git a/apier/v1/apier.go b/apier/v1/apier.go index f65b3df94..6d489468d 100644 --- a/apier/v1/apier.go +++ b/apier/v1/apier.go @@ -572,7 +572,7 @@ func (self *ApierV1) GetActions(actsId string, reply *[]*utils.TPAction) error { bf := engAct.Balance if bf != nil { act.BalanceType = bf.GetType() - act.Units = bf.Value.String() + act.Units = strconv.FormatFloat(bf.GetValue(), 'f', -1, 64) act.Directions = bf.GetDirections().String() act.DestinationIds = bf.GetDestinationIDs().String() act.RatingSubject = bf.GetRatingSubject() diff --git a/apier/v2/accounts.go b/apier/v2/accounts.go index 08f6832dd..83d3d56f2 100644 --- a/apier/v2/accounts.go +++ b/apier/v2/accounts.go @@ -33,7 +33,7 @@ func (self *ApierV2) GetAccounts(attr utils.AttrGetAccounts, reply *[]*engine.Ac var accountKeys []string var err error if len(attr.AccountIds) == 0 { - if accountKeys, err = self.AccountDb.GetKeysForPrefix(utils.ACCOUNT_PREFIX+utils.ConcatenatedKey(attr.Tenant), true); err != nil { + if accountKeys, err = self.AccountDb.GetKeysForPrefix(utils.ACCOUNT_PREFIX+attr.Tenant, true); err != nil { return err } } else { diff --git a/apier/v2/apier.go b/apier/v2/apier.go index 9f5f4cc2a..4bf68bd4f 100644 --- a/apier/v2/apier.go +++ b/apier/v2/apier.go @@ -21,6 +21,7 @@ package v2 import ( "errors" "fmt" + "math" "os" "path" "strings" @@ -263,3 +264,57 @@ func (self *ApierV2) LoadTariffPlanFromFolder(attrs utils.AttrLoadTpFromFolder, *reply = *li return nil } + +type AttrGetActions struct { + ActionIDs []string + Offset int // Set the item offset + Limit int // Limit number of items retrieved +} + +// Retrieves actions attached to specific ActionsId within cache +func (self *ApierV2) GetActions(attr AttrGetActions, reply *map[string]engine.Actions) error { + var actionKeys []string + var err error + if len(attr.ActionIDs) == 0 { + if actionKeys, err = self.AccountDb.GetKeysForPrefix(utils.ACTION_PREFIX, false); err != nil { + return err + } + } else { + for _, accID := range attr.ActionIDs { + if len(accID) == 0 { // Source of error returned from redis (key not found) + continue + } + actionKeys = append(actionKeys, utils.ACCOUNT_PREFIX+accID) + } + } + if len(actionKeys) == 0 { + return nil + } + if attr.Offset > len(actionKeys) { + attr.Offset = len(actionKeys) + } + if attr.Offset < 0 { + attr.Offset = 0 + } + var limitedActions []string + if attr.Limit != 0 { + max := math.Min(float64(attr.Offset+attr.Limit), float64(len(actionKeys))) + limitedActions = actionKeys[attr.Offset:int(max)] + } else { + limitedActions = actionKeys[attr.Offset:] + } + retActions := make(map[string]engine.Actions) + for _, accKey := range limitedActions { + key := accKey[len(utils.ACTION_PREFIX):] + acts, err := self.RatingDb.GetActions(key, false) + if err != nil { + return utils.NewErrServerError(err) + } + if len(acts) > 0 { + retActions[key] = acts + + } + } + *reply = retActions + return nil +} diff --git a/console/actions.go b/console/actions.go index 25e9af0e5..1d2504029 100644 --- a/console/actions.go +++ b/console/actions.go @@ -18,7 +18,10 @@ along with this program. If not, see package console -import "github.com/cgrates/cgrates/utils" +import ( + "github.com/cgrates/cgrates/apier/v2" + "github.com/cgrates/cgrates/engine" +) func init() { c := &CmdGetActions{ @@ -33,7 +36,7 @@ func init() { type CmdGetActions struct { name string rpcMethod string - rpcParams *StringWrapper + rpcParams *v2.AttrGetActions *CommandExecuter } @@ -47,7 +50,7 @@ func (self *CmdGetActions) RpcMethod() string { func (self *CmdGetActions) RpcParams(reset bool) interface{} { if reset || self.rpcParams == nil { - self.rpcParams = &StringWrapper{} + self.rpcParams = &v2.AttrGetActions{} } return self.rpcParams } @@ -57,6 +60,6 @@ func (self *CmdGetActions) PostprocessRpcParams() error { } func (self *CmdGetActions) RpcResult() interface{} { - a := make([]*utils.TPAction, 0) + a := make(map[string]engine.Actions, 0) return &a } diff --git a/data/tariffplans/testtp/Actions.csv b/data/tariffplans/testtp/Actions.csv index 680866202..20d61a27d 100644 --- a/data/tariffplans/testtp/Actions.csv +++ b/data/tariffplans/testtp/Actions.csv @@ -10,3 +10,5 @@ TOPUP_DATA_r,*topup,,,,*data,*out,,DATA_DEST,datar,,*unlimited,,50000000000,10,f TOPUP_VOICE,*topup,,,,*voice,*out,,GERMANY_MOBILE,,,*unlimited,,50000,10,false,false,10 TOPUP_NEG,*topup,,,,*voice,*out,,GERMANY;!GERMANY_MOBILE,*zero1m,,*unlimited,,100,10,false,false,10 RPC,*cgr_rpc,"{""Address"": ""localhost:2013"",""Transport"":""*gob"",""Method"":""ApierV2.SetAccount"",""Attempts"":1,""Async"" :false,""Params"":{""Account"":""rpc"",""Tenant"":""cgrates.org""}}",,,,,,,,,,,,,,, +DID,*debit,,,,*monetary,*out,,*any,,,*unlimited,*any,"{""Method"":""*incremental"",""Params"":{""Units"":1, ""Interval"":""month"",""Increment"":""day""}}",10.0,,,10.0 +DID,*cdrlog,"{""action"":""^DID"",""prev_balance"":""BalanceValue""}",,,*monetary,*out,,*any,,,*unlimited,,,10.0,,,10.0 diff --git a/engine/storage_interface.go b/engine/storage_interface.go index 96a4b32c6..5fd6657db 100644 --- a/engine/storage_interface.go +++ b/engine/storage_interface.go @@ -204,6 +204,7 @@ func NewCodecMsgpackMarshaler() *CodecMsgpackMarshaler { cmm := &CodecMsgpackMarshaler{new(codec.MsgpackHandle)} mh := cmm.mh mh.MapType = reflect.TypeOf(map[string]interface{}(nil)) + mh.RawToString = true return cmm } diff --git a/engine/tp_reader.go b/engine/tp_reader.go index a2a71470b..4eaf2e81f 100644 --- a/engine/tp_reader.go +++ b/engine/tp_reader.go @@ -514,7 +514,7 @@ func (tpr *TpReader) LoadActions() (err error) { } } acts[idx] = &Action{ - Id: tag + strconv.Itoa(idx), + Id: tag, ActionType: tpact.Identifier, //BalanceType: tpact.BalanceType, Weight: tpact.Weight, @@ -990,7 +990,7 @@ func (tpr *TpReader) LoadAccountActionsFiltered(qriedAA *TpAccountAction) error } } acts[idx] = &Action{ - Id: tag + strconv.Itoa(idx), + Id: tag, ActionType: tpact.Identifier, //BalanceType: tpact.BalanceType, Weight: tpact.Weight, @@ -1338,7 +1338,7 @@ func (tpr *TpReader) LoadCdrStatsFiltered(tag string, save bool) (err error) { } } acts[idx] = &Action{ - Id: tag + strconv.Itoa(idx), + Id: tag, ActionType: tpact.Identifier, //BalanceType: tpact.BalanceType, Weight: tpact.Weight,