Skip to content

Commit

Permalink
Refactor (Range).checkOutsideRange
Browse files Browse the repository at this point in the history
Replace existing method with implementation provided by Google
Gemini (model 'Gemini 1.5 Flash').

Include commented out alternative implementation from ChatGPT
(model 'GPT-4o') for contrast.

refs GH-278
  • Loading branch information
atc0005 committed Nov 6, 2024
1 parent 12c2ed5 commit c1b8929
Showing 1 changed file with 46 additions and 21 deletions.
67 changes: 46 additions & 21 deletions range.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,30 +47,55 @@ func (r Range) CheckRange(value string) bool {
//
// [Nagios Plugin Dev Guidelines: Threshold and Ranges]: https://nagios-plugins.org/doc/guidelines.html#THRESHOLDFORMAT
func (r Range) checkOutsideRange(valueAsAFloat float64) bool {
switch {
case !r.EndInfinity && !r.StartInfinity:
if r.Start <= valueAsAFloat && valueAsAFloat <= r.End {
return false
}
return true

case !r.StartInfinity && r.EndInfinity:
if valueAsAFloat >= r.Start {
return false
}
return true

case r.StartInfinity && !r.EndInfinity:
if valueAsAFloat <= r.End {
return false
}
return true

default:
return false
// This alternative implementation was provided by Google Gemini (model
// 'Gemini 1.5 Flash').
//
// Explanation of the logic:
//
// Infinite Bounds:
//
// If the start is infinite, the value is outside the range only if it's
// greater than the end. If the end is infinite, the value is outside the
// range only if it's less than the start.
//
// Finite Bounds:
//
// The value is outside the range if it's either less than the start or
// greater than the end.

// Handle infinite bounds first
if r.StartInfinity {
return valueAsAFloat > r.End
} else if r.EndInfinity {
return valueAsAFloat < r.Start
}

// Handle finite bounds
return valueAsAFloat < r.Start || valueAsAFloat > r.End
}

// checkOutsideRange is provided by ChatGPT (model 'GPT-4o') as a
// simplification of the original checkOutsideRange function.
// func (r Range) checkOutsideRange(value float64) bool {
// // Explanation of the Simplification:
// //
// // Each case is now focused only on the conditions that make the value
// // outside the range. The final default case covers the fully infinite
// // range, simplifying the logic to just return false since no bounds
// // restrict the range.
//
// switch {
// case !r.StartInfinity && value < r.Start:
// return true
// case !r.EndInfinity && value > r.End:
// return true
// case r.StartInfinity && r.EndInfinity:
// return false
// default:
// return false
// }
// }

// ParseRangeString static method to construct a Range object from the string
// representation based on the [Nagios Plugin Dev Guidelines: Threshold and
// Ranges] definition.
Expand Down

0 comments on commit c1b8929

Please sign in to comment.