Skip to content

Commit 73d795f

Browse files
authored
chore: Revert to only using 1 timezone support for template DAUs (#7721)
* chore: Revert to only using 1 timezone support for template DAUs Keeping the logic to support more in case we optimize later
1 parent 022372d commit 73d795f

File tree

2 files changed

+47
-17
lines changed

2 files changed

+47
-17
lines changed

coderd/metricscache/metricscache.go

+13-4
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,23 @@ import (
2121
"github.com/coder/retry"
2222
)
2323

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

32+
// templateTimezoneOffsets are the timezones each template will use for it's DAU
33+
// calculations. This is expensive as each template needs to do each timezone, so keep this list
34+
// very small.
35+
var templateTimezoneOffsets = []int{
36+
// Only do one for now. If people request more accurate template DAU, we can
37+
// fix this. But it adds too much cost, so optimization is needed first.
38+
0, // UTC - is listed first intentionally.
39+
}
40+
3241
// Cache holds the template metrics.
3342
// The aggregation queries responsible for these values can take up to a minute
3443
// on large deployments. Even in small deployments, aggregation queries can
@@ -166,7 +175,7 @@ func (c *Cache) refreshDeploymentDAUs(ctx context.Context) error {
166175
ctx = dbauthz.AsSystemRestricted(ctx)
167176

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

201210
for _, template := range templates {
202-
for _, tzOffset := range timezoneOffsets {
211+
for _, tzOffset := range templateTimezoneOffsets {
203212
rows, err := c.database.GetTemplateDAUs(ctx, database.GetTemplateDAUsParams{
204213
TemplateID: template.ID,
205214
TzOffset: int32(tzOffset),

coderd/metricscache/metricscache_test.go

+34-13
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,14 @@ func TestCache_TemplateUsers(t *testing.T) {
4848
uniqueUsers int
4949
}
5050
tests := []struct {
51-
name string
52-
args args
53-
want want
51+
name string
52+
args args
53+
tplWant want
54+
// dauWant is optional
55+
dauWant []codersdk.DAUEntry
5456
tzOffset int
5557
}{
56-
{name: "empty", args: args{}, want: want{nil, 0}},
58+
{name: "empty", args: args{}, tplWant: want{nil, 0}},
5759
{
5860
name: "one hole",
5961
args: args{
@@ -62,7 +64,7 @@ func TestCache_TemplateUsers(t *testing.T) {
6264
statRow(zebra, dateH(2022, 8, 30, 0)),
6365
},
6466
},
65-
want: want{[]codersdk.DAUEntry{
67+
tplWant: want{[]codersdk.DAUEntry{
6668
{
6769
Date: date(2022, 8, 27),
6870
Amount: 1,
@@ -90,7 +92,7 @@ func TestCache_TemplateUsers(t *testing.T) {
9092
statRow(zebra, dateH(2022, 8, 29, 0)),
9193
},
9294
},
93-
want: want{[]codersdk.DAUEntry{
95+
tplWant: want{[]codersdk.DAUEntry{
9496
{
9597
Date: date(2022, 8, 27),
9698
Amount: 1,
@@ -116,7 +118,7 @@ func TestCache_TemplateUsers(t *testing.T) {
116118
statRow(tiger, dateH(2022, 1, 7, 0)),
117119
},
118120
},
119-
want: want{[]codersdk.DAUEntry{
121+
tplWant: want{[]codersdk.DAUEntry{
120122
{
121123
Date: date(2022, 1, 1),
122124
Amount: 2,
@@ -159,7 +161,13 @@ func TestCache_TemplateUsers(t *testing.T) {
159161
statRow(tiger, dateH(2022, 1, 2, 0)),
160162
},
161163
},
162-
want: want{[]codersdk.DAUEntry{
164+
tplWant: want{[]codersdk.DAUEntry{
165+
{
166+
Date: date(2022, 1, 2),
167+
Amount: 2,
168+
},
169+
}, 2},
170+
dauWant: []codersdk.DAUEntry{
163171
{
164172
Date: date(2022, 1, 1),
165173
Amount: 2,
@@ -168,7 +176,7 @@ func TestCache_TemplateUsers(t *testing.T) {
168176
Date: date(2022, 1, 2),
169177
Amount: 2,
170178
},
171-
}, 2},
179+
},
172180
},
173181
{
174182
name: "tzOffsetPreviousDay",
@@ -181,11 +189,17 @@ func TestCache_TemplateUsers(t *testing.T) {
181189
statRow(tiger, dateH(2022, 1, 2, 0)),
182190
},
183191
},
184-
want: want{[]codersdk.DAUEntry{
192+
dauWant: []codersdk.DAUEntry{
185193
{
186194
Date: date(2022, 1, 1),
187195
Amount: 2,
188196
},
197+
},
198+
tplWant: want{[]codersdk.DAUEntry{
199+
{
200+
Date: date(2022, 1, 2),
201+
Amount: 2,
202+
},
189203
}, 2},
190204
},
191205
}
@@ -223,11 +237,18 @@ func TestCache_TemplateUsers(t *testing.T) {
223237
gotUniqueUsers, ok := cache.TemplateUniqueUsers(template.ID)
224238
require.True(t, ok)
225239

240+
if tt.dauWant != nil {
241+
_, dauResponse, ok := cache.DeploymentDAUs(tt.tzOffset)
242+
require.True(t, ok)
243+
require.Equal(t, tt.dauWant, dauResponse.Entries)
244+
}
245+
226246
offset, gotEntries, ok := cache.TemplateDAUs(template.ID, tt.tzOffset)
227247
require.True(t, ok)
228-
require.Equal(t, offset, tt.tzOffset)
229-
require.Equal(t, tt.want.entries, gotEntries.Entries)
230-
require.Equal(t, tt.want.uniqueUsers, gotUniqueUsers)
248+
// Template only supports 0 offset.
249+
require.Equal(t, 0, offset)
250+
require.Equal(t, tt.tplWant.entries, gotEntries.Entries)
251+
require.Equal(t, tt.tplWant.uniqueUsers, gotUniqueUsers)
231252
})
232253
}
233254
}

0 commit comments

Comments
 (0)