2015-11-13 17:39:36 +00:00
|
|
|
package influxdb
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"testing"
|
|
|
|
|
2016-01-20 18:57:35 +00:00
|
|
|
"github.com/influxdata/telegraf/testutil"
|
2015-11-13 17:39:36 +00:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestUDPInflux(t *testing.T) {
|
|
|
|
i := InfluxDB{
|
|
|
|
URLs: []string{"udp://localhost:8089"},
|
|
|
|
}
|
|
|
|
|
|
|
|
err := i.Connect()
|
|
|
|
require.NoError(t, err)
|
2016-01-27 23:15:14 +00:00
|
|
|
err = i.Write(testutil.MockMetrics())
|
2015-11-13 17:39:36 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestHTTPInflux(t *testing.T) {
|
|
|
|
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
fmt.Fprintln(w, `{"results":[{}]}`)
|
|
|
|
}))
|
|
|
|
defer ts.Close()
|
|
|
|
|
|
|
|
i := InfluxDB{
|
|
|
|
URLs: []string{ts.URL},
|
|
|
|
}
|
|
|
|
|
|
|
|
err := i.Connect()
|
|
|
|
require.NoError(t, err)
|
2016-01-27 23:15:14 +00:00
|
|
|
err = i.Write(testutil.MockMetrics())
|
2015-11-13 17:39:36 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
2016-07-14 04:33:21 +00:00
|
|
|
|
2016-07-22 09:17:16 +00:00
|
|
|
func TestDownsampling_mean(t *testing.T) {
|
|
|
|
ds := &Downsampling{}
|
2016-07-14 04:33:21 +00:00
|
|
|
|
2016-07-23 12:41:58 +00:00
|
|
|
err := ds.Add(testutil.TestMetric(120))
|
2016-07-22 09:17:16 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2016-07-23 12:41:58 +00:00
|
|
|
err = ds.Add(testutil.TestMetric(80))
|
2016-07-22 09:17:16 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2016-07-23 12:41:58 +00:00
|
|
|
aggregations := []Aggregation{
|
|
|
|
Aggregation{
|
|
|
|
FieldName: "value",
|
|
|
|
FuncName: "mean",
|
|
|
|
Alias: "mean_value",
|
|
|
|
},
|
|
|
|
}
|
2016-07-22 09:17:16 +00:00
|
|
|
|
2016-07-23 12:41:58 +00:00
|
|
|
aggr, err := ds.Mean(aggregations...)
|
2016-07-14 04:33:21 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2016-07-23 12:41:58 +00:00
|
|
|
require.Equal(t, int64(100), aggr.Fields()["mean_value"])
|
2016-07-14 04:33:21 +00:00
|
|
|
}
|
2016-07-22 11:15:41 +00:00
|
|
|
|
|
|
|
func TestDownsamling_sum(t *testing.T) {
|
|
|
|
ds := &Downsampling{}
|
2016-07-23 12:41:58 +00:00
|
|
|
|
|
|
|
err := ds.Add(testutil.TestMetric(120))
|
2016-07-22 11:15:41 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2016-07-23 12:41:58 +00:00
|
|
|
err = ds.Add(testutil.TestMetric(80))
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
aggregations := []Aggregation{
|
|
|
|
Aggregation{
|
|
|
|
FieldName: "value",
|
|
|
|
FuncName: "mean",
|
|
|
|
Alias: "sum_value",
|
2016-07-22 11:15:41 +00:00
|
|
|
},
|
2016-07-23 12:41:58 +00:00
|
|
|
}
|
|
|
|
aggr, err := ds.Sum(aggregations...)
|
2016-07-22 11:15:41 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2016-07-23 12:41:58 +00:00
|
|
|
require.Equal(t, int64(200), aggr.Fields()["sum_value"])
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDownsampling_aggregate(t *testing.T) {
|
|
|
|
ds := &Downsampling{}
|
|
|
|
|
|
|
|
err := ds.Add(testutil.TestMetric(120))
|
2016-07-22 11:15:41 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2016-07-23 12:41:58 +00:00
|
|
|
err = ds.Add(testutil.TestMetric(80))
|
2016-07-22 11:15:41 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2016-07-23 12:41:58 +00:00
|
|
|
aggregations := []Aggregation{
|
|
|
|
Aggregation{
|
|
|
|
FieldName: "value",
|
|
|
|
FuncName: "mean",
|
|
|
|
Alias: "mean_value",
|
|
|
|
},
|
|
|
|
Aggregation{
|
|
|
|
FieldName: "value",
|
|
|
|
FuncName: "sum",
|
|
|
|
Alias: "sum_value",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
ds.Aggregations = make(map[string][]Aggregation)
|
|
|
|
ds.AddAggregations(aggregations...)
|
|
|
|
|
|
|
|
aggr, err := ds.Aggregate()
|
2016-07-22 11:15:41 +00:00
|
|
|
require.NoError(t, err)
|
2016-07-23 12:41:58 +00:00
|
|
|
|
|
|
|
require.Equal(t, int64(100), aggr.Fields()["mean_value"])
|
|
|
|
require.Equal(t, int64(200), aggr.Fields()["sum_value"])
|
|
|
|
|
2016-07-22 11:15:41 +00:00
|
|
|
}
|