Skip to content

Commit cb975fc

Browse files
committed
fix user-activity response on no rows
1 parent 4cae730 commit cb975fc

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

coderd/insights.go

+14
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package coderd
22

33
import (
44
"context"
5+
"database/sql"
56
"fmt"
67
"net/http"
78
"strings"
@@ -102,6 +103,19 @@ func (api *API) insightsUserActivity(rw http.ResponseWriter, r *http.Request) {
102103
TemplateIDs: templateIDs,
103104
})
104105
if err != nil {
106+
// No data is not an error.
107+
if xerrors.Is(err, sql.ErrNoRows) {
108+
httpapi.Write(ctx, rw, http.StatusOK, codersdk.UserActivityInsightsResponse{
109+
Report: codersdk.UserActivityInsightsReport{
110+
StartTime: startTime,
111+
EndTime: endTime,
112+
TemplateIDs: []uuid.UUID{},
113+
Users: []codersdk.UserActivity{},
114+
},
115+
})
116+
return
117+
}
118+
// Check authorization.
105119
if httpapi.Is404Error(err) {
106120
httpapi.ResourceNotFound(rw)
107121
return

coderd/insights_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ func TestUserActivityInsights_SanityCheck(t *testing.T) {
136136
Pubsub: ps,
137137
Logger: &logger,
138138
IncludeProvisionerDaemon: true,
139-
AgentStatsRefreshInterval: time.Millisecond * 50,
139+
AgentStatsRefreshInterval: time.Millisecond * 100,
140140
DatabaseRolluper: dbrollup.New(
141141
logger.Named("dbrollup"),
142142
db,
@@ -170,7 +170,7 @@ func TestUserActivityInsights_SanityCheck(t *testing.T) {
170170
y, m, d := time.Now().UTC().Date()
171171
today := time.Date(y, m, d, 0, 0, 0, 0, time.UTC)
172172

173-
ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitLong)
173+
ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitSuperLong)
174174
defer cancel()
175175

176176
// Connect to the agent to generate usage/latency stats.
@@ -212,7 +212,7 @@ func TestUserActivityInsights_SanityCheck(t *testing.T) {
212212
return false
213213
}
214214
return len(userActivities.Report.Users) > 0 && userActivities.Report.Users[0].Seconds > 0
215-
}, testutil.WaitMedium, testutil.IntervalFast, "user activity is missing")
215+
}, testutil.WaitSuperLong, testutil.IntervalMedium, "user activity is missing")
216216

217217
// We got our latency data, close the connection.
218218
_ = sess.Close()

0 commit comments

Comments
 (0)