Skip to content

Commit

Permalink
Review Threshold and add DoesViolate()
Browse files Browse the repository at this point in the history
  • Loading branch information
lazyfrosch committed Mar 29, 2021
1 parent db26eb0 commit 60499fd
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 13 deletions.
18 changes: 9 additions & 9 deletions threshold.go
Expand Up @@ -81,16 +81,17 @@ func ParseThreshold(spec string) (t *Threshold, err error) {
return
}

// String returns the plain representation of the Threshold
func (t Threshold) String() (s string) {
s = BoundToString(t.Upper)
s = BoundaryToString(t.Upper)

// remove upper ~, which is the default
if s == "~" {
s = ""
}

if t.Lower != 0 {
s = BoundToString(t.Lower) + ":" + s
s = BoundaryToString(t.Lower) + ":" + s
}

if t.Inside {
Expand All @@ -102,11 +103,15 @@ func (t Threshold) String() (s string) {

// Compares a value against the threshold, and returns true if the value violates the threshold.
func (t Threshold) DoesViolate(value float64) bool {
panic("not yet implemented") // TODO: implement me
if t.Inside {
return value >= t.Lower && value <= t.Upper
} else {
return value < t.Lower || value > t.Upper
}
}

// Convert a threshold bound to its string representation
func BoundToString(value float64) (s string) {
func BoundaryToString(value float64) (s string) {
s = fmt.Sprintf("%g", value)

// In the threshold context, the sign derives from lower and upper bound, we only need the ~ notation
Expand All @@ -116,8 +121,3 @@ func BoundToString(value float64) (s string) {

return
}

// TODO: remove
func formatRange(someRange Threshold) string {
return someRange.String()
}
59 changes: 55 additions & 4 deletions threshold_test.go
Expand Up @@ -15,10 +15,10 @@ var testThresholds = map[string]*Threshold{
"": nil,
}

func TestBoundToString(t *testing.T) {
assert.Equal(t, "10", BoundToString(10))
assert.Equal(t, "10.1", BoundToString(10.1))
assert.Equal(t, "10.0000000000001", BoundToString(10.0000000000001))
func TestBoundaryToString(t *testing.T) {
assert.Equal(t, "10", BoundaryToString(10))
assert.Equal(t, "10.1", BoundaryToString(10.1))
assert.Equal(t, "10.0000000000001", BoundaryToString(10.0000000000001))
}

func TestParseThreshold(t *testing.T) {
Expand All @@ -41,3 +41,54 @@ func TestThreshold_String(t *testing.T) {
}
}
}

// Threshold Generate an alert if x...
// 10 < 0 or > 10, (outside the range of {0 .. 10})
// 10: < 10, (outside {10 .. ∞})
// ~:10 > 10, (outside the range of {-∞ .. 10})
// 10:20 < 10 or > 20, (outside the range of {10 .. 20})
// @10:20 ≥ 10 and ≤ 20, (inside the range of {10 .. 20})
func TestThreshold_DoesViolate(t *testing.T) {
thr, err := ParseThreshold("10")
assert.NoError(t, err)
assert.True(t, thr.DoesViolate(11))
assert.False(t, thr.DoesViolate(10))
assert.False(t, thr.DoesViolate(0))
assert.True(t, thr.DoesViolate(-1))

thr, err = ParseThreshold("10:")
assert.NoError(t, err)
assert.False(t, thr.DoesViolate(3000))
assert.False(t, thr.DoesViolate(10))
assert.True(t, thr.DoesViolate(9))
assert.True(t, thr.DoesViolate(0))
assert.True(t, thr.DoesViolate(-1))

thr, err = ParseThreshold("~:10")
assert.NoError(t, err)
assert.False(t, thr.DoesViolate(-3000))
assert.False(t, thr.DoesViolate(0))
assert.False(t, thr.DoesViolate(10))
assert.True(t, thr.DoesViolate(11))
assert.True(t, thr.DoesViolate(3000))

thr, err = ParseThreshold("10:20")
assert.NoError(t, err)
assert.False(t, thr.DoesViolate(10))
assert.False(t, thr.DoesViolate(15))
assert.False(t, thr.DoesViolate(20))
assert.True(t, thr.DoesViolate(9))
assert.True(t, thr.DoesViolate(-1))
assert.True(t, thr.DoesViolate(20.1))
assert.True(t, thr.DoesViolate(3000))

thr, err = ParseThreshold("@10:20")
assert.NoError(t, err)
assert.True(t, thr.DoesViolate(10))
assert.True(t, thr.DoesViolate(15))
assert.True(t, thr.DoesViolate(20))
assert.False(t, thr.DoesViolate(9))
assert.False(t, thr.DoesViolate(-1))
assert.False(t, thr.DoesViolate(20.1))
assert.False(t, thr.DoesViolate(3000))
}

0 comments on commit 60499fd

Please sign in to comment.