Skip to content
This repository was archived by the owner on Aug 23, 2023. It is now read-only.

Commit 3b731e3

Browse files
authored
Merge pull request #708 from raintank/find-fromto
support from/to patterns for find query
2 parents 1d73330 + d22a939 commit 3b731e3

File tree

2 files changed

+58
-41
lines changed

2 files changed

+58
-41
lines changed

api/graphite.go

+49-35
Original file line numberDiff line numberDiff line change
@@ -136,43 +136,15 @@ func (s *Server) findSeriesRemote(orgId int, patterns []string, seenAfter int64,
136136
}
137137

138138
func (s *Server) renderMetrics(ctx *middleware.Context, request models.GraphiteRender) {
139+
139140
now := time.Now()
140141
defaultFrom := uint32(now.Add(-time.Duration(24) * time.Hour).Unix())
141142
defaultTo := uint32(now.Unix())
142-
143-
var loc *time.Location
144-
switch request.Tz {
145-
case "":
146-
loc = timeZone
147-
case "local":
148-
loc = time.Local
149-
default:
150-
var err error
151-
loc, err = time.LoadLocation(request.Tz)
152-
if err != nil {
153-
response.Write(ctx, response.NewError(http.StatusBadRequest, err.Error()))
154-
return
155-
}
156-
}
157-
158-
from := request.From
159-
to := request.To
160-
if to == "" {
161-
to = request.Until
162-
}
163-
164-
fromUnix, err := dur.ParseDateTime(from, loc, now, defaultFrom)
165-
if err != nil {
166-
response.Write(ctx, response.NewError(http.StatusBadRequest, err.Error()))
167-
return
168-
}
169-
170-
toUnix, err := dur.ParseDateTime(to, loc, now, defaultTo)
143+
fromUnix, toUnix, err := getFromTo(request.FromTo, now, defaultFrom, defaultTo)
171144
if err != nil {
172145
response.Write(ctx, response.NewError(http.StatusBadRequest, err.Error()))
173146
return
174147
}
175-
176148
if fromUnix >= toUnix {
177149
response.Write(ctx, response.NewError(http.StatusBadRequest, InvalidTimeRangeErr.Error()))
178150
return
@@ -246,8 +218,15 @@ func (s *Server) renderMetrics(ctx *middleware.Context, request models.GraphiteR
246218
}
247219

248220
func (s *Server) metricsFind(ctx *middleware.Context, request models.GraphiteFind) {
221+
now := time.Now()
222+
var defaultFrom, defaultTo uint32
223+
fromUnix, toUnix, err := getFromTo(request.FromTo, now, defaultFrom, defaultTo)
224+
if err != nil {
225+
response.Write(ctx, response.NewError(http.StatusBadRequest, err.Error()))
226+
return
227+
}
249228
nodes := make([]idx.Node, 0)
250-
series, err := s.findSeries(ctx.OrgId, []string{request.Query}, request.From)
229+
series, err := s.findSeries(ctx.OrgId, []string{request.Query}, int64(fromUnix))
251230
if err != nil {
252231
response.Write(ctx, response.WrapError(err))
253232
return
@@ -273,7 +252,7 @@ func (s *Server) metricsFind(ctx *middleware.Context, request models.GraphiteFin
273252
case "completer":
274253
response.Write(ctx, response.NewJson(200, findCompleter(nodes), request.Jsonp))
275254
case "pickle":
276-
response.Write(ctx, response.NewPickle(200, findPickle(nodes, request)))
255+
response.Write(ctx, response.NewPickle(200, findPickle(nodes, request, fromUnix, toUnix)))
277256
}
278257
}
279258

@@ -383,11 +362,11 @@ func findCompleter(nodes []idx.Node) models.SeriesCompleter {
383362
return result
384363
}
385364

386-
func findPickle(nodes []idx.Node, request models.GraphiteFind) models.SeriesPickle {
365+
func findPickle(nodes []idx.Node, request models.GraphiteFind, fromUnix, toUnix uint32) models.SeriesPickle {
387366
result := make([]models.SeriesPickleItem, len(nodes))
388367
var intervals [][]int64
389-
if request.From != 0 && request.Until != 0 {
390-
intervals = [][]int64{{request.From, request.Until}}
368+
if fromUnix != 0 && toUnix != 0 {
369+
intervals = [][]int64{{int64(fromUnix), int64(toUnix)}}
391370
}
392371
for i, g := range nodes {
393372
result[i] = models.NewSeriesPickleItem(g.Path, g.Leaf, intervals)
@@ -598,3 +577,38 @@ func (s *Server) executePlan(orgId int, plan expr.Plan) ([]models.Series, error)
598577
planRunDuration.Value(time.Since(preRun))
599578
return out, err
600579
}
580+
581+
func getFromTo(ft models.FromTo, now time.Time, defaultFrom, defaultTo uint32) (uint32, uint32, error) {
582+
loc, err := getLocation(ft.Tz)
583+
if err != nil {
584+
return 0, 0, err
585+
}
586+
587+
from := ft.From
588+
to := ft.To
589+
if to == "" {
590+
to = ft.Until
591+
}
592+
593+
fromUnix, err := dur.ParseDateTime(from, loc, now, defaultFrom)
594+
if err != nil {
595+
return 0, 0, err
596+
}
597+
598+
toUnix, err := dur.ParseDateTime(to, loc, now, defaultTo)
599+
if err != nil {
600+
return 0, 0, err
601+
}
602+
603+
return fromUnix, toUnix, nil
604+
}
605+
606+
func getLocation(desc string) (*time.Location, error) {
607+
switch desc {
608+
case "":
609+
return timeZone, nil
610+
case "local":
611+
return time.Local, nil
612+
}
613+
return time.LoadLocation(desc)
614+
}

api/models/graphite.go

+9-6
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,21 @@ import (
1111
"gopkg.in/macaron.v1"
1212
)
1313

14+
type FromTo struct {
15+
From string `json:"from" form:"from"`
16+
Until string `json:"until" form:"until"`
17+
To string `json:"to" form:"to"` // graphite uses 'until' but we allow to alternatively cause it's shorter
18+
Tz string `json:"tz" form:"tz"`
19+
}
20+
1421
type GraphiteRender struct {
22+
FromTo
1523
MaxDataPoints uint32 `json:"maxDataPoints" form:"maxDataPoints" binding:"Default(800)"`
1624
Targets []string `json:"target" form:"target"`
1725
TargetsRails []string `form:"target[]"` // # Rails/PHP/jQuery common practice format: ?target[]=path.1&target[]=path.2 -> like graphite, we allow this.
18-
From string `json:"from" form:"from"`
19-
Until string `json:"until" form:"until"`
20-
To string `json:"to" form:"to"`
2126
Format string `json:"format" form:"format" binding:"In(,json,msgp,pickle)"`
2227
NoProxy bool `json:"local" form:"local"` //this is set to true by graphite-web when it passes request to cluster servers
2328
Process string `json:"process" form:"process" binding:"In(,none,stable,any);Default(stable)"`
24-
Tz string `json:"tz" form:"tz"`
2529
}
2630

2731
func (gr GraphiteRender) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
@@ -49,9 +53,8 @@ func (gr GraphiteRender) Validate(ctx *macaron.Context, errs binding.Errors) bin
4953
}
5054

5155
type GraphiteFind struct {
56+
FromTo
5257
Query string `json:"query" form:"query" binding:"Required"`
53-
From int64 `json:"from" form:"from"`
54-
Until int64 `json:"until" form:"until"`
5558
Format string `json:"format" form:"format" binding:"In(,completer,json,treejson,pickle)"`
5659
Jsonp string `json:"jsonp" form:"jsonp"`
5760
}

0 commit comments

Comments
 (0)