From 0e612082029d8c5d9bf840a859692950fa74a222 Mon Sep 17 00:00:00 2001 From: Regan Kuchan Date: Mon, 30 Nov 2015 16:29:55 -0800 Subject: [PATCH 1/5] Create trig plugin --- plugins/all/all.go | 1 + 1 file changed, 1 insertion(+) 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" From cd5bcce2c20c46871499bcd9a05d7343da17a586 Mon Sep 17 00:00:00 2001 From: Regan Kuchan Date: Mon, 30 Nov 2015 16:36:40 -0800 Subject: [PATCH 2/5] Add actual plugin --- plugins/trig/trig.go | 50 +++++++++++++++++++++++++++++++++++++++ plugins/trig/trig_test.go | 38 +++++++++++++++++++++++++++++ 2 files changed, 88 insertions(+) create mode 100644 plugins/trig/trig.go create mode 100644 plugins/trig/trig_test.go diff --git a/plugins/trig/trig.go b/plugins/trig/trig.go new file mode 100644 index 000000000..4c04daf8c --- /dev/null +++ b/plugins/trig/trig.go @@ -0,0 +1,50 @@ +package trig + +import ( + "math" + "fmt" + + "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 "Insert trig data" +} + +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) + + fmt.Printf("%#v\n",fields) + + return nil +} + + +func init() { + +plugins.Add("Trig", func() plugins.Plugin { return &Trig{x: 0.0} }) +} \ No newline at end of file diff --git a/plugins/trig/trig_test.go b/plugins/trig/trig_test.go new file mode 100644 index 000000000..5bd1fe79f --- /dev/null +++ b/plugins/trig/trig_test.go @@ -0,0 +1,38 @@ +package trig + +import ( + "testing" + "math" + "fmt" + + "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 + + fmt.Printf("%#v\n",fields) + + assert.True(t, acc.CheckFieldsValue("trig", fields)) + + } +} + From 9e663d74da143ff315ddcd525e979fe7e25ddaa8 Mon Sep 17 00:00:00 2001 From: Regan Kuchan Date: Mon, 30 Nov 2015 16:40:47 -0800 Subject: [PATCH 3/5] Format code appropriately --- plugins/trig/trig.go | 17 ++++++++--------- plugins/trig/trig_test.go | 16 +++++++--------- 2 files changed, 15 insertions(+), 18 deletions(-) diff --git a/plugins/trig/trig.go b/plugins/trig/trig.go index 4c04daf8c..410bce9de 100644 --- a/plugins/trig/trig.go +++ b/plugins/trig/trig.go @@ -1,14 +1,14 @@ package trig import ( - "math" "fmt" + "math" "github.com/influxdb/telegraf/plugins" ) type Trig struct { - x float64 + x float64 Amplitude float64 } @@ -26,8 +26,8 @@ func (s *Trig) Description() string { } 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 + 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 @@ -36,15 +36,14 @@ func (s *Trig) Gather(acc plugins.Accumulator) error { tags := make(map[string]string) s.x += 1.0 - acc.AddFields("trig",fields,tags) + acc.AddFields("trig", fields, tags) - fmt.Printf("%#v\n",fields) + fmt.Printf("%#v\n", fields) return nil } - func init() { -plugins.Add("Trig", func() plugins.Plugin { return &Trig{x: 0.0} }) -} \ No newline at end of file + 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 index 5bd1fe79f..218cfad6c 100644 --- a/plugins/trig/trig_test.go +++ b/plugins/trig/trig_test.go @@ -1,9 +1,9 @@ package trig import ( - "testing" - "math" "fmt" + "math" + "testing" "github.com/influxdb/telegraf/testutil" "github.com/stretchr/testify/assert" @@ -15,13 +15,12 @@ func TestTrig(t *testing.T) { Amplitude: 10.0, } - - for i:=0.0; i < 10.0; i++ { + 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 + sine := math.Sin((i*math.Pi)/5.0) * s.Amplitude + cosine := math.Cos((i*math.Pi)/5.0) * s.Amplitude s.Gather(&acc) @@ -29,10 +28,9 @@ func TestTrig(t *testing.T) { fields["sine"] = sine fields["cosine"] = cosine - fmt.Printf("%#v\n",fields) + fmt.Printf("%#v\n", fields) assert.True(t, acc.CheckFieldsValue("trig", fields)) - + } } - From 9fa5ea847d8bc18ee695423b06167392897e06e2 Mon Sep 17 00:00:00 2001 From: Regan Kuchan Date: Mon, 30 Nov 2015 16:42:24 -0800 Subject: [PATCH 4/5] Remove unnecessary printing --- plugins/trig/trig.go | 3 --- plugins/trig/trig_test.go | 3 --- 2 files changed, 6 deletions(-) diff --git a/plugins/trig/trig.go b/plugins/trig/trig.go index 410bce9de..610a5852b 100644 --- a/plugins/trig/trig.go +++ b/plugins/trig/trig.go @@ -1,7 +1,6 @@ package trig import ( - "fmt" "math" "github.com/influxdb/telegraf/plugins" @@ -38,8 +37,6 @@ func (s *Trig) Gather(acc plugins.Accumulator) error { s.x += 1.0 acc.AddFields("trig", fields, tags) - fmt.Printf("%#v\n", fields) - return nil } diff --git a/plugins/trig/trig_test.go b/plugins/trig/trig_test.go index 218cfad6c..24218fe11 100644 --- a/plugins/trig/trig_test.go +++ b/plugins/trig/trig_test.go @@ -1,7 +1,6 @@ package trig import ( - "fmt" "math" "testing" @@ -28,8 +27,6 @@ func TestTrig(t *testing.T) { fields["sine"] = sine fields["cosine"] = cosine - fmt.Printf("%#v\n", fields) - assert.True(t, acc.CheckFieldsValue("trig", fields)) } From 3d0e3982d76e46eb01fa42561edc04128ccf46a3 Mon Sep 17 00:00:00 2001 From: Regan Kuchan Date: Tue, 1 Dec 2015 16:39:39 -0800 Subject: [PATCH 5/5] Update trig.go --- plugins/trig/trig.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/plugins/trig/trig.go b/plugins/trig/trig.go index 610a5852b..e966cbd46 100644 --- a/plugins/trig/trig.go +++ b/plugins/trig/trig.go @@ -12,8 +12,8 @@ type Trig struct { } var TrigConfig = ` -# Set the amplitude -amplitude = 10.0 + # Set the amplitude + amplitude = 10.0 ` func (s *Trig) SampleConfig() string { @@ -21,7 +21,7 @@ func (s *Trig) SampleConfig() string { } func (s *Trig) Description() string { - return "Insert trig data" + return "Inserts sine and cosine waves for demonstration purposes" } func (s *Trig) Gather(acc plugins.Accumulator) error {