Skip to content

Commit 5b9fdcb

Browse files
committed
Break out toAgentModels
We will need this in the recent connections view as well.
1 parent e743907 commit 5b9fdcb

File tree

2 files changed

+60
-60
lines changed

2 files changed

+60
-60
lines changed

src/main/kotlin/com/coder/gateway/sdk/v2/models/Workspace.kt

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
package com.coder.gateway.sdk.v2.models
22

3+
import com.coder.gateway.models.WorkspaceAgentModel
4+
import com.coder.gateway.models.WorkspaceAndAgentStatus
5+
import com.coder.gateway.models.WorkspaceVersionStatus
6+
import com.coder.gateway.sdk.Arch
7+
import com.coder.gateway.sdk.OS
38
import com.google.gson.annotations.SerializedName
49
import java.time.Instant
510
import java.util.UUID
@@ -24,4 +29,48 @@ data class Workspace(
2429
@SerializedName("autostart_schedule") val autostartSchedule: String?,
2530
@SerializedName("ttl_ms") val ttlMillis: Long?,
2631
@SerializedName("last_used_at") val lastUsedAt: Instant,
27-
)
32+
)
33+
34+
fun Workspace.toAgentModels(): Set<WorkspaceAgentModel> {
35+
val wam = this.latestBuild.resources.filter { it.agents != null }.flatMap { it.agents!! }.map { agent ->
36+
val workspaceWithAgentName = "${this.name}.${agent.name}"
37+
val wm = WorkspaceAgentModel(
38+
this.id,
39+
this.name,
40+
workspaceWithAgentName,
41+
this.templateID,
42+
this.templateName,
43+
this.templateIcon,
44+
null,
45+
WorkspaceVersionStatus.from(this),
46+
this.latestBuild.status,
47+
WorkspaceAndAgentStatus.from(this, agent),
48+
this.latestBuild.transition,
49+
OS.from(agent.operatingSystem),
50+
Arch.from(agent.architecture),
51+
agent.expandedDirectory ?: agent.directory,
52+
)
53+
54+
wm
55+
}.toSet()
56+
if (wam.isNullOrEmpty()) {
57+
val wm = WorkspaceAgentModel(
58+
this.id,
59+
this.name,
60+
this.name,
61+
this.templateID,
62+
this.templateName,
63+
this.templateIcon,
64+
null,
65+
WorkspaceVersionStatus.from(this),
66+
this.latestBuild.status,
67+
WorkspaceAndAgentStatus.from(this),
68+
this.latestBuild.transition,
69+
null,
70+
null,
71+
null
72+
)
73+
return setOf(wm)
74+
}
75+
return wam
76+
}

src/main/kotlin/com/coder/gateway/views/steps/CoderWorkspacesStepView.kt

Lines changed: 10 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import com.coder.gateway.models.TokenSource
77
import com.coder.gateway.models.WorkspaceAgentModel
88
import com.coder.gateway.models.WorkspaceAndAgentStatus
99
import com.coder.gateway.models.WorkspaceVersionStatus
10-
import com.coder.gateway.sdk.Arch
1110
import com.coder.gateway.sdk.CoderCLIManager
1211
import com.coder.gateway.sdk.CoderRestClientService
1312
import com.coder.gateway.sdk.CoderSemVer
@@ -20,8 +19,8 @@ import com.coder.gateway.sdk.ex.AuthenticationResponseException
2019
import com.coder.gateway.sdk.ex.TemplateResponseException
2120
import com.coder.gateway.sdk.ex.WorkspaceResponseException
2221
import com.coder.gateway.sdk.toURL
23-
import com.coder.gateway.sdk.v2.models.Workspace
2422
import com.coder.gateway.sdk.v2.models.WorkspaceStatus
23+
import com.coder.gateway.sdk.v2.models.toAgentModels
2524
import com.coder.gateway.sdk.withPath
2625
import com.coder.gateway.services.CoderSettingsState
2726
import com.intellij.ide.ActivityTracker
@@ -659,7 +658,15 @@ class CoderWorkspacesStepView(val setNextButtonEnabled: (Boolean) -> Unit) : Cod
659658
val timeBeforeRequestingWorkspaces = System.currentTimeMillis()
660659
try {
661660
val ws = clientService.client.workspaces()
662-
val ams = ws.flatMap { it.toAgentModels() }.toSet()
661+
val ams = ws.flatMap { it.toAgentModels() }
662+
ams.forEach {
663+
cs.launch(Dispatchers.IO) {
664+
it.templateIcon = iconDownloader.load(it.templateIconPath, it.name)
665+
withContext(Dispatchers.Main) {
666+
tableOfWorkspaces.updateUI()
667+
}
668+
}
669+
}
663670
val timeAfterRequestingWorkspaces = System.currentTimeMillis()
664671
logger.info("Retrieving the workspaces took: ${timeAfterRequestingWorkspaces - timeBeforeRequestingWorkspaces} millis")
665672
return@withContext ams
@@ -675,62 +682,6 @@ class CoderWorkspacesStepView(val setNextButtonEnabled: (Boolean) -> Unit) : Cod
675682
}
676683
}
677684

678-
private fun Workspace.toAgentModels(): Set<WorkspaceAgentModel> {
679-
val wam = this.latestBuild.resources.filter { it.agents != null }.flatMap { it.agents!! }.map { agent ->
680-
val workspaceWithAgentName = "${this.name}.${agent.name}"
681-
val wm = WorkspaceAgentModel(
682-
this.id,
683-
this.name,
684-
workspaceWithAgentName,
685-
this.templateID,
686-
this.templateName,
687-
this.templateIcon,
688-
null,
689-
WorkspaceVersionStatus.from(this),
690-
this.latestBuild.status,
691-
WorkspaceAndAgentStatus.from(this, agent),
692-
this.latestBuild.transition,
693-
OS.from(agent.operatingSystem),
694-
Arch.from(agent.architecture),
695-
agent.expandedDirectory ?: agent.directory,
696-
)
697-
cs.launch(Dispatchers.IO) {
698-
wm.templateIcon = iconDownloader.load(wm.templateIconPath, wm.name)
699-
withContext(Dispatchers.Main) {
700-
tableOfWorkspaces.updateUI()
701-
}
702-
}
703-
wm
704-
}.toSet()
705-
706-
if (wam.isNullOrEmpty()) {
707-
val wm = WorkspaceAgentModel(
708-
this.id,
709-
this.name,
710-
this.name,
711-
this.templateID,
712-
this.templateName,
713-
this.templateIcon,
714-
null,
715-
WorkspaceVersionStatus.from(this),
716-
this.latestBuild.status,
717-
WorkspaceAndAgentStatus.from(this),
718-
this.latestBuild.transition,
719-
null,
720-
null,
721-
null
722-
)
723-
cs.launch(Dispatchers.IO) {
724-
wm.templateIcon = iconDownloader.load(wm.templateIconPath, wm.name)
725-
withContext(Dispatchers.Main) {
726-
tableOfWorkspaces.updateUI()
727-
}
728-
}
729-
return setOf(wm)
730-
}
731-
return wam
732-
}
733-
734685
override fun onPrevious() {
735686
super.onPrevious()
736687
logger.info("Going back to the main view")

0 commit comments

Comments
 (0)