4
4
"encoding/json"
5
5
"net/http"
6
6
"os"
7
+ "reflect"
7
8
"strconv"
8
9
"time"
9
10
@@ -19,8 +20,6 @@ import (
19
20
"github.com/coder/coder/codersdk"
20
21
)
21
22
22
- const AgentStatIntervalEnv = "CODER_AGENT_STAT_INTERVAL"
23
-
24
23
func FillEmptyDAUDays (rows []database.GetDAUsFromAgentStatsRow ) []database.GetDAUsFromAgentStatsRow {
25
24
var newRows []database.GetDAUsFromAgentStatsRow
26
25
@@ -77,6 +76,8 @@ func (api *API) daus(rw http.ResponseWriter, r *http.Request) {
77
76
httpapi .Write (rw , http .StatusOK , resp )
78
77
}
79
78
79
+ const AgentStatIntervalEnv = "CODER_AGENT_STAT_INTERVAL"
80
+
80
81
func (api * API ) workspaceAgentReportStats (rw http.ResponseWriter , r * http.Request ) {
81
82
api .websocketWaitMutex .Lock ()
82
83
api .websocketWaitGroup .Add (1 )
@@ -123,6 +124,7 @@ func (api *API) workspaceAgentReportStats(rw http.ResponseWriter, r *http.Reques
123
124
}
124
125
defer conn .Close (websocket .StatusAbnormalClosure , "" )
125
126
127
+ // The complexity of the DAU query is inversely proportional to this interval.
126
128
var interval = time .Minute
127
129
128
130
// Allow overriding the stat interval for debugging and testing purposes.
@@ -141,6 +143,7 @@ func (api *API) workspaceAgentReportStats(rw http.ResponseWriter, r *http.Reques
141
143
142
144
ctx := r .Context ()
143
145
timer := time .NewTicker (interval )
146
+ var lastReport codersdk.AgentStatsReportResponse
144
147
for {
145
148
err := wsjson .Write (ctx , conn , codersdk.AgentStatsReportRequest {})
146
149
if err != nil {
@@ -170,15 +173,20 @@ func (api *API) workspaceAgentReportStats(rw http.ResponseWriter, r *http.Reques
170
173
return
171
174
}
172
175
176
+ // Avoid inserting duplicate rows to preserve DB space.
177
+ var insert = ! reflect .DeepEqual (lastReport , rep )
178
+
173
179
api .Logger .Debug (ctx , "read stats report" ,
174
180
slog .F ("agent" , workspaceAgent .ID ),
175
181
slog .F ("resource" , resource .ID ),
176
182
slog .F ("workspace" , workspace .ID ),
183
+ slog .F ("insert" , insert ),
177
184
slog .F ("payload" , rep ),
178
185
)
179
186
180
- // Avoid inserting empty rows to preserve DB space.
181
- if len (rep .ProtocolStats ) > 0 {
187
+ if insert {
188
+ lastReport = rep
189
+
182
190
_ , err = api .Database .InsertAgentStat (ctx , database.InsertAgentStatParams {
183
191
ID : uuid .NewString (),
184
192
CreatedAt : time .Now (),
0 commit comments