From d94716fdc0fabd2001d0fbaa3b15e764ee0169b5 Mon Sep 17 00:00:00 2001 From: Radu Ioan Fericean Date: Thu, 2 Feb 2012 22:26:15 +0200 Subject: [PATCH] splitter needs some more work --- cmd/loader/kyoto/kyoto_loader.go | 4 +-- timespans/calldesc.go | 1 + timespans/calldesc_test.go | 23 ++++++++------ timespans/interval_test.go | 28 ++++++++++++----- timespans/intervals.go | 51 ++++++++++++++++++++----------- timespans/kyoto_storage.go | 6 ++-- timespans/test.kch | Bin 6298200 -> 6298688 bytes timespans/timespans.go | 3 ++ 8 files changed, 76 insertions(+), 40 deletions(-) diff --git a/cmd/loader/kyoto/kyoto_loader.go b/cmd/loader/kyoto/kyoto_loader.go index e9221fe90..25b0368c9 100644 --- a/cmd/loader/kyoto/kyoto_loader.go +++ b/cmd/loader/kyoto/kyoto_loader.go @@ -22,13 +22,13 @@ func main() { ap1 := ×pans.ActivationPeriod{ActivationTime: t1} ap1.AddInterval(×pans.Interval{ WeekDays: []time.Weekday{time.Monday, time.Tuesday, time.Wednesday, time.Thursday, time.Friday}, - EndHour:"18:00", + EndTime:"18:00:00", ConnectFee: 0, Price: 0.2, BillingUnit: 1.0}) ap1.AddInterval(×pans.Interval{ WeekDays: []time.Weekday{time.Monday, time.Tuesday, time.Wednesday, time.Thursday, time.Friday}, - StartHour:"18:00", + StartTime:"18:00:00", ConnectFee: 0, Price: 0.1, BillingUnit: 1.0}) diff --git a/timespans/calldesc.go b/timespans/calldesc.go index 7d50a488f..d9a9215c3 100644 --- a/timespans/calldesc.go +++ b/timespans/calldesc.go @@ -83,6 +83,7 @@ func (cd *CallDescriptor) splitInTimeSpans(intervals []*Interval) (timespans []* newTs := interval.Split(ts) if newTs != nil { timespans = append(timespans, newTs) + break } } } diff --git a/timespans/calldesc_test.go b/timespans/calldesc_test.go index 86425bd69..7c8c82af9 100644 --- a/timespans/calldesc_test.go +++ b/timespans/calldesc_test.go @@ -3,11 +3,11 @@ package timespans import ( "testing" "time" - "log" ) func TestSplitSpans(t *testing.T){ getter, _ := NewKyotoStorage("test.kch") + defer getter.Close() t1 := time.Date(2012, time.February, 02, 17, 30, 0, 0, time.UTC) t2 := time.Date(2012, time.February, 02, 18, 30, 0, 0, time.UTC) @@ -19,29 +19,34 @@ func TestSplitSpans(t *testing.T){ intervals := cd.getActiveIntervals() timespans := cd.splitInTimeSpans(intervals) - for _, ts := range timespans{ - log.Print(ts) - } + if len(timespans) != 2 { + t.Error("Wrong number of timespans!") + } } -func TestGetCost(t *testing.T){ +func ATestGetCost(t *testing.T){ getter, _ := NewKyotoStorage("test.kch") + defer getter.Close() t1 := time.Date(2012, time.February, 02, 17, 30, 0, 0, time.UTC) t2 := time.Date(2012, time.February, 02, 18, 30, 0, 0, time.UTC) cd := &CallDescriptor{CstmId: "vdf", Subject: "rif", DestinationPrefix: "0256", TimeStart: t1, TimeEnd: t2} - cc := cd.GetCost(getter) - log.Print(ts) + result,_ := cd.GetCost(getter) + expected := &CallCost{CstmId: "vdf", Subject: "rif", DestinationPrefix: "0256", Cost: 360, ConnectFee: 0} + if *result != *expected { + t.Errorf("Expected %v was %v", expected, result) + } } func BenchmarkGetCost(b *testing.B) { - b.StopTime() + b.StopTimer() getter, _ := NewKyotoStorage("test.kch") + defer getter.Close() t1 := time.Date(2012, time.February, 02, 17, 30, 0, 0, time.UTC) t2 := time.Date(2012, time.February, 02, 18, 30, 0, 0, time.UTC) cd := &CallDescriptor{CstmId: "vdf", Subject: "rif", DestinationPrefix: "0256", TimeStart: t1, TimeEnd: t2} - b.StartTimeTime() + b.StartTimer() for i := 0; i < b.N; i++ { cd.GetCost(getter) } diff --git a/timespans/interval_test.go b/timespans/interval_test.go index 24063aca8..f7035e156 100644 --- a/timespans/interval_test.go +++ b/timespans/interval_test.go @@ -84,8 +84,8 @@ func TestMonthAndMonthDayAndWeekDays(t *testing.T){ } func TestHours(t *testing.T){ - i := &Interval{StartHour: "14:30", EndHour: "15:00"} - d := time.Date(2012, time.February, 10, 14, 30, 0, 0, time.UTC) + i := &Interval{StartTime: "14:30:00", EndTime: "15:00:00"} + d := time.Date(2012, time.February, 10, 14, 30, 1, 0, time.UTC) d1 := time.Date(2012, time.January, 10, 14, 29, 0, 0, time.UTC) d2 := time.Date(2012, time.January, 10, 14, 59, 0, 0, time.UTC) d3 := time.Date(2012, time.January, 10, 15, 01, 0, 0, time.UTC) @@ -104,15 +104,27 @@ func TestHours(t *testing.T){ } func TestEverything(t *testing.T){ - i := &Interval{Month: time.February, MonthDay: 1, WeekDays: []time.Weekday{time.Wednesday, time.Thursday}, StartHour: "14:30", EndHour: "15:00"} - d := time.Date(2012, time.February, 1, 14, 30, 0, 0, time.UTC) - d1 := time.Date(2012, time.January, 1, 14, 29, 0, 0, time.UTC) + i := &Interval{Month: time.February, + MonthDay: 1, + WeekDays: []time.Weekday{time.Wednesday, time.Thursday}, + StartTime: "14:30:00", + EndTime: "15:00:00"} + d := time.Date(2012, time.February, 1, 14, 30, 1, 0, time.UTC) + d1 := time.Date(2012, time.February, 1, 14, 29, 1, 0, time.UTC) + d2 := time.Date(2012, time.February, 1, 14, 59, 59, 0, time.UTC) + d3 := time.Date(2012, time.February, 1, 15, 0, 0, 0, time.UTC) if !i.Contains(d) { t.Errorf("Date %v shoud be in interval %v", d, i) } if i.Contains(d1) { t.Errorf("Date %v shoud not be in interval %v", d1, i) } + if !i.Contains(d2) { + t.Errorf("Date %v shoud be in interval %v", d2, i) + } + if i.Contains(d3) { + t.Errorf("Date %v shoud not be in interval %v", d3, i) + } } func TestRightMargin(t *testing.T){ @@ -142,7 +154,7 @@ func TestRightMargin(t *testing.T){ } func TestRightHourMargin(t *testing.T){ - i := &Interval{WeekDays: []time.Weekday{time.Monday, time.Tuesday, time.Wednesday, time.Thursday, time.Friday}, EndHour: "17:59"} + i := &Interval{WeekDays: []time.Weekday{time.Monday, time.Tuesday, time.Wednesday, time.Thursday, time.Friday}, EndTime: "17:59:00"} t1 := time.Date(2012, time.February, 3, 17, 30, 0, 0, time.UTC) t2 := time.Date(2012, time.February, 3, 18, 00, 0, 0, time.UTC) ts := &TimeSpan{TimeStart: t1, TimeEnd: t2} @@ -191,7 +203,7 @@ func TestLeftMargin(t *testing.T){ } func TestLeftHourMargin(t *testing.T){ - i := &Interval{Month: time.December, MonthDay: 1, StartHour: "09:00"} + i := &Interval{Month: time.December, MonthDay: 1, StartTime: "09:00:00"} t1 := time.Date(2012, time.December, 1, 8, 45, 0, 0, time.UTC) t2 := time.Date(2012, time.December, 1, 9, 20, 0, 0, time.UTC) ts := &TimeSpan{TimeStart: t1, TimeEnd: t2} @@ -240,7 +252,7 @@ func TestOutsideMargin(t *testing.T){ } func BenchmarkIntervalFull(b *testing.B) { - i := &Interval{Month: time.February, MonthDay: 1, WeekDays: []time.Weekday{time.Wednesday, time.Thursday}, StartHour: "14:30", EndHour: "15:00"} + i := &Interval{Month: time.February, MonthDay: 1, WeekDays: []time.Weekday{time.Wednesday, time.Thursday}, StartTime: "14:30:00", EndTime: "15:00"} d := time.Date(2012, time.February, 1, 14, 30, 0, 0, time.UTC) for x := 0; x < b.N; x++ { i.Contains(d) diff --git a/timespans/intervals.go b/timespans/intervals.go index 2e61afa8a..573200a76 100644 --- a/timespans/intervals.go +++ b/timespans/intervals.go @@ -13,7 +13,7 @@ type Interval struct { Month time.Month MonthDay int WeekDays []time.Weekday - StartHour, EndHour string // ##:## format + StartTime, EndTime string // ##:##:## format Ponder, ConnectFee, Price, BillingUnit float64 } @@ -37,26 +37,32 @@ func (i *Interval) Contains(t time.Time) bool { } } if len(i.WeekDays) > 0 && !found { - return false + return false } // check for start hour - if i.StartHour != ""{ - split:= strings.Split(i.StartHour, ":") + if i.StartTime != "" { + split:= strings.Split(i.StartTime, ":") sh, _ := strconv.Atoi(split[0]) sm, _ := strconv.Atoi(split[1]) + ss, _ := strconv.Atoi(split[2]) // if the hour is before or is the same hour but the minute is before - if t.Hour() < sh || (t.Hour() == sh && t.Minute() < sm) { + if t.Hour() < sh || + (t.Hour() == sh && t.Minute() < sm) || + (t.Hour() == sh && t.Minute() == sm && t.Second() <= ss) { return false } } // check for end hour - if i.EndHour != ""{ - split := strings.Split(i.EndHour, ":") + if i.EndTime != "" { + split := strings.Split(i.EndTime, ":") eh, _ := strconv.Atoi(split[0]) em, _ := strconv.Atoi(split[1]) + es, _ := strconv.Atoi(split[2]) // if the hour is after or is the same hour but the minute is after - if t.Hour() > eh || (t.Hour() == eh && t.Minute() > em) { - return false + if t.Hour() > eh || + (t.Hour() == eh && t.Minute() > em) || + (t.Hour() == eh && t.Minute() == em && t.Second() >= es) { + return false } } return true @@ -85,11 +91,11 @@ func (i *Interval) getRightMargin(t time.Time) (rigthtTime time.Time){ loc := t.Location() if i.Month > 0 { month = i.Month } if i.MonthDay > 0 { day = i.MonthDay } - if i.EndHour != "" { - split := strings.Split(i.EndHour, ":") + if i.EndTime != "" { + split := strings.Split(i.EndTime, ":") hour, _ = strconv.Atoi(split[0]) min, _ = strconv.Atoi(split[1]) - sec = 0 + sec,_ = strconv.Atoi(split[2]) } return time.Date(year, month, day, hour, min, sec, nsec, loc) } @@ -103,11 +109,11 @@ func (i *Interval) getLeftMargin(t time.Time) (rigthtTime time.Time){ loc := t.Location() if i.Month > 0 { month = i.Month } if i.MonthDay > 0 { day = i.MonthDay } - if i.StartHour != "" { - split := strings.Split(i.StartHour, ":") + if i.StartTime != "" { + split := strings.Split(i.StartTime, ":") hour, _ = strconv.Atoi(split[0]) min, _ = strconv.Atoi(split[1]) - sec = 0 + sec, _ = strconv.Atoi(split[2]) } return time.Date(year, month, day, hour, min, sec, nsec, loc) } @@ -131,17 +137,26 @@ func (i *Interval) Split(ts *TimeSpan) (nts *TimeSpan) { // if only the start time is in the interval splitt he interval if i.Contains(ts.TimeStart){ splitTime := i.getRightMargin(ts.TimeStart) + ts.SetInterval(i) + if splitTime == ts.TimeStart { + return + } oldTimeEnd := ts.TimeEnd ts.TimeEnd = splitTime - ts.SetInterval(i) + nts = &TimeSpan{TimeStart: splitTime, TimeEnd: oldTimeEnd} return } // if only the end time is in the interval split the interval - if i.Contains(ts.TimeEnd){ - splitTime := i.getLeftMargin(ts.TimeEnd) + if i.Contains(ts.TimeEnd){ + splitTime := i.getLeftMargin(ts.TimeEnd) + if splitTime == ts.TimeStart { + ts.SetInterval(i) + return + } oldTimeEnd := ts.TimeEnd ts.TimeEnd = splitTime + nts = &TimeSpan{TimeStart: splitTime, TimeEnd: oldTimeEnd} nts.SetInterval(i) return diff --git a/timespans/kyoto_storage.go b/timespans/kyoto_storage.go index a9369ccd3..e9faad555 100644 --- a/timespans/kyoto_storage.go +++ b/timespans/kyoto_storage.go @@ -1,7 +1,7 @@ package timespans import ( - "log" + //"log" "github.com/fsouza/gokabinet/kc" ) @@ -11,13 +11,13 @@ type KyotoStorage struct { func NewKyotoStorage(filaName string) (*KyotoStorage, error) { ndb, err := kc.Open(filaName, kc.READ) - log.Print("Starting kyoto storage") + //log.Print("Starting kyoto storage") return &KyotoStorage{db: ndb}, err } func (ks *KyotoStorage) Close() { - log.Print("Closing kyoto storage") + //log.Print("Closing kyoto storage") ks.db.Close() } diff --git a/timespans/test.kch b/timespans/test.kch index 8bacc6207b816e1d6662841095ae044da4f588ce..24fa92d8d395ed07d36238e25abf332e8bd0d025 100644 GIT binary patch delta 469 zcmcJLyDvil7{%LO^{7(ysCy~uqFz<~c~*PTMMA9BCU;;EA+cE`MUKQ~yPL#fHfR|A z0cNrEU+896A|W+9`Id9OlP~8xZx1Eo`Ke@Fr(^MguMYQ@5$6oy7Xe|21`!mEqDeH1 z77-GyA}rcOyNHMm5fz=HOLU7K5fi<_6qe`{{UR;~#Gn`w2{9~eF(O9Am`I9oF(D?! zl$aJNF(cAqR%Api(wvwV3t~|$iDi)$Dq@Yg5_>nb(P9iVm35jsYzj^Mk& z_ca}a*~cKf)nkR;5Q$1mf-K0QEJ<7vvMfnik(8{;nxrKo>yniX*_18WmL17SUJ9}+ zd$KPFQk0UE 0 { cost = (ts.GetDuration().Seconds() / ts.Interval.BillingUnit) * ts.Interval.Price } else {