@@ -18,12 +18,22 @@ import (
18
18
"github.com/coder/coder/testutil"
19
19
)
20
20
21
+ func dateH (year , month , day , hour int ) time.Time {
22
+ return time .Date (year , time .Month (month ), day , hour , 0 , 0 , 0 , time .UTC )
23
+ }
24
+
21
25
func date (year , month , day int ) time.Time {
22
26
return time .Date (year , time .Month (month ), day , 0 , 0 , 0 , 0 , time .UTC )
23
27
}
24
28
25
29
func TestCache_TemplateUsers (t * testing.T ) {
26
30
t .Parallel ()
31
+ statRow := func (user uuid.UUID , date time.Time ) database.InsertWorkspaceAgentStatParams {
32
+ return database.InsertWorkspaceAgentStatParams {
33
+ CreatedAt : date ,
34
+ UserID : user ,
35
+ }
36
+ }
27
37
28
38
var (
29
39
zebra = uuid.UUID {1 }
@@ -38,24 +48,21 @@ func TestCache_TemplateUsers(t *testing.T) {
38
48
uniqueUsers int
39
49
}
40
50
tests := []struct {
41
- name string
42
- args args
43
- want want
51
+ name string
52
+ args args
53
+ want want
54
+ tzOffset int
44
55
}{
45
- {"empty" , args {}, want {nil , 0 }},
56
+ {name : "empty" , args : args {}, want : want {nil , 0 }},
46
57
{
47
- "one hole" , args {
58
+ name : "one hole" ,
59
+ args : args {
48
60
rows : []database.InsertWorkspaceAgentStatParams {
49
- {
50
- CreatedAt : date (2022 , 8 , 27 ),
51
- UserID : zebra ,
52
- },
53
- {
54
- CreatedAt : date (2022 , 8 , 30 ),
55
- UserID : zebra ,
56
- },
61
+ statRow (zebra , dateH (2022 , 8 , 27 , 0 )),
62
+ statRow (zebra , dateH (2022 , 8 , 30 , 0 )),
57
63
},
58
- }, want {[]codersdk.DAUEntry {
64
+ },
65
+ want : want {[]codersdk.DAUEntry {
59
66
{
60
67
Date : date (2022 , 8 , 27 ),
61
68
Amount : 1 ,
@@ -74,88 +81,111 @@ func TestCache_TemplateUsers(t *testing.T) {
74
81
},
75
82
}, 1 },
76
83
},
77
- {"no holes" , args {
78
- rows : []database.InsertWorkspaceAgentStatParams {
84
+ {
85
+ name : "no holes" ,
86
+ args : args {
87
+ rows : []database.InsertWorkspaceAgentStatParams {
88
+ statRow (zebra , dateH (2022 , 8 , 27 , 0 )),
89
+ statRow (zebra , dateH (2022 , 8 , 28 , 0 )),
90
+ statRow (zebra , dateH (2022 , 8 , 29 , 0 )),
91
+ },
92
+ },
93
+ want : want {[]codersdk.DAUEntry {
79
94
{
80
- CreatedAt : date (2022 , 8 , 27 ),
81
- UserID : zebra ,
95
+ Date : date (2022 , 8 , 27 ),
96
+ Amount : 1 ,
82
97
},
83
98
{
84
- CreatedAt : date (2022 , 8 , 28 ),
85
- UserID : zebra ,
99
+ Date : date (2022 , 8 , 28 ),
100
+ Amount : 1 ,
86
101
},
87
102
{
88
- CreatedAt : date (2022 , 8 , 29 ),
89
- UserID : zebra ,
103
+ Date : date (2022 , 8 , 29 ),
104
+ Amount : 1 ,
105
+ },
106
+ }, 1 }},
107
+ {
108
+ name : "holes" ,
109
+ args : args {
110
+ rows : []database.InsertWorkspaceAgentStatParams {
111
+ statRow (zebra , dateH (2022 , 1 , 1 , 0 )),
112
+ statRow (tiger , dateH (2022 , 1 , 1 , 0 )),
113
+ statRow (zebra , dateH (2022 , 1 , 4 , 0 )),
114
+ statRow (zebra , dateH (2022 , 1 , 7 , 0 )),
115
+ statRow (tiger , dateH (2022 , 1 , 7 , 0 )),
90
116
},
91
117
},
92
- }, want {[]codersdk.DAUEntry {
93
- {
94
- Date : date (2022 , 8 , 27 ),
95
- Amount : 1 ,
96
- },
97
- {
98
- Date : date (2022 , 8 , 28 ),
99
- Amount : 1 ,
100
- },
101
- {
102
- Date : date (2022 , 8 , 29 ),
103
- Amount : 1 ,
104
- },
105
- }, 1 }},
106
- {"holes" , args {
107
- rows : []database.InsertWorkspaceAgentStatParams {
118
+ want : want {[]codersdk.DAUEntry {
119
+ {
120
+ Date : date (2022 , 1 , 1 ),
121
+ Amount : 2 ,
122
+ },
108
123
{
109
- CreatedAt : date (2022 , 1 , 1 ),
110
- UserID : zebra ,
124
+ Date : date (2022 , 1 , 2 ),
125
+ Amount : 0 ,
111
126
},
112
127
{
113
- CreatedAt : date (2022 , 1 , 1 ),
114
- UserID : tiger ,
128
+ Date : date (2022 , 1 , 3 ),
129
+ Amount : 0 ,
115
130
},
116
131
{
117
- CreatedAt : date (2022 , 1 , 4 ),
118
- UserID : zebra ,
132
+ Date : date (2022 , 1 , 4 ),
133
+ Amount : 1 ,
119
134
},
120
135
{
121
- CreatedAt : date (2022 , 1 , 7 ),
122
- UserID : zebra ,
136
+ Date : date (2022 , 1 , 5 ),
137
+ Amount : 0 ,
123
138
},
124
139
{
125
- CreatedAt : date (2022 , 1 , 7 ),
126
- UserID : tiger ,
140
+ Date : date (2022 , 1 , 6 ),
141
+ Amount : 0 ,
142
+ },
143
+ {
144
+ Date : date (2022 , 1 , 7 ),
145
+ Amount : 2 ,
146
+ },
147
+ }, 2 }},
148
+ {
149
+ name : "tzOffset" ,
150
+ tzOffset : - 1 ,
151
+ args : args {
152
+ rows : []database.InsertWorkspaceAgentStatParams {
153
+ statRow (zebra , dateH (2022 , 1 , 2 , 1 )),
154
+ statRow (tiger , dateH (2022 , 1 , 2 , 1 )),
155
+ // With offset these should be in the previous day
156
+ statRow (zebra , dateH (2022 , 1 , 2 , 0 )),
157
+ statRow (tiger , dateH (2022 , 1 , 2 , 0 )),
127
158
},
128
159
},
129
- }, want {[]codersdk.DAUEntry {
130
- {
131
- Date : date (2022 , 1 , 1 ),
132
- Amount : 2 ,
133
- },
134
- {
135
- Date : date (2022 , 1 , 2 ),
136
- Amount : 0 ,
137
- },
138
- {
139
- Date : date (2022 , 1 , 3 ),
140
- Amount : 0 ,
141
- },
142
- {
143
- Date : date (2022 , 1 , 4 ),
144
- Amount : 1 ,
145
- },
146
- {
147
- Date : date (2022 , 1 , 5 ),
148
- Amount : 0 ,
149
- },
150
- {
151
- Date : date (2022 , 1 , 6 ),
152
- Amount : 0 ,
153
- },
154
- {
155
- Date : date (2022 , 1 , 7 ),
156
- Amount : 2 ,
160
+ want : want {[]codersdk.DAUEntry {
161
+ {
162
+ Date : date (2022 , 1 , 1 ),
163
+ Amount : 2 ,
164
+ },
165
+ {
166
+ Date : date (2022 , 1 , 2 ),
167
+ Amount : 2 ,
168
+ },
169
+ }, 2 },
170
+ },
171
+ {
172
+ name : "tzOffsetPreviousDay" ,
173
+ tzOffset : - 6 ,
174
+ args : args {
175
+ rows : []database.InsertWorkspaceAgentStatParams {
176
+ statRow (zebra , dateH (2022 , 1 , 2 , 1 )),
177
+ statRow (tiger , dateH (2022 , 1 , 2 , 1 )),
178
+ statRow (zebra , dateH (2022 , 1 , 2 , 0 )),
179
+ statRow (tiger , dateH (2022 , 1 , 2 , 0 )),
180
+ },
157
181
},
158
- }, 2 }},
182
+ want : want {[]codersdk.DAUEntry {
183
+ {
184
+ Date : date (2022 , 1 , 1 ),
185
+ Amount : 2 ,
186
+ },
187
+ }, 2 },
188
+ },
159
189
}
160
190
161
191
for _ , tt := range tests {
@@ -182,7 +212,7 @@ func TestCache_TemplateUsers(t *testing.T) {
182
212
}
183
213
184
214
require .Eventuallyf (t , func () bool {
185
- _ , _ , ok := cache .TemplateDAUs (template .ID , 0 )
215
+ _ , _ , ok := cache .TemplateDAUs (template .ID , tt . tzOffset )
186
216
return ok
187
217
}, testutil .WaitShort , testutil .IntervalMedium ,
188
218
"TemplateDAUs never populated" ,
@@ -191,9 +221,9 @@ func TestCache_TemplateUsers(t *testing.T) {
191
221
gotUniqueUsers , ok := cache .TemplateUniqueUsers (template .ID )
192
222
require .True (t , ok )
193
223
194
- offset , gotEntries , ok := cache .TemplateDAUs (template .ID , 0 )
224
+ offset , gotEntries , ok := cache .TemplateDAUs (template .ID , tt . tzOffset )
195
225
require .True (t , ok )
196
- require .Equal (t , offset , 0 )
226
+ require .Equal (t , offset , tt . tzOffset )
197
227
require .Equal (t , tt .want .entries , gotEntries .Entries )
198
228
require .Equal (t , tt .want .uniqueUsers , gotUniqueUsers )
199
229
})
0 commit comments