Skip to content

Commit 7a45360

Browse files
authored
feat: support order property of coder_agent (#12121)
1 parent c66e665 commit 7a45360

21 files changed

+378
-276
lines changed

coderd/database/dbgen/dbgen.go

+1
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@ func WorkspaceAgent(t testing.TB, db database.Store, orig database.WorkspaceAgen
176176
TroubleshootingURL: takeFirst(orig.TroubleshootingURL, "https://example.com"),
177177
MOTDFile: takeFirst(orig.TroubleshootingURL, ""),
178178
DisplayApps: append([]database.DisplayApp{}, orig.DisplayApps...),
179+
DisplayOrder: takeFirst(orig.DisplayOrder, 1),
179180
})
180181
require.NoError(t, err, "insert workspace agent")
181182
return agt

coderd/database/dbmem/dbmem.go

+1
Original file line numberDiff line numberDiff line change
@@ -5648,6 +5648,7 @@ func (q *FakeQuerier) InsertWorkspaceAgent(_ context.Context, arg database.Inser
56485648
MOTDFile: arg.MOTDFile,
56495649
LifecycleState: database.WorkspaceAgentLifecycleStateCreated,
56505650
DisplayApps: arg.DisplayApps,
5651+
DisplayOrder: arg.DisplayOrder,
56515652
}
56525653

56535654
q.workspaceAgents = append(q.workspaceAgents, agent)

coderd/database/dump.sql

+3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ALTER TABLE workspace_agents DROP COLUMN display_order;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
ALTER TABLE workspace_agents ADD COLUMN display_order integer NOT NULL DEFAULT 0;
2+
3+
COMMENT ON COLUMN workspace_agents.display_order
4+
IS 'Specifies the order in which to display agents in user interfaces.';

coderd/database/models.go

+2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/database/queries.sql.go

+18-8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/database/queries/workspaceagents.sql

+3-2
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,11 @@ INSERT INTO
4646
connection_timeout_seconds,
4747
troubleshooting_url,
4848
motd_file,
49-
display_apps
49+
display_apps,
50+
display_order
5051
)
5152
VALUES
52-
($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17) RETURNING *;
53+
($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17, $18) RETURNING *;
5354

5455
-- name: UpdateWorkspaceAgentConnectionByID :exec
5556
UPDATE

coderd/provisionerdserver/provisionerdserver.go

+1
Original file line numberDiff line numberDiff line change
@@ -1524,6 +1524,7 @@ func InsertWorkspaceResource(ctx context.Context, db database.Store, jobID uuid.
15241524
DisplayApps: convertDisplayApps(prAgent.GetDisplayApps()),
15251525
InstanceMetadata: pqtype.NullRawMessage{},
15261526
ResourceMetadata: pqtype.NullRawMessage{},
1527+
DisplayOrder: int32(prAgent.Order),
15271528
})
15281529
if err != nil {
15291530
return xerrors.Errorf("insert agent: %w", err)

coderd/workspacebuilds.go

+23
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ import (
55
"database/sql"
66
"errors"
77
"fmt"
8+
"math"
89
"net/http"
10+
"sort"
911
"strconv"
1012
"time"
1113

@@ -905,10 +907,22 @@ func (api *API) convertWorkspaceBuild(
905907

906908
resources := resourcesByJobID[job.ProvisionerJob.ID]
907909
apiResources := make([]codersdk.WorkspaceResource, 0)
910+
resourceAgentsMinOrder := map[uuid.UUID]int32{} // map[resource.ID]minOrder
908911
for _, resource := range resources {
909912
agents := agentsByResourceID[resource.ID]
913+
sort.Slice(agents, func(i, j int) bool {
914+
if agents[i].DisplayOrder != agents[j].DisplayOrder {
915+
return agents[i].DisplayOrder < agents[j].DisplayOrder
916+
}
917+
return agents[i].Name < agents[j].Name
918+
})
919+
910920
apiAgents := make([]codersdk.WorkspaceAgent, 0)
921+
resourceAgentsMinOrder[resource.ID] = math.MaxInt32
922+
911923
for _, agent := range agents {
924+
resourceAgentsMinOrder[resource.ID] = min(resourceAgentsMinOrder[resource.ID], agent.DisplayOrder)
925+
912926
apps := appsByAgentID[agent.ID]
913927
scripts := scriptsByAgentID[agent.ID]
914928
logSources := logSourcesByAgentID[agent.ID]
@@ -924,6 +938,15 @@ func (api *API) convertWorkspaceBuild(
924938
metadata := append(make([]database.WorkspaceResourceMetadatum, 0), metadataByResourceID[resource.ID]...)
925939
apiResources = append(apiResources, convertWorkspaceResource(resource, apiAgents, metadata))
926940
}
941+
sort.Slice(apiResources, func(i, j int) bool {
942+
orderI := resourceAgentsMinOrder[apiResources[i].ID]
943+
orderJ := resourceAgentsMinOrder[apiResources[j].ID]
944+
if orderI != orderJ {
945+
return orderI < orderJ
946+
}
947+
return apiResources[i].Name < apiResources[j].Name
948+
})
949+
927950
apiJob := convertProvisionerJob(job)
928951
transition := codersdk.WorkspaceTransition(build.Transition)
929952
return codersdk.WorkspaceBuild{

0 commit comments

Comments
 (0)