fix: strip timezone information from a date in dau response (#11962)

* fix: strip timezone information from a date in dau response

Timezone information is lost, so do not forward it to the client.

* fix: timezone offset should be flipped
* Make tests deterministic
This commit is contained in:
Steven Masley
2024-01-31 16:01:50 -06:00
committed by GitHub
parent 76e73287a5
commit ac64155282
12 changed files with 111 additions and 67 deletions
+2 -2
View File
@@ -8877,8 +8877,8 @@ const docTemplate = `{
"type": "integer"
},
"date": {
"type": "string",
"format": "date-time"
"description": "Date is a string formatted as 2024-01-31.\nTimezone and time information is not included.",
"type": "string"
}
}
},
+2 -2
View File
@@ -7913,8 +7913,8 @@
"type": "integer"
},
"date": {
"type": "string",
"format": "date-time"
"description": "Date is a string formatted as 2024-01-31.\nTimezone and time information is not included.",
"type": "string"
}
}
},
+8 -4
View File
@@ -39,6 +39,9 @@ import (
func TestDeploymentInsights(t *testing.T) {
t.Parallel()
clientTz, err := time.LoadLocation("America/Chicago")
require.NoError(t, err)
client := coderdtest.New(t, &coderdtest.Options{
IncludeProvisionerDaemon: true,
AgentStatsRefreshInterval: time.Millisecond * 100,
@@ -64,7 +67,7 @@ func TestDeploymentInsights(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitLong)
defer cancel()
daus, err := client.DeploymentDAUs(context.Background(), codersdk.TimezoneOffsetHour(time.UTC))
daus, err := client.DeploymentDAUs(context.Background(), codersdk.TimezoneOffsetHour(clientTz))
require.NoError(t, err)
res, err := client.Workspaces(ctx, codersdk.WorkspaceFilter{})
@@ -84,22 +87,23 @@ func TestDeploymentInsights(t *testing.T) {
_ = sshConn.Close()
wantDAUs := &codersdk.DAUsResponse{
TZHourOffset: codersdk.TimezoneOffsetHour(clientTz),
Entries: []codersdk.DAUEntry{
{
Date: time.Now().UTC().Truncate(time.Hour * 24),
Date: time.Now().In(clientTz).Format("2006-01-02"),
Amount: 1,
},
},
}
require.Eventuallyf(t, func() bool {
daus, err = client.DeploymentDAUs(ctx, codersdk.TimezoneOffsetHour(time.UTC))
daus, err = client.DeploymentDAUs(ctx, codersdk.TimezoneOffsetHour(clientTz))
require.NoError(t, err)
return len(daus.Entries) > 0
},
testutil.WaitShort, testutil.IntervalFast,
"deployment daus never loaded",
)
gotDAUs, err := client.DeploymentDAUs(ctx, codersdk.TimezoneOffsetHour(time.UTC))
gotDAUs, err := client.DeploymentDAUs(ctx, codersdk.TimezoneOffsetHour(clientTz))
require.NoError(t, err)
require.Equal(t, gotDAUs, wantDAUs)
+7 -1
View File
@@ -22,6 +22,10 @@ import (
"github.com/coder/retry"
)
func OnlyDate(t time.Time) string {
return t.Format("2006-01-02")
}
// deploymentTimezoneOffsets are the timezones that are cached and supported.
// Any non-listed timezone offsets will need to use the closest supported one.
var deploymentTimezoneOffsets = []int{
@@ -166,7 +170,9 @@ func convertDAUResponse[T dauRow](rows []T, tzOffset int) codersdk.DAUsResponse
var resp codersdk.DAUsResponse
for _, date := range fillEmptyDays(dates) {
resp.Entries = append(resp.Entries, codersdk.DAUEntry{
Date: date,
// This date is truncated to 00:00:00 of the given day, so only
// return date information.
Date: OnlyDate(date),
Amount: len(respMap[date]),
})
}
+19 -19
View File
@@ -67,19 +67,19 @@ func TestCache_TemplateUsers(t *testing.T) {
},
tplWant: want{[]codersdk.DAUEntry{
{
Date: date(2022, 8, 27),
Date: metricscache.OnlyDate(date(2022, 8, 27)),
Amount: 1,
},
{
Date: date(2022, 8, 28),
Date: metricscache.OnlyDate(date(2022, 8, 28)),
Amount: 0,
},
{
Date: date(2022, 8, 29),
Date: metricscache.OnlyDate(date(2022, 8, 29)),
Amount: 0,
},
{
Date: date(2022, 8, 30),
Date: metricscache.OnlyDate(date(2022, 8, 30)),
Amount: 1,
},
}, 1},
@@ -95,15 +95,15 @@ func TestCache_TemplateUsers(t *testing.T) {
},
tplWant: want{[]codersdk.DAUEntry{
{
Date: date(2022, 8, 27),
Date: metricscache.OnlyDate(date(2022, 8, 27)),
Amount: 1,
},
{
Date: date(2022, 8, 28),
Date: metricscache.OnlyDate(date(2022, 8, 28)),
Amount: 1,
},
{
Date: date(2022, 8, 29),
Date: metricscache.OnlyDate(date(2022, 8, 29)),
Amount: 1,
},
}, 1},
@@ -121,31 +121,31 @@ func TestCache_TemplateUsers(t *testing.T) {
},
tplWant: want{[]codersdk.DAUEntry{
{
Date: date(2022, 1, 1),
Date: metricscache.OnlyDate(date(2022, 1, 1)),
Amount: 2,
},
{
Date: date(2022, 1, 2),
Date: metricscache.OnlyDate(date(2022, 1, 2)),
Amount: 0,
},
{
Date: date(2022, 1, 3),
Date: metricscache.OnlyDate(date(2022, 1, 3)),
Amount: 0,
},
{
Date: date(2022, 1, 4),
Date: metricscache.OnlyDate(date(2022, 1, 4)),
Amount: 1,
},
{
Date: date(2022, 1, 5),
Date: metricscache.OnlyDate(date(2022, 1, 5)),
Amount: 0,
},
{
Date: date(2022, 1, 6),
Date: metricscache.OnlyDate(date(2022, 1, 6)),
Amount: 0,
},
{
Date: date(2022, 1, 7),
Date: metricscache.OnlyDate(date(2022, 1, 7)),
Amount: 2,
},
}, 2},
@@ -164,17 +164,17 @@ func TestCache_TemplateUsers(t *testing.T) {
},
tplWant: want{[]codersdk.DAUEntry{
{
Date: date(2022, 1, 2),
Date: metricscache.OnlyDate(date(2022, 1, 2)),
Amount: 2,
},
}, 2},
dauWant: []codersdk.DAUEntry{
{
Date: date(2022, 1, 1),
Date: metricscache.OnlyDate(date(2022, 1, 1)),
Amount: 2,
},
{
Date: date(2022, 1, 2),
Date: metricscache.OnlyDate(date(2022, 1, 2)),
Amount: 2,
},
},
@@ -192,13 +192,13 @@ func TestCache_TemplateUsers(t *testing.T) {
},
dauWant: []codersdk.DAUEntry{
{
Date: date(2022, 1, 1),
Date: metricscache.OnlyDate(date(2022, 1, 1)),
Amount: 2,
},
},
tplWant: want{[]codersdk.DAUEntry{
{
Date: date(2022, 1, 2),
Date: metricscache.OnlyDate(date(2022, 1, 2)),
Amount: 2,
},
}, 2},
+1 -1
View File
@@ -1405,7 +1405,7 @@ func TestTemplateMetrics(t *testing.T) {
wantDAUs := &codersdk.DAUsResponse{
Entries: []codersdk.DAUEntry{
{
Date: time.Now().UTC().Truncate(time.Hour * 24),
Date: time.Now().UTC().Truncate(time.Hour * 24).Format("2006-01-02"),
Amount: 1,
},
},