Tested + fixed bug in GROUP BY interval calculation
parent
fad1d744b7
commit
68af7508d4
|
@ -221,15 +221,11 @@ func (g *GroupByVar) Exec(query string) {
|
|||
}
|
||||
|
||||
func (g *GroupByVar) String() string {
|
||||
intervalNS := g.ReportingInterval.Nanoseconds()
|
||||
// prevent division by zero
|
||||
if intervalNS == 0 || g.Resolution == 0 {
|
||||
return " "
|
||||
duration := g.Duration.Nanoseconds() / (g.ReportingInterval.Nanoseconds() * int64(g.Resolution))
|
||||
if duration == 0 {
|
||||
duration = 1
|
||||
}
|
||||
|
||||
//TODO(timraymond): ascertain group by resolution
|
||||
duration := g.Duration.Nanoseconds() / g.ReportingInterval.Nanoseconds() * int64(g.Resolution)
|
||||
return "time(" + strconv.Itoa(int(duration)/1000000) + "s)"
|
||||
return "time(" + strconv.Itoa(int(duration)) + "s)"
|
||||
}
|
||||
|
||||
func (g *GroupByVar) Name() string {
|
||||
|
@ -271,6 +267,7 @@ type Query struct {
|
|||
type TemplateVars []TemplateVariable
|
||||
|
||||
func (t *TemplateVars) UnmarshalJSON(text []byte) error {
|
||||
// TODO: Need to test that server throws an error when :interval:'s Resolution or ReportingInterval or zero-value
|
||||
rawVars := bytes.NewReader(text)
|
||||
dec := json.NewDecoder(rawVars)
|
||||
|
||||
|
|
|
@ -2,6 +2,7 @@ package influx
|
|||
|
||||
import (
|
||||
"encoding/json"
|
||||
"reflect"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
|
@ -171,6 +172,28 @@ func TestTemplateReplace(t *testing.T) {
|
|||
},
|
||||
want: `SELECT mean(usage_idle) from "cpu" WHERE time > now() - 4320h group by time(1555s)`,
|
||||
},
|
||||
{
|
||||
name: "auto group by failing condition",
|
||||
query: `SELECT mean(usage_idle) FROM "cpu" WHERE time > :dashboardTime: GROUP BY :interval:`,
|
||||
vars: []chronograf.TemplateVariable{
|
||||
&chronograf.GroupByVar{
|
||||
Var: ":interval:",
|
||||
Resolution: 115,
|
||||
ReportingInterval: 10 * time.Second,
|
||||
},
|
||||
chronograf.BasicTemplateVar{
|
||||
Var: ":dashboardTime:",
|
||||
Values: []chronograf.BasicTemplateValue{
|
||||
{
|
||||
Value: "now() - 1h",
|
||||
Type: "constant",
|
||||
Selected: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
want: `SELECT mean(usage_idle) FROM "cpu" WHERE time > now() - 1h GROUP BY time(3s)`,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
|
@ -186,12 +209,11 @@ func Test_TemplateVarsUnmarshalling(t *testing.T) {
|
|||
req := `[
|
||||
{
|
||||
"tempVar": ":interval:",
|
||||
"duration": 15552000,
|
||||
"resolution": 1000,
|
||||
"reportingInterval": 10
|
||||
},
|
||||
{
|
||||
"tempVar": "cpu",
|
||||
"tempVar": ":cpu:",
|
||||
"values": [
|
||||
{
|
||||
"type": "tagValue",
|
||||
|
@ -202,9 +224,22 @@ func Test_TemplateVarsUnmarshalling(t *testing.T) {
|
|||
}
|
||||
]`
|
||||
|
||||
expected := []string{
|
||||
"time(1555s)",
|
||||
"'cpu-total'",
|
||||
expected := []chronograf.TemplateVariable{
|
||||
&chronograf.GroupByVar{
|
||||
Var: ":interval:",
|
||||
Resolution: 1000,
|
||||
ReportingInterval: 10 * time.Nanosecond,
|
||||
},
|
||||
chronograf.BasicTemplateVar{
|
||||
Var: ":cpu:",
|
||||
Values: []chronograf.BasicTemplateValue{
|
||||
{
|
||||
Value: "cpu-total",
|
||||
Type: "tagValue",
|
||||
Selected: false,
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
var tvars chronograf.TemplateVars
|
||||
|
@ -217,9 +252,46 @@ func Test_TemplateVarsUnmarshalling(t *testing.T) {
|
|||
t.Fatal("Expected", len(expected), "vars but found", len(tvars))
|
||||
}
|
||||
|
||||
for idx, tvar := range tvars {
|
||||
if actual := tvar.String(); expected[idx] != actual {
|
||||
t.Error("Unexpected tvar. Want:", expected[idx], "Got:", actual)
|
||||
}
|
||||
if !reflect.DeepEqual(*(tvars[0].(*chronograf.GroupByVar)), *(expected[0].(*chronograf.GroupByVar))) {
|
||||
t.Errorf("UnmarshalJSON() = \n%#v\n want \n%#v\n", *(tvars[0].(*chronograf.GroupByVar)), *(expected[0].(*chronograf.GroupByVar)))
|
||||
}
|
||||
|
||||
if !reflect.DeepEqual(tvars[1].(chronograf.BasicTemplateVar), expected[1].(chronograf.BasicTemplateVar)) {
|
||||
t.Errorf("UnmarshalJSON() = \n%#v\n want \n%#v\n", tvars[1].(chronograf.BasicTemplateVar), expected[1].(chronograf.BasicTemplateVar))
|
||||
}
|
||||
}
|
||||
|
||||
func TestGroupByVarString(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
tvar *chronograf.GroupByVar
|
||||
want string
|
||||
}{
|
||||
{
|
||||
name: "String() calculates the GROUP BY interval",
|
||||
tvar: &chronograf.GroupByVar{
|
||||
Resolution: 700,
|
||||
ReportingInterval: 10 * time.Second,
|
||||
Duration: 24 * time.Hour,
|
||||
},
|
||||
want: "time(12s)",
|
||||
},
|
||||
{
|
||||
name: "String() outputs a minimum of 1s intervals",
|
||||
tvar: &chronograf.GroupByVar{
|
||||
Resolution: 100000,
|
||||
ReportingInterval: 10 * time.Second,
|
||||
Duration: time.Hour,
|
||||
},
|
||||
want: "time(1s)",
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
got := tt.tvar.String()
|
||||
if got != tt.want {
|
||||
t.Errorf("TestGroupByVarString %s =\n%s\nwant\n%s", tt.name, got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue