Skip to content

Commit

Permalink
Merge pull request #112 from mackerelio/mkr-fetch-many-hosts
Browse files Browse the repository at this point in the history
Support fetch command to retrieve many hosts
  • Loading branch information
itchyny committed Sep 14, 2017
2 parents 4bb36c9 + 04ef024 commit 756caec
Showing 1 changed file with 21 additions and 3 deletions.
24 changes: 21 additions & 3 deletions commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,17 @@ func doThrow(c *cli.Context) error {
return nil
}

func split(ids []string, count int) [][]string {
xs := make([][]string, 0, (len(ids)+count-1)/count)
for i, name := range ids {
if i%count == 0 {
xs = append(xs, []string{})
}
xs[len(xs)-1] = append(xs[len(xs)-1], name)
}
return xs
}

func doFetch(c *cli.Context) error {
argHostIDs := c.Args()
optMetricNames := c.StringSlice("name")
Expand All @@ -462,10 +473,17 @@ func doFetch(c *cli.Context) error {
os.Exit(1)
}

metricValues, err := newMackerelFromContext(c).FetchLatestMetricValues(argHostIDs, optMetricNames)
logger.DieIf(err)
allMetricValues := make(mkr.LatestMetricValues)
// Fetches 100 hosts per one request (to avoid URL maximum length).
for _, hostIds := range split(argHostIDs, 100) {
metricValues, err := newMackerelFromContext(c).FetchLatestMetricValues(hostIds, optMetricNames)
logger.DieIf(err)
for key := range metricValues {
allMetricValues[key] = metricValues[key]
}
}

PrettyPrintJSON(metricValues)
PrettyPrintJSON(allMetricValues)
return nil
}

Expand Down

0 comments on commit 756caec

Please sign in to comment.