diff --git a/apier/tpactiontimings.go b/apier/tpactiontimings.go index 802e53b9f..55b944e83 100644 --- a/apier/tpactiontimings.go +++ b/apier/tpactiontimings.go @@ -41,8 +41,8 @@ func (self *Apier) SetTPActionTimings(attrs utils.ApiTPActionTimings, reply *str return errors.New(utils.ERR_DUPLICATE) } ats := make(map[string][]*utils.TPActionTimingsRow, 1) // Only one id will be stored in the map - for _,at := range attrs.ActionTimings { - ats[attrs.ActionTimingsId] = append( ats[attrs.ActionTimingsId], &utils.TPActionTimingsRow{at.ActionsId, at.TimingId, at.Weight} ) + for _, at := range attrs.ActionTimings { + ats[attrs.ActionTimingsId] = append(ats[attrs.ActionTimingsId], &utils.TPActionTimingsRow{at.ActionsId, at.TimingId, at.Weight}) } if err := self.StorDb.SetTPActionTimings(attrs.TPid, ats); err != nil { return fmt.Errorf("%s:%s", utils.ERR_SERVER_ERROR, err.Error()) @@ -52,7 +52,7 @@ func (self *Apier) SetTPActionTimings(attrs utils.ApiTPActionTimings, reply *str } type AttrGetTPActionTimings struct { - TPid string // Tariff plan id + TPid string // Tariff plan id ActionTimingsId string // ActionTimings id } @@ -66,11 +66,11 @@ func (self *Apier) GetTPActionTimings(attrs AttrGetTPActionTimings, reply *utils } else if len(ats) == 0 { return errors.New(utils.ERR_NOT_FOUND) } else { // Got the data we need, convert it from []TPActionTimingsRow into ApiTPActionTimings - atRply := &utils.ApiTPActionTimings{ attrs.TPid, attrs.ActionTimingsId, make([]utils.ApiActionTiming, len(ats[attrs.ActionTimingsId])) } - for idx,row := range ats[attrs.ActionTimingsId] { - atRply.ActionTimings[idx] = utils.ApiActionTiming{ row.ActionsId, row.TimingId, row.Weight } + atRply := &utils.ApiTPActionTimings{attrs.TPid, attrs.ActionTimingsId, make([]utils.ApiActionTiming, len(ats[attrs.ActionTimingsId]))} + for idx, row := range ats[attrs.ActionTimingsId] { + atRply.ActionTimings[idx] = utils.ApiActionTiming{row.ActionsId, row.TimingId, row.Weight} } - *reply = *atRply + *reply = *atRply } return nil } @@ -93,5 +93,3 @@ func (self *Apier) GetTPActionTimingIds(attrs AttrGetTPActionTimingIds, reply *[ } return nil } - - diff --git a/apier/tpactiontriggers.go b/apier/tpactiontriggers.go index af85c1a9f..021f7b213 100644 --- a/apier/tpactiontriggers.go +++ b/apier/tpactiontriggers.go @@ -21,15 +21,14 @@ package apier import ( "errors" "fmt" - "github.com/cgrates/cgrates/utils" "github.com/cgrates/cgrates/rater" + "github.com/cgrates/cgrates/utils" ) - // Creates a new ActionTriggers profile within a tariff plan func (self *Apier) SetTPActionTriggers(attrs utils.ApiTPActionTriggers, reply *string) error { - if missing := utils.MissingStructFields(&attrs, - []string{"TPid","ActionTriggersId",}); len(missing) != 0 { + if missing := utils.MissingStructFields(&attrs, + []string{"TPid", "ActionTriggersId"}); len(missing) != 0 { return fmt.Errorf("%s:%v", utils.ERR_MANDATORY_IE_MISSING, missing) } if exists, err := self.StorDb.ExistsTPActionTriggers(attrs.TPid, attrs.ActionTriggersId); err != nil { @@ -39,26 +38,25 @@ func (self *Apier) SetTPActionTriggers(attrs utils.ApiTPActionTriggers, reply *s } aTriggers := make([]*rater.ActionTrigger, len(attrs.ActionTriggers)) for idx, at := range attrs.ActionTriggers { - requiredFields := []string{"BalanceId","Direction","ThresholdType","ThresholdValue","ActionsId","Weight"} + requiredFields := []string{"BalanceId", "Direction", "ThresholdType", "ThresholdValue", "ActionsId", "Weight"} if missing := utils.MissingStructFields(&at, requiredFields); len(missing) != 0 { return fmt.Errorf("%s:Balance:%s:%v", utils.ERR_MANDATORY_IE_MISSING, at.BalanceId, missing) } at := &rater.ActionTrigger{ - BalanceId: at.BalanceId, - Direction: at.Direction, - ThresholdType: at.ThresholdType, - ThresholdValue: at.ThresholdValue, - DestinationId: at.DestinationId, - Weight: at.Weight, - ActionsId: at.ActionsId, - } - aTriggers[idx] = at + BalanceId: at.BalanceId, + Direction: at.Direction, + ThresholdType: at.ThresholdType, + ThresholdValue: at.ThresholdValue, + DestinationId: at.DestinationId, + Weight: at.Weight, + ActionsId: at.ActionsId, + } + aTriggers[idx] = at } - - + ats := map[string][]*rater.ActionTrigger{ attrs.ActionTriggersId: aTriggers} - + if err := self.StorDb.SetTPActionTriggers(attrs.TPid, ats); err != nil { return fmt.Errorf("%s:%s", utils.ERR_SERVER_ERROR, err.Error()) } @@ -67,7 +65,7 @@ func (self *Apier) SetTPActionTriggers(attrs utils.ApiTPActionTriggers, reply *s } type AttrGetTPActionTriggers struct { - TPid string // Tariff plan id + TPid string // Tariff plan id ActionTriggersId string // ActionTrigger id } @@ -82,20 +80,20 @@ func (self *Apier) GetTPActionTriggers(attrs AttrGetTPActionTriggers, reply *uti return errors.New(utils.ERR_NOT_FOUND) } else { aTriggers := make([]utils.ApiActionTrigger, len(ats[attrs.ActionTriggersId])) - for idx,row := range ats[attrs.ActionTriggersId] { + for idx, row := range ats[attrs.ActionTriggersId] { aTriggers[idx] = utils.ApiActionTrigger{ - BalanceId: row.BalanceId, - Direction: row.Direction, - ThresholdType: row.ThresholdType, - ThresholdValue: row.ThresholdValue, - DestinationId: row.DestinationId, - ActionsId: row.ActionsId, - Weight: row.Weight, - } + BalanceId: row.BalanceId, + Direction: row.Direction, + ThresholdType: row.ThresholdType, + ThresholdValue: row.ThresholdValue, + DestinationId: row.DestinationId, + ActionsId: row.ActionsId, + Weight: row.Weight, + } } - atRply := &utils.ApiTPActionTriggers{ attrs.TPid, attrs.ActionTriggersId, aTriggers } - - *reply = *atRply + atRply := &utils.ApiTPActionTriggers{attrs.TPid, attrs.ActionTriggersId, aTriggers} + + *reply = *atRply } return nil } @@ -118,4 +116,3 @@ func (self *Apier) GetTPActionTriggerIds(attrs AttrGetTPActionTriggerIds, reply } return nil } - diff --git a/docs/api_tpactiontriggers.rst b/docs/api_tpactiontriggers.rst new file mode 100644 index 000000000..1d1156018 --- /dev/null +++ b/docs/api_tpactiontriggers.rst @@ -0,0 +1,222 @@ +Apier.SetTPActionTriggers ++++++++++++++++++++++++++ + +Creates a new ActionTriggers profile within a tariff plan. + +**Request**: + + Data: + :: + + type ApiTPActionTriggers struct { + TPid string // Tariff plan id + ActionTriggersId string // Profile id + ActionTriggers []ApiActionTrigger // Set of triggers grouped in this profile + + } + + type ApiActionTrigger struct { + BalanceId string // Id of the balance this trigger monitors + Direction string // Traffic direction + ThresholdType string // This threshold type + ThresholdValue float64 // Threshold + DestinationId string // Id of the destination profile + ActionsId string // Actions which will execute on threshold reached + Weight float64 // weight + } + + Mandatory parameters: ``[]string{"TPid", "ActionTriggersId","BalanceId", "Direction", "ThresholdType", "ThresholdValue", "ActionsId", "Weight"}`` + + *JSON sample*: + :: + + { + "id": 2, + "method": "Apier.SetTPActionTriggers", + "params": [ + { + "ActionTriggers": [ + { + "ActionsId": "ACTION_1", + "BalanceId": "MONETARY", + "DestinationId": "", + "Direction": "OUT", + "ThresholdType": "MIN_BALANCE", + "ThresholdValue": 5, + "Weight": 10 + } + ], + "ActionTriggersId": "SAMPLE_ATS_1", + "TPid": "SAMPLE_TP_2" + } + ] + } + +**Reply**: + + Data: + :: + + string + + Possible answers: + ``OK`` - Success. + + *JSON sample*: + :: + + { + "error": null, + "id": 2, + "result": "OK" + } + +**Errors**: + + ``MANDATORY_IE_MISSING`` - Mandatory parameter missing from request. + + ``SERVER_ERROR`` - Server error occurred. + + ``DUPLICATE`` - The specified combination of TPid/ActionTriggersId already present in StorDb. + + +Apier.GetTPActionTriggers ++++++++++++++++++++++++++ + +Queries specific ActionTriggers profile on tariff plan. + +**Request**: + + Data: + :: + + type AttrGetTPActionTriggers struct { + TPid string // Tariff plan id + ActionTriggersId string // ActionTrigger id + } + + Mandatory parameters: ``[]string{"TPid", "ActionTriggersId"}`` + + *JSON sample*: + :: + + { + "id": 0, + "method": "Apier.GetTPActionTriggers", + "params": [ + { + "ActionTriggersId": "SAMPLE_ATS_1", + "TPid": "SAMPLE_TP_2" + } + ] + } + +**Reply**: + + Data: + :: + + type ApiTPActionTriggers struct { + TPid string // Tariff plan id + ActionTriggersId string // Profile id + ActionTriggers []ApiActionTrigger // Set of triggers grouped in this profile + + } + + type ApiActionTrigger struct { + BalanceId string // Id of the balance this trigger monitors + Direction string // Traffic direction + ThresholdType string // This threshold type + ThresholdValue float64 // Threshold + DestinationId string // Id of the destination profile + ActionsId string // Actions which will execute on threshold reached + Weight float64 // weight + } + + *JSON sample*: + :: + + { + "error": null, + "id": 0, + "result": { + "ActionTriggers": [ + { + "ActionsId": "ACTION_1", + "BalanceId": "MONETARY", + "DestinationId": "", + "Direction": "OUT", + "ThresholdType": "MIN_BALANCE", + "ThresholdValue": 5, + "Weight": 10 + } + ], + "ActionTriggersId": "SAMPLE_ATS_1", + "TPid": "SAMPLE_TP_2" + } + } + +**Errors**: + + ``MANDATORY_IE_MISSING`` - Mandatory parameter missing from request. + + ``SERVER_ERROR`` - Server error occurred. + + ``NOT_FOUND`` - Requested ActionTriggersId profile not found. + + +Apier.GetTPActionTriggerIds ++++++++++++++++++++++++++++ + +Queries ActionTriggers identities on specific tariff plan. + +**Request**: + + Data: + :: + + type AttrGetTPActionTriggerIds struct { + TPid string // Tariff plan id + } + + Mandatory parameters: ``[]string{"TPid"}`` + + *JSON sample*: + :: + + { + "id": 5, + "method": "Apier.GetTPActionTriggerIds", + "params": [ + { + "TPid": "SAMPLE_TP_2" + } + ] + } + +**Reply**: + + Data: + :: + + []string + + *JSON sample*: + :: + + { + "error": null, + "id": 5, + "result": [ + "SAMPLE_ATS_1", + "SAMPLE_ATS_2" + ] +} + +**Errors**: + + ``MANDATORY_IE_MISSING`` - Mandatory parameter missing from request. + + ``SERVER_ERROR`` - Server error occurred. + + ``NOT_FOUND`` - There are no ActionTriggers profiles defined on the selected TPid. diff --git a/docs/apicalls.rst b/docs/apicalls.rst index 7ba97252f..7fa865bf8 100644 --- a/docs/apicalls.rst +++ b/docs/apicalls.rst @@ -199,6 +199,14 @@ ActionTimings api_tpactiontimings +ActionTriggers +~~~~~~~~~~~~ + +.. toctree:: + :maxdepth: 2 + + api_tpactiontriggers + 6.1.5. Management API --------------------- diff --git a/rater/storage_mongo.go b/rater/storage_mongo.go index 215721248..d70947521 100644 --- a/rater/storage_mongo.go +++ b/rater/storage_mongo.go @@ -281,15 +281,15 @@ func (ms *MongoStorage) GetTPActionTimingIds(tpid string) ([]string, error) { } func (ms *MongoStorage) ExistsTPActionTriggers(tpid, atId string) (bool, error) { - return false, errors.New(utils.ERR_NOT_IMPLEMENTED) + return false, errors.New(utils.ERR_NOT_IMPLEMENTED) } func (ms *MongoStorage) SetTPActionTriggers(tpid string, ats map[string][]*ActionTrigger) error { - return errors.New(utils.ERR_NOT_IMPLEMENTED) + return errors.New(utils.ERR_NOT_IMPLEMENTED) } func (ms *MongoStorage) GetTPActionTriggerIds(tpid string) ([]string, error) { - return nil, errors.New(utils.ERR_NOT_IMPLEMENTED) + return nil, errors.New(utils.ERR_NOT_IMPLEMENTED) } func (ms *MongoStorage) GetActions(key string) (as Actions, err error) { diff --git a/rater/storage_redis.go b/rater/storage_redis.go index 521366a46..f3f8a555a 100644 --- a/rater/storage_redis.go +++ b/rater/storage_redis.go @@ -236,15 +236,15 @@ func (rs *RedisStorage) GetTPActionTimingIds(tpid string) ([]string, error) { } func (rs *RedisStorage) ExistsTPActionTriggers(tpid, atId string) (bool, error) { - return false, errors.New(utils.ERR_NOT_IMPLEMENTED) + return false, errors.New(utils.ERR_NOT_IMPLEMENTED) } func (rs *RedisStorage) SetTPActionTriggers(tpid string, ats map[string][]*ActionTrigger) error { - return errors.New(utils.ERR_NOT_IMPLEMENTED) + return errors.New(utils.ERR_NOT_IMPLEMENTED) } func (rs *RedisStorage) GetTPActionTriggerIds(tpid string) ([]string, error) { - return nil, errors.New(utils.ERR_NOT_IMPLEMENTED) + return nil, errors.New(utils.ERR_NOT_IMPLEMENTED) } func (rs *RedisStorage) GetActions(key string) (as Actions, err error) { diff --git a/rater/storage_sql.go b/rater/storage_sql.go index 23acb897a..f83a5df4e 100644 --- a/rater/storage_sql.go +++ b/rater/storage_sql.go @@ -665,20 +665,20 @@ func (self *SQLStorage) GetTPActionTimingIds(tpid string) ([]string, error) { } func (self *SQLStorage) ExistsTPActionTriggers(tpid, atId string) (bool, error) { - var exists bool + var exists bool err := self.Db.QueryRow(fmt.Sprintf("SELECT EXISTS (SELECT 1 FROM %s WHERE tpid='%s' AND tag='%s')", utils.TBL_TP_ACTION_TRIGGERS, tpid, atId)).Scan(&exists) if err != nil { return false, err } - return exists, nil + return exists, nil } func (self *SQLStorage) SetTPActionTriggers(tpid string, ats map[string][]*ActionTrigger) error { - if len(ats) == 0 { + if len(ats) == 0 { return nil //Nothing to set } - qry := fmt.Sprintf("INSERT INTO %s (tpid,tag,balance_tag,direction,threshold_type,threshold_value,destination_tag,actions_tag,weight) VALUES ", - utils.TBL_TP_ACTION_TRIGGERS) + qry := fmt.Sprintf("INSERT INTO %s (tpid,tag,balance_tag,direction,threshold_type,threshold_value,destination_tag,actions_tag,weight) VALUES ", + utils.TBL_TP_ACTION_TRIGGERS) for atId, atRows := range ats { for idx, atsRow := range atRows { if idx != 0 { //Consecutive values after the first will be prefixed with "," as separator @@ -696,7 +696,7 @@ func (self *SQLStorage) SetTPActionTriggers(tpid string, ats map[string][]*Actio } func (self *SQLStorage) GetTPActionTriggerIds(tpid string) ([]string, error) { - rows, err := self.Db.Query(fmt.Sprintf("SELECT DISTINCT tag FROM %s where tpid='%s'", utils.TBL_TP_ACTION_TRIGGERS, tpid)) + rows, err := self.Db.Query(fmt.Sprintf("SELECT DISTINCT tag FROM %s where tpid='%s'", utils.TBL_TP_ACTION_TRIGGERS, tpid)) if err != nil { return nil, err } @@ -1098,7 +1098,7 @@ func (self *SQLStorage) GetTpActionTimings(tpid, tag string) (ats map[string][]* func (self *SQLStorage) GetTpActionTriggers(tpid, tag string) (map[string][]*ActionTrigger, error) { ats := make(map[string][]*ActionTrigger) - q := fmt.Sprintf("SELECT tpid,tag,balance_tag,direction,threshold_type,threshold_value,destination_tag,actions_tag,weight FROM %s WHERE tpid='%s'", + q := fmt.Sprintf("SELECT tpid,tag,balance_tag,direction,threshold_type,threshold_value,destination_tag,actions_tag,weight FROM %s WHERE tpid='%s'", utils.TBL_TP_ACTION_TRIGGERS, tpid) if tag != "" { q += fmt.Sprintf(" AND tag='%s'", tag) diff --git a/utils/apitpdata.go b/utils/apitpdata.go index cf48d6d8b..20d56003c 100644 --- a/utils/apitpdata.go +++ b/utils/apitpdata.go @@ -103,8 +103,8 @@ type Action struct { } type ApiTPActionTimings struct { - TPid string // Tariff plan id - ActionTimingsId string // ActionTimings id + TPid string // Tariff plan id + ActionTimingsId string // ActionTimings id ActionTimings []ApiActionTiming // Set of ActionTiming bindings this profile will group } @@ -115,20 +115,20 @@ type ApiActionTiming struct { } type ApiTPActionTriggers struct { - TPid string // Tariff plan id - ActionTriggersId string // Profile id - ActionTriggers []ApiActionTrigger // Set of triggers grouped in this profile - + TPid string // Tariff plan id + ActionTriggersId string // Profile id + ActionTriggers []ApiActionTrigger // Set of triggers grouped in this profile + } type ApiActionTrigger struct { - BalanceId string // Id of the balance this trigger monitors - Direction string // Traffic direction - ThresholdType string // This threshold type - ThresholdValue float64 // Threshold - DestinationId string // Id of the destination profile - ActionsId string // Actions which will execute on threshold reached - Weight float64 // weight + BalanceId string // Id of the balance this trigger monitors + Direction string // Traffic direction + ThresholdType string // This threshold type + ThresholdValue float64 // Threshold + DestinationId string // Id of the destination profile + ActionsId string // Actions which will execute on threshold reached + Weight float64 // weight } type TPAccountActions struct { diff --git a/utils/tpdata.go b/utils/tpdata.go index 3b1fe33e8..a3c5001c3 100644 --- a/utils/tpdata.go +++ b/utils/tpdata.go @@ -18,11 +18,9 @@ along with this program. If not, see package utils - // Represents a single row in .csv or storDb, id will be used as key in the map holding all rows type TPActionTimingsRow struct { ActionsId string // Actions id TimingId string // Timing profile id Weight float64 // Binding's weight } -