Skip to content

fix: generate typescript types for healthcheck pkg #8846

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Aug 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 15 additions & 15 deletions coderd/apidoc/docs.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 15 additions & 15 deletions coderd/apidoc/swagger.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions coderd/healthcheck/accessurl.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/coder/coder/coderd/util/ptr"
)

// @typescript-generate AccessURLReport
type AccessURLReport struct {
AccessURL string `json:"access_url"`
Healthy bool `json:"healthy"`
Expand Down
16 changes: 10 additions & 6 deletions coderd/healthcheck/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@ import (
"github.com/coder/coder/coderd/database"
)

// @typescript-generate DatabaseReport
type DatabaseReport struct {
Healthy bool `json:"healthy"`
Reachable bool `json:"reachable"`
Latency time.Duration `json:"latency"`
Error *string `json:"error"`
Healthy bool `json:"healthy"`
Reachable bool `json:"reachable"`
Latency string `json:"latency"`
LatencyMs int `json:"latency_ms"`
Error *string `json:"error"`
}

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

// Take the median ping.
r.Latency = pings[pingCount/2]
latency := pings[pingCount/2]
r.Latency = latency.String()
r.LatencyMs = int(latency.Milliseconds())
// Somewhat arbitrary, but if the latency is over 15ms, we consider it
// unhealthy.
if r.Latency < 15*time.Millisecond {
if latency < 15*time.Millisecond {
r.Healthy = true
}
r.Reachable = true
Expand Down
6 changes: 4 additions & 2 deletions coderd/healthcheck/database_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ func TestDatabase(t *testing.T) {

assert.True(t, report.Healthy)
assert.True(t, report.Reachable)
assert.Equal(t, ping, report.Latency)
assert.Equal(t, ping.String(), report.Latency)
assert.Equal(t, int(ping.Milliseconds()), report.LatencyMs)
assert.Nil(t, report.Error)
})

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

assert.True(t, report.Healthy)
assert.True(t, report.Reachable)
assert.Equal(t, time.Millisecond, report.Latency)
assert.Equal(t, time.Millisecond.String(), report.Latency)
assert.Equal(t, 1, report.LatencyMs)
assert.Nil(t, report.Error)
})
}
20 changes: 14 additions & 6 deletions coderd/healthcheck/derp.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"github.com/coder/coder/coderd/util/ptr"
)

// @typescript-generate DERPReport
type DERPReport struct {
Healthy bool `json:"healthy"`

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

// @typescript-generate DERPRegionReport
type DERPRegionReport struct {
mu sync.Mutex
Healthy bool `json:"healthy"`
Expand All @@ -44,6 +46,8 @@ type DERPRegionReport struct {
NodeReports []*DERPNodeReport `json:"node_reports"`
Error *string `json:"error"`
}

// @typescript-generate DERPNodeReport
type DERPNodeReport struct {
mu sync.Mutex
clientCounter int
Expand All @@ -53,7 +57,8 @@ type DERPNodeReport struct {

ServerInfo derp.ServerInfoMessage `json:"node_info"`
CanExchangeMessages bool `json:"can_exchange_messages"`
RoundTripPing time.Duration `json:"round_trip_ping"`
RoundTripPing string `json:"round_trip_ping"`
RoundTripPingMs int `json:"round_trip_ping_ms"`
UsesWebsocket bool `json:"uses_websocket"`
ClientLogs [][]string `json:"client_logs"`
ClientErrs [][]string `json:"client_errs"`
Expand All @@ -62,10 +67,11 @@ type DERPNodeReport struct {
STUN DERPStunReport `json:"stun"`
}

// @typescript-generate DERPStunReport
type DERPStunReport struct {
Enabled bool
CanSTUN bool
Error error
Error *string
}

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

r.mu.Lock()
r.CanExchangeMessages = true
r.RoundTripPing = time.Since(*t)
rtt := time.Since(*t)
r.RoundTripPing = rtt.String()
r.RoundTripPingMs = int(rtt.Milliseconds())
r.mu.Unlock()

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

addr, port, err := r.stunAddr(ctx)
if err != nil {
r.STUN.Error = xerrors.Errorf("get stun addr: %w", err)
r.STUN.Error = convertError(xerrors.Errorf("get stun addr: %w", err))
return
}

// We only create a prober to call ProbeUDP manually.
p, err := prober.DERP(prober.New(), "", time.Second, time.Second, time.Second)
if err != nil {
r.STUN.Error = xerrors.Errorf("create prober: %w", err)
r.STUN.Error = convertError(xerrors.Errorf("create prober: %w", err))
return
}

err = p.ProbeUDP(addr, port)(ctx)
if err != nil {
r.STUN.Error = xerrors.Errorf("probe stun: %w", err)
r.STUN.Error = convertError(xerrors.Errorf("probe stun: %w", err))
return
}

Expand Down
14 changes: 7 additions & 7 deletions coderd/healthcheck/derp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func TestDERP(t *testing.T) {
for _, node := range region.NodeReports {
assert.True(t, node.Healthy)
assert.True(t, node.CanExchangeMessages)
assert.Positive(t, node.RoundTripPing)
assert.NotEmpty(t, node.RoundTripPing)
assert.Len(t, node.ClientLogs, 2)
assert.Len(t, node.ClientLogs[0], 1)
assert.Len(t, node.ClientErrs[0], 0)
Expand All @@ -76,7 +76,7 @@ func TestDERP(t *testing.T) {

assert.False(t, node.STUN.Enabled)
assert.False(t, node.STUN.CanSTUN)
assert.NoError(t, node.STUN.Error)
assert.Nil(t, node.STUN.Error)
}
}
})
Expand Down Expand Up @@ -111,7 +111,7 @@ func TestDERP(t *testing.T) {
for _, node := range region.NodeReports {
assert.True(t, node.Healthy)
assert.True(t, node.CanExchangeMessages)
assert.Positive(t, node.RoundTripPing)
assert.NotEmpty(t, node.RoundTripPing)
assert.Len(t, node.ClientLogs, 2)
assert.Len(t, node.ClientLogs[0], 1)
assert.Len(t, node.ClientErrs[0], 0)
Expand All @@ -120,7 +120,7 @@ func TestDERP(t *testing.T) {

assert.True(t, node.STUN.Enabled)
assert.True(t, node.STUN.CanSTUN)
assert.NoError(t, node.STUN.Error)
assert.Nil(t, node.STUN.Error)
}
}
})
Expand Down Expand Up @@ -174,7 +174,7 @@ func TestDERP(t *testing.T) {
for _, node := range region.NodeReports {
assert.False(t, node.Healthy)
assert.True(t, node.CanExchangeMessages)
assert.Positive(t, node.RoundTripPing)
assert.NotEmpty(t, node.RoundTripPing)
assert.Len(t, node.ClientLogs, 2)
assert.Len(t, node.ClientLogs[0], 3)
assert.Len(t, node.ClientLogs[1], 3)
Expand All @@ -185,7 +185,7 @@ func TestDERP(t *testing.T) {

assert.False(t, node.STUN.Enabled)
assert.False(t, node.STUN.CanSTUN)
assert.NoError(t, node.STUN.Error)
assert.Nil(t, node.STUN.Error)
}
}
})
Expand Down Expand Up @@ -227,7 +227,7 @@ func TestDERP(t *testing.T) {

assert.True(t, node.STUN.Enabled)
assert.True(t, node.STUN.CanSTUN)
assert.NoError(t, node.STUN.Error)
assert.Nil(t, node.STUN.Error)
}
}
})
Expand Down
1 change: 1 addition & 0 deletions coderd/healthcheck/healthcheck.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ type Checker interface {
Database(ctx context.Context, opts *DatabaseReportOptions) DatabaseReport
}

// @typescript-generate Report
type Report struct {
// Time is the time the report was generated at.
Time time.Time `json:"time"`
Expand Down
Loading