Skip to content

Commit 94fcd52

Browse files
committed
threshold
1 parent a6a7f7b commit 94fcd52

File tree

1 file changed

+58
-3
lines changed

1 file changed

+58
-3
lines changed

components/ide/jetbrains/backend-plugin/src/main/kotlin/io/gitpod/jetbrains/remote/GitpodSystemMetricsWidget.kt

Lines changed: 58 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,29 @@ import com.intellij.openapi.project.Project
88
import com.intellij.openapi.wm.StatusBar
99
import com.intellij.openapi.wm.StatusBarWidget
1010
import com.intellij.openapi.wm.impl.status.MemoryUsagePanel
11+
import com.intellij.ui.JBColor
12+
import com.intellij.util.ui.JBUI
1113
import java.awt.event.ActionListener
1214
import java.lang.management.ManagementFactory
1315
import javax.swing.Timer
16+
import com.intellij.notification.NotificationGroupManager
17+
import com.intellij.notification.NotificationType
18+
import com.intellij.openapi.application.ApplicationManager
1419

1520
class GitpodSystemMetricsWidget(private val project: Project) : StatusBarWidget {
1621
private var updateTimer: Timer? = null
1722
private val memoryPanel = MemoryUsagePanel()
23+
private var lastNotificationTime = 0L
24+
private val NOTIFICATION_COOLDOWN = 5 * 60 * 1000 // 5 minutes in milliseconds
25+
26+
companion object {
27+
private const val WARNING_THRESHOLD = 0.75 // 75% of max memory
28+
private const val CRITICAL_THRESHOLD = 0.90 // 90% of max memory
29+
}
1830

1931
init {
2032
startUpdates()
33+
memoryPanel.border = JBUI.Borders.empty(0, 2)
2134
}
2235

2336
private fun startUpdates() {
@@ -37,13 +50,55 @@ class GitpodSystemMetricsWidget(private val project: Project) : StatusBarWidget
3750

3851
private fun updateMetrics() {
3952
val memoryBean = ManagementFactory.getMemoryMXBean()
40-
val usedMemory = memoryBean.heapMemoryUsage.used / (1024 * 1024)
41-
val maxMemory = memoryBean.heapMemoryUsage.max / (1024 * 1024)
53+
val usedMemory = memoryBean.heapMemoryUsage.used
54+
val maxMemory = memoryBean.heapMemoryUsage.max
55+
val memoryRatio = usedMemory.toDouble() / maxMemory.toDouble()
56+
val usedMemoryMB = usedMemory / (1024 * 1024)
57+
val maxMemoryMB = maxMemory / (1024 * 1024)
4258
val cpuLoad = ManagementFactory.getOperatingSystemMXBean().systemLoadAverage
4359

44-
val metrics = "Mem: ${usedMemory}M/${maxMemory}M | CPU: %.1f%%".format(cpuLoad * 100)
60+
val metrics = buildString {
61+
append("Mem: ${usedMemoryMB}M/${maxMemoryMB}M")
62+
when {
63+
memoryRatio >= CRITICAL_THRESHOLD -> append(" ⚠️") // Critical warning
64+
memoryRatio >= WARNING_THRESHOLD -> append("") // Warning
65+
}
66+
append(" | CPU: %.1f%%".format(cpuLoad * 100))
67+
}
68+
4569
memoryPanel.text = metrics
70+
71+
// Update text color based on memory usage
72+
memoryPanel.foreground = when {
73+
memoryRatio >= CRITICAL_THRESHOLD -> JBColor.RED
74+
memoryRatio >= WARNING_THRESHOLD -> JBColor.ORANGE
75+
else -> JBColor.foreground()
76+
}
77+
4678
memoryPanel.parent?.repaint()
79+
80+
if (memoryRatio >= CRITICAL_THRESHOLD) {
81+
showMemoryWarning(usedMemoryMB, maxMemoryMB)
82+
}
83+
}
84+
85+
private fun showMemoryWarning(usedMemoryMB: Long, maxMemoryMB: Long) {
86+
val now = System.currentTimeMillis()
87+
if (now - lastNotificationTime < NOTIFICATION_COOLDOWN) {
88+
return
89+
}
90+
91+
lastNotificationTime = now
92+
93+
ApplicationManager.getApplication().invokeLater {
94+
NotificationGroupManager.getInstance()
95+
.getNotificationGroup("Gitpod Notifications")
96+
.createNotification(
97+
"High Memory Usage Warning",
98+
"Memory usage is critically high (${usedMemoryMB}MB/${maxMemoryMB}MB). Consider saving your work and restarting the IDE.",
99+
NotificationType.WARNING
100+
).notify(project)
101+
}
47102
}
48103

49104
override fun install(statusBar: StatusBar) {

0 commit comments

Comments
 (0)