From c346017cb83c40a351576a6056e6baecd2686e0f Mon Sep 17 00:00:00 2001 From: Chris Schinnerl Date: Tue, 20 Feb 2024 18:03:56 +0100 Subject: [PATCH 1/3] bus: change response type for paginated alerts request --- alerts/alerts.go | 18 +++++++++++++++--- bus/bus.go | 9 +++++++++ bus/client/alerts.go | 8 +++----- internal/testing/cluster_test.go | 13 ++++++++----- 4 files changed, 35 insertions(+), 13 deletions(-) diff --git a/alerts/alerts.go b/alerts/alerts.go index 424196d4f..f5e19231a 100644 --- a/alerts/alerts.go +++ b/alerts/alerts.go @@ -69,6 +69,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. @@ -176,12 +182,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) } @@ -196,8 +206,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) { diff --git a/bus/bus.go b/bus/bus.go index 9fb4c1254..6cce5256d 100644 --- a/bus/bus.go +++ b/bus/bus.go @@ -1716,6 +1716,15 @@ func (b *bus) gougingParams(ctx context.Context) (api.GougingParams, error) { } func (b *bus) handleGETAlerts(jc jape.Context) { + if jc.Request.FormValue("offset") != "" || jc.Request.FormValue("limit") != "" { + b.handleGETAlertsPaginated(jc) + return + } + ar := b.alertMgr.Active(0, -1) + jc.Encode(ar.Alerts) +} + +func (b *bus) handleGETAlertsPaginated(jc jape.Context) { offset, limit := 0, -1 if jc.DecodeForm("offset", &offset) != nil { return diff --git a/bus/client/alerts.go b/bus/client/alerts.go index bff3c13a5..1b876e877 100644 --- a/bus/client/alerts.go +++ b/bus/client/alerts.go @@ -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 } diff --git a/internal/testing/cluster_test.go b/internal/testing/cluster_test.go index 39d6b373a..f0734a0f3 100644 --- a/internal/testing/cluster_test.go +++ b/internal/testing/cluster_test.go @@ -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 } @@ -1960,14 +1960,16 @@ 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") @@ -1975,7 +1977,8 @@ func TestAlerts(t *testing.T) { // dismiss all tt.OK(b.DismissAllAlerts(context.Background())) - foundAlerts, err = b.Alerts(alerts.AlertsOpts{}) + ar, err = b.Alerts(alerts.AlertsOpts{}) + foundAlerts = ar.Alerts tt.OK(err) if len(foundAlerts) != 0 { t.Fatal("expected 0 alerts", len(foundAlerts)) From ce3e6924e1604c61c9b528749185b451107530c9 Mon Sep 17 00:00:00 2001 From: Chris Schinnerl Date: Tue, 20 Feb 2024 18:52:02 +0100 Subject: [PATCH 2/3] bus: revert dismissall alerts endpoint --- alerts/alerts.go | 17 ----------------- bus/bus.go | 7 ------- bus/client/alerts.go | 5 ----- internal/testing/cluster_test.go | 9 --------- 4 files changed, 38 deletions(-) diff --git a/alerts/alerts.go b/alerts/alerts.go index f5e19231a..b0d4963c6 100644 --- a/alerts/alerts.go +++ b/alerts/alerts.go @@ -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. @@ -142,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 @@ -252,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...) diff --git a/bus/bus.go b/bus/bus.go index 6cce5256d..6c80f065a 100644 --- a/bus/bus.go +++ b/bus/bus.go @@ -1738,13 +1738,6 @@ func (b *bus) handleGETAlertsPaginated(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 diff --git a/bus/client/alerts.go b/bus/client/alerts.go index 1b876e877..7f2bf9aa7 100644 --- a/bus/client/alerts.go +++ b/bus/client/alerts.go @@ -25,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 { diff --git a/internal/testing/cluster_test.go b/internal/testing/cluster_test.go index f0734a0f3..5d439f5b7 100644 --- a/internal/testing/cluster_test.go +++ b/internal/testing/cluster_test.go @@ -1974,15 +1974,6 @@ func TestAlerts(t *testing.T) { if len(foundAlerts) != 1 || foundAlerts[0].ID != alert2.ID { t.Fatal("wrong alert") } - - // dismiss all - tt.OK(b.DismissAllAlerts(context.Background())) - ar, err = b.Alerts(alerts.AlertsOpts{}) - foundAlerts = ar.Alerts - tt.OK(err) - if len(foundAlerts) != 0 { - t.Fatal("expected 0 alerts", len(foundAlerts)) - } } func TestMultipartUploads(t *testing.T) { From 709acfe4cccd0f9886d6497afd1646135b0bdb0b Mon Sep 17 00:00:00 2001 From: Chris Schinnerl Date: Wed, 21 Feb 2024 10:54:25 +0100 Subject: [PATCH 3/3] bus: fix japecheck --- bus/bus.go | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/bus/bus.go b/bus/bus.go index 6c80f065a..70471fa05 100644 --- a/bus/bus.go +++ b/bus/bus.go @@ -1715,16 +1715,16 @@ func (b *bus) gougingParams(ctx context.Context) (api.GougingParams, error) { }, nil } -func (b *bus) handleGETAlerts(jc jape.Context) { - if jc.Request.FormValue("offset") != "" || jc.Request.FormValue("limit") != "" { - b.handleGETAlertsPaginated(jc) - return - } +func (b *bus) handleGETAlertsDeprecated(jc jape.Context) { ar := b.alertMgr.Active(0, -1) jc.Encode(ar.Alerts) } -func (b *bus) handleGETAlertsPaginated(jc jape.Context) { +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