Skip to content

Commit

Permalink
Merge pull request #987 from SiaFoundation/chris/alerts-pagination
Browse files Browse the repository at this point in the history
Change response type for paginated alerts request
  • Loading branch information
ChrisSchinnerl committed Feb 23, 2024
2 parents 542709a + 709acfe commit 1192264
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 49 deletions.
35 changes: 15 additions & 20 deletions alerts/alerts.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ type (
Alerter interface {
RegisterAlert(_ context.Context, a Alert) error
DismissAlerts(_ context.Context, ids ...types.Hash256) error
DismissAllAlerts(_ context.Context) error
}

// Severity indicates the severity of an alert.
Expand Down Expand Up @@ -69,6 +68,12 @@ type (
Offset int
Limit int
}

AlertsResponse struct {
Alerts []Alert `json:"alerts"`
HasMore bool `json:"hasMore"`
Total int `json:"total"`
}
)

// String implements the fmt.Stringer interface.
Expand Down Expand Up @@ -136,17 +141,6 @@ func (m *Manager) RegisterAlert(ctx context.Context, alert Alert) error {
})
}

// DismissAllAlerts implements the Alerter interface.
func (m *Manager) DismissAllAlerts(ctx context.Context) error {
m.mu.Lock()
toDismiss := make([]types.Hash256, 0, len(m.alerts))
for alertID := range m.alerts {
toDismiss = append(toDismiss, alertID)
}
m.mu.Unlock()
return m.DismissAlerts(ctx, toDismiss...)
}

// DismissAlerts implements the Alerter interface.
func (m *Manager) DismissAlerts(ctx context.Context, ids ...types.Hash256) error {
var dismissed []types.Hash256
Expand Down Expand Up @@ -176,12 +170,16 @@ func (m *Manager) DismissAlerts(ctx context.Context, ids ...types.Hash256) error
}

// Active returns the host's active alerts.
func (m *Manager) Active(offset, limit int) []Alert {
func (m *Manager) Active(offset, limit int) AlertsResponse {
m.mu.Lock()
defer m.mu.Unlock()

resp := AlertsResponse{
Total: len(m.alerts),
}

if offset >= len(m.alerts) {
return nil
return resp
} else if limit == -1 {
limit = len(m.alerts)
}
Expand All @@ -196,8 +194,10 @@ func (m *Manager) Active(offset, limit int) []Alert {
alerts = alerts[offset:]
if limit < len(alerts) {
alerts = alerts[:limit]
resp.HasMore = true
}
return alerts
resp.Alerts = alerts
return resp
}

func (m *Manager) RegisterWebhookBroadcaster(b webhooks.Broadcaster) {
Expand Down Expand Up @@ -240,11 +240,6 @@ func (a *originAlerter) RegisterAlert(ctx context.Context, alert Alert) error {
return a.alerter.RegisterAlert(ctx, alert)
}

// DismissAllAlerts implements the Alerter interface.
func (a *originAlerter) DismissAllAlerts(ctx context.Context) error {
return a.alerter.DismissAllAlerts(ctx)
}

// DismissAlerts implements the Alerter interface.
func (a *originAlerter) DismissAlerts(ctx context.Context, ids ...types.Hash256) error {
return a.alerter.DismissAlerts(ctx, ids...)
Expand Down
16 changes: 9 additions & 7 deletions bus/bus.go
Original file line number Diff line number Diff line change
Expand Up @@ -1725,7 +1725,16 @@ func (b *bus) gougingParams(ctx context.Context) (api.GougingParams, error) {
}, nil
}

func (b *bus) handleGETAlertsDeprecated(jc jape.Context) {
ar := b.alertMgr.Active(0, -1)
jc.Encode(ar.Alerts)
}

func (b *bus) handleGETAlerts(jc jape.Context) {
if jc.Request.FormValue("offset") == "" && jc.Request.FormValue("limit") == "" {
b.handleGETAlertsDeprecated(jc)
return
}
offset, limit := 0, -1
if jc.DecodeForm("offset", &offset) != nil {
return
Expand All @@ -1739,13 +1748,6 @@ func (b *bus) handleGETAlerts(jc jape.Context) {
}

func (b *bus) handlePOSTAlertsDismiss(jc jape.Context) {
var all bool
if jc.DecodeForm("all", &all) != nil {
return
} else if all {
jc.Check("failed to dismiss all alerts", b.alertMgr.DismissAllAlerts(jc.Request.Context()))
return
}
var ids []types.Hash256
if jc.Decode(&ids) != nil {
return
Expand Down
13 changes: 3 additions & 10 deletions bus/client/alerts.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,13 @@ import (
)

// Alerts fetches the active alerts from the bus.
func (c *Client) Alerts(opts alerts.AlertsOpts) (alerts []alerts.Alert, err error) {
func (c *Client) Alerts(opts alerts.AlertsOpts) (resp alerts.AlertsResponse, err error) {
values := url.Values{}
if opts.Offset != 0 {
values.Set("offset", fmt.Sprint(opts.Offset))
}
values.Set("offset", fmt.Sprint(opts.Offset))
if opts.Limit != 0 {
values.Set("limit", fmt.Sprint(opts.Limit))
}
err = c.c.GET("/alerts?"+values.Encode(), &alerts)
err = c.c.GET("/alerts?"+values.Encode(), &resp)
return
}

Expand All @@ -27,11 +25,6 @@ func (c *Client) DismissAlerts(ctx context.Context, ids ...types.Hash256) error
return c.dismissAlerts(ctx, false, ids...)
}

// DismissAllAlerts dimisses all registered alerts.
func (c *Client) DismissAllAlerts(ctx context.Context) error {
return c.dismissAlerts(ctx, true)
}

func (c *Client) dismissAlerts(ctx context.Context, all bool, ids ...types.Hash256) error {
values := url.Values{}
if all {
Expand Down
18 changes: 6 additions & 12 deletions internal/testing/cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1923,9 +1923,9 @@ func TestAlerts(t *testing.T) {
tt.OK(b.RegisterAlert(context.Background(), alert))
findAlert := func(id types.Hash256) *alerts.Alert {
t.Helper()
alerts, err := b.Alerts(alerts.AlertsOpts{})
ar, err := b.Alerts(alerts.AlertsOpts{})
tt.OK(err)
for _, alert := range alerts {
for _, alert := range ar.Alerts {
if alert.ID == id {
return &alert
}
Expand Down Expand Up @@ -1960,26 +1960,20 @@ func TestAlerts(t *testing.T) {
}

// try to find with offset = 1
foundAlerts, err := b.Alerts(alerts.AlertsOpts{Offset: 1})
ar, err := b.Alerts(alerts.AlertsOpts{Offset: 1})
foundAlerts := ar.Alerts
tt.OK(err)
if len(foundAlerts) != 1 || foundAlerts[0].ID != alert.ID {
t.Fatal("wrong alert")
}

// try to find with limit = 1
foundAlerts, err = b.Alerts(alerts.AlertsOpts{Limit: 1})
ar, err = b.Alerts(alerts.AlertsOpts{Limit: 1})
foundAlerts = ar.Alerts
tt.OK(err)
if len(foundAlerts) != 1 || foundAlerts[0].ID != alert2.ID {
t.Fatal("wrong alert")
}

// dismiss all
tt.OK(b.DismissAllAlerts(context.Background()))
foundAlerts, err = b.Alerts(alerts.AlertsOpts{})
tt.OK(err)
if len(foundAlerts) != 0 {
t.Fatal("expected 0 alerts", len(foundAlerts))
}
}

func TestMultipartUploads(t *testing.T) {
Expand Down

0 comments on commit 1192264

Please sign in to comment.