cleaned data series

This commit is contained in:
Radu Ioan Fericean
2012-05-30 15:08:21 +03:00
parent 33a9ae9967
commit 3d6d7289d1
7 changed files with 70 additions and 92 deletions

View File

@@ -49,12 +49,9 @@ func (ap *ActivationPeriod) store() (result string) {
result += strconv.FormatInt(ap.ActivationTime.UnixNano(), 10) + ";"
for _, i := range ap.Intervals {
var is string
is = strconv.Itoa(int(i.Month)) + "|"
is += strconv.Itoa(i.MonthDay) + "|"
for _, wd := range i.WeekDays {
is += strconv.Itoa(int(wd)) + ","
}
is = strings.TrimRight(is, ",") + "|"
is = i.Months.store() + "|"
is += i.MonthDays.store() + "|"
is += i.WeekDays.store() + "|"
is += i.StartTime + "|"
is += i.EndTime + "|"
is += strconv.FormatFloat(i.Ponder, 'f', -1, 64) + "|"
@@ -77,15 +74,9 @@ func (ap *ActivationPeriod) restore(input string) {
for _, is := range elements[1 : len(elements)-1] {
i := &Interval{}
ise := strings.Split(is, "|")
month, _ := strconv.Atoi(ise[0])
i.Month = time.Month(month)
i.MonthDay, _ = strconv.Atoi(ise[1])
for _, d := range strings.Split(ise[2], ",") {
if d != "" {
wd, _ := strconv.Atoi(d)
i.WeekDays = append(i.WeekDays, time.Weekday(wd))
}
}
i.Months.restore(ise[0])
i.MonthDays.restore(ise[1])
i.WeekDays.restore(ise[2])
i.StartTime = ise[3]
i.EndTime = ise[4]
i.Ponder, _ = strconv.ParseFloat(ise[5], 64)

View File

@@ -55,8 +55,8 @@ func TestApRestoreRedis(t *testing.T) {
func TestApStoreRestore(t *testing.T) {
d := time.Date(2012, time.February, 1, 14, 30, 1, 0, time.UTC)
i := &Interval{Month: time.February,
MonthDay: 1,
i := &Interval{Months: []time.Month{time.February},
MonthDays: []int{1},
WeekDays: []time.Weekday{time.Wednesday, time.Thursday},
StartTime: "14:30:00",
EndTime: "15:00:00"}
@@ -130,8 +130,8 @@ func BenchmarkActivationPeriodRestore(b *testing.B) {
func BenchmarkActivationPeriodStoreRestore(b *testing.B) {
b.StopTimer()
d := time.Date(2012, time.February, 1, 14, 30, 1, 0, time.UTC)
i := &Interval{Month: time.February,
MonthDay: 1,
i := &Interval{Months: []time.Month{time.February},
MonthDays: []int{1},
WeekDays: []time.Weekday{time.Wednesday, time.Thursday},
StartTime: "14:30:00",
EndTime: "15:00:00"}

View File

@@ -22,18 +22,16 @@ import (
"time"
"strings"
"strconv"
"log"
)
// Defines months series
type Months struct {
Id string
Series []time.Month
}
type Months []time.Month
// Return true if the specified date is inside the series
func (m *Months) Contains(month time.Month) (result bool) {
func (m Months) Contains(month time.Month) (result bool) {
result = false
for _, ms := range m.Series {
for _, ms := range m {
if ms == month {
result = true
break
@@ -45,9 +43,9 @@ func (m *Months) Contains(month time.Month) (result bool) {
/*
Serializes the month for the storage. Used for key-value storages.
*/
func (m *Months) store() (result string) {
for _, ms := range m.Series {
result += strconv.Itoa(int(ms)) + "|"
func (m Months) store() (result string) {
for _, ms := range m {
result += strconv.Itoa(int(ms)) + ","
}
return
}
@@ -56,24 +54,22 @@ func (m *Months) store() (result string) {
De-serializes the month for the storage. Used for key-value storages.
*/
func (m *Months) restore(input string) {
elements := strings.Split(input, "|")
elements := strings.Split(input, ",")
for _, ms := range elements {
if month, err := strconv.Atoi(ms); err == nil {
m.Series = append(m.Series, time.Month(month))
*m = append(*m, time.Month(month))
}
}
log.Print("here: ", m)
}
// Defines month days series
type MonthDays struct {
Id string
Series []int
}
type MonthDays []int
// Return true if the specified date is inside the series
func (md *MonthDays) Contains(monthDay int) (result bool) {
func (md MonthDays) Contains(monthDay int) (result bool) {
result = false
for _, mds := range md.Series {
for _, mds := range md {
if mds == monthDay {
result = true
break
@@ -85,9 +81,9 @@ func (md *MonthDays) Contains(monthDay int) (result bool) {
/*
Serializes the month days for the storage. Used for key-value storages.
*/
func (md *MonthDays) store() (result string) {
for _, mds := range md.Series {
result += strconv.Itoa(mds) + "|"
func (md MonthDays) store() (result string) {
for _, mds := range md {
result += strconv.Itoa(mds) + ","
}
return
}
@@ -96,24 +92,21 @@ func (md *MonthDays) store() (result string) {
De-serializes the month days for the storage. Used for key-value storages.
*/
func (md *MonthDays) restore(input string) {
elements := strings.Split(input, "|")
elements := strings.Split(input, ",")
for _, mds := range elements {
if day, err := strconv.Atoi(mds); err == nil {
md.Series = append(md.Series, day)
*md = append(*md, day)
}
}
}
// Defines week days series
type WeekDays struct {
Id string
Series []time.Weekday
}
type WeekDays []time.Weekday
// Return true if the specified date is inside the series
func (wd *WeekDays) Contains(weekDay time.Weekday) (result bool) {
func (wd WeekDays) Contains(weekDay time.Weekday) (result bool) {
result = false
for _, wds := range wd.Series {
for _, wds := range wd {
if wds == weekDay {
result = true
break
@@ -125,9 +118,9 @@ func (wd *WeekDays) Contains(weekDay time.Weekday) (result bool) {
/*
Serializes the week days for the storage. Used for key-value storages.
*/
func (wd *WeekDays) store() (result string) {
for _, wds := range wd.Series {
result += strconv.Itoa(int(wds)) + "|"
func (wd WeekDays) store() (result string) {
for _, wds := range wd {
result += strconv.Itoa(int(wds)) + ","
}
return
}
@@ -136,10 +129,10 @@ func (wd *WeekDays) store() (result string) {
De-serializes the week days for the storage. Used for key-value storages.
*/
func (wd *WeekDays) restore(input string) {
elements := strings.Split(input, "|")
elements := strings.Split(input, ",")
for _, wds := range elements {
if day, err := strconv.Atoi(wds); err == nil {
wd.Series = append(wd.Series, time.Weekday(day))
*wd = append(*wd, time.Weekday(day))
}
}
}

View File

@@ -25,12 +25,12 @@ import (
)
func TestMonthStoreRestore(t *testing.T) {
m := &Months{Id: "SUMMER", Series: []time.Month{5, 6, 7, 8}}
m := Months{5, 6, 7, 8}
r := m.store()
if r != "5|6|7|8|" {
if r != "5,6,7,8," {
t.Errorf("Error serializing months: %v", r)
}
o := &Months{Id: "SUMMER"}
o := Months{}
o.restore(r)
if !reflect.DeepEqual(o, m) {
t.Errorf("Expected %v was %v", m, o)
@@ -38,12 +38,12 @@ func TestMonthStoreRestore(t *testing.T) {
}
func TestMonthDayStoreRestore(t *testing.T) {
md := &MonthDays{Id: "CHRISTMAS", Series: []int{24, 25, 26}}
md := MonthDays{24, 25, 26}
r := md.store()
if r != "24|25|26|" {
if r != "24,25,26," {
t.Errorf("Error serializing month days: %v", r)
}
o := &MonthDays{Id: "CHRISTMAS"}
o := MonthDays{}
o.restore(r)
if !reflect.DeepEqual(o, md) {
t.Errorf("Expected %v was %v", md, o)
@@ -51,12 +51,12 @@ func TestMonthDayStoreRestore(t *testing.T) {
}
func TestWeekDayStoreRestore(t *testing.T) {
wd := &WeekDays{Id: "WEEKEND", Series: []time.Weekday{6, 7}}
wd := WeekDays{time.Saturday, time.Sunday}
r := wd.store()
if r != "6|7|" {
if r != "6,0," {
t.Errorf("Error serializing week days: %v", r)
}
o := &WeekDays{Id: "WEEKEND"}
o := WeekDays{}
o.restore(r)
if !reflect.DeepEqual(o, wd) {
t.Errorf("Expected %v was %v", wd, o)

View File

@@ -29,9 +29,9 @@ import (
Defines a time interval for which a certain set of prices will apply
*/
type Interval struct {
Month time.Month
MonthDay int
WeekDays []time.Weekday
Months Months
MonthDays MonthDays
WeekDays WeekDays
StartTime, EndTime string // ##:##:## format
Ponder, ConnectFee, Price, BillingUnit float64
}
@@ -40,22 +40,16 @@ type Interval struct {
Returns true if the received time is inside the interval
*/
func (i *Interval) Contains(t time.Time) bool {
// check for month
if i.Month > 0 && t.Month() != i.Month {
// check for months
if len(i.Months) > 0 && !i.Months.Contains(t.Month()) {
return false
}
// check for month day
if i.MonthDay > 0 && t.Day() != i.MonthDay {
// check for month days
if len(i.MonthDays) > 0 && !i.MonthDays.Contains(t.Day()) {
return false
}
// check for weekdays
found := false
for _, wd := range i.WeekDays {
if t.Weekday() == wd {
found = true
}
}
if len(i.WeekDays) > 0 && !found {
if len(i.WeekDays) > 0 && !i.WeekDays.Contains(t.Weekday()) {
return false
}
// check for start hour
@@ -94,11 +88,11 @@ func (i *Interval) getRightMargin(t time.Time) (rigthtTime time.Time) {
year, month, day := t.Year(), t.Month(), t.Day()
hour, min, sec, nsec := 23, 59, 59, 0
loc := t.Location()
if i.Month > 0 {
month = i.Month
if len(i.Months) > 0 {
month = i.Months[len(i.Months)-1]
}
if i.MonthDay > 0 {
day = i.MonthDay
if len(i.MonthDays) > 0 {
day = i.MonthDays[len(i.MonthDays)-1]
}
if i.EndTime != "" {
split := strings.Split(i.EndTime, ":")
@@ -116,11 +110,11 @@ func (i *Interval) getLeftMargin(t time.Time) (rigthtTime time.Time) {
year, month, day := t.Year(), t.Month(), t.Day()
hour, min, sec, nsec := 0, 0, 0, 0
loc := t.Location()
if i.Month > 0 {
month = i.Month
if len(i.Months) > 0 {
month = i.Months[0]
}
if i.MonthDay > 0 {
day = i.MonthDay
if len(i.MonthDays) > 0 {
day = i.MonthDays[0]
}
if i.StartTime != "" {
split := strings.Split(i.StartTime, ":")

View File

@@ -24,7 +24,7 @@ import (
)
func TestMonth(t *testing.T) {
i := &Interval{Month: time.February}
i := &Interval{Months: Months{time.February}}
d := time.Date(2012, time.February, 10, 23, 0, 0, 0, time.UTC)
d1 := time.Date(2012, time.January, 10, 23, 0, 0, 0, time.UTC)
if !i.Contains(d) {
@@ -36,7 +36,7 @@ func TestMonth(t *testing.T) {
}
func TestMonthDay(t *testing.T) {
i := &Interval{MonthDay: 10}
i := &Interval{MonthDays: MonthDays{10}}
d := time.Date(2012, time.February, 10, 23, 0, 0, 0, time.UTC)
d1 := time.Date(2012, time.February, 11, 23, 0, 0, 0, time.UTC)
if !i.Contains(d) {
@@ -48,7 +48,7 @@ func TestMonthDay(t *testing.T) {
}
func TestMonthAndMonthDay(t *testing.T) {
i := &Interval{Month: time.February, MonthDay: 10}
i := &Interval{Months: Months{time.February}, MonthDays: MonthDays{10}}
d := time.Date(2012, time.February, 10, 23, 0, 0, 0, time.UTC)
d1 := time.Date(2012, time.February, 11, 23, 0, 0, 0, time.UTC)
d2 := time.Date(2012, time.January, 10, 23, 0, 0, 0, time.UTC)
@@ -83,8 +83,8 @@ func TestWeekDays(t *testing.T) {
}
func TestMonthAndMonthDayAndWeekDays(t *testing.T) {
i := &Interval{Month: time.February, MonthDay: 1, WeekDays: []time.Weekday{time.Wednesday}}
i2 := &Interval{Month: time.February, MonthDay: 2, WeekDays: []time.Weekday{time.Wednesday, time.Thursday}}
i := &Interval{Months: Months{time.February}, MonthDays: MonthDays{1}, WeekDays: []time.Weekday{time.Wednesday}}
i2 := &Interval{Months: Months{time.February}, MonthDays: MonthDays{2}, WeekDays: []time.Weekday{time.Wednesday, time.Thursday}}
d := time.Date(2012, time.February, 1, 23, 0, 0, 0, time.UTC)
d1 := time.Date(2012, time.February, 2, 23, 0, 0, 0, time.UTC)
if !i.Contains(d) {
@@ -122,8 +122,8 @@ func TestHours(t *testing.T) {
}
func TestEverything(t *testing.T) {
i := &Interval{Month: time.February,
MonthDay: 1,
i := &Interval{Months: Months{time.February},
MonthDays: MonthDays{1},
WeekDays: []time.Weekday{time.Wednesday, time.Thursday},
StartTime: "14:30:00",
EndTime: "15:00:00"}
@@ -146,7 +146,7 @@ func TestEverything(t *testing.T) {
}
func BenchmarkIntervalContainsDate(b *testing.B) {
i := &Interval{Month: time.February, MonthDay: 1, WeekDays: []time.Weekday{time.Wednesday, time.Thursday}, StartTime: "14:30:00", EndTime: "15:00:00"}
i := &Interval{Months: Months{time.February}, MonthDays: MonthDays{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, 0, 0, time.UTC)
for x := 0; x < b.N; x++ {
i.Contains(d)

View File

@@ -99,7 +99,7 @@ func TestLeftMargin(t *testing.T) {
}
func TestLeftHourMargin(t *testing.T) {
i := &Interval{Month: time.December, MonthDay: 1, StartTime: "09:00:00"}
i := &Interval{Months: Months{time.December}, MonthDays: MonthDays{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}