Skip to content

Commit c795a0e

Browse files
authored
feat: Fix Deployment DAUs to work with local timezones (#7647)
* chore: Add timezone param to DAU SQL query * Merge DAUs response * Pass time offsets to metricscache
1 parent 68658b5 commit c795a0e

File tree

29 files changed

+601
-305
lines changed

29 files changed

+601
-305
lines changed

coderd/apidoc/docs.go

Lines changed: 16 additions & 24 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/apidoc/swagger.json

Lines changed: 16 additions & 24 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/database/dbauthz/system.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -205,19 +205,19 @@ func (q *querier) GetTemplateAverageBuildTime(ctx context.Context, arg database.
205205
}
206206

207207
// Only used by metrics cache.
208-
func (q *querier) GetTemplateDAUs(ctx context.Context, templateID uuid.UUID) ([]database.GetTemplateDAUsRow, error) {
208+
func (q *querier) GetTemplateDAUs(ctx context.Context, arg database.GetTemplateDAUsParams) ([]database.GetTemplateDAUsRow, error) {
209209
if err := q.authorizeContext(ctx, rbac.ActionRead, rbac.ResourceSystem); err != nil {
210210
return nil, err
211211
}
212-
return q.db.GetTemplateDAUs(ctx, templateID)
212+
return q.db.GetTemplateDAUs(ctx, arg)
213213
}
214214

215215
// Only used by metrics cache.
216-
func (q *querier) GetDeploymentDAUs(ctx context.Context) ([]database.GetDeploymentDAUsRow, error) {
216+
func (q *querier) GetDeploymentDAUs(ctx context.Context, tzOffset int32) ([]database.GetDeploymentDAUsRow, error) {
217217
if err := q.authorizeContext(ctx, rbac.ActionRead, rbac.ResourceSystem); err != nil {
218218
return nil, err
219219
}
220-
return q.db.GetDeploymentDAUs(ctx)
220+
return q.db.GetDeploymentDAUs(ctx, tzOffset)
221221
}
222222

223223
// UpdateWorkspaceBuildCostByID is used by the provisioning system to update the cost of a workspace build.

coderd/database/dbfake/databasefake.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -435,21 +435,21 @@ func (q *fakeQuerier) InsertWorkspaceAgentStat(_ context.Context, p database.Ins
435435
return stat, nil
436436
}
437437

438-
func (q *fakeQuerier) GetTemplateDAUs(_ context.Context, templateID uuid.UUID) ([]database.GetTemplateDAUsRow, error) {
438+
func (q *fakeQuerier) GetTemplateDAUs(_ context.Context, arg database.GetTemplateDAUsParams) ([]database.GetTemplateDAUsRow, error) {
439439
q.mutex.RLock()
440440
defer q.mutex.RUnlock()
441441

442442
seens := make(map[time.Time]map[uuid.UUID]struct{})
443443

444444
for _, as := range q.workspaceAgentStats {
445-
if as.TemplateID != templateID {
445+
if as.TemplateID != arg.TemplateID {
446446
continue
447447
}
448448
if as.ConnectionCount == 0 {
449449
continue
450450
}
451451

452-
date := as.CreatedAt.Truncate(time.Hour * 24)
452+
date := as.CreatedAt.UTC().Add(time.Duration(arg.TzOffset) * time.Hour).Truncate(time.Hour * 24)
453453

454454
dateEntry := seens[date]
455455
if dateEntry == nil {
@@ -478,7 +478,7 @@ func (q *fakeQuerier) GetTemplateDAUs(_ context.Context, templateID uuid.UUID) (
478478
return rs, nil
479479
}
480480

481-
func (q *fakeQuerier) GetDeploymentDAUs(_ context.Context) ([]database.GetDeploymentDAUsRow, error) {
481+
func (q *fakeQuerier) GetDeploymentDAUs(_ context.Context, tzOffset int32) ([]database.GetDeploymentDAUsRow, error) {
482482
q.mutex.RLock()
483483
defer q.mutex.RUnlock()
484484

@@ -488,7 +488,7 @@ func (q *fakeQuerier) GetDeploymentDAUs(_ context.Context) ([]database.GetDeploy
488488
if as.ConnectionCount == 0 {
489489
continue
490490
}
491-
date := as.CreatedAt.Truncate(time.Hour * 24)
491+
date := as.CreatedAt.UTC().Add(time.Duration(tzOffset) * time.Hour).Truncate(time.Hour * 24)
492492

493493
dateEntry := seens[date]
494494
if dateEntry == nil {

coderd/database/dbmock/store.go

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/database/querier.go

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/database/queries.sql.go

Lines changed: 11 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/database/queries/workspaceagentstats.sql

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ VALUES
2424

2525
-- name: GetTemplateDAUs :many
2626
SELECT
27-
(created_at at TIME ZONE 'UTC')::date as date,
27+
(created_at at TIME ZONE cast(@tz_offset::integer as text))::date as date,
2828
user_id
2929
FROM
3030
workspace_agent_stats
@@ -38,7 +38,7 @@ ORDER BY
3838

3939
-- name: GetDeploymentDAUs :many
4040
SELECT
41-
(created_at at TIME ZONE 'UTC')::date as date,
41+
(created_at at TIME ZONE cast(@tz_offset::integer as text))::date as date,
4242
user_id
4343
FROM
4444
workspace_agent_stats

coderd/insights.go

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import (
1313
// @Security CoderSessionToken
1414
// @Produce json
1515
// @Tags Insights
16-
// @Success 200 {object} codersdk.DeploymentDAUsResponse
16+
// @Success 200 {object} codersdk.DAUsResponse
1717
// @Router /insights/daus [get]
1818
func (api *API) deploymentDAUs(rw http.ResponseWriter, r *http.Request) {
1919
ctx := r.Context()
@@ -22,9 +22,21 @@ func (api *API) deploymentDAUs(rw http.ResponseWriter, r *http.Request) {
2222
return
2323
}
2424

25-
resp, _ := api.metricsCache.DeploymentDAUs()
25+
vals := r.URL.Query()
26+
p := httpapi.NewQueryParamParser()
27+
tzOffset := p.Int(vals, 0, "tz_offset")
28+
p.ErrorExcessParams(vals)
29+
if len(p.Errors) > 0 {
30+
httpapi.Write(ctx, rw, http.StatusBadRequest, codersdk.Response{
31+
Message: "Query parameters have invalid values.",
32+
Validations: p.Errors,
33+
})
34+
return
35+
}
36+
37+
_, resp, _ := api.metricsCache.DeploymentDAUs(tzOffset)
2638
if resp == nil || resp.Entries == nil {
27-
httpapi.Write(ctx, rw, http.StatusOK, &codersdk.DeploymentDAUsResponse{
39+
httpapi.Write(ctx, rw, http.StatusOK, &codersdk.DAUsResponse{
2840
Entries: []codersdk.DAUEntry{},
2941
})
3042
return

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy