Skip to content

Commit 100fb9d

Browse files
committed
impl: show ssh network metrics in the Settings tab (2)
Discarded the download/upload stats, changed the text status, and I've added support for Coder Connect. UTs covering the status generation were also added.
1 parent 3f2ab4f commit 100fb9d

File tree

5 files changed

+123
-4
lines changed

5 files changed

+123
-4
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
## Unreleased
44

5+
### Added
6+
7+
- render network status in the Settings tab, under `Additional environment information` section.
8+
59
## 0.2.1 - 2025-05-05
610

711
### Changed

src/main/kotlin/com/coder/toolbox/CoderRemoteEnvironment.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ class CoderRemoteEnvironment(
180180
return@launch
181181
}
182182
context.logger.debug("$id metrics: $metrics")
183-
additionalEnvironmentInformation.put(context.i18n.ptrl("Network Metrics"), metrics.toPretty())
183+
additionalEnvironmentInformation.put(context.i18n.ptrl("Network Status"), metrics.toPretty())
184184
} catch (e: Exception) {
185185
context.logger.error(
186186
e,

src/main/kotlin/com/coder/toolbox/sdk/v2/models/NetworkMetrics.kt

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ package com.coder.toolbox.sdk.v2.models
22

33
import com.squareup.moshi.Json
44
import com.squareup.moshi.JsonClass
5+
import java.text.DecimalFormat
6+
7+
private val formatter = DecimalFormat("#.00")
58

69
/**
710
* Coder ssh network metrics. All properties are optional
@@ -32,10 +35,15 @@ data class NetworkMetrics(
3235
val usingCoderConnect: Boolean?
3336
) {
3437
fun toPretty(): String {
38+
if (usingCoderConnect == true) {
39+
return "You're connected using Coder Connect"
40+
}
3541
return if (p2p == true) {
36-
"Direct (${latency}ms) \u00B7 Download \u2193 $downloadBytesSec b/s \u00B7 Upload \u2191 $uploadBytesSec b/s"
42+
"Direct (${formatter.format(latency)}ms). You're connected peer-to-peer"
3743
} else {
38-
"$preferredDerp (${latency}ms) \u00B7 Download \u2193 $downloadBytesSec b/s \u00B7 Upload \u2191 $uploadBytesSec b/s"
44+
val derpLatency = derpLatency!![preferredDerp]
45+
val workspaceLatency = latency!!.minus(derpLatency!!)
46+
"You ↔ $preferredDerp (${formatter.format(derpLatency)}ms) ↔ Workspace (${formatter.format(workspaceLatency)}ms). You are connected through a relay"
3947
}
4048
}
4149
}

src/main/resources/localization/defaultMessages.po

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,5 +133,5 @@ msgstr ""
133133
msgid "SSH network metrics directory"
134134
msgstr ""
135135

136-
msgid "Network Metrics"
136+
msgid "Network Status"
137137
msgstr ""
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
package com.coder.toolbox.sdk.v2.models
2+
3+
import kotlin.test.Test
4+
import kotlin.test.assertEquals
5+
6+
class NetworkMetricsTest {
7+
8+
@Test
9+
fun `toPretty should return message for Coder Connect`() {
10+
val metrics = NetworkMetrics(
11+
p2p = null,
12+
latency = null,
13+
preferredDerp = null,
14+
derpLatency = null,
15+
uploadBytesSec = null,
16+
downloadBytesSec = null,
17+
usingCoderConnect = true
18+
)
19+
20+
val expected = "You're connected using Coder Connect"
21+
assertEquals(expected, metrics.toPretty())
22+
}
23+
24+
@Test
25+
fun `toPretty should return message for P2P connection`() {
26+
val metrics = NetworkMetrics(
27+
p2p = true,
28+
latency = 35.526,
29+
preferredDerp = null,
30+
derpLatency = null,
31+
uploadBytesSec = null,
32+
downloadBytesSec = null,
33+
usingCoderConnect = false
34+
)
35+
36+
val expected = "Direct (35.53ms). You're connected peer-to-peer"
37+
assertEquals(expected, metrics.toPretty())
38+
}
39+
40+
@Test
41+
fun `toPretty should round latency with more than two decimals correctly for P2P`() {
42+
val metrics = NetworkMetrics(
43+
p2p = true,
44+
latency = 42.6789,
45+
preferredDerp = null,
46+
derpLatency = null,
47+
uploadBytesSec = null,
48+
downloadBytesSec = null,
49+
usingCoderConnect = false
50+
)
51+
52+
val expected = "Direct (42.68ms). You're connected peer-to-peer"
53+
assertEquals(expected, metrics.toPretty())
54+
}
55+
56+
@Test
57+
fun `toPretty should pad latency with one decimal correctly for P2P`() {
58+
val metrics = NetworkMetrics(
59+
p2p = true,
60+
latency = 12.5,
61+
preferredDerp = null,
62+
derpLatency = null,
63+
uploadBytesSec = null,
64+
downloadBytesSec = null,
65+
usingCoderConnect = false
66+
)
67+
68+
val expected = "Direct (12.50ms). You're connected peer-to-peer"
69+
assertEquals(expected, metrics.toPretty())
70+
}
71+
72+
@Test
73+
fun `toPretty should return message for DERP relay connection`() {
74+
val metrics = NetworkMetrics(
75+
p2p = false,
76+
latency = 80.0,
77+
preferredDerp = "derp1",
78+
derpLatency = mapOf("derp1" to 30.0),
79+
uploadBytesSec = null,
80+
downloadBytesSec = null,
81+
usingCoderConnect = false
82+
)
83+
84+
val expected = "You ↔ derp1 (30.00ms) ↔ Workspace (50.00ms). You are connected through a relay"
85+
assertEquals(expected, metrics.toPretty())
86+
}
87+
88+
@Test
89+
fun `toPretty should round and pad latencies correctly for DERP`() {
90+
val metrics = NetworkMetrics(
91+
p2p = false,
92+
latency = 78.1267,
93+
preferredDerp = "derp2",
94+
derpLatency = mapOf("derp2" to 23.5),
95+
uploadBytesSec = null,
96+
downloadBytesSec = null,
97+
usingCoderConnect = false
98+
)
99+
100+
// Total latency: 78.1267
101+
// DERP latency: 23.5 → formatted as 23.50
102+
// Workspace latency: 78.1267 - 23.5 = 54.6267 → formatted as 54.63
103+
104+
val expected = "You ↔ derp2 (23.50ms) ↔ Workspace (54.63ms). You are connected through a relay"
105+
assertEquals(expected, metrics.toPretty())
106+
}
107+
}

0 commit comments

Comments
 (0)