Skip to content

Commit

Permalink
cmd/bosun: add esmonthly func
Browse files Browse the repository at this point in the history
  • Loading branch information
kylebrandt committed Oct 20, 2016
1 parent bee5d18 commit 3438de5
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 8 deletions.
34 changes: 27 additions & 7 deletions cmd/bosun/expr/elastic.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,14 @@ var Elastic = map[string]parse.Func{
F: ESIndicies,
},
"esdaily": {
Args: []models.FuncType{models.TypeString, models.TypeString, models.TypeString},
VArgs: true,
VArgsPos: 1,
Return: models.TypeESIndexer,
F: ESDaily,
Args: []models.FuncType{models.TypeString, models.TypeString, models.TypeString},
Return: models.TypeESIndexer,
F: ESDaily,
},
"esmonthly": {
Args: []models.FuncType{models.TypeString, models.TypeString, models.TypeString},
Return: models.TypeESIndexer,
F: ESMonthly,
},
"esls": {
Args: []models.FuncType{models.TypeString},
Expand Down Expand Up @@ -277,10 +280,10 @@ func (e ElasticHosts) Query(r *ElasticRequest) (*elastic.SearchResult, error) {
if err != nil {
return nil, err
}
if (res.Shards == nil) {
if res.Shards == nil {
return nil, fmt.Errorf("no shard info in reply, should not be here please file issue")
}
if (res.Shards.Successful == 0) {
if res.Shards.Successful == 0 {
return nil, fmt.Errorf("no successful shards in result, perhaps the index does exist, total shards: %v, failed shards: %v", res.Shards.Total, res.Shards.Failed)
}
return res, nil
Expand Down Expand Up @@ -371,6 +374,23 @@ func ESDaily(e *State, T miniprofiler.Timer, timeField, indexRoot, layout string
return &r, nil
}

func ESMonthly(e *State, T miniprofiler.Timer, timeField, indexRoot, layout string) (*Results, error) {
var r Results
indexer := ESIndexer{}
indexer.TimeField = timeField
indexer.Generate = func(start, end *time.Time) []string {
var indices []string
truncStart := now.New(*start).BeginningOfMonth()
truncEnd := now.New(*end).BeginningOfMonth()
for d := truncStart; !d.After(truncEnd); d = d.AddDate(0, 1, 0) {
indices = append(indices, fmt.Sprintf("%v%v", indexRoot, d.Format(layout)))
}
return indices
}
r.Results = append(r.Results, &Result{Value: indexer})
return &r, nil
}

func ESCount(e *State, T miniprofiler.Timer, indexer ESIndexer, keystring string, filter ESQuery, interval, sduration, eduration string) (r *Results, err error) {
return ESDateHistogram(e, T, indexer, keystring, filter.Query, interval, sduration, eduration, "", "", 0)
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/bosun/expr/expr.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ func (e ESQuery) MarshalJSON() ([]byte, error) {

type ESIndexer struct {
TimeField string
Generate func(startDuration, endDuration *time.Time) ([]string)
Generate func(startDuration, endDuration *time.Time) []string
}

func (e ESIndexer) Type() models.FuncType { return models.TypeESIndexer }
Expand Down

0 comments on commit 3438de5

Please sign in to comment.