use consolFun instead of aggrFunc with resample (#408)

This commit is contained in:
yubo 2020-11-18 11:13:45 +08:00 committed by GitHub
parent c430657738
commit d3992b81ef
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 31 additions and 24 deletions

View File

@ -115,13 +115,18 @@ func xcludeResp(iter ident.TagIterator) *dataobj.XcludeResp {
}
func aggregateResp(data []*dataobj.TsdbQueryResponse, opts dataobj.QueryDataForUI) []*dataobj.TsdbQueryResponse {
if len(data) < 2 || opts.AggrFunc == "" {
if len(data) < 2 {
return data
}
// resample the 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, 直接所有曲线进行计算
@ -182,7 +187,7 @@ func aggregateResp(data []*dataobj.TsdbQueryResponse, opts dataobj.QueryDataForU
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)
if l <= 0 {
return []*dataobj.RRDData{}
@ -192,7 +197,7 @@ func resample(data []*dataobj.RRDData, start, end, step int64, aggrFunc string)
ts := start
if t := data[0].Timestamp; t > start {
ts = t - t%step
ts = t
}
j := 0
@ -220,7 +225,7 @@ func resample(data []*dataobj.RRDData, start, end, step int64, aggrFunc string)
}
ret = append(ret, &dataobj.RRDData{
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 {
case "sum":
return sum(data)
case "avg":
case "avg", "AVERAGE":
return avg(data)
case "max":
case "max", "MAX":
return max(data)
case "min":
case "min", "MIN":
return min(data)
// case "last":
default:

View File

@ -36,6 +36,7 @@ type M3dbSection struct {
DocsLimit int `yaml:"docsLimit"`
MinStep int `yaml:"minStep"`
Config client.Configuration `yaml:",inline"`
timeLimit int64 `yaml:"-"`
}
type Client struct {
@ -65,6 +66,8 @@ func NewClient(cfg M3dbSection) (*Client, error) {
cfg.MinStep = 1
}
cfg.timeLimit = int64(86400 * cfg.DaysLimit)
ret := &Client{
namespace: cfg.Namespace,
config: &cfg,
@ -476,30 +479,29 @@ func (cfg M3dbSection) validateQueryDataForUI(in *dataobj.QueryDataForUI) (err e
return fmt.Errorf("%s is invalid aggrfunc", in.AggrFunc)
}
if err := cfg.validateTime(in.Start, in.End, &in.Step); err != nil {
return err
}
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 in.End <= in.Start {
return fmt.Errorf("query time range is invalid end %d <= start %d", in.End, in.Start)
}
if cfg.DaysLimit > 0 {
if days := int((end - start) / 86400); days > cfg.DaysLimit {
return fmt.Errorf("query time reange in invalid, daysLimit(%d/%d)", 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)
in.Start = t
}
}
if *step == 0 {
*step = int((end - start) / MAX_PONINTS)
if in.Step > 0 {
if n := (in.End - in.Start) / int64(in.Step); n > MAX_PONINTS {
in.Step = 0
}
}
if *step > cfg.MinStep {
*step = cfg.MinStep
if in.Step <= 0 {
in.Step = int((in.End - in.Start) / MAX_PONINTS)
}
if in.Step < cfg.MinStep {
in.Step = cfg.MinStep
}
return nil
}