func bandTSDB(e *State, T miniprofiler.Timer, query, duration, period string, num float64, rfunc func(*Results, *opentsdb.Response, time.Duration) error) (r *Results, err error) { r = new(Results) r.IgnoreOtherUnjoined = true r.IgnoreUnjoined = true T.Step("band", func(T miniprofiler.Timer) { var d, p opentsdb.Duration d, err = opentsdb.ParseDuration(duration) if err != nil { return } p, err = opentsdb.ParseDuration(period) if err != nil { return } if num < 1 || num > 100 { err = fmt.Errorf("num out of bounds") } var q *opentsdb.Query q, err = opentsdb.ParseQuery(query, e.tsdbContext.Version()) if err != nil { return } if !e.tsdbContext.Version().FilterSupport() { if err = e.Search.Expand(q); err != nil { return } } req := opentsdb.Request{ Queries: []*opentsdb.Query{q}, } now := e.now req.End = now.Unix() req.Start = now.Add(time.Duration(-d)).Unix() if err = req.SetTime(e.now); err != nil { return } for i := 0; i < int(num); i++ { now = now.Add(time.Duration(-p)) req.End = now.Unix() req.Start = now.Add(time.Duration(-d)).Unix() var s opentsdb.ResponseSet s, err = timeTSDBRequest(e, T, &req) if err != nil { return } for _, res := range s { if e.squelched(res.Tags) { continue } //offset := e.now.Sub(now.Add(time.Duration(p-d))) offset := e.now.Sub(now) if err = rfunc(r, res, offset); err != nil { return } } } }) return }
func Query(e *State, T miniprofiler.Timer, query, sduration, eduration string) (r *Results, err error) { r = new(Results) q, err := opentsdb.ParseQuery(query, e.tsdbContext.Version()) if q == nil && err != nil { return } if !e.tsdbContext.Version().FilterSupport() { if err = e.Search.Expand(q); err != nil { return } } sd, err := opentsdb.ParseDuration(sduration) if err != nil { return } req := opentsdb.Request{ Queries: []*opentsdb.Query{q}, Start: fmt.Sprintf("%s-ago", sd), } if eduration != "" { var ed opentsdb.Duration ed, err = opentsdb.ParseDuration(eduration) if err != nil { return } req.End = fmt.Sprintf("%s-ago", ed) } var s opentsdb.ResponseSet if err = req.SetTime(e.now); err != nil { return } s, err = timeTSDBRequest(e, T, &req) if err != nil { return } for _, res := range s { if e.squelched(res.Tags) { continue } values := make(Series) for k, v := range res.DPS { i, err := strconv.ParseInt(k, 10, 64) if err != nil { return nil, err } values[time.Unix(i, 0).UTC()] = float64(v) } r.Results = append(r.Results, &Result{ Value: values, Group: res.Tags, }) } return }
func tagQuery(args []parse.Node) (parse.Tags, error) { n := args[0].(*parse.StringNode) q, err := opentsdb.ParseQuery(n.Text) if q == nil && err != nil { return nil, err } t := make(parse.Tags) for k := range q.Tags { t[k] = struct{}{} } return t, nil }
func tagQuery(args []parse.Node) (parse.Tags, error) { n := args[0].(*parse.StringNode) // Since all 2.1 queries are valid 2.2 queries, at this time // we can just use 2.2 to parse to identify group by tags q, err := opentsdb.ParseQuery(n.Text, opentsdb.Version2_2) if q == nil && err != nil { return nil, err } t := make(parse.Tags) for k := range q.GroupByTags { t[k] = struct{}{} } return t, nil }
func Over(e *State, T miniprofiler.Timer, query, duration, period string, num float64) (r *Results, err error) { r = new(Results) r.IgnoreOtherUnjoined = true r.IgnoreUnjoined = true T.Step("band", func(T miniprofiler.Timer) { var d, p opentsdb.Duration d, err = opentsdb.ParseDuration(duration) if err != nil { return } p, err = opentsdb.ParseDuration(period) if err != nil { return } if num < 1 || num > 100 { err = fmt.Errorf("num out of bounds") } var q *opentsdb.Query q, err = opentsdb.ParseQuery(query, e.tsdbContext.Version()) if err != nil { return } if !e.tsdbContext.Version().FilterSupport() { if err = e.Search.Expand(q); err != nil { return } } req := opentsdb.Request{ Queries: []*opentsdb.Query{q}, } now := e.now req.End = now.Unix() req.Start = now.Add(time.Duration(-d)).Unix() for i := 0; i < int(num); i++ { var s opentsdb.ResponseSet s, err = timeTSDBRequest(e, T, &req) if err != nil { return } offset := e.now.Sub(now) for _, res := range s { if e.squelched(res.Tags) { continue } values := make(Series) a := &Result{Group: res.Tags.Merge(opentsdb.TagSet{"shift": offset.String()})} for k, v := range res.DPS { i, err := strconv.ParseInt(k, 10, 64) if err != nil { return } values[time.Unix(i, 0).Add(offset).UTC()] = float64(v) } a.Value = values r.Results = append(r.Results, a) } now = now.Add(time.Duration(-p)) req.End = now.Unix() req.Start = now.Add(time.Duration(-d)).Unix() } }) return }
func bandRangeTSDB(e *State, T miniprofiler.Timer, query, rangeStart, rangeEnd, period string, num float64, rfunc func(*Results, *opentsdb.Response) error) (r *Results, err error) { r = new(Results) r.IgnoreOtherUnjoined = true r.IgnoreUnjoined = true T.Step("bandRange", func(T miniprofiler.Timer) { var from, to, p opentsdb.Duration from, err = opentsdb.ParseDuration(rangeStart) if err != nil { return } to, err = opentsdb.ParseDuration(rangeEnd) if err != nil { return } p, err = opentsdb.ParseDuration(period) if err != nil { return } if num < 1 || num > 100 { err = fmt.Errorf("num out of bounds") } var q *opentsdb.Query q, err = opentsdb.ParseQuery(query, e.tsdbContext.Version()) if err != nil { return } if err = e.Search.Expand(q); err != nil { return } req := opentsdb.Request{ Queries: []*opentsdb.Query{q}, } now := e.now req.End = now.Unix() st := now.Add(time.Duration(from)).Unix() req.Start = st if err = req.SetTime(e.now); err != nil { return } for i := 0; i < int(num); i++ { now = now.Add(time.Duration(-p)) end := now.Add(time.Duration(-to)).Unix() req.End = &end st := now.Add(time.Duration(-from)).Unix() req.Start = &st var s opentsdb.ResponseSet s, err = timeTSDBRequest(e, T, &req) if err != nil { return } for _, res := range s { if e.squelched(res.Tags) { continue } if err = rfunc(r, res); err != nil { return } } } }) return }
func Band(e *State, T miniprofiler.Timer, query, duration, period string, num float64) (r *Results, err error) { r = new(Results) r.IgnoreOtherUnjoined = true r.IgnoreUnjoined = true T.Step("band", func(T miniprofiler.Timer) { var d, p opentsdb.Duration d, err = opentsdb.ParseDuration(duration) if err != nil { return } p, err = opentsdb.ParseDuration(period) if err != nil { return } if num < 1 || num > 100 { err = fmt.Errorf("expr: Band: num out of bounds") } q, err := opentsdb.ParseQuery(query) if q == nil && err != nil { return } if err = e.Search.Expand(q); err != nil { return } req := opentsdb.Request{ Queries: []*opentsdb.Query{q}, } now := e.now req.End = now.Unix() req.Start = now.Add(time.Duration(-d)).Unix() if err = req.SetTime(e.now); err != nil { return } for i := 0; i < int(num); i++ { now = now.Add(time.Duration(-p)) req.End = now.Unix() req.Start = now.Add(time.Duration(-d)).Unix() var s opentsdb.ResponseSet s, err = timeTSDBRequest(e, T, &req) if err != nil { return } for _, res := range s { if e.squelched(res.Tags) { continue } newarr := true for _, a := range r.Results { if !a.Group.Equal(res.Tags) { continue } newarr = false values := a.Value.(Series) for k, v := range res.DPS { i, e := strconv.ParseInt(k, 10, 64) if e != nil { err = e return } values[time.Unix(i, 0).UTC()] = float64(v) } } if newarr { values := make(Series) a := &Result{Group: res.Tags} for k, v := range res.DPS { i, e := strconv.ParseInt(k, 10, 64) if e != nil { err = e return } values[time.Unix(i, 0).UTC()] = float64(v) } a.Value = values r.Results = append(r.Results, a) } } } }) return }