Adding TPDestRateTiming APIs and attached documentation

This commit is contained in:
DanB
2013-07-11 14:44:54 +02:00
parent 341c96552d
commit aa1c2021d3
13 changed files with 471 additions and 84 deletions

View File

@@ -26,7 +26,6 @@ import (
"github.com/cgrates/cgrates/utils"
)
// Creates a new DestinationRate profile within a tariff plan
func (self *Apier) SetTPDestinationRate(attrs utils.TPDestinationRate, reply *string) error {
if missing := utils.MissingStructFields(&attrs, []string{"TPid", "DestinationRateId", "DestinationRates"}); len(missing) != 0 {
@@ -45,7 +44,7 @@ func (self *Apier) SetTPDestinationRate(attrs utils.TPDestinationRate, reply *st
}
type AttrGetTPDestinationRate struct {
TPid string // Tariff plan id
TPid string // Tariff plan id
DestinationRateId string // Rate id
}

View File

@@ -0,0 +1,83 @@
/*
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 <http://www.gnu.org/licenses/>
*/
package apier
// This file deals with tp_destrates_timing management over APIs
import (
"errors"
"fmt"
"github.com/cgrates/cgrates/utils"
)
// Creates a new DestinationRateTiming profile within a tariff plan
func (self *Apier) SetTPDestRateTiming(attrs utils.TPDestRateTiming, reply *string) error {
if missing := utils.MissingStructFields(&attrs, []string{"TPid", "DestRateTimingId", "DestRateTimings"}); len(missing) != 0 {
return fmt.Errorf("%s:%v", utils.ERR_MANDATORY_IE_MISSING, missing)
}
if exists, err := self.StorDb.ExistsTPDestRateTiming(attrs.TPid, attrs.DestRateTimingId); err != nil {
return fmt.Errorf("%s:%s", utils.ERR_SERVER_ERROR, err.Error())
} else if exists {
return errors.New(utils.ERR_DUPLICATE)
}
if err := self.StorDb.SetTPDestRateTiming(&attrs); err != nil {
return fmt.Errorf("%s:%s", utils.ERR_SERVER_ERROR, err.Error())
}
*reply = "OK"
return nil
}
type AttrGetTPDestRateTiming struct {
TPid string // Tariff plan id
DestRateTimingId string // Rate id
}
// Queries specific DestRateTiming profile on tariff plan
func (self *Apier) GetTPDestRateTiming(attrs AttrGetTPDestRateTiming, reply *utils.TPDestRateTiming) error {
if missing := utils.MissingStructFields(&attrs, []string{"TPid", "DestRateTimingId"}); len(missing) != 0 { //Params missing
return fmt.Errorf("%s:%v", utils.ERR_MANDATORY_IE_MISSING, missing)
}
if dr, err := self.StorDb.GetTPDestRateTiming(attrs.TPid, attrs.DestRateTimingId); err != nil {
return fmt.Errorf("%s:%s", utils.ERR_SERVER_ERROR, err.Error())
} else if dr == nil {
return errors.New(utils.ERR_NOT_FOUND)
} else {
*reply = *dr
}
return nil
}
type AttrTPDestRateTimingIds struct {
TPid string // Tariff plan id
}
// Queries DestRateTiming identities on specific tariff plan.
func (self *Apier) GetTPDestRateTimingIds(attrs AttrGetTPRateIds, reply *[]string) error {
if missing := utils.MissingStructFields(&attrs, []string{"TPid"}); len(missing) != 0 { //Params missing
return fmt.Errorf("%s:%v", utils.ERR_MANDATORY_IE_MISSING, missing)
}
if ids, err := self.StorDb.GetTPDestRateTimingIds(attrs.TPid); err != nil {
return fmt.Errorf("%s:%s", utils.ERR_SERVER_ERROR, err.Error())
} else if ids == nil {
return errors.New(utils.ERR_NOT_FOUND)
} else {
*reply = ids
}
return nil
}

View File

@@ -66,15 +66,16 @@ CREATE TABLE `tp_destination_rates` (
-- Table structure for table `tp_rate_timings`
--
CREATE TABLE `tp_destination_rate_timings` (
CREATE TABLE `tp_destrate_timings` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`tpid` char(40) NOT NULL,
`tag` varchar(24) NOT NULL,
`destination_rates_tag` varchar(24) NOT NULL,
`timings_tag` varchar(24) NOT NULL,
`destrates_tag` varchar(24) NOT NULL,
`timing_tag` varchar(24) NOT NULL,
`weight` DECIMAL(5,2) NOT NULL,
PRIMARY KEY (`id`),
KEY `tpid` (`tpid`)
KEY `tpid` (`tpid`),
UNIQUE KEY `tpid_tag_destrates_timings_weight` (`tpid`,`tag`,`destrates_tag`,`timing_tag`,`weight`)
);
--

View File

@@ -0,0 +1,205 @@
Apier.SetTPDestRateTiming
+++++++++++++++++++++++++
Creates a new DestinationRateTiming profile within a tariff plan.
**Request**:
Data:
::
type TPDestRateTiming struct {
TPid string // Tariff plan id
DestRateTimingId string // DestinationRate profile id
DestRateTimings []DestRateTiming // Set of destinationid-rateid bindings
}
type DestRateTiming struct {
DestRatesId string // The DestinationRate identity
TimingId string // The timing identity
Weight float64 // Binding priority taken into consideration when more DestinationRates are active on a time slot
}
Mandatory parameters: ``[]string{"TPid", "DestRateTimingId", "DestRateTimings"}``
*JSON sample*:
::
{
"id": 0,
"method": "Apier.SetTPDestRateTiming",
"params": [
{
"DestRateTimingId": "SAMPLE_DRTIMING_1",
"DestRateTimings": [
{
"DestRatesId": "SAMPLE_DR_1",
"TimingId": "SAMPLE_TIMING_1",
"Weight": 10
}
],
"TPid": "SAMPLE_TP"
}
]
}
**Reply**:
Data:
::
string
Possible answers:
``OK`` - Success.
*JSON sample*:
::
{
"error": null,
"id": 0,
"result": "OK"
}
**Errors**:
``MANDATORY_IE_MISSING`` - Mandatory parameter missing from request.
``SERVER_ERROR`` - Server error occurred.
``DUPLICATE`` - The specified combination of TPid/DestRateTimingId already exists in StorDb.
Apier.GetTPDestRateTiming
+++++++++++++++++++++++++
Queries specific DestRateTiming profile on tariff plan.
**Request**:
Data:
::
type AttrGetTPDestRateTiming struct {
TPid string // Tariff plan id
DestRateTimingId string // Rate id
}
Mandatory parameters: ``[]string{"TPid", "DestRateTimingId"}``
*JSON sample*:
::
{
"id": 4,
"method": "Apier.GetTPDestRateTiming",
"params": [
{
"DestRateTimingId": "SAMPLE_DRTIMING_1",
"TPid": "SAMPLE_TP"
}
]
}
**Reply**:
Data:
::
type TPDestRateTiming struct {
TPid string // Tariff plan id
DestRateTimingId string // DestinationRate profile id
DestRateTimings []DestRateTiming // Set of destinationid-rateid bindings
}
type DestRateTiming struct {
DestRatesId string // The DestinationRate identity
TimingId string // The timing identity
Weight float64 // Binding priority taken into consideration when more DestinationRates are active on a time slot
}
*JSON sample*:
::
{
"error": null,
"id": 4,
"result": {
"DestRateTimingId": "SAMPLE_DRTIMING_1",
"DestRateTimings": [
{
"DestRatesId": "SAMPLE_DR_1",
"TimingId": "SAMPLE_TIMING_1",
"Weight": 10
}
],
"TPid": "SAMPLE_TP"
}
}
**Errors**:
``MANDATORY_IE_MISSING`` - Mandatory parameter missing from request.
``SERVER_ERROR`` - Server error occurred.
``NOT_FOUND`` - Requested DestRateTiming profile not found.
Apier.GetTPDestRateTimingIds
++++++++++++++++++++++++++++
Queries DestRateTiming identities on specific tariff plan.
**Request**:
Data:
::
type AttrTPDestRateTimingIds struct {
TPid string // Tariff plan id
}
Mandatory parameters: ``[]string{"TPid"}``
*JSON sample*:
::
{
"id": 5,
"method": "Apier.GetTPDestRateTimingIds",
"params": [
{
"TPid": "SAMPLE_TP"
}
]
}
**Reply**:
Data:
::
[]string
*JSON sample*:
::
{
"error": null,
"id": 5,
"result": [
"SAMPLE_DRTIMING_1",
"SAMPLE_DRTIMING_2",
"SAMPLE_DRTIMING_3"
]
}
**Errors**:
``MANDATORY_IE_MISSING`` - Mandatory parameter missing from request.
``SERVER_ERROR`` - Server error occurred.
``NOT_FOUND`` - Requested tariff plan not found.

View File

@@ -1,5 +1,5 @@
Apier.SetTPRate
+++++++++++++++++
+++++++++++++++
Creates a new rate within a tariff plan.
@@ -52,7 +52,7 @@ Creates a new rate within a tariff plan.
"TPid": "SAMPLE_TP"
}
]
}
}
**Reply**:
@@ -159,6 +159,7 @@ Queries specific rate on tariff plan.
"TPid": "SAMPLE_TP"
}
}
**Errors**:
``MANDATORY_IE_MISSING`` - Mandatory parameter missing from request.
@@ -169,7 +170,7 @@ Queries specific rate on tariff plan.
Apier.GetTPRateIds
+++++++++++++++++++++++++
++++++++++++++++++
Queries rate identities on tariff plan.
@@ -216,7 +217,7 @@ Queries rate identities on tariff plan.
"SAMPLE_RATE_3",
"SAMPLE_RATE_4"
]
}
}
**Errors**:
@@ -227,4 +228,3 @@ Queries rate identities on tariff plan.
``NOT_FOUND`` - Requested tariff plan not found.

View File

@@ -166,13 +166,13 @@ DestinationRates
api_tpdestinationrates
GetTPRateTiming
DestinationRateTiming
~~~~~~~~~~~~~~~~~~~~~
SetTPRateTiming
.. toctree::
:maxdepth: 2
DeleteTPRateTiming
GetAllTPRateTinings
api_tpdestratetimings
GetTPRatingProfile
@@ -184,47 +184,10 @@ DeleteTPProfile
GetAllTPRatingProfiles
GetTPAction
SetTPAction
DeleteTPAction
GetAllTPActions
GetTPActionTiming
SetTPActionTiming
DeleteTPActionTiming
GetAllTPActionTimings
GetTPActionTrigger
SetTPActionTrigger
DeleteTPActionTrigger
GetAllTPActionTriggers
GetTPAccountAction
SetTPAccountAction
DeleteTPAccountAction
GetAllTPAccountActions
ImportWithOverride
ImportWithFlush
GetAllTPTariffPlanIds
6.1.5. Management API
---------------------

View File

@@ -75,6 +75,10 @@ type DataStorage interface {
SetTPDestinationRate(*utils.TPDestinationRate) error
GetTPDestinationRate(string, string) (*utils.TPDestinationRate, error)
GetTPDestinationRateIds(string) ([]string, error)
ExistsTPDestRateTiming(string, string) (bool, error)
SetTPDestRateTiming(*utils.TPDestRateTiming) error
GetTPDestRateTiming(string, string) (*utils.TPDestRateTiming, error)
GetTPDestRateTimingIds(string) ([]string, error)
// End Apier functions
GetActions(string) (Actions, error)
SetActions(string, Actions) error

View File

@@ -141,6 +141,23 @@ func (ms *MapStorage) GetTPDestinationRateIds(tpid string) ([]string, error) {
return nil, errors.New(utils.ERR_NOT_IMPLEMENTED)
}
func (ms *MapStorage) ExistsTPDestRateTiming(tpid, drtId string) (bool, error) {
return false, errors.New(utils.ERR_NOT_IMPLEMENTED)
}
func (ms *MapStorage) SetTPDestRateTiming(drt *utils.TPDestRateTiming) error {
return errors.New(utils.ERR_NOT_IMPLEMENTED)
}
func (ms *MapStorage) GetTPDestRateTiming(tpid, drtId string) (*utils.TPDestRateTiming, error) {
return nil, nil
}
func (ms *MapStorage) GetTPDestRateTimingIds(tpid string) ([]string, error) {
return nil, errors.New(utils.ERR_NOT_IMPLEMENTED)
}
func (ms *MapStorage) GetActions(key string) (as Actions, err error) {
if values, ok := ms.dict[ACTION_PREFIX+key]; ok {

View File

@@ -216,6 +216,22 @@ func (ms *MongoStorage) GetTPDestinationRateIds(tpid string) ([]string, error) {
return nil, errors.New(utils.ERR_NOT_IMPLEMENTED)
}
func (ms *MongoStorage) ExistsTPDestRateTiming(tpid, drtId string) (bool, error) {
return false, errors.New(utils.ERR_NOT_IMPLEMENTED)
}
func (ms *MongoStorage) SetTPDestRateTiming(drt *utils.TPDestRateTiming) error {
return errors.New(utils.ERR_NOT_IMPLEMENTED)
}
func (ms *MongoStorage) GetTPDestRateTiming(tpid, drtId string) (*utils.TPDestRateTiming, error) {
return nil, nil
}
func (ms *MongoStorage) GetTPDestRateTimingIds(tpid string) ([]string, error) {
return nil, errors.New(utils.ERR_NOT_IMPLEMENTED)
}
func (ms *MongoStorage) GetActions(key string) (as Actions, err error) {
result := AcKeyValue{}
err = ms.db.C("actions").Find(bson.M{"key": key}).One(&result)

View File

@@ -171,6 +171,22 @@ func (rs *RedisStorage) GetTPDestinationRateIds(tpid string) ([]string, error) {
return nil, errors.New(utils.ERR_NOT_IMPLEMENTED)
}
func (rs *RedisStorage) ExistsTPDestRateTiming(tpid, drtId string) (bool, error) {
return false, errors.New(utils.ERR_NOT_IMPLEMENTED)
}
func (rs *RedisStorage) SetTPDestRateTiming(drt *utils.TPDestRateTiming) error {
return errors.New(utils.ERR_NOT_IMPLEMENTED)
}
func (rs *RedisStorage) GetTPDestRateTiming(tpid, drtId string) (*utils.TPDestRateTiming, error) {
return nil, nil
}
func (rs *RedisStorage) GetTPDestRateTimingIds(tpid string) ([]string, error) {
return nil, errors.New(utils.ERR_NOT_IMPLEMENTED)
}
func (rs *RedisStorage) GetActions(key string) (as Actions, err error) {
var values string
if values, err = rs.db.Get(ACTION_PREFIX + key); err == nil {

View File

@@ -57,7 +57,7 @@ func (self *SQLStorage) SetDestination(d *Destination) (err error) {
// Return a list with all TPids defined in the system, even if incomplete, isolated in some table.
func (self *SQLStorage) GetTPIds() ([]string, error) {
rows, err := self.Db.Query(
fmt.Sprintf("(SELECT tpid FROM %s) UNION (SELECT tpid FROM %s) UNION (SELECT tpid FROM %s) UNION (SELECT tpid FROM %s) UNION (SELECT tpid FROM %s) UNION (SELECT tpid FROM %s)", utils.TBL_TP_TIMINGS, utils.TBL_TP_DESTINATIONS, utils.TBL_TP_RATES, utils.TBL_TP_DESTINATION_RATES, utils.TBL_TP_DESTINATION_RATE_TIMINGS, utils.TBL_TP_RATE_PROFILES))
fmt.Sprintf("(SELECT tpid FROM %s) UNION (SELECT tpid FROM %s) UNION (SELECT tpid FROM %s) UNION (SELECT tpid FROM %s) UNION (SELECT tpid FROM %s) UNION (SELECT tpid FROM %s)", utils.TBL_TP_TIMINGS, utils.TBL_TP_DESTINATIONS, utils.TBL_TP_RATES, utils.TBL_TP_DESTINATION_RATES, utils.TBL_TP_DESTRATE_TIMINGS, utils.TBL_TP_RATE_PROFILES))
if err != nil {
return nil, err
}
@@ -280,7 +280,7 @@ func (self *SQLStorage) SetTPDestinationRate(dr *utils.TPDestinationRate) error
return nil //Nothing to set
}
// Using multiple values in query to spare some network processing time
qry := fmt.Sprintf("INSERT INTO %s (tpid, tag, destinations_tag, rates_tag) VALUES", utils.TBL_TP_DESTINATION_RATES)
qry := fmt.Sprintf("INSERT INTO %s (tpid, tag, destinations_tag, rates_tag) VALUES ", utils.TBL_TP_DESTINATION_RATES)
for idx, drPair := range dr.DestinationRates {
if idx!=0 { //Consecutive values after the first will be prefixed with "," as separator
qry += ","
@@ -339,6 +339,80 @@ func (self *SQLStorage) GetTPDestinationRateIds(tpid string) ([]string, error) {
return ids, nil
}
func (self *SQLStorage) ExistsTPDestRateTiming(tpid, drtId string) (bool, error) {
var exists bool
err := self.Db.QueryRow(fmt.Sprintf("SELECT EXISTS (SELECT 1 FROM %s WHERE tpid='%s' AND tag='%s')", utils.TBL_TP_DESTRATE_TIMINGS, tpid, drtId)).Scan(&exists)
if err != nil {
return false, err
}
return exists, nil
}
func (self *SQLStorage) SetTPDestRateTiming(drt *utils.TPDestRateTiming) error {
if len(drt.DestRateTimings) == 0 {
return nil //Nothing to set
}
// Using multiple values in query to spare some network processing time
qry := fmt.Sprintf("INSERT INTO %s (tpid, tag, destrates_tag, timing_tag, weight) VALUES ", utils.TBL_TP_DESTRATE_TIMINGS)
for idx, drtPair := range drt.DestRateTimings {
if idx!=0 { //Consecutive values after the first will be prefixed with "," as separator
qry += ","
}
qry += fmt.Sprintf("('%s','%s','%s','%s',%f)", drt.TPid, drt.DestRateTimingId, drtPair.DestRatesId, drtPair.TimingId, drtPair.Weight)
}
if _, err := self.Db.Exec(qry); err != nil {
return err
}
return nil
}
func (self *SQLStorage) GetTPDestRateTiming(tpid, drtId string) (*utils.TPDestRateTiming, error) {
rows, err := self.Db.Query(fmt.Sprintf("SELECT destrates_tag, timing_tag, weight from %s where tpid='%s' and tag='%s'", utils.TBL_TP_DESTRATE_TIMINGS, tpid, drtId))
if err != nil {
return nil, err
}
defer rows.Close()
drt := &utils.TPDestRateTiming{TPid: tpid, DestRateTimingId: drtId}
i := 0
for rows.Next() {
i++ //Keep here a reference so we know we got at least one result
var drTag, timingTag string
var weight float64
err = rows.Scan(&drTag, &timingTag, &weight)
if err != nil {
return nil, err
}
drt.DestRateTimings = append(drt.DestRateTimings, utils.DestRateTiming{drTag, timingTag,weight})
}
if i == 0 {
return nil, nil
}
return drt, nil
}
func (self *SQLStorage) GetTPDestRateTimingIds(tpid string) ([]string, error) {
rows, err := self.Db.Query(fmt.Sprintf("SELECT DISTINCT tag FROM %s where tpid='%s'", utils.TBL_TP_DESTRATE_TIMINGS, tpid))
if err != nil {
return nil, err
}
defer rows.Close()
ids := []string{}
i := 0
for rows.Next() {
i++ //Keep here a reference so we know we got at least one
var id string
err = rows.Scan(&id)
if err != nil {
return nil, err
}
ids = append(ids, id)
}
if i == 0 {
return nil, nil
}
return ids, nil
}
func (self *SQLStorage) GetActions(string) (as Actions, err error) {
return
}
@@ -570,7 +644,7 @@ func (self *SQLStorage) GetTpTimings(tpid, tag string) (map[string]*Timing, erro
func (self *SQLStorage) GetTpDestinationRateTimings(tpid, tag string) ([]*DestinationRateTiming, error) {
var rts []*DestinationRateTiming
q := fmt.Sprintf("SELECT * FROM %s WHERE tpid=%s", utils.TBL_TP_DESTINATION_RATE_TIMINGS, tpid)
q := fmt.Sprintf("SELECT * FROM %s WHERE tpid=%s", utils.TBL_TP_DESTRATE_TIMINGS, tpid)
if tag != "" {
q += "AND tag=" + tag
}

View File

@@ -1,27 +1,27 @@
package utils
const (
LOCALHOST = "127.0.0.1"
FSCDR_FILE_CSV = "freeswitch_file_csv"
FSCDR_HTTP_JSON = "freeswitch_http_json"
NOT_IMPLEMENTED = "not implemented"
PREPAID = "prepaid"
POSTPAID = "postpaid"
PSEUDOPREPAID = "pseudoprepaid"
RATED = "rated"
ERR_NOT_IMPLEMENTED = "NOT_IMPLEMENTED"
ERR_SERVER_ERROR = "SERVER_ERROR"
ERR_NOT_FOUND = "NOT_FOUND"
ERR_MANDATORY_IE_MISSING = "MANDATORY_IE_MISSING"
ERR_DUPLICATE = "DUPLICATE"
TBL_TP_TIMINGS = "tp_timings"
TBL_TP_DESTINATIONS = "tp_destinations"
TBL_TP_RATES = "tp_rates"
TBL_TP_DESTINATION_RATES = "tp_destination_rates"
TBL_TP_DESTINATION_RATE_TIMINGS = "tp_destination_rate_timings"
TBL_TP_RATE_PROFILES = "tp_rate_profiles"
TBL_TP_ACTIONS = "tp_actions"
TBL_TP_ACTION_TIMINGS = "tp_action_timings"
TBL_TP_ACTION_TRIGGERS = "tp_action_triggers"
TBL_TP_ACCOUNT_ACTIONS = "tp_account_actions"
LOCALHOST = "127.0.0.1"
FSCDR_FILE_CSV = "freeswitch_file_csv"
FSCDR_HTTP_JSON = "freeswitch_http_json"
NOT_IMPLEMENTED = "not implemented"
PREPAID = "prepaid"
POSTPAID = "postpaid"
PSEUDOPREPAID = "pseudoprepaid"
RATED = "rated"
ERR_NOT_IMPLEMENTED = "NOT_IMPLEMENTED"
ERR_SERVER_ERROR = "SERVER_ERROR"
ERR_NOT_FOUND = "NOT_FOUND"
ERR_MANDATORY_IE_MISSING = "MANDATORY_IE_MISSING"
ERR_DUPLICATE = "DUPLICATE"
TBL_TP_TIMINGS = "tp_timings"
TBL_TP_DESTINATIONS = "tp_destinations"
TBL_TP_RATES = "tp_rates"
TBL_TP_DESTINATION_RATES = "tp_destination_rates"
TBL_TP_DESTRATE_TIMINGS = "tp_destrate_timings"
TBL_TP_RATE_PROFILES = "tp_rate_profiles"
TBL_TP_ACTIONS = "tp_actions"
TBL_TP_ACTION_TIMINGS = "tp_action_timings"
TBL_TP_ACTION_TRIGGERS = "tp_action_triggers"
TBL_TP_ACCOUNT_ACTIONS = "tp_account_actions"
)

View File

@@ -34,16 +34,25 @@ type RateSlot struct {
Weight float64 // Rate's priority when dealing with grouped rates
}
type TPDestinationRate struct {
TPid string // Tariff plan id
DestinationRateId string // DestinationRate profile id
DestinationRates []DestinationRate // Set of destinationid-rateid bindings
TPid string // Tariff plan id
DestinationRateId string // DestinationRate profile id
DestinationRates []DestinationRate // Set of destinationid-rateid bindings
}
type DestinationRate struct {
DestinationId string // The destination identity
RateId string // The rate identity
RateId string // The rate identity
}
type TPDestRateTiming struct {
TPid string // Tariff plan id
DestRateTimingId string // DestinationRate profile id
DestRateTimings []DestRateTiming // Set of destinationid-rateid bindings
}
type DestRateTiming struct {
DestRatesId string // The DestinationRate identity
TimingId string // The timing identity
Weight float64 // Binding priority taken into consideration when more DestinationRates are active on a time slot
}