From b7c6314a0992f609dfde4158dd4132a932f78150 Mon Sep 17 00:00:00 2001 From: DanB Date: Fri, 12 Jul 2013 08:40:28 +0200 Subject: [PATCH] Finished implementing TPRateProfile APIs --- apier/tprateprofiles.go | 2 +- rater/storage_sql.go | 63 +++++++++++++++++++++++++++++++++++++++-- utils/tpdata.go | 2 +- 3 files changed, 63 insertions(+), 4 deletions(-) diff --git a/apier/tprateprofiles.go b/apier/tprateprofiles.go index e1c96bad9..36c1bc5aa 100644 --- a/apier/tprateprofiles.go +++ b/apier/tprateprofiles.go @@ -64,7 +64,7 @@ func (self *Apier) GetTPRateProfile(attrs AttrGetTPRateProfile, reply *utils.TPR } // Queries RateProfile identities on specific tariff plan. -func (self *Apier) GetTPRateProfiles(attrs utils.AttrTPRateProfileIds, reply *[]string) error { +func (self *Apier) GetTPRateProfileIds(attrs utils.AttrTPRateProfileIds, 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) } diff --git a/rater/storage_sql.go b/rater/storage_sql.go index f292ff1da..4b471e539 100644 --- a/rater/storage_sql.go +++ b/rater/storage_sql.go @@ -444,11 +444,70 @@ func (self *SQLStorage) SetTPRateProfile(rp *utils.TPRateProfile) error { } func (self *SQLStorage) GetTPRateProfile(tpid, rpId string) (*utils.TPRateProfile, error) { - return nil, nil + rows, err := self.Db.Query(fmt.Sprintf("SELECT tenant,tor,direction,subject,activation_time,destrates_timing_tag,rates_fallback_subject FROM %s WHERE tpid='%s' AND tag='%s'", utils.TBL_TP_RATE_PROFILES, tpid, rpId)) + if err != nil { + return nil, err + } + defer rows.Close() + rp := &utils.TPRateProfile{TPid: tpid, RateProfileId: rpId} + i := 0 + for rows.Next() { + i++ //Keep here a reference so we know we got at least one result + var tenant, tor, direction, subject, drtId, fallbackSubj string + var aTime int64 + err = rows.Scan(&tenant, &tor, &direction, &subject, &aTime, &drtId, &fallbackSubj) + if err != nil { + return nil, err + } + if i == 1 { // Save some info on first iteration + rp.Tenant = tenant + rp.TOR = tor + rp.Direction = direction + rp.Subject = subject + rp.RatesFallbackSubject = fallbackSubj + } + rp.RatingActivations = append(rp.RatingActivations, utils.RatingActivation{aTime, drtId}) + } + if i == 0 { + return nil, nil + } + return rp, nil } func (self *SQLStorage) GetTPRateProfileIds(filters *utils.AttrTPRateProfileIds) ([]string, error) { - return nil, nil + qry := fmt.Sprintf("SELECT DISTINCT tag FROM %s where tpid='%s'", utils.TBL_TP_RATE_PROFILES, filters.TPid) + if filters.Tenant != "" { + qry += fmt.Sprintf(" AND tenant='%s'", filters.Tenant) + } + if filters.TOR != "" { + qry += fmt.Sprintf(" AND tor='%s'", filters.TOR) + } + if filters.Direction != "" { + qry += fmt.Sprintf(" AND direction='%s'", filters.Direction) + } + if filters.Subject != "" { + qry += fmt.Sprintf(" AND subject='%s'", filters.Subject) + } + rows, err := self.Db.Query(qry) + 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) { diff --git a/utils/tpdata.go b/utils/tpdata.go index c05850c38..514d894ba 100644 --- a/utils/tpdata.go +++ b/utils/tpdata.go @@ -69,7 +69,7 @@ type TPRateProfile struct { } type RatingActivation struct { - ActivationTime int64 // Time when this profile will become active + ActivationTime int64 // Time when this profile will become active, defined as unix epoch time DestRateTimingId string // Id of DestRateTiming profile }