From 4b3b41fea5f34994c36145fbc54a1d74df5e44f5 Mon Sep 17 00:00:00 2001 From: Regan Kuchan Date: Mon, 30 Nov 2015 16:29:55 -0800 Subject: [PATCH] Create trig plugin closes #404 --- plugins/all/all.go | 1 + plugins/trig/trig.go | 46 +++++++++++++++++++++++++++++++++++++++ plugins/trig/trig_test.go | 33 ++++++++++++++++++++++++++++ 3 files changed, 80 insertions(+) create mode 100644 plugins/trig/trig.go create mode 100644 plugins/trig/trig_test.go diff --git a/plugins/all/all.go b/plugins/all/all.go index 37e7d27ea..ba0859f65 100644 --- a/plugins/all/all.go +++ b/plugins/all/all.go @@ -28,6 +28,7 @@ import ( _ "github.com/influxdb/telegraf/plugins/rethinkdb" _ "github.com/influxdb/telegraf/plugins/statsd" _ "github.com/influxdb/telegraf/plugins/system" + _ "github.com/influxdb/telegraf/plugins/trig" _ "github.com/influxdb/telegraf/plugins/twemproxy" _ "github.com/influxdb/telegraf/plugins/zfs" _ "github.com/influxdb/telegraf/plugins/zookeeper" diff --git a/plugins/trig/trig.go b/plugins/trig/trig.go new file mode 100644 index 000000000..e966cbd46 --- /dev/null +++ b/plugins/trig/trig.go @@ -0,0 +1,46 @@ +package trig + +import ( + "math" + + "github.com/influxdb/telegraf/plugins" +) + +type Trig struct { + x float64 + Amplitude float64 +} + +var TrigConfig = ` + # Set the amplitude + amplitude = 10.0 +` + +func (s *Trig) SampleConfig() string { + return TrigConfig +} + +func (s *Trig) Description() string { + return "Inserts sine and cosine waves for demonstration purposes" +} + +func (s *Trig) Gather(acc plugins.Accumulator) error { + sinner := math.Sin((s.x*math.Pi)/5.0) * s.Amplitude + cosinner := math.Cos((s.x*math.Pi)/5.0) * s.Amplitude + + fields := make(map[string]interface{}) + fields["sine"] = sinner + fields["cosine"] = cosinner + + tags := make(map[string]string) + + s.x += 1.0 + acc.AddFields("trig", fields, tags) + + return nil +} + +func init() { + + plugins.Add("Trig", func() plugins.Plugin { return &Trig{x: 0.0} }) +} diff --git a/plugins/trig/trig_test.go b/plugins/trig/trig_test.go new file mode 100644 index 000000000..24218fe11 --- /dev/null +++ b/plugins/trig/trig_test.go @@ -0,0 +1,33 @@ +package trig + +import ( + "math" + "testing" + + "github.com/influxdb/telegraf/testutil" + "github.com/stretchr/testify/assert" + // "github.com/stretchr/testify/require" +) + +func TestTrig(t *testing.T) { + s := &Trig{ + Amplitude: 10.0, + } + + for i := 0.0; i < 10.0; i++ { + + var acc testutil.Accumulator + + sine := math.Sin((i*math.Pi)/5.0) * s.Amplitude + cosine := math.Cos((i*math.Pi)/5.0) * s.Amplitude + + s.Gather(&acc) + + fields := make(map[string]interface{}) + fields["sine"] = sine + fields["cosine"] = cosine + + assert.True(t, acc.CheckFieldsValue("trig", fields)) + + } +}