Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(inputs.mock): Add baseline option to sine #15270

Merged
merged 1 commit into from
Apr 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading