From d5dc2d242cc4340a5edccb02c438996f198b75ad Mon Sep 17 00:00:00 2001 From: Radu Ioan Fericean Date: Thu, 16 Aug 2012 12:59:32 +0300 Subject: [PATCH] added unique id to actions and action trigger --- timespans/action.go | 17 ++++++++++------- timespans/action_timing.go | 2 +- timespans/action_trigger.go | 19 +++++++++++-------- timespans/actions_test.go | 8 +++++--- timespans/csvreader.go | 2 ++ timespans/userbalance_test.go | 5 +++-- 6 files changed, 32 insertions(+), 21 deletions(-) diff --git a/timespans/action.go b/timespans/action.go index 82a9edf2b..dc8f2cc02 100644 --- a/timespans/action.go +++ b/timespans/action.go @@ -29,6 +29,7 @@ import ( Structure to be filled for each tariff plan with the bonus value for received calls minutes. */ type Action struct { + Id string ActionType string BalanceId string Direction string @@ -184,6 +185,7 @@ func (apl ActionPriotityList) Sort() { Serializes the action for the storage. Used for key-value storages. */ func (a *Action) store() (result string) { + result += a.Id + "|" result += a.ActionType + "|" result += a.BalanceId + "|" result += a.Direction + "|" @@ -201,13 +203,14 @@ De-serializes the action for the storage. Used for key-value storages. */ func (a *Action) restore(input string) { elements := strings.Split(input, "|") - a.ActionType = elements[0] - a.BalanceId = elements[1] - a.Direction = elements[2] - a.Units, _ = strconv.ParseFloat(elements[3], 64) - a.Weight, _ = strconv.ParseFloat(elements[4], 64) - if len(elements) == 6 { + a.Id = elements[0] + a.ActionType = elements[1] + a.BalanceId = elements[2] + a.Direction = elements[3] + a.Units, _ = strconv.ParseFloat(elements[4], 64) + a.Weight, _ = strconv.ParseFloat(elements[5], 64) + if len(elements) == 7 { a.MinuteBucket = &MinuteBucket{} - a.MinuteBucket.restore(elements[5]) + a.MinuteBucket.restore(elements[6]) } } diff --git a/timespans/action_timing.go b/timespans/action_timing.go index 73028db66..efdcc59eb 100644 --- a/timespans/action_timing.go +++ b/timespans/action_timing.go @@ -35,7 +35,7 @@ const ( ) type ActionTiming struct { - Id string // identify the timing + Id string // uniquely identify the timing Tag string // informative purpos only UserBalanceIds []string Timing *Interval diff --git a/timespans/action_trigger.go b/timespans/action_trigger.go index 021799540..e8a7d48f4 100644 --- a/timespans/action_trigger.go +++ b/timespans/action_trigger.go @@ -26,6 +26,7 @@ import ( ) type ActionTrigger struct { + Id string // uniquely identify the timing BalanceId string Direction string ThresholdValue float64 @@ -82,6 +83,7 @@ func (atpl ActionTriggerPriotityList) Sort() { Serializes the action trigger for the storage. Used for key-value storages. */ func (at *ActionTrigger) store() (result string) { + result += at.Id + ";" result += at.BalanceId + ";" result += at.Direction + ";" result += at.DestinationId + ";" @@ -97,14 +99,15 @@ De-serializes the action timing for the storage. Used for key-value storages. */ func (at *ActionTrigger) restore(input string) { elements := strings.Split(input, ";") - if len(elements) != 7 { + if len(elements) != 8 { return } - at.BalanceId = elements[0] - at.Direction = elements[1] - at.DestinationId = elements[2] - at.ActionsId = elements[3] - at.ThresholdValue, _ = strconv.ParseFloat(elements[4], 64) - at.Weight, _ = strconv.ParseFloat(elements[5], 64) - at.Executed, _ = strconv.ParseBool(elements[6]) + at.Id = elements[0] + at.BalanceId = elements[1] + at.Direction = elements[2] + at.DestinationId = elements[3] + at.ActionsId = elements[4] + at.ThresholdValue, _ = strconv.ParseFloat(elements[5], 64) + at.Weight, _ = strconv.ParseFloat(elements[6], 64) + at.Executed, _ = strconv.ParseBool(elements[7]) } diff --git a/timespans/actions_test.go b/timespans/actions_test.go index 43aba3054..4a5c9e3bb 100644 --- a/timespans/actions_test.go +++ b/timespans/actions_test.go @@ -59,6 +59,7 @@ func TestActionTimingStoreRestore(t *testing.T) { func TestActionTriggerStoreRestore(t *testing.T) { at := &ActionTrigger{ + Id: "some_uuid", BalanceId: CREDIT, Direction: OUTBOUND, ThresholdValue: 100.0, @@ -67,7 +68,7 @@ func TestActionTriggerStoreRestore(t *testing.T) { ActionsId: "Commando", } r := at.store() - if string(r) != "MONETARY;OUT;NAT;Commando;100;10;false" { + if string(r) != "some_uuid;MONETARY;OUT;NAT;Commando;100;10;false" { t.Errorf("Error serializing action trigger: %v", string(r)) } o := &ActionTrigger{} @@ -751,6 +752,7 @@ func TestUUID(t *testing.T) { func TestActionTriggerLogging(t *testing.T) { at := &ActionTrigger{ + Id: "some_uuid", BalanceId: CREDIT, Direction: OUTBOUND, ThresholdValue: 100.0, @@ -763,7 +765,7 @@ func TestActionTriggerLogging(t *testing.T) { t.Error("Error getting actions for the action timing: ", err) } storageGetter.LogActionTrigger("rif", at, as) - expected := "rif*MONETARY;OUT;NAT;TEST_ACTIONS;100;10;false*TOPUP|MONETARY|OUT|10|0" + expected := "rif*some_uuid;MONETARY;OUT;NAT;TEST_ACTIONS;100;10;false*|TOPUP|MONETARY|OUT|10|0" var key string for k, v := range storageGetter.(*MapStorage).dict { if strings.Contains(k, LOG_PREFIX) && strings.Contains(string(v), expected) { @@ -802,7 +804,7 @@ func TestActionTimingLogging(t *testing.T) { t.Error("Error getting actions for the action trigger: ", err) } storageGetter.LogActionTiming(at, as) - expected := "some uuid|test|one,two,three|;1,2,3,4,5,6,7,8,9,10,11,12;1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31;1,2,3,4,5;18:00:00;00:00:00;10;0;1;60;1|10|TEST_ACTIONS*TOPUP|MONETARY|OUT|10|0" + expected := "some uuid|test|one,two,three|;1,2,3,4,5,6,7,8,9,10,11,12;1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31;1,2,3,4,5;18:00:00;00:00:00;10;0;1;60;1|10|TEST_ACTIONS*|TOPUP|MONETARY|OUT|10|0" var key string for k, v := range storageGetter.(*MapStorage).dict { if strings.Contains(k, LOG_PREFIX) && strings.Contains(string(v), expected) { diff --git a/timespans/csvreader.go b/timespans/csvreader.go index b58b0e4d0..407c6109c 100644 --- a/timespans/csvreader.go +++ b/timespans/csvreader.go @@ -379,6 +379,7 @@ func (csvr *CSVReader) LoadActions(fn string, comma rune) (err error) { return errors.New(fmt.Sprintf("Could not parse action weight: %v", err)) } a = &Action{ + Id: GenUUID(), ActionType: record[1], BalanceId: record[2], Direction: record[3], @@ -466,6 +467,7 @@ func (csvr *CSVReader) LoadActionTriggers(fn string, comma rune) (err error) { return errors.New(fmt.Sprintf("Could not parse action trigger weight: %v", err)) } at := &ActionTrigger{ + Id: GenUUID(), BalanceId: record[1], Direction: record[2], ThresholdValue: value, diff --git a/timespans/userbalance_test.go b/timespans/userbalance_test.go index d9c4b2144..bb9311b4a 100644 --- a/timespans/userbalance_test.go +++ b/timespans/userbalance_test.go @@ -53,6 +53,7 @@ func TestUserBalanceStoreRestore(t *testing.T) { MinuteBuckets: []*MinuteBucket{&MinuteBucket{Weight: 20, Price: 1, DestinationId: "NAT"}, &MinuteBucket{Weight: 10, Price: 10, Percent: 0, DestinationId: "RET"}}, } at := &ActionTrigger{ + Id: "some_uuid", BalanceId: CREDIT, Direction: OUTBOUND, ThresholdValue: 100.0, @@ -69,8 +70,8 @@ func TestUserBalanceStoreRestore(t *testing.T) { ActionTriggers: ActionTriggerPriotityList{at, at, at}, } r := ub.store() - if string(r) != "rif|postpaid|SMSOUT:14#INTERNETOUT:1024|0;20;1;0;NAT#0;10;10;0;RET|OUT/SMS/100/0;20;1;0;NAT,0;10;10;0;RET#OUT/SMS/100/0;20;1;0;NAT,0;10;10;0;RET|MONETARY;OUT;NAT;Commando;100;10;false#MONETARY;OUT;NAT;Commando;100;10;false#MONETARY;OUT;NAT;Commando;100;10;false" && - string(r) != "rif|postpaid|INTERNETOUT:1024#SMSOUT:14|0;20;1;0;NAT#0;10;10;0;RET|OUT/SMS/100/0;20;1;0;NAT,0;10;10;0;RET#OUT/SMS/100/0;20;1;0;NAT,0;10;10;0;RET|MONETARY;OUT;NAT;Commando;100;10;false#MONETARY;OUT;NAT;Commando;100;10;false#MONETARY;OUT;NAT;Commando;100;10;false" { + if string(r) != "rif|postpaid|SMSOUT:14#INTERNETOUT:1024|0;20;1;0;NAT#0;10;10;0;RET|OUT/SMS/100/0;20;1;0;NAT,0;10;10;0;RET#OUT/SMS/100/0;20;1;0;NAT,0;10;10;0;RET|some_uuid;MONETARY;OUT;NAT;Commando;100;10;false#some_uuid;MONETARY;OUT;NAT;Commando;100;10;false#some_uuid;MONETARY;OUT;NAT;Commando;100;10;false" && + string(r) != "rif|postpaid|INTERNETOUT:1024#SMSOUT:14|0;20;1;0;NAT#0;10;10;0;RET|OUT/SMS/100/0;20;1;0;NAT,0;10;10;0;RET#OUT/SMS/100/0;20;1;0;NAT,0;10;10;0;RET|some_uuid;MONETARY;OUT;NAT;Commando;100;10;false#some_uuid;MONETARY;OUT;NAT;Commando;100;10;false#some_uuid;MONETARY;OUT;NAT;Commando;100;10;false" { t.Errorf("Error serializing action timing: %v", string(r)) } o := &UserBalance{}