Skip to content

chore: Revert to only using 1 timezone support for template DAUs #7721

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 2 commits into from
May 30, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
17 changes: 13 additions & 4 deletions coderd/metricscache/metricscache.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,23 @@ import (
"github.com/coder/retry"
)

// timezoneOffsets are the timezones that are cached and supported.
// deploymentTimezoneOffsets are the timezones that are cached and supported.
// Any non-listed timezone offsets will need to use the closest supported one.
var timezoneOffsets = []int{
var deploymentTimezoneOffsets = []int{
0, // UTC - is listed first intentionally.
-12, -11, -10, -9, -8, -7, -6, -5, -4, -3, -2, -1,
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
}

// templateTimezoneOffsets are the timezones each template will use for it's DAU
// calculations. This is expensive as each template needs to do each timezone, so keep this list
// very small.
var templateTimezoneOffsets = []int{
// Only do one for now. If people request more accurate template DAU, we can
// fix this. But it adds too much cost, so optimization is needed first.
0, // UTC - is listed first intentionally.
}

// Cache holds the template metrics.
// The aggregation queries responsible for these values can take up to a minute
// on large deployments. Even in small deployments, aggregation queries can
Expand Down Expand Up @@ -166,7 +175,7 @@ func (c *Cache) refreshDeploymentDAUs(ctx context.Context) error {
ctx = dbauthz.AsSystemRestricted(ctx)

deploymentDAUs := make(map[int]codersdk.DAUsResponse)
for _, tzOffset := range timezoneOffsets {
for _, tzOffset := range deploymentTimezoneOffsets {
rows, err := c.database.GetDeploymentDAUs(ctx, int32(tzOffset))
if err != nil {
return err
Expand Down Expand Up @@ -199,7 +208,7 @@ func (c *Cache) refreshTemplateDAUs(ctx context.Context) error {
}

for _, template := range templates {
for _, tzOffset := range timezoneOffsets {
for _, tzOffset := range templateTimezoneOffsets {
rows, err := c.database.GetTemplateDAUs(ctx, database.GetTemplateDAUsParams{
TemplateID: template.ID,
TzOffset: int32(tzOffset),
Expand Down
47 changes: 34 additions & 13 deletions coderd/metricscache/metricscache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,14 @@ func TestCache_TemplateUsers(t *testing.T) {
uniqueUsers int
}
tests := []struct {
name string
args args
want want
name string
args args
tplWant want
// dauWant is optional
dauWant []codersdk.DAUEntry
tzOffset int
}{
{name: "empty", args: args{}, want: want{nil, 0}},
{name: "empty", args: args{}, tplWant: want{nil, 0}},
{
name: "one hole",
args: args{
Expand All @@ -62,7 +64,7 @@ func TestCache_TemplateUsers(t *testing.T) {
statRow(zebra, dateH(2022, 8, 30, 0)),
},
},
want: want{[]codersdk.DAUEntry{
tplWant: want{[]codersdk.DAUEntry{
{
Date: date(2022, 8, 27),
Amount: 1,
Expand Down Expand Up @@ -90,7 +92,7 @@ func TestCache_TemplateUsers(t *testing.T) {
statRow(zebra, dateH(2022, 8, 29, 0)),
},
},
want: want{[]codersdk.DAUEntry{
tplWant: want{[]codersdk.DAUEntry{
{
Date: date(2022, 8, 27),
Amount: 1,
Expand All @@ -116,7 +118,7 @@ func TestCache_TemplateUsers(t *testing.T) {
statRow(tiger, dateH(2022, 1, 7, 0)),
},
},
want: want{[]codersdk.DAUEntry{
tplWant: want{[]codersdk.DAUEntry{
{
Date: date(2022, 1, 1),
Amount: 2,
Expand Down Expand Up @@ -159,7 +161,13 @@ func TestCache_TemplateUsers(t *testing.T) {
statRow(tiger, dateH(2022, 1, 2, 0)),
},
},
want: want{[]codersdk.DAUEntry{
tplWant: want{[]codersdk.DAUEntry{
{
Date: date(2022, 1, 2),
Amount: 2,
},
}, 2},
dauWant: []codersdk.DAUEntry{
{
Date: date(2022, 1, 1),
Amount: 2,
Expand All @@ -168,7 +176,7 @@ func TestCache_TemplateUsers(t *testing.T) {
Date: date(2022, 1, 2),
Amount: 2,
},
}, 2},
},
},
{
name: "tzOffsetPreviousDay",
Expand All @@ -181,11 +189,17 @@ func TestCache_TemplateUsers(t *testing.T) {
statRow(tiger, dateH(2022, 1, 2, 0)),
},
},
want: want{[]codersdk.DAUEntry{
dauWant: []codersdk.DAUEntry{
{
Date: date(2022, 1, 1),
Amount: 2,
},
},
tplWant: want{[]codersdk.DAUEntry{
{
Date: date(2022, 1, 2),
Amount: 2,
},
}, 2},
},
}
Expand Down Expand Up @@ -223,11 +237,18 @@ func TestCache_TemplateUsers(t *testing.T) {
gotUniqueUsers, ok := cache.TemplateUniqueUsers(template.ID)
require.True(t, ok)

if tt.dauWant != nil {
_, dauResponse, ok := cache.DeploymentDAUs(tt.tzOffset)
require.True(t, ok)
require.Equal(t, tt.dauWant, dauResponse.Entries)
}

offset, gotEntries, ok := cache.TemplateDAUs(template.ID, tt.tzOffset)
require.True(t, ok)
require.Equal(t, offset, tt.tzOffset)
require.Equal(t, tt.want.entries, gotEntries.Entries)
require.Equal(t, tt.want.uniqueUsers, gotUniqueUsers)
// Template only supports 0 offset.
require.Equal(t, 0, offset)
require.Equal(t, tt.tplWant.entries, gotEntries.Entries)
require.Equal(t, tt.tplWant.uniqueUsers, gotUniqueUsers)
})
}
}
Expand Down