Skip to content

refactor: define insights interval#9693

Merged
mtojek merged 8 commits into
mainfrom
9495-refactor-interval
Sep 15, 2023
Merged

refactor: define insights interval#9693
mtojek merged 8 commits into
mainfrom
9495-refactor-interval

Conversation

@mtojek

@mtojek mtojek commented Sep 15, 2023

Copy link
Copy Markdown
Member

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 johnstcn and mafredri September 15, 2023 10:46
@mtojek mtojek marked this pull request as ready for review September 15, 2023 10:46
Comment thread coderd/database/queries/insights.sql

@mafredri mafredri left a comment

Copy link
Copy Markdown
Member

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.

Comment thread coderd/database/dbfake/dbfake.go Outdated

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{})}}

@mafredri mafredri Sep 15, 2023

Copy link
Copy Markdown
Member

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
Copy Markdown
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)
	}
}

Comment thread coderd/database/queries/insights.sql Outdated
@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