From 3395a05327f16b2ea1f2574080769cecb6746518 Mon Sep 17 00:00:00 2001 From: Andrea Falzetti Date: Wed, 31 Aug 2022 23:25:54 +0000 Subject: [PATCH] feat(jetbrains): show workspace class in backend control center --- .../gitpod/jetbrains/remote/GitpodManager.kt | 5 +- .../remote/GitpodMetricControlProvider.kt | 61 +++++++++++++++++-- .../jetbrains/remote/GitpodMetricProvider.kt | 22 ++++++- 3 files changed, 77 insertions(+), 11 deletions(-) diff --git a/components/ide/jetbrains/backend-plugin/src/main/kotlin/io/gitpod/jetbrains/remote/GitpodManager.kt b/components/ide/jetbrains/backend-plugin/src/main/kotlin/io/gitpod/jetbrains/remote/GitpodManager.kt index 46a51be5706f17..4204ba401487ff 100644 --- a/components/ide/jetbrains/backend-plugin/src/main/kotlin/io/gitpod/jetbrains/remote/GitpodManager.kt +++ b/components/ide/jetbrains/backend-plugin/src/main/kotlin/io/gitpod/jetbrains/remote/GitpodManager.kt @@ -223,14 +223,16 @@ class GitpodManager : Disposable { } } + var infoResponse: WorkspaceInfoResponse? = null val pendingInfo = CompletableFuture() + private val infoJob = GlobalScope.launch { if (application.isHeadlessEnvironment) { return@launch } try { // TODO(ak) replace retry with proper handling of grpc errors - val infoResponse = retry(3) { + infoResponse = retry(3) { InfoServiceGrpc .newFutureStub(supervisorChannel) .workspaceInfo(Info.WorkspaceInfoRequest.newBuilder().build()) @@ -391,5 +393,4 @@ class GitpodManager : Disposable { metricsJob.cancel() } } - } diff --git a/components/ide/jetbrains/backend-plugin/src/main/kotlin/io/gitpod/jetbrains/remote/GitpodMetricControlProvider.kt b/components/ide/jetbrains/backend-plugin/src/main/kotlin/io/gitpod/jetbrains/remote/GitpodMetricControlProvider.kt index 59957e844276b1..75d744c2db230d 100644 --- a/components/ide/jetbrains/backend-plugin/src/main/kotlin/io/gitpod/jetbrains/remote/GitpodMetricControlProvider.kt +++ b/components/ide/jetbrains/backend-plugin/src/main/kotlin/io/gitpod/jetbrains/remote/GitpodMetricControlProvider.kt @@ -7,20 +7,69 @@ package io.gitpod.jetbrains.remote import com.jetbrains.ide.model.uiautomation.BeControl import com.jetbrains.rd.ui.bedsl.dsl.VerticalGridBuilder import com.jetbrains.rd.ui.bedsl.dsl.verticalGrid +import com.jetbrains.rd.ui.bedsl.dsl.* import com.jetbrains.rd.util.lifetime.Lifetime -import com.jetbrains.rd.util.reactive.Property import com.jetbrains.rdserver.diagnostics.BackendDiagnosticsService import com.jetbrains.rdserver.unattendedHost.customization.controlCenter.performance.MetricControlProvider import com.jetbrains.rdserver.unattendedHost.customization.controlCenter.performance.createProgressBar import com.jetbrains.rdserver.unattendedHost.customization.controlCenter.performance.createProgressRow +import com.jetbrains.rd.util.reactive.Property class GitpodMetricControlProvider : MetricControlProvider { override val id: String = "gitpodMetricsControl" override fun getControl(lifetime: Lifetime): BeControl { + val backendDiagnosticsService = BackendDiagnosticsService.Companion.getInstance() + return verticalGrid { - val backendDiagnosticsService = BackendDiagnosticsService.Companion.getInstance() - createCpuControl(this, backendDiagnosticsService, lifetime) - createMemoryControl(this, backendDiagnosticsService, lifetime) + row { + horizontalGrid { + column { + label("Workspace") + } + }.withMargin { margin(0, 0, 0, 10) } + } + createWorkspaceClassControlRow(this, backendDiagnosticsService, lifetime) + row { + verticalGrid { + createCpuControl(this, backendDiagnosticsService, lifetime) + createMemoryControl(this, backendDiagnosticsService, lifetime) + }.withMargin { margin(0, 0, 0, 10) } + } + row { + horizontalGrid { + column { + label("Node") + } + }.withMargin { margin(0, 0, 0, 10) } + } + } + } + + private fun createWorkspaceClassControlRow(ctx: VerticalGridBuilder, backendDiagnosticsService: BackendDiagnosticsService, lifetime: Lifetime) { + val labelProperty = Property("") + + val workspaceClassMetric = backendDiagnosticsService.getMetric("gitpod_workspace_class") + val workspaceClass = workspaceClassMetric.toString() + + if (workspaceClass == "") { + return + } + + fun updateLabel() { + labelProperty.set(workspaceClass) + } + updateLabel() + + workspaceClassMetric.valueProperty.change.advise(lifetime) { + updateLabel() + } + + return ctx.row { + horizontalGrid { + column { + label("Class: $workspaceClass") + } + }.withMargin { margin(0, 0, 0, 10) } } } @@ -29,7 +78,7 @@ class GitpodMetricControlProvider : MetricControlProvider { val cpuTotal = backendDiagnosticsService.getMetric("gitpod_workspace_cpu_total") val cpuPercentage = backendDiagnosticsService.getMetric("gitpod_workspace_cpu_percentage") val cpuPercentageProperty = Property("$cpuPercentage %") - val label = "Workspace CPU" + val label = "CPU" val progressBar = createProgressBar(lifetime, cpuPercentage.valueProperty, cpuPercentageProperty) val labelProperty = Property("") @@ -51,7 +100,7 @@ class GitpodMetricControlProvider : MetricControlProvider { val memoryTotal = backendDiagnosticsService.getMetric("gitpod_workspace_memory_total") val memoryPercentage = backendDiagnosticsService.getMetric("gitpod_workspace_memory_percentage") val memoryPercentageProperty = Property("$memoryPercentage %") - val label = "Workspace Memory" + val label = "Memory" val progressBar = createProgressBar(lifetime, memoryPercentage.valueProperty, memoryPercentageProperty) val labelProperty = Property("") diff --git a/components/ide/jetbrains/backend-plugin/src/main/kotlin/io/gitpod/jetbrains/remote/GitpodMetricProvider.kt b/components/ide/jetbrains/backend-plugin/src/main/kotlin/io/gitpod/jetbrains/remote/GitpodMetricProvider.kt index 6d0e48c93a6445..7381466cd681bb 100644 --- a/components/ide/jetbrains/backend-plugin/src/main/kotlin/io/gitpod/jetbrains/remote/GitpodMetricProvider.kt +++ b/components/ide/jetbrains/backend-plugin/src/main/kotlin/io/gitpod/jetbrains/remote/GitpodMetricProvider.kt @@ -9,7 +9,7 @@ import com.jetbrains.rd.platform.codeWithMe.unattendedHost.metrics.Metric import com.jetbrains.rd.platform.codeWithMe.unattendedHost.metrics.MetricType import com.jetbrains.rd.platform.codeWithMe.unattendedHost.metrics.MetricsStatus import com.jetbrains.rd.platform.codeWithMe.unattendedHost.metrics.providers.MetricProvider -import io.gitpod.jetbrains.remote.GitpodManager +import io.gitpod.supervisor.api.Info.WorkspaceInfoResponse.WorkspaceClass import io.gitpod.supervisor.api.Status.ResourceStatusSeverity import kotlin.math.roundToInt @@ -19,6 +19,7 @@ class GitpodMetricProvider: MetricProvider { override val id: String = "gitpodMetricsProvider" override fun getMetrics(): Map { val resourceStatus = manager.resourceStatus + val info = manager.infoResponse val cpuUsed = resourceStatus?.cpu?.used?.toDouble() ?: 0.0 val cpuTotal = resourceStatus?.cpu?.limit?.toDouble() ?: 0.0 @@ -28,17 +29,20 @@ class GitpodMetricProvider: MetricProvider { val memoryUsed = convertBytesToGB(resourceStatus?.memory?.used ?: 0) val memoryTotal = convertBytesToGB(resourceStatus?.memory?.limit ?: 0) - val memorySeverity = resourceStatus?.memory?.severity ?:ResourceStatusSeverity.normal + val memorySeverity = resourceStatus?.memory?.severity ?: ResourceStatusSeverity.normal val memoryPercentage = (memoryUsed / memoryTotal) * 100 val memoryStatus = getSeverityStatus(memorySeverity) + val workspaceClass = formatWorkspaceClass(info?.workspaceClass) + return mapOf( "gitpod_workspace_cpu_used" to Metric(MetricType.PERFORMANCE, MetricsStatus.NORMAL, roundTo(cpuUsed, 0)), "gitpod_workspace_cpu_total" to Metric(MetricType.PERFORMANCE, MetricsStatus.NORMAL, roundTo(cpuTotal, 0)), "gitpod_workspace_cpu_percentage" to Metric(MetricType.PERFORMANCE, cpuStatus, (cpuPercentage * 1000.0).roundToInt() / 1000.0), "gitpod_workspace_memory_used" to Metric(MetricType.PERFORMANCE, MetricsStatus.NORMAL, roundTo(memoryUsed, 2)), "gitpod_workspace_memory_total" to Metric(MetricType.PERFORMANCE, MetricsStatus.NORMAL, roundTo(memoryTotal, 2)), - "gitpod_workspace_memory_percentage" to Metric(MetricType.PERFORMANCE, memoryStatus, (memoryPercentage * 1000.0).roundToInt() / 1000.0) + "gitpod_workspace_memory_percentage" to Metric(MetricType.PERFORMANCE, memoryStatus, (memoryPercentage * 1000.0).roundToInt() / 1000.0), + "gitpod_workspace_class" to Metric(MetricType.OTHER, MetricsStatus.NORMAL, workspaceClass) ) } @@ -59,4 +63,16 @@ class GitpodMetricProvider: MetricProvider { MetricsStatus.NORMAL } } + + private fun formatWorkspaceClass(workspaceClass: WorkspaceClass?): String { + if (workspaceClass == null || workspaceClass.displayName == "") { + return "" + } + + if (workspaceClass.description == "") { + return workspaceClass.displayName + } + + return "${workspaceClass.displayName}: ${workspaceClass.description}" + } }