Skip to content

Commit 08887b3

Browse files
authored
Merge pull request #133 from coder/fix-perf-issues-when-resolving-agents
fix: perf issues when resolving agents
2 parents f678a76 + 29fa2e7 commit 08887b3

File tree

3 files changed

+67
-18
lines changed

3 files changed

+67
-18
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
### Changed
1111
- renamed the plugin from `Coder Gateway` to `Gateway`
12+
- workspaces and agents are now resolved and displayed progressively
1213

1314
### Fixed
1415
- icon rendering on macOS

src/main/kotlin/com/coder/gateway/models/WorkspaceAgentModel.kt

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,28 @@ data class WorkspaceAgentModel(
1919
val agentOS: OS?,
2020
val agentArch: Arch?,
2121
val homeDirectory: String?
22-
)
22+
) {
23+
override fun equals(other: Any?): Boolean {
24+
if (this === other) return true
25+
if (javaClass != other?.javaClass) return false
26+
27+
other as WorkspaceAgentModel
28+
29+
if (workspaceID != other.workspaceID) return false
30+
if (workspaceName != other.workspaceName) return false
31+
if (name != other.name) return false
32+
if (templateID != other.templateID) return false
33+
if (templateName != other.templateName) return false
34+
35+
return true
36+
}
37+
38+
override fun hashCode(): Int {
39+
var result = workspaceID.hashCode()
40+
result = 31 * result + workspaceName.hashCode()
41+
result = 31 * result + name.hashCode()
42+
result = 31 * result + templateID.hashCode()
43+
result = 31 * result + templateName.hashCode()
44+
return result
45+
}
46+
}

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

Lines changed: 41 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ import kotlinx.coroutines.cancel
6464
import kotlinx.coroutines.delay
6565
import kotlinx.coroutines.isActive
6666
import kotlinx.coroutines.launch
67+
import kotlinx.coroutines.runBlocking
6768
import kotlinx.coroutines.withContext
6869
import org.zeroturnaround.exec.ProcessExecutor
6970
import java.awt.Component
@@ -399,12 +400,20 @@ class CoderWorkspacesStepView(val enableNextButtonCallback: (Boolean) -> Unit) :
399400

400401
val authTask = object : Task.Modal(null, CoderGatewayBundle.message("gateway.connector.view.coder.workspaces.cli.downloader.dialog.title"), false) {
401402
override fun run(pi: ProgressIndicator) {
402-
403403
pi.apply {
404404
isIndeterminate = false
405-
text = "Downloading coder cli..."
405+
text = "Retrieving Workspaces..."
406406
fraction = 0.1
407407
}
408+
runBlocking {
409+
loadWorkspaces()
410+
}
411+
412+
pi.apply {
413+
isIndeterminate = false
414+
text = "Downloading Coder CLI..."
415+
fraction = 0.3
416+
}
408417

409418
cliManager.downloadCLI()
410419
if (getOS() != OS.WINDOWS) {
@@ -413,7 +422,7 @@ class CoderWorkspacesStepView(val enableNextButtonCallback: (Boolean) -> Unit) :
413422
logger.info("chmod +x ${cliManager.localCli.toAbsolutePath()} $chmodOutput")
414423
}
415424
pi.apply {
416-
text = "Configuring coder cli..."
425+
text = "Configuring Coder CLI..."
417426
fraction = 0.5
418427
}
419428

@@ -424,7 +433,7 @@ class CoderWorkspacesStepView(val enableNextButtonCallback: (Boolean) -> Unit) :
424433
logger.info("Result of `${localWizardModel.localCliPath} config-ssh --yes --use-previous-options`: $sshConfigOutput")
425434

426435
pi.apply {
427-
text = "Remove old coder cli versions..."
436+
text = "Remove old Coder CLI versions..."
428437
fraction = 0.9
429438
}
430439
cliManager.removeOldCli()
@@ -467,35 +476,50 @@ class CoderWorkspacesStepView(val enableNextButtonCallback: (Boolean) -> Unit) :
467476

468477
poller = cs.launch {
469478
while (isActive) {
470-
loadWorkspaces()
471479
delay(5000)
480+
loadWorkspaces()
472481
}
473482
}
474483
}
475484

476485
private suspend fun loadWorkspaces() {
477-
val workspaceList = withContext(Dispatchers.IO) {
486+
withContext(Dispatchers.IO) {
487+
val timeBeforeRequestingWorkspaces = System.currentTimeMillis()
478488
try {
479-
return@withContext coderClient.workspaces().collectAgents()
489+
val ws = coderClient.workspaces()
490+
val timeAfterRequestingWorkspaces = System.currentTimeMillis()
491+
logger.info("Retrieving the workspaces took: ${timeAfterRequestingWorkspaces - timeBeforeRequestingWorkspaces} millis")
492+
ws.resolveAndDisplayAgents()
480493
} catch (e: Exception) {
481494
logger.error("Could not retrieve workspaces for ${coderClient.me.username} on ${coderClient.coderURL}. Reason: $e")
482-
emptyList()
483495
}
484496
}
497+
}
485498

486-
withContext(Dispatchers.Main) {
487-
val selectedWorkspace = tableOfWorkspaces.selectedObject?.name
488-
listTableModelOfWorkspaces.items = workspaceList
489-
if (selectedWorkspace != null) {
490-
tableOfWorkspaces.selectItem(selectedWorkspace)
499+
private fun List<Workspace>.resolveAndDisplayAgents() {
500+
this.forEach { workspace ->
501+
cs.launch(Dispatchers.IO) {
502+
val timeBeforeRequestingAgents = System.currentTimeMillis()
503+
workspace.agentModels().forEach { am ->
504+
withContext(Dispatchers.Main) {
505+
val selectedWorkspace = tableOfWorkspaces.selectedObject?.name
506+
if (listTableModelOfWorkspaces.indexOf(am) >= 0) {
507+
val index = listTableModelOfWorkspaces.indexOf(am)
508+
listTableModelOfWorkspaces.setItem(index, am)
509+
} else {
510+
listTableModelOfWorkspaces.addRow(am)
511+
}
512+
if (selectedWorkspace != null) {
513+
tableOfWorkspaces.selectItem(selectedWorkspace)
514+
}
515+
}
516+
}
517+
val timeAfterRequestingAgents = System.currentTimeMillis()
518+
logger.info("Retrieving the agents for ${workspace.name} took: ${timeAfterRequestingAgents - timeBeforeRequestingAgents} millis")
491519
}
492520
}
493521
}
494522

495-
private fun List<Workspace>.collectAgents(): List<WorkspaceAgentModel> {
496-
return this.flatMap { it.agentModels() }.toList()
497-
}
498-
499523
private fun Workspace.agentModels(): List<WorkspaceAgentModel> {
500524
return try {
501525
val agents = coderClient.workspaceAgentsByTemplate(this)

0 commit comments

Comments
 (0)