Skip to content

Commit af77912

Browse files
committed
fix: strip timezone information from a date in dau response
Timezone information is lost, so do not forward it to the client.
1 parent 4604db0 commit af77912

File tree

4 files changed

+16
-8
lines changed

4 files changed

+16
-8
lines changed

coderd/insights_test.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ import (
3939
func TestDeploymentInsights(t *testing.T) {
4040
t.Parallel()
4141

42+
clientTz, err := time.LoadLocation("America/Chicago")
43+
require.NoError(t, err)
44+
4245
client := coderdtest.New(t, &coderdtest.Options{
4346
IncludeProvisionerDaemon: true,
4447
AgentStatsRefreshInterval: time.Millisecond * 100,
@@ -64,7 +67,7 @@ func TestDeploymentInsights(t *testing.T) {
6467
ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitLong)
6568
defer cancel()
6669

67-
daus, err := client.DeploymentDAUs(context.Background(), codersdk.TimezoneOffsetHour(time.UTC))
70+
daus, err := client.DeploymentDAUs(context.Background(), codersdk.TimezoneOffsetHour(clientTz))
6871
require.NoError(t, err)
6972

7073
res, err := client.Workspaces(ctx, codersdk.WorkspaceFilter{})
@@ -84,22 +87,23 @@ func TestDeploymentInsights(t *testing.T) {
8487
_ = sshConn.Close()
8588

8689
wantDAUs := &codersdk.DAUsResponse{
90+
TZHourOffset: codersdk.TimezoneOffsetHour(clientTz),
8791
Entries: []codersdk.DAUEntry{
8892
{
89-
Date: time.Now().UTC().Truncate(time.Hour * 24),
93+
Date: time.Now().In(clientTz).Format("2006-01-02"),
9094
Amount: 1,
9195
},
9296
},
9397
}
9498
require.Eventuallyf(t, func() bool {
95-
daus, err = client.DeploymentDAUs(ctx, codersdk.TimezoneOffsetHour(time.UTC))
99+
daus, err = client.DeploymentDAUs(ctx, codersdk.TimezoneOffsetHour(clientTz))
96100
require.NoError(t, err)
97101
return len(daus.Entries) > 0
98102
},
99103
testutil.WaitShort, testutil.IntervalFast,
100104
"deployment daus never loaded",
101105
)
102-
gotDAUs, err := client.DeploymentDAUs(ctx, codersdk.TimezoneOffsetHour(time.UTC))
106+
gotDAUs, err := client.DeploymentDAUs(ctx, codersdk.TimezoneOffsetHour(clientTz))
103107
require.NoError(t, err)
104108
require.Equal(t, gotDAUs, wantDAUs)
105109

coderd/metricscache/metricscache.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,9 @@ func convertDAUResponse[T dauRow](rows []T, tzOffset int) codersdk.DAUsResponse
166166
var resp codersdk.DAUsResponse
167167
for _, date := range fillEmptyDays(dates) {
168168
resp.Entries = append(resp.Entries, codersdk.DAUEntry{
169-
Date: date,
169+
// This date is truncated to 00:00:00 of the given day, so only
170+
// return date information.
171+
Date: date.Format("2006-01-02"),
170172
Amount: len(respMap[date]),
171173
})
172174
}

coderd/templates_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1405,7 +1405,7 @@ func TestTemplateMetrics(t *testing.T) {
14051405
wantDAUs := &codersdk.DAUsResponse{
14061406
Entries: []codersdk.DAUEntry{
14071407
{
1408-
Date: time.Now().UTC().Truncate(time.Hour * 24),
1408+
Date: time.Now().UTC().Truncate(time.Hour * 24).Format("2006-01-02"),
14091409
Amount: 1,
14101410
},
14111411
},

codersdk/deployment.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2168,8 +2168,10 @@ type DAUsResponse struct {
21682168
}
21692169

21702170
type DAUEntry struct {
2171-
Date time.Time `json:"date" format:"date-time"`
2172-
Amount int `json:"amount"`
2171+
// Date is a string formatted as 2024-01-31.
2172+
// Timezone and time information is not included.
2173+
Date string `json:"date"`
2174+
Amount int `json:"amount"`
21732175
}
21742176

21752177
type DAURequest struct {

0 commit comments

Comments
 (0)