diff --git a/apier/tp.go b/apier/tp.go new file mode 100644 index 000000000..0ee9ebc3d --- /dev/null +++ b/apier/tp.go @@ -0,0 +1,42 @@ +/* +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 +*/ + +package apier + +// Tariff plan related APIs + +import ( + "errors" + "fmt" + "github.com/cgrates/cgrates/utils" +) + +type AttrGetTPIds struct { +} + +// Queries tarrif plan identities gathered from all tables. +func (self *Apier) GetTPIds(attrs AttrGetTPIds, reply *[]string) error { + if ids, err := self.StorDb.GetTPIds(); 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 +} diff --git a/data/storage/mysql/create_tariffplan_tables.sql b/data/storage/mysql/create_tariffplan_tables.sql index b2abea748..f39f90b1d 100644 --- a/data/storage/mysql/create_tariffplan_tables.sql +++ b/data/storage/mysql/create_tariffplan_tables.sql @@ -37,10 +37,25 @@ CREATE TABLE `tp_rates` ( `id` int(11) NOT NULL AUTO_INCREMENT, `tpid` char(40) NOT NULL, `tag` varchar(24) NOT NULL, - `destinations_tag` varchar(24) NOT NULL, `connect_fee` DECIMAL(5,4) NOT NULL, `rate` DECIMAL(5,4) NOT NULL, + `rated_units` INT(11) NOT NULL, `rate_increments` INT(11) NOT NULL, + `weight` DECIMAL(5,2) NOT NULL, + PRIMARY KEY (`id`), + KEY `tpid` (`tpid`) +); + +-- +-- Table structure for table `destination_rates` +-- + +CREATE TABLE `tp_destination_rates` ( + `id` int(11) NOT NULL AUTO_INCREMENT, + `tpid` char(40) NOT NULL, + `tag` varchar(24) NOT NULL, + `destinations_tag` varchar(24) NOT NULL, + `rates_tag` varchar(24) NOT NULL, PRIMARY KEY (`id`), KEY `tpid` (`tpid`) ); @@ -49,11 +64,11 @@ CREATE TABLE `tp_rates` ( -- Table structure for table `tp_rate_timings` -- -CREATE TABLE `tp_rate_timings` ( +CREATE TABLE `tp_destination_rate_timings` ( `id` int(11) NOT NULL AUTO_INCREMENT, `tpid` char(40) NOT NULL, `tag` varchar(24) NOT NULL, - `rates_tag` varchar(24) NOT NULL, + `destination_rates_tag` varchar(24) NOT NULL, `timings_tag` varchar(24) NOT NULL, `weight` DECIMAL(5,2) NOT NULL, PRIMARY KEY (`id`), diff --git a/docs/api_tp.rst b/docs/api_tp.rst new file mode 100644 index 000000000..061aa0730 --- /dev/null +++ b/docs/api_tp.rst @@ -0,0 +1,49 @@ +Apier.GetTPDestinationIds ++++++++++++++++++++++++++ + +// Queries tarrif plan identities gathered from all tables. + +**Request**: + + Data: + :: + + type AttrGetTPIds struct { + } + + *JSON sample*: + :: + + { + "id": 9, + "method": "Apier.GetTPIds", + "params": [] + } + + +**Reply**: + + Data: + :: + + []string + + *JSON sample*: + :: + + { + "error": null, + "id": 9, + "result": [ + "SAMPLE_TP", + "SAMPLE_TP_2" + ] + } + + + +**Errors**: + + ``SERVER_ERROR`` - Server error occurred. + + ``NOT_FOUND`` - No tariff plans defined. diff --git a/docs/apicalls.rst b/docs/apicalls.rst index 8300fbb25..bfa0376df 100644 --- a/docs/apicalls.rst +++ b/docs/apicalls.rst @@ -121,6 +121,15 @@ FlushCache These operate on a tpid +TariffPlan +~~~~~~~~~~ + +.. toctree:: + :maxdepth: 2 + + api_tp + + Destinations ~~~~~~~~~~~~ diff --git a/rater/storage_interface.go b/rater/storage_interface.go index 53a3a9f4d..64c1755b5 100644 --- a/rater/storage_interface.go +++ b/rater/storage_interface.go @@ -58,6 +58,7 @@ type DataStorage interface { GetDestination(string) (*Destination, error) SetDestination(*Destination) error // Apier functions + GetTPIds() ([]string, error) SetTPTiming(string, *Timing) error ExistsTPTiming(string, string) (bool, error) GetTPTiming(string, string) (*Timing, error) diff --git a/rater/storage_map.go b/rater/storage_map.go index 3faa3454b..b90b5171b 100644 --- a/rater/storage_map.go +++ b/rater/storage_map.go @@ -73,6 +73,10 @@ func (ms *MapStorage) SetDestination(dest *Destination) (err error) { return } +func (ms *MapStorage) GetTPIds() ([]string, error) { + return nil, errors.New(utils.ERR_NOT_IMPLEMENTED) +} + func (ms *MapStorage) SetTPTiming(tpid string, tm *Timing) error { return errors.New(utils.ERR_NOT_IMPLEMENTED) } diff --git a/rater/storage_mongo.go b/rater/storage_mongo.go index 8f0837a97..ec4b6696e 100644 --- a/rater/storage_mongo.go +++ b/rater/storage_mongo.go @@ -147,6 +147,10 @@ func (ms *MongoStorage) SetDestination(dest *Destination) error { return ms.db.C("destinations").Insert(dest) } +func (ms *MongoStorage) GetTPIds() ([]string, error) { + return nil, errors.New(utils.ERR_NOT_IMPLEMENTED) +} + func (ms *MongoStorage) SetTPTiming(tpid string, tm *Timing) error { return errors.New(utils.ERR_NOT_IMPLEMENTED) } diff --git a/rater/storage_redis.go b/rater/storage_redis.go index cb431bce4..a1e516b00 100644 --- a/rater/storage_redis.go +++ b/rater/storage_redis.go @@ -102,6 +102,10 @@ func (rs *RedisStorage) SetDestination(dest *Destination) (err error) { return } +func (rs *RedisStorage) GetTPIds() ([]string, error) { + return nil, errors.New(utils.ERR_NOT_IMPLEMENTED) +} + func (rs *RedisStorage) SetTPTiming(tpid string, tm *Timing) error { return errors.New(utils.ERR_NOT_IMPLEMENTED) } diff --git a/rater/storage_sql.go b/rater/storage_sql.go index 42411d547..93fb4f3d3 100644 --- a/rater/storage_sql.go +++ b/rater/storage_sql.go @@ -54,6 +54,31 @@ func (self *SQLStorage) SetDestination(d *Destination) (err error) { return } +// 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)) + 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) SetTPTiming(tpid string, tm *Timing) error { if _, err := self.Db.Exec(fmt.Sprintf("INSERT INTO %s (tpid, tag, years, months, month_days, week_days, time) VALUES('%s','%s','%s','%s','%s','%s','%s')", utils.TBL_TP_TIMINGS, tpid, tm.Id, tm.Years.Serialize(";"), tm.Months.Serialize(";"), tm.MonthDays.Serialize(";"),