Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: fix end of week period and add tests #28

Merged
merged 1 commit into from
May 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions charts/sort_filter_events.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ func getFirstDateOfPeriod(periodKey string) time.Time {
case common.Day:
lastDay, _ = time.Parse("2006-01-02", periodKey)
case common.Week:
periodTime, _ := time.Parse("2006-W01", periodKey)
lastDay = periodTime.AddDate(0, 0, (7 - int(periodTime.Weekday()))).Add(time.Duration(23)*time.Hour + time.Duration(59)*time.Minute + time.Duration(59)*time.Second)
periodTime, _ := reports.ParseYearWeek(periodKey)
lastDay = periodTime.AddDate(0, 0, 6).Add(time.Duration(23)*time.Hour + time.Duration(59)*time.Minute + time.Duration(59)*time.Second)
case common.Month:
periodTime, _ := time.Parse("2006-01", periodKey)

Expand Down
46 changes: 46 additions & 0 deletions charts/sort_filter_events_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package charts

import (
"testing"
"time"
)

func TestGetFirstDateOfPeriod_Day(t *testing.T) {
expected := time.Date(2023, time.May, 1, 0, 0, 0, 0, time.UTC)
result := getFirstDateOfPeriod("2023-05-01")
if !result.Equal(expected) {
t.Errorf("Expected %v, but got %v", expected, result)
}
}

func TestGetFirstDateOfPeriod_Week(t *testing.T) {
expected := time.Date(2023, time.April, 30, 23, 59, 59, 0, time.UTC)
result := getFirstDateOfPeriod("2023-W17")
if !result.Equal(expected) {
t.Errorf("Expected %v, but got %v", expected, result)
}
}

func TestGetFirstDateOfPeriod_Month(t *testing.T) {
expected := time.Date(2023, time.May, 31, 23, 59, 59, 0, time.UTC)
result := getFirstDateOfPeriod("2023-05")
if !result.Equal(expected) {
t.Errorf("Expected %v, but got %v", expected, result)
}
}

func TestGetFirstDateOfPeriod_Quarter(t *testing.T) {
expected := time.Date(2023, time.March, 31, 0, 0, 0, 0, time.UTC)
result := getFirstDateOfPeriod("2023-Q1")
if !result.Equal(expected) {
t.Errorf("Expected %v, but got %v", expected, result)
}
}

func TestGetFirstDateOfPeriod_Year(t *testing.T) {
expected := time.Date(2023, time.December, 31, 23, 59, 59, 0, time.UTC)
result := getFirstDateOfPeriod("2023")
if !result.Equal(expected) {
t.Errorf("Expected %v, but got %v", expected, result)
}
}
22 changes: 21 additions & 1 deletion reports/reports.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ func GetTimeGrain(periodKey string) (common.TimeGrain, error) {
if err == nil {
return common.Day, nil
}
_, err = time.Parse("2006-W01", periodKey)
_, err = ParseYearWeek(periodKey)
if err == nil {
return common.Week, nil
}
Expand All @@ -248,6 +248,26 @@ func GetTimeGrain(periodKey string) (common.TimeGrain, error) {
return "", fmt.Errorf("invalid period key: %s", periodKey)
}

func ParseYearWeek(yearWeek string) (time.Time, error) {
if len(yearWeek) != 8 {
return time.Time{}, fmt.Errorf("invalid year week format: %s", yearWeek)
}
year, err := strconv.Atoi(yearWeek[0:4])
if err != nil {
return time.Time{}, err
}

week, err := strconv.Atoi(yearWeek[6:])
if err != nil {
return time.Time{}, err
}

// Get the first day of the week (Monday)
firstDay := time.Date(year, 1, 1, 0, 0, 0, 0, time.UTC).AddDate(0, 0, 7*(week-1)+1)

return firstDay, nil
}

func ParseQuarterDate(s string) (time.Time, error) {
parts := strings.Split(s, "-")
if len(parts) != 2 {
Expand Down
20 changes: 20 additions & 0 deletions reports/reports_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package reports

import (
"testing"
"time"
)

func TestParseYearWeek(t *testing.T) {
yearWeek := "2023-W17" // Monday 24 April to Sunday 30 April 2023
expectedFirstDay := time.Date(2023, time.April, 24, 0, 0, 0, 0, time.UTC)

firstDay, err := ParseYearWeek(yearWeek)
if err != nil {
t.Errorf("Error parsing year week: %v", err)
}

if !firstDay.Equal(expectedFirstDay) {
t.Errorf("Expected first day of week for %s to be %v, but got %v", yearWeek, expectedFirstDay, firstDay)
}
}
Loading