Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 8bb7e17

Browse files
authored
chore!: remove GET workspaceagents/me/report-stats (#5530)
* chore!: remove GET workspaceagents/me/report-stats * Fix: tests
1 parent d124fab commit 8bb7e17

File tree

3 files changed

+0
-135
lines changed

3 files changed

+0
-135
lines changed

coderd/coderd.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -512,9 +512,6 @@ func New(options *Options) *API {
512512
r.Get("/gitsshkey", api.agentGitSSHKey)
513513
r.Get("/coordinate", api.workspaceAgentCoordinate)
514514
r.Post("/report-stats", api.workspaceAgentReportStats)
515-
// DEPRECATED in favor of the POST endpoint above.
516-
// TODO: remove in January 2023
517-
r.Get("/report-stats", api.workspaceAgentReportStatsWebsocket)
518515
})
519516
r.Route("/{workspaceagent}", func(r chi.Router) {
520517
r.Use(

coderd/coderdtest/authorize.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,6 @@ func AGPLRoutes(a *AuthTester) (map[string]string, map[string]RouteCheck) {
7171
"GET:/api/v2/workspaceagents/me/coordinate": {NoAuthorize: true},
7272
"POST:/api/v2/workspaceagents/me/version": {NoAuthorize: true},
7373
"POST:/api/v2/workspaceagents/me/app-health": {NoAuthorize: true},
74-
"GET:/api/v2/workspaceagents/me/report-stats": {NoAuthorize: true},
7574
"POST:/api/v2/workspaceagents/me/report-stats": {NoAuthorize: true},
7675

7776
// These endpoints have more assertions. This is good, add more endpoints to assert if you can!

coderd/workspaceagents.go

Lines changed: 0 additions & 131 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111
"net/http"
1212
"net/netip"
1313
"net/url"
14-
"reflect"
1514
"strconv"
1615
"strings"
1716
"time"
@@ -22,7 +21,6 @@ import (
2221
"golang.org/x/oauth2"
2322
"golang.org/x/xerrors"
2423
"nhooyr.io/websocket"
25-
"nhooyr.io/websocket/wsjson"
2624
"tailscale.com/tailcfg"
2725

2826
"cdr.dev/slog"
@@ -828,135 +826,6 @@ func (api *API) workspaceAgentReportStats(rw http.ResponseWriter, r *http.Reques
828826
})
829827
}
830828

831-
func (api *API) workspaceAgentReportStatsWebsocket(rw http.ResponseWriter, r *http.Request) {
832-
ctx := r.Context()
833-
834-
api.WebsocketWaitMutex.Lock()
835-
api.WebsocketWaitGroup.Add(1)
836-
api.WebsocketWaitMutex.Unlock()
837-
defer api.WebsocketWaitGroup.Done()
838-
839-
workspaceAgent := httpmw.WorkspaceAgent(r)
840-
workspace, err := api.Database.GetWorkspaceByAgentID(ctx, workspaceAgent.ID)
841-
if err != nil {
842-
httpapi.Write(ctx, rw, http.StatusBadRequest, codersdk.Response{
843-
Message: "Failed to get workspace.",
844-
Detail: err.Error(),
845-
})
846-
return
847-
}
848-
849-
conn, err := websocket.Accept(rw, r, &websocket.AcceptOptions{
850-
CompressionMode: websocket.CompressionDisabled,
851-
})
852-
if err != nil {
853-
httpapi.Write(ctx, rw, http.StatusBadRequest, codersdk.Response{
854-
Message: "Failed to accept websocket.",
855-
Detail: err.Error(),
856-
})
857-
return
858-
}
859-
go httpapi.Heartbeat(ctx, conn)
860-
861-
defer conn.Close(websocket.StatusGoingAway, "")
862-
863-
var lastReport codersdk.AgentStatsReportResponse
864-
latestStat, err := api.Database.GetLatestAgentStat(ctx, workspaceAgent.ID)
865-
if err == nil {
866-
err = json.Unmarshal(latestStat.Payload, &lastReport)
867-
if err != nil {
868-
api.Logger.Debug(ctx, "unmarshal stat payload", slog.Error(err))
869-
conn.Close(websocket.StatusInternalError, httpapi.WebsocketCloseSprintf("unmarshal stat payload: %s", err))
870-
return
871-
}
872-
}
873-
874-
// Allow overriding the stat interval for debugging and testing purposes.
875-
timer := time.NewTicker(api.AgentStatsRefreshInterval)
876-
defer timer.Stop()
877-
878-
go func() {
879-
for {
880-
err := wsjson.Write(ctx, conn, codersdk.AgentStatsReportRequest{})
881-
if err != nil {
882-
conn.Close(websocket.StatusInternalError, httpapi.WebsocketCloseSprintf("write report request: %s", err))
883-
return
884-
}
885-
886-
select {
887-
case <-timer.C:
888-
continue
889-
case <-ctx.Done():
890-
conn.Close(websocket.StatusNormalClosure, "")
891-
return
892-
}
893-
}
894-
}()
895-
896-
for {
897-
var rep codersdk.AgentStatsReportResponse
898-
err = wsjson.Read(ctx, conn, &rep)
899-
if err != nil {
900-
conn.Close(websocket.StatusInternalError, httpapi.WebsocketCloseSprintf("read report response: %s", err))
901-
return
902-
}
903-
904-
repJSON, err := json.Marshal(rep)
905-
if err != nil {
906-
api.Logger.Debug(ctx, "marshal stat json", slog.Error(err))
907-
conn.Close(websocket.StatusInternalError, httpapi.WebsocketCloseSprintf("marshal stat json: %s", err))
908-
return
909-
}
910-
911-
// Avoid inserting duplicate rows to preserve DB space.
912-
// We will see duplicate reports when on idle connections
913-
// (e.g. web terminal left open) or when there are no connections at
914-
// all.
915-
// We also don't want to update the workspace last used at on duplicate
916-
// reports.
917-
updateDB := !reflect.DeepEqual(lastReport, rep)
918-
919-
api.Logger.Debug(ctx, "read stats report",
920-
slog.F("interval", api.AgentStatsRefreshInterval),
921-
slog.F("agent", workspaceAgent.ID),
922-
slog.F("workspace", workspace.ID),
923-
slog.F("update_db", updateDB),
924-
slog.F("payload", rep),
925-
)
926-
927-
if updateDB {
928-
go activityBumpWorkspace(api.Logger.Named("activity_bump"), api.Database, workspace.ID)
929-
930-
lastReport = rep
931-
932-
_, err = api.Database.InsertAgentStat(ctx, database.InsertAgentStatParams{
933-
ID: uuid.New(),
934-
CreatedAt: database.Now(),
935-
AgentID: workspaceAgent.ID,
936-
WorkspaceID: workspace.ID,
937-
UserID: workspace.OwnerID,
938-
TemplateID: workspace.TemplateID,
939-
Payload: json.RawMessage(repJSON),
940-
})
941-
if err != nil {
942-
api.Logger.Debug(ctx, "insert agent stat", slog.Error(err))
943-
conn.Close(websocket.StatusInternalError, httpapi.WebsocketCloseSprintf("insert agent stat: %s", err))
944-
return
945-
}
946-
947-
err = api.Database.UpdateWorkspaceLastUsedAt(ctx, database.UpdateWorkspaceLastUsedAtParams{
948-
ID: workspace.ID,
949-
LastUsedAt: database.Now(),
950-
})
951-
if err != nil {
952-
api.Logger.Debug(ctx, "update workspace last used at", slog.Error(err))
953-
conn.Close(websocket.StatusInternalError, httpapi.WebsocketCloseSprintf("update workspace last used at: %s", err))
954-
return
955-
}
956-
}
957-
}
958-
}
959-
960829
func (api *API) postWorkspaceAppHealth(rw http.ResponseWriter, r *http.Request) {
961830
ctx := r.Context()
962831
workspaceAgent := httpmw.WorkspaceAgent(r)

0 commit comments

Comments
 (0)