Skip to content

Commit

Permalink
Merge pull request #184 from mackerelio/fix-alerts-default-limit
Browse files Browse the repository at this point in the history
Fix the default limit of mkr alerts
  • Loading branch information
itchyny authored Nov 27, 2018
2 parents 5095350 + b1297bc commit 5ad8d45
Showing 1 changed file with 25 additions and 6 deletions.
31 changes: 25 additions & 6 deletions alerts.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package main

import (
"errors"
"fmt"
"math"
"os"
"regexp"
"strings"
Expand All @@ -24,7 +26,7 @@ var commandAlerts = cli.Command{
Action: doAlertsRetrieve,
Flags: []cli.Flag{
cli.BoolFlag{Name: "with-closed, w", Usage: "Display open alert including close alert. default: false"},
cli.IntFlag{Name: "limit, l", Value: 100, Usage: "Set the number of alerts to display at once when withClosed is active. default: 100"},
cli.IntFlag{Name: "limit, l", Value: defaultAlertsLimit, Usage: fmt.Sprintf("Set the number of alerts to display. Default is set to %d when -with-closed is set, otherwise all the open alerts are displayed.", defaultAlertsLimit)},
},
Subcommands: []cli.Command{
{
Expand All @@ -48,7 +50,7 @@ var commandAlerts = cli.Command{
},
cli.BoolTFlag{Name: "color, c", Usage: "Colorize output. default: true"},
cli.BoolFlag{Name: "with-closed, w", Usage: "Display open alert including close alert. default: false"},
cli.IntFlag{Name: "limit, l", Value: 100, Usage: "Set the number of alerts to display at once when withClosed is active. default: 100"},
cli.IntFlag{Name: "limit, l", Value: defaultAlertsLimit, Usage: fmt.Sprintf("Set the number of alerts to display. Default is set to %d when -with-closed is set, otherwise all the open alerts are displayed.", defaultAlertsLimit)},
},
},
{
Expand All @@ -67,6 +69,8 @@ var commandAlerts = cli.Command{
},
}

const defaultAlertsLimit int = 100

type alertSet struct {
Alert *mkr.Alert
Host *mkr.Host
Expand Down Expand Up @@ -241,8 +245,7 @@ func formatCheckMessage(msg string) string {
func doAlertsRetrieve(c *cli.Context) error {
client := newMackerelFromContext(c)
withClosed := c.Bool("with-closed")
limit := c.Int("limit")
alerts, err := fetchAlerts(client, withClosed, limit)
alerts, err := fetchAlerts(client, withClosed, getAlertsLimit(c, withClosed))
logger.DieIf(err)
PrettyPrintJSON(alerts)
return nil
Expand All @@ -253,8 +256,7 @@ func doAlertsList(c *cli.Context) error {
filterStatuses := c.StringSlice("host-status")
client := newMackerelFromContext(c)
withClosed := c.Bool("with-closed")
limit := c.Int("limit")
alerts, err := fetchAlerts(client, withClosed, limit)
alerts, err := fetchAlerts(client, withClosed, getAlertsLimit(c, withClosed))
logger.DieIf(err)

joinedAlerts := joinMonitorsAndHosts(client, alerts)
Expand Down Expand Up @@ -296,7 +298,21 @@ func doAlertsList(c *cli.Context) error {
return nil
}

func getAlertsLimit(c *cli.Context, withClosed bool) int {
if c.IsSet("limit") {
return c.Int("limit")
}
if withClosed {
return defaultAlertsLimit
}
// When -limit is not set, mkr alerts should print all the open alerts.
return math.MaxInt32
}

func fetchAlerts(client *mkr.Client, withClosed bool, limit int) ([]*mkr.Alert, error) {
if limit < 0 {
return nil, errors.New("limit should not be negative")
}
var resp *mkr.AlertsResp
var err error
if withClosed {
Expand Down Expand Up @@ -326,6 +342,9 @@ func fetchAlerts(client *mkr.Client, withClosed bool, limit int) ([]*mkr.Alert,
}
if resp.NextID != "" {
for {
if limit <= len(resp.Alerts) {
break
}
nextResp, err := client.FindAlertsByNextID(resp.NextID)
if err != nil {
return nil, err
Expand Down

0 comments on commit 5ad8d45

Please sign in to comment.