Skip to content

refactor: define insights interval #9693

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

Merged
merged 8 commits into from
Sep 15, 2023
Merged

refactor: define insights interval #9693

merged 8 commits into from
Sep 15, 2023

Conversation

mtojek
Copy link
Member

@mtojek mtojek commented Sep 15, 2023

Related: #9495

This PR exposes interval_days for "coderd" to customize the time range in template insights queries. In the follow up, I will continue with #9684.

Note: As I renamed GetTemplateDailyInsights to GetTemplateInsightsByInterval, the formatter adjusted the order of functions accordingly, so this PR may seem to be relatively large.

@mtojek mtojek self-assigned this Sep 15, 2023
@mtojek mtojek requested review from mafredri and johnstcn September 15, 2023 10:46
@mtojek mtojek marked this pull request as ready for review September 15, 2023 10:46
Copy link
Member

@mafredri mafredri left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work! I'm happy this was ~ as minimal a change as I had imagined.


interval := time.Duration(arg.IntervalDays) * 24 * time.Hour

statsByInterval := []statByInterval{{arg.StartTime, arg.StartTime.Add(interval), make(map[uuid.UUID]struct{}), make(map[uuid.UUID]struct{})}}
Copy link
Member

@mafredri mafredri Sep 15, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this could still use .AddDate, the reason I point it out is caution with DST. E.g. does it behave the same as an interval when crossing the DST boundary?

I think Go assumes this for you, but I think it would be good to confirm 👍🏻

So the question is: Does the clock stay 00:00 when using Add(interval) and DST is part of time range? (A good test would be both 24time.Hour and 168time.Hour.)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is more like a Go question, and .Add(duration) doesn't preserve the time change (DST), so good catch! I will switch to .AddDate() to have consistent 00:00 hour windows.

func TestAddDateWithDST(t *testing.T) {
	// Set the time zone to a location that observes DST
	tz, err := time.LoadLocation("America/New_York")
	if err != nil {
		t.Fatal(err)
	}

	// Create a time in the specified time zone
	date := time.Date(2023, time.March, 11, 0, 0, 0, 0, tz)

	// Add a duration of one day to the date
	// newDate := date.Add(48 * time.Hour)
	newDate := date.AddDate(0, 0, 2)

	// Since DST starts on March 12, 2023, in America/New_York, adding one day should account for DST
	// and the resulting date should be 23 hours ahead
	expectedDate := time.Date(2023, time.March, 13, 0, 0, 0, 0, tz)

	// Compare the expected date with the actual result
	if !newDate.Equal(expectedDate) {
		t.Errorf("Expected: %s, Got: %s", expectedDate, newDate)
	}
}

@mtojek mtojek enabled auto-merge (squash) September 15, 2023 11:59
@mtojek mtojek merged commit d0d64bb into main Sep 15, 2023
@mtojek mtojek deleted the 9495-refactor-interval branch September 15, 2023 12:01
@github-actions github-actions bot locked and limited conversation to collaborators Sep 15, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants