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

Skip to content

Commit 28d6db5

Browse files
committed
Add deployment stats endpoint
1 parent ddf9841 commit 28d6db5

File tree

17 files changed

+494
-108
lines changed

17 files changed

+494
-108
lines changed

cli/deployment/config.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -406,7 +406,7 @@ func newConfig() *codersdk.DeploymentConfig {
406406
Usage: "How frequently agent stats are recorded",
407407
Flag: "agent-stats-refresh-interval",
408408
Hidden: true,
409-
Default: 10 * time.Minute,
409+
Default: time.Minute,
410410
},
411411
AgentFallbackTroubleshootingURL: &codersdk.DeploymentConfigField[string]{
412412
Name: "Agent Fallback Troubleshooting URL",

coderd/apidoc/docs.go

Lines changed: 99 additions & 25 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: 91 additions & 21 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/coderd.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -398,15 +398,16 @@ func New(options *Options) *API {
398398
r.Post("/csp/reports", api.logReportCSPViolations)
399399

400400
r.Get("/buildinfo", buildInfo)
401+
r.Route("/deployment", func(r chi.Router) {
402+
r.Use(apiKeyMiddleware)
403+
r.Get("/config", api.deploymentConfig)
404+
r.Get("/stats", api.deploymentStats)
405+
})
401406
r.Route("/experiments", func(r chi.Router) {
402407
r.Use(apiKeyMiddleware)
403408
r.Get("/", api.handleExperimentsGet)
404409
})
405410
r.Get("/updatecheck", api.updateCheck)
406-
r.Route("/config", func(r chi.Router) {
407-
r.Use(apiKeyMiddleware)
408-
r.Get("/deployment", api.deploymentConfig)
409-
})
410411
r.Route("/audit", func(r chi.Router) {
411412
r.Use(
412413
apiKeyMiddleware,

coderd/database/dbauthz/system.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,10 @@ func (q *querier) DeleteOldWorkspaceAgentStats(ctx context.Context) error {
201201
return q.db.DeleteOldWorkspaceAgentStats(ctx)
202202
}
203203

204+
func (q *querier) GetDeploymentWorkspaceAgentStats(ctx context.Context, createdAfter time.Time) (database.GetDeploymentWorkspaceAgentStatsRow, error) {
205+
return q.db.GetDeploymentWorkspaceAgentStats(ctx, createdAfter)
206+
}
207+
204208
func (q *querier) GetParameterSchemasCreatedAfter(ctx context.Context, createdAt time.Time) ([]database.ParameterSchema, error) {
205209
return q.db.GetParameterSchemasCreatedAfter(ctx, createdAt)
206210
}

coderd/database/dbfake/databasefake.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,9 @@ func (q *fakeQuerier) GetDeploymentWorkspaceAgentStats(_ context.Context, create
276276

277277
latestAgentStats := map[uuid.UUID]database.WorkspaceAgentStat{}
278278
for _, agentStat := range q.workspaceAgentStats {
279-
latestAgentStats[agentStat.AgentID] = agentStat
279+
if agentStat.CreatedAt.After(createdAfter) {
280+
latestAgentStats[agentStat.AgentID] = agentStat
281+
}
280282
}
281283

282284
stat := database.GetDeploymentWorkspaceAgentStatsRow{}

coderd/deployment.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package coderd
2+
3+
import (
4+
"net/http"
5+
6+
"github.com/coder/coder/coderd/httpapi"
7+
"github.com/coder/coder/coderd/rbac"
8+
"github.com/coder/coder/codersdk"
9+
)
10+
11+
// @Summary Get deployment config
12+
// @ID get-deployment-config
13+
// @Security CoderSessionToken
14+
// @Produce json
15+
// @Tags General
16+
// @Success 200 {object} codersdk.DeploymentConfig
17+
// @Router /deployment/config [get]
18+
func (api *API) deploymentConfig(rw http.ResponseWriter, r *http.Request) {
19+
if !api.Authorize(r, rbac.ActionRead, rbac.ResourceDeploymentConfig) {
20+
httpapi.Forbidden(rw)
21+
return
22+
}
23+
24+
httpapi.Write(r.Context(), rw, http.StatusOK, api.DeploymentConfig)
25+
}
26+
27+
// @Summary Get deployment stats
28+
// @ID get-deployment-stats
29+
// @Security CoderSessionToken
30+
// @Produce json
31+
// @Tags General
32+
// @Success 200 {object} codersdk.DeploymentStats
33+
// @Router /deployment/stats [get]
34+
func (api *API) deploymentStats(rw http.ResponseWriter, r *http.Request) {
35+
if !api.Authorize(r, rbac.ActionRead, rbac.ResourceDeploymentConfig) {
36+
httpapi.Forbidden(rw)
37+
return
38+
}
39+
40+
stats, ok := api.metricsCache.DeploymentStats()
41+
if !ok {
42+
httpapi.Write(r.Context(), rw, http.StatusBadRequest, codersdk.Response{
43+
Message: "Deployment stats are still processing!",
44+
})
45+
return
46+
}
47+
48+
httpapi.Write(r.Context(), rw, http.StatusOK, stats)
49+
}

0 commit comments

Comments
 (0)