Add actual plugin

This commit is contained in:
Regan Kuchan 2015-11-30 16:36:40 -08:00
parent 0e61208202
commit cd5bcce2c2
2 changed files with 88 additions and 0 deletions

50
plugins/trig/trig.go Normal file
View File

@ -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} })
}

38
plugins/trig/trig_test.go Normal file
View File

@ -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))
}
}