Skip to content

fix: pass in time parameter to prevent flakes #11023

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 6 commits into from
Dec 4, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
7 changes: 5 additions & 2 deletions coderd/insights.go
Original file line number Diff line number Diff line change
Expand Up @@ -550,6 +550,9 @@ func parseInsightsStartAndEndTime(ctx context.Context, rw http.ResponseWriter, s
{"end_time", endTimeString, &endTime},
} {
t, err := time.Parse(insightsTimeLayout, qp.value)
// strip monotonic clock reading
// so presentation time and arithmetic operations are consistent
t = t.Round(0)
Copy link
Member

Choose a reason for hiding this comment

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

I don't think we need to strip the monotonic clock here since we're parsing a string. Monotonic clock should only be present when time.Now() is used. 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah I just learned that after making this :P

if err != nil {
httpapi.Write(ctx, rw, http.StatusBadRequest, codersdk.Response{
Message: "Query parameter has invalid value.",
Expand Down Expand Up @@ -604,7 +607,7 @@ func parseInsightsStartAndEndTime(ctx context.Context, rw http.ResponseWriter, s
Validations: []codersdk.ValidationError{
{
Field: qp.name,
Detail: fmt.Sprintf("Query param %q must have the clock set to 00:00:00", qp.name),
Detail: fmt.Sprintf("Query param %q must have the clock set to 00:00:00, got %s", qp.name, qp.value),
},
},
})
Expand All @@ -615,7 +618,7 @@ func parseInsightsStartAndEndTime(ctx context.Context, rw http.ResponseWriter, s
Validations: []codersdk.ValidationError{
{
Field: qp.name,
Detail: fmt.Sprintf("Query param %q must have the clock set to %02d:00:00", qp.name, h),
Detail: fmt.Sprintf("Query param %q must have the clock set to %02d:00:00, got %s", qp.name, h, qp.value),
},
},
})
Expand Down
6 changes: 6 additions & 0 deletions coderd/insights_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,18 @@ import (
func Test_parseInsightsStartAndEndTime(t *testing.T) {
t.Parallel()

t.Logf("machine location: %s", time.Now().Location())
layout := insightsTimeLayout
now := time.Now().UTC()
t.Logf("now: %s", now)
t.Logf("now location: %s", now.Location())
y, m, d := now.Date()
today := time.Date(y, m, d, 0, 0, 0, 0, time.UTC)
t.Logf("today: %s", today)
thisHour := time.Date(y, m, d, now.Hour(), 0, 0, 0, time.UTC)
t.Logf("thisHour: %s", thisHour)
thisHourRoundUp := thisHour.Add(time.Hour)
t.Logf("thisHourRoundUp: %s", thisHourRoundUp)

helsinki, err := time.LoadLocation("Europe/Helsinki")
require.NoError(t, err)
Expand Down