From 5e75234edecc4a0b689582e9d51263dac47037cb Mon Sep 17 00:00:00 2001 From: DanB Date: Thu, 17 Jul 2014 20:29:13 +0200 Subject: [PATCH] Query filters for ratedAccount and ratedSubject --- engine/storage_sql.go | 28 ++++++++++++ engine/storage_sql_local_test.go | 75 ++++++++++++++++++++------------ 2 files changed, 76 insertions(+), 27 deletions(-) diff --git a/engine/storage_sql.go b/engine/storage_sql.go index b68d0a61e..808531030 100644 --- a/engine/storage_sql.go +++ b/engine/storage_sql.go @@ -852,6 +852,34 @@ func (self *SQLStorage) GetStoredCdrs(cgrIds, runIds, tors, cdrHosts, cdrSources } fltr.Write(qIds.Bytes()) } + if len(ratedAccounts) != 0 { + qIds := bytes.NewBufferString(" (") + for idx, ratedAccount := range ratedAccounts { + if idx != 0 { + qIds.WriteString(" OR") + } + qIds.WriteString(fmt.Sprintf(" %s.account='%s'", utils.TBL_COST_DETAILS, ratedAccount)) + } + qIds.WriteString(" )") + if fltr.Len() != 0 { + fltr.WriteString(" AND") + } + fltr.Write(qIds.Bytes()) + } + if len(ratedSubjects) != 0 { + qIds := bytes.NewBufferString(" (") + for idx, ratedSubject := range ratedSubjects { + if idx != 0 { + qIds.WriteString(" OR") + } + qIds.WriteString(fmt.Sprintf(" %s.subject='%s'", utils.TBL_COST_DETAILS, ratedSubject)) + } + qIds.WriteString(" )") + if fltr.Len() != 0 { + fltr.WriteString(" AND") + } + fltr.Write(qIds.Bytes()) + } if orderIdStart != 0 { if fltr.Len() != 0 { fltr.WriteString(" AND") diff --git a/engine/storage_sql_local_test.go b/engine/storage_sql_local_test.go index 2c83b8766..316f1194a 100644 --- a/engine/storage_sql_local_test.go +++ b/engine/storage_sql_local_test.go @@ -217,6 +217,40 @@ func TestSetRatedCdr(t *testing.T) { } } +func TestCallCost(t *testing.T) { + if !*testLocal { + return + } + cgrId := utils.Sha1("bbb1", time.Date(2013, 12, 7, 8, 42, 24, 0, time.UTC).String()) + cc := &CallCost{ + Direction: "*out", + Category: "call", + Tenant: "cgrates.org", + Subject: "91001", + Account: "8001", + Destination: "1002", + TOR: utils.VOICE, + Timespans: []*TimeSpan{ + &TimeSpan{ + TimeStart: time.Date(2013, 9, 10, 13, 40, 0, 0, time.UTC), + TimeEnd: time.Date(2013, 9, 10, 13, 41, 0, 0, time.UTC), + }, + &TimeSpan{ + TimeStart: time.Date(2013, 9, 10, 13, 41, 0, 0, time.UTC), + TimeEnd: time.Date(2013, 9, 10, 13, 41, 30, 0, time.UTC), + }, + }, + } + if err := mysqlDb.LogCallCost(cgrId, TEST_SQL, utils.DEFAULT_RUNID, cc); err != nil { + t.Error(err.Error()) + } + if ccRcv, err := mysqlDb.GetCallCostLog(cgrId, TEST_SQL, utils.DEFAULT_RUNID); err != nil { + t.Error(err.Error()) + } else if !reflect.DeepEqual(cc, ccRcv) { + t.Errorf("Expecting call cost: %v, received: %v", cc, ccRcv) + } +} + func TestGetStoredCdrs(t *testing.T) { if !*testLocal { return @@ -382,6 +416,20 @@ func TestGetStoredCdrs(t *testing.T) { } else if len(storedCdrs) != 4 { t.Error("Unexpected number of StoredCdrs returned: ", storedCdrs) } + // Filter on ratedAccount + if storedCdrs, err := mysqlDb.GetStoredCdrs(nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, + nil, nil, []string{"8001"}, nil, 0, 0, timeStart, timeEnd, false, false, false); err != nil { + t.Error(err.Error()) + } else if len(storedCdrs) != 1 { + t.Error("Unexpected number of StoredCdrs returned: ", storedCdrs) + } + // Filter on ratedSubject + if storedCdrs, err := mysqlDb.GetStoredCdrs(nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, + nil, nil, nil, []string{"91001"}, 0, 0, timeStart, timeEnd, false, false, false); err != nil { + t.Error(err.Error()) + } else if len(storedCdrs) != 1 { + t.Error("Unexpected number of StoredCdrs returned: ", storedCdrs) + } // Filter on ignoreErr if storedCdrs, err := mysqlDb.GetStoredCdrs(nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, 0, 0, timeStart, timeEnd, true, false, false); err != nil { t.Error(err.Error()) @@ -446,33 +494,6 @@ func TestGetStoredCdrs(t *testing.T) { } } -func TestCallCost(t *testing.T) { - if !*testLocal { - return - } - cgrId := utils.Sha1("bbb1", "123") - cc := &CallCost{ - Timespans: []*TimeSpan{ - &TimeSpan{ - TimeStart: time.Date(2013, 9, 10, 13, 40, 0, 0, time.UTC), - TimeEnd: time.Date(2013, 9, 10, 13, 41, 0, 0, time.UTC), - }, - &TimeSpan{ - TimeStart: time.Date(2013, 9, 10, 13, 41, 0, 0, time.UTC), - TimeEnd: time.Date(2013, 9, 10, 13, 41, 30, 0, time.UTC), - }, - }, - } - if err := mysqlDb.LogCallCost(cgrId, TEST_SQL, TEST_SQL, cc); err != nil { - t.Error(err.Error()) - } - if ccRcv, err := mysqlDb.GetCallCostLog(cgrId, TEST_SQL, TEST_SQL); err != nil { - t.Error(err.Error()) - } else if !reflect.DeepEqual(cc, ccRcv) { - t.Errorf("Expecting call cost: %v, received: %v", cc, ccRcv) - } -} - func TestRemStoredCdrs(t *testing.T) { if !*testLocal { return