Skip to content

Commit

Permalink
feat(inputs.mock): Add baseline option to sine (#15270)
Browse files Browse the repository at this point in the history
  • Loading branch information
LarsStegman authored Apr 30, 2024
1 parent ae50801 commit ee9df0a
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 3 deletions.
3 changes: 2 additions & 1 deletion plugins/inputs/mock/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ See the [CONFIGURATION.md][CONFIGURATION.md] for more details.
# name = "wave"
# amplitude = 1.0
# period = 0.5
# base_line = 0.0
# [[inputs.mock.step]]
# name = "plus_one"
# start = 0.0
Expand All @@ -62,7 +63,7 @@ The available algorithms for generating mock data include:
* Constant - generate a field with the given value of type string, float, int
or bool
* Random Float - generate a random float, inclusive of min and max
* Sine Wave - produce a sine wave with a certain amplitude and period
* Sine Wave - produce a sine wave with a certain amplitude, period and baseline
* Step - always add the step value, negative values accepted
* Stock - generate fake, stock-like price values based on a volatility variable

Expand Down
3 changes: 2 additions & 1 deletion plugins/inputs/mock/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ type sineWave struct {
Name string `toml:"name"`
Amplitude float64 `toml:"amplitude"`
Period float64 `toml:"period"`
BaseLine float64 `toml:"base_line"`
}

type step struct {
Expand Down Expand Up @@ -117,7 +118,7 @@ func (m *Mock) generateRandomFloat64(fields map[string]interface{}) {
// Create sine waves
func (m *Mock) generateSineWave(fields map[string]interface{}) {
for _, field := range m.SineWave {
fields[field.Name] = math.Sin(float64(m.counter)*field.Period*math.Pi) * field.Amplitude
fields[field.Name] = math.Sin(float64(m.counter)*field.Period*math.Pi)*field.Amplitude + field.BaseLine
}
}

Expand Down
3 changes: 2 additions & 1 deletion plugins/inputs/mock/mock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ func TestGather(t *testing.T) {
Name: "sine",
Amplitude: 1.0,
Period: 0.5,
BaseLine: 2.0,
}
testStep := &step{
Name: "step",
Expand Down Expand Up @@ -87,7 +88,7 @@ func TestGather(t *testing.T) {
require.GreaterOrEqual(t, 6.0, v)
require.LessOrEqual(t, 1.0, v)
case "sine":
require.Equal(t, 0.0, v)
require.Equal(t, 2.0, v)
case "step":
require.Equal(t, 0.0, v)
default:
Expand Down
1 change: 1 addition & 0 deletions plugins/inputs/mock/sample.conf
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
# name = "wave"
# amplitude = 1.0
# period = 0.5
# base_line = 0.0
# [[inputs.mock.step]]
# name = "plus_one"
# start = 0.0
Expand Down

0 comments on commit ee9df0a

Please sign in to comment.