use consolFun instead of aggrFunc with resample (#408)
This commit is contained in:
parent
c430657738
commit
d3992b81ef
|
@ -115,13 +115,18 @@ func xcludeResp(iter ident.TagIterator) *dataobj.XcludeResp {
|
||||||
}
|
}
|
||||||
|
|
||||||
func aggregateResp(data []*dataobj.TsdbQueryResponse, opts dataobj.QueryDataForUI) []*dataobj.TsdbQueryResponse {
|
func aggregateResp(data []*dataobj.TsdbQueryResponse, opts dataobj.QueryDataForUI) []*dataobj.TsdbQueryResponse {
|
||||||
if len(data) < 2 || opts.AggrFunc == "" {
|
if len(data) < 2 {
|
||||||
return data
|
return data
|
||||||
}
|
}
|
||||||
|
|
||||||
// resample the data
|
// resample the data
|
||||||
for _, v := range data {
|
for _, v := range data {
|
||||||
v.Values = resample(v.Values, opts.Start, opts.End, int64(opts.Step), opts.AggrFunc)
|
v.Values = resample(v.Values, opts.Start, opts.End, int64(opts.Step), opts.ConsolFunc)
|
||||||
|
}
|
||||||
|
|
||||||
|
// aggregateResp
|
||||||
|
if opts.AggrFunc == "" {
|
||||||
|
return data
|
||||||
}
|
}
|
||||||
|
|
||||||
// 没有聚合 tag, 或者曲线没有其他 tags, 直接所有曲线进行计算
|
// 没有聚合 tag, 或者曲线没有其他 tags, 直接所有曲线进行计算
|
||||||
|
@ -182,7 +187,7 @@ func aggregateResp(data []*dataobj.TsdbQueryResponse, opts dataobj.QueryDataForU
|
||||||
return aggrDatas
|
return aggrDatas
|
||||||
}
|
}
|
||||||
|
|
||||||
func resample(data []*dataobj.RRDData, start, end, step int64, aggrFunc string) []*dataobj.RRDData {
|
func resample(data []*dataobj.RRDData, start, end, step int64, consolFunc string) []*dataobj.RRDData {
|
||||||
l := int((end - start) / step)
|
l := int((end - start) / step)
|
||||||
if l <= 0 {
|
if l <= 0 {
|
||||||
return []*dataobj.RRDData{}
|
return []*dataobj.RRDData{}
|
||||||
|
@ -192,7 +197,7 @@ func resample(data []*dataobj.RRDData, start, end, step int64, aggrFunc string)
|
||||||
|
|
||||||
ts := start
|
ts := start
|
||||||
if t := data[0].Timestamp; t > start {
|
if t := data[0].Timestamp; t > start {
|
||||||
ts = t - t%step
|
ts = t
|
||||||
}
|
}
|
||||||
|
|
||||||
j := 0
|
j := 0
|
||||||
|
@ -220,7 +225,7 @@ func resample(data []*dataobj.RRDData, start, end, step int64, aggrFunc string)
|
||||||
}
|
}
|
||||||
ret = append(ret, &dataobj.RRDData{
|
ret = append(ret, &dataobj.RRDData{
|
||||||
Timestamp: ts,
|
Timestamp: ts,
|
||||||
Value: aggrData(aggrFunc, get()),
|
Value: aggrData(consolFunc, get()),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -234,11 +239,11 @@ func aggrData(fn string, data []dataobj.JsonFloat) dataobj.JsonFloat {
|
||||||
switch fn {
|
switch fn {
|
||||||
case "sum":
|
case "sum":
|
||||||
return sum(data)
|
return sum(data)
|
||||||
case "avg":
|
case "avg", "AVERAGE":
|
||||||
return avg(data)
|
return avg(data)
|
||||||
case "max":
|
case "max", "MAX":
|
||||||
return max(data)
|
return max(data)
|
||||||
case "min":
|
case "min", "MIN":
|
||||||
return min(data)
|
return min(data)
|
||||||
// case "last":
|
// case "last":
|
||||||
default:
|
default:
|
||||||
|
|
|
@ -36,6 +36,7 @@ type M3dbSection struct {
|
||||||
DocsLimit int `yaml:"docsLimit"`
|
DocsLimit int `yaml:"docsLimit"`
|
||||||
MinStep int `yaml:"minStep"`
|
MinStep int `yaml:"minStep"`
|
||||||
Config client.Configuration `yaml:",inline"`
|
Config client.Configuration `yaml:",inline"`
|
||||||
|
timeLimit int64 `yaml:"-"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type Client struct {
|
type Client struct {
|
||||||
|
@ -65,6 +66,8 @@ func NewClient(cfg M3dbSection) (*Client, error) {
|
||||||
cfg.MinStep = 1
|
cfg.MinStep = 1
|
||||||
}
|
}
|
||||||
|
|
||||||
|
cfg.timeLimit = int64(86400 * cfg.DaysLimit)
|
||||||
|
|
||||||
ret := &Client{
|
ret := &Client{
|
||||||
namespace: cfg.Namespace,
|
namespace: cfg.Namespace,
|
||||||
config: &cfg,
|
config: &cfg,
|
||||||
|
@ -476,30 +479,29 @@ func (cfg M3dbSection) validateQueryDataForUI(in *dataobj.QueryDataForUI) (err e
|
||||||
return fmt.Errorf("%s is invalid aggrfunc", in.AggrFunc)
|
return fmt.Errorf("%s is invalid aggrfunc", in.AggrFunc)
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := cfg.validateTime(in.Start, in.End, &in.Step); err != nil {
|
if in.End <= in.Start {
|
||||||
return err
|
return fmt.Errorf("query time range is invalid end %d <= start %d", in.End, in.Start)
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (cfg M3dbSection) validateTime(start, end int64, step *int) error {
|
|
||||||
if end <= start {
|
|
||||||
return fmt.Errorf("query time range is invalid end %d <= start %d", end, start)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if cfg.DaysLimit > 0 {
|
if cfg.DaysLimit > 0 {
|
||||||
if days := int((end - start) / 86400); days > cfg.DaysLimit {
|
if t := in.End - cfg.timeLimit; in.Start < t {
|
||||||
return fmt.Errorf("query time reange in invalid, daysLimit(%d/%d)", days, cfg.DaysLimit)
|
// return fmt.Errorf("query time reange in invalid, daysLimit(%d/%d)", days, cfg.DaysLimit)
|
||||||
|
in.Start = t
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if *step == 0 {
|
if in.Step > 0 {
|
||||||
*step = int((end - start) / MAX_PONINTS)
|
if n := (in.End - in.Start) / int64(in.Step); n > MAX_PONINTS {
|
||||||
|
in.Step = 0
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if *step > cfg.MinStep {
|
if in.Step <= 0 {
|
||||||
*step = cfg.MinStep
|
in.Step = int((in.End - in.Start) / MAX_PONINTS)
|
||||||
|
}
|
||||||
|
|
||||||
|
if in.Step < cfg.MinStep {
|
||||||
|
in.Step = cfg.MinStep
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue