Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
fix user-activity response on no rows
  • Loading branch information
mafredri committed Mar 25, 2024
commit cb975fcdc1b4bf05ed6e13b3aefc8550f54dc909
14 changes: 14 additions & 0 deletions coderd/insights.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package coderd

import (
"context"
"database/sql"
"fmt"
"net/http"
"strings"
Expand Down Expand Up @@ -102,6 +103,19 @@ func (api *API) insightsUserActivity(rw http.ResponseWriter, r *http.Request) {
TemplateIDs: templateIDs,
})
if err != nil {
// No data is not an error.
if xerrors.Is(err, sql.ErrNoRows) {
httpapi.Write(ctx, rw, http.StatusOK, codersdk.UserActivityInsightsResponse{
Report: codersdk.UserActivityInsightsReport{
StartTime: startTime,
EndTime: endTime,
TemplateIDs: []uuid.UUID{},
Users: []codersdk.UserActivity{},
},
})
return
}
// Check authorization.
if httpapi.Is404Error(err) {
httpapi.ResourceNotFound(rw)
return
Expand Down
6 changes: 3 additions & 3 deletions coderd/insights_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ func TestUserActivityInsights_SanityCheck(t *testing.T) {
Pubsub: ps,
Logger: &logger,
IncludeProvisionerDaemon: true,
AgentStatsRefreshInterval: time.Millisecond * 50,
AgentStatsRefreshInterval: time.Millisecond * 100,
DatabaseRolluper: dbrollup.New(
logger.Named("dbrollup"),
db,
Expand Down Expand Up @@ -170,7 +170,7 @@ func TestUserActivityInsights_SanityCheck(t *testing.T) {
y, m, d := time.Now().UTC().Date()
today := time.Date(y, m, d, 0, 0, 0, 0, time.UTC)

ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitLong)
ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitSuperLong)
defer cancel()

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

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