Skip to content

Commit 8ee500c

Browse files
authored
fix: generate typescript types for healthcheck pkg (#8846)
1 parent 72780c8 commit 8ee500c

21 files changed

+333
-204
lines changed

coderd/apidoc/docs.go

+15-15
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/apidoc/swagger.json

+15-15
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/healthcheck/accessurl.go

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"github.com/coder/coder/coderd/util/ptr"
1313
)
1414

15+
// @typescript-generate AccessURLReport
1516
type AccessURLReport struct {
1617
AccessURL string `json:"access_url"`
1718
Healthy bool `json:"healthy"`

coderd/healthcheck/database.go

+10-6
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,13 @@ import (
1010
"github.com/coder/coder/coderd/database"
1111
)
1212

13+
// @typescript-generate DatabaseReport
1314
type DatabaseReport struct {
14-
Healthy bool `json:"healthy"`
15-
Reachable bool `json:"reachable"`
16-
Latency time.Duration `json:"latency"`
17-
Error *string `json:"error"`
15+
Healthy bool `json:"healthy"`
16+
Reachable bool `json:"reachable"`
17+
Latency string `json:"latency"`
18+
LatencyMs int `json:"latency_ms"`
19+
Error *string `json:"error"`
1820
}
1921

2022
type DatabaseReportOptions struct {
@@ -39,10 +41,12 @@ func (r *DatabaseReport) Run(ctx context.Context, opts *DatabaseReportOptions) {
3941
slices.Sort(pings)
4042

4143
// Take the median ping.
42-
r.Latency = pings[pingCount/2]
44+
latency := pings[pingCount/2]
45+
r.Latency = latency.String()
46+
r.LatencyMs = int(latency.Milliseconds())
4347
// Somewhat arbitrary, but if the latency is over 15ms, we consider it
4448
// unhealthy.
45-
if r.Latency < 15*time.Millisecond {
49+
if latency < 15*time.Millisecond {
4650
r.Healthy = true
4751
}
4852
r.Reachable = true

coderd/healthcheck/database_test.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ func TestDatabase(t *testing.T) {
3535

3636
assert.True(t, report.Healthy)
3737
assert.True(t, report.Reachable)
38-
assert.Equal(t, ping, report.Latency)
38+
assert.Equal(t, ping.String(), report.Latency)
39+
assert.Equal(t, int(ping.Milliseconds()), report.LatencyMs)
3940
assert.Nil(t, report.Error)
4041
})
4142

@@ -81,7 +82,8 @@ func TestDatabase(t *testing.T) {
8182

8283
assert.True(t, report.Healthy)
8384
assert.True(t, report.Reachable)
84-
assert.Equal(t, time.Millisecond, report.Latency)
85+
assert.Equal(t, time.Millisecond.String(), report.Latency)
86+
assert.Equal(t, 1, report.LatencyMs)
8587
assert.Nil(t, report.Error)
8688
})
8789
}

coderd/healthcheck/derp.go

+14-6
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"github.com/coder/coder/coderd/util/ptr"
2525
)
2626

27+
// @typescript-generate DERPReport
2728
type DERPReport struct {
2829
Healthy bool `json:"healthy"`
2930

@@ -36,6 +37,7 @@ type DERPReport struct {
3637
Error *string `json:"error"`
3738
}
3839

40+
// @typescript-generate DERPRegionReport
3941
type DERPRegionReport struct {
4042
mu sync.Mutex
4143
Healthy bool `json:"healthy"`
@@ -44,6 +46,8 @@ type DERPRegionReport struct {
4446
NodeReports []*DERPNodeReport `json:"node_reports"`
4547
Error *string `json:"error"`
4648
}
49+
50+
// @typescript-generate DERPNodeReport
4751
type DERPNodeReport struct {
4852
mu sync.Mutex
4953
clientCounter int
@@ -53,7 +57,8 @@ type DERPNodeReport struct {
5357

5458
ServerInfo derp.ServerInfoMessage `json:"node_info"`
5559
CanExchangeMessages bool `json:"can_exchange_messages"`
56-
RoundTripPing time.Duration `json:"round_trip_ping"`
60+
RoundTripPing string `json:"round_trip_ping"`
61+
RoundTripPingMs int `json:"round_trip_ping_ms"`
5762
UsesWebsocket bool `json:"uses_websocket"`
5863
ClientLogs [][]string `json:"client_logs"`
5964
ClientErrs [][]string `json:"client_errs"`
@@ -62,10 +67,11 @@ type DERPNodeReport struct {
6267
STUN DERPStunReport `json:"stun"`
6368
}
6469

70+
// @typescript-generate DERPStunReport
6571
type DERPStunReport struct {
6672
Enabled bool
6773
CanSTUN bool
68-
Error error
74+
Error *string
6975
}
7076

7177
type DERPReportOptions struct {
@@ -251,7 +257,9 @@ func (r *DERPNodeReport) doExchangeMessage(ctx context.Context) {
251257

252258
r.mu.Lock()
253259
r.CanExchangeMessages = true
254-
r.RoundTripPing = time.Since(*t)
260+
rtt := time.Since(*t)
261+
r.RoundTripPing = rtt.String()
262+
r.RoundTripPingMs = int(rtt.Milliseconds())
255263
r.mu.Unlock()
256264

257265
cancel()
@@ -301,20 +309,20 @@ func (r *DERPNodeReport) doSTUNTest(ctx context.Context) {
301309

302310
addr, port, err := r.stunAddr(ctx)
303311
if err != nil {
304-
r.STUN.Error = xerrors.Errorf("get stun addr: %w", err)
312+
r.STUN.Error = convertError(xerrors.Errorf("get stun addr: %w", err))
305313
return
306314
}
307315

308316
// We only create a prober to call ProbeUDP manually.
309317
p, err := prober.DERP(prober.New(), "", time.Second, time.Second, time.Second)
310318
if err != nil {
311-
r.STUN.Error = xerrors.Errorf("create prober: %w", err)
319+
r.STUN.Error = convertError(xerrors.Errorf("create prober: %w", err))
312320
return
313321
}
314322

315323
err = p.ProbeUDP(addr, port)(ctx)
316324
if err != nil {
317-
r.STUN.Error = xerrors.Errorf("probe stun: %w", err)
325+
r.STUN.Error = convertError(xerrors.Errorf("probe stun: %w", err))
318326
return
319327
}
320328

coderd/healthcheck/derp_test.go

+7-7
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ func TestDERP(t *testing.T) {
6767
for _, node := range region.NodeReports {
6868
assert.True(t, node.Healthy)
6969
assert.True(t, node.CanExchangeMessages)
70-
assert.Positive(t, node.RoundTripPing)
70+
assert.NotEmpty(t, node.RoundTripPing)
7171
assert.Len(t, node.ClientLogs, 2)
7272
assert.Len(t, node.ClientLogs[0], 1)
7373
assert.Len(t, node.ClientErrs[0], 0)
@@ -76,7 +76,7 @@ func TestDERP(t *testing.T) {
7676

7777
assert.False(t, node.STUN.Enabled)
7878
assert.False(t, node.STUN.CanSTUN)
79-
assert.NoError(t, node.STUN.Error)
79+
assert.Nil(t, node.STUN.Error)
8080
}
8181
}
8282
})
@@ -111,7 +111,7 @@ func TestDERP(t *testing.T) {
111111
for _, node := range region.NodeReports {
112112
assert.True(t, node.Healthy)
113113
assert.True(t, node.CanExchangeMessages)
114-
assert.Positive(t, node.RoundTripPing)
114+
assert.NotEmpty(t, node.RoundTripPing)
115115
assert.Len(t, node.ClientLogs, 2)
116116
assert.Len(t, node.ClientLogs[0], 1)
117117
assert.Len(t, node.ClientErrs[0], 0)
@@ -120,7 +120,7 @@ func TestDERP(t *testing.T) {
120120

121121
assert.True(t, node.STUN.Enabled)
122122
assert.True(t, node.STUN.CanSTUN)
123-
assert.NoError(t, node.STUN.Error)
123+
assert.Nil(t, node.STUN.Error)
124124
}
125125
}
126126
})
@@ -174,7 +174,7 @@ func TestDERP(t *testing.T) {
174174
for _, node := range region.NodeReports {
175175
assert.False(t, node.Healthy)
176176
assert.True(t, node.CanExchangeMessages)
177-
assert.Positive(t, node.RoundTripPing)
177+
assert.NotEmpty(t, node.RoundTripPing)
178178
assert.Len(t, node.ClientLogs, 2)
179179
assert.Len(t, node.ClientLogs[0], 3)
180180
assert.Len(t, node.ClientLogs[1], 3)
@@ -185,7 +185,7 @@ func TestDERP(t *testing.T) {
185185

186186
assert.False(t, node.STUN.Enabled)
187187
assert.False(t, node.STUN.CanSTUN)
188-
assert.NoError(t, node.STUN.Error)
188+
assert.Nil(t, node.STUN.Error)
189189
}
190190
}
191191
})
@@ -227,7 +227,7 @@ func TestDERP(t *testing.T) {
227227

228228
assert.True(t, node.STUN.Enabled)
229229
assert.True(t, node.STUN.CanSTUN)
230-
assert.NoError(t, node.STUN.Error)
230+
assert.Nil(t, node.STUN.Error)
231231
}
232232
}
233233
})

coderd/healthcheck/healthcheck.go

+1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ type Checker interface {
2929
Database(ctx context.Context, opts *DatabaseReportOptions) DatabaseReport
3030
}
3131

32+
// @typescript-generate Report
3233
type Report struct {
3334
// Time is the time the report was generated at.
3435
Time time.Time `json:"time"`

0 commit comments

Comments
 (0)