Skip to content

Commit a67979b

Browse files
authored
Ratio filter (#40)
* fix ratio filtering * fix output wide labels * added unit tests for float64 comparisons
1 parent 80cb0b3 commit a67979b

File tree

4 files changed

+59
-2
lines changed

4 files changed

+59
-2
lines changed

pkg/selector/comparators.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,17 @@ func isSupportedWithRangeInt(instanceTypeValue *int, target *IntRangeFilter) boo
5555
return isSupportedWithRangeInt64(instanceTypeValueInt64, target)
5656
}
5757

58+
func isSupportedWithFloat64(instanceTypeValue *float64, target *float64) bool {
59+
if target == nil {
60+
return true
61+
}
62+
if instanceTypeValue == nil {
63+
return false
64+
}
65+
// compare up to values' two decimal floor
66+
return math.Floor(*instanceTypeValue*100)/100 == math.Floor(*target*100)/100
67+
}
68+
5869
func isSupportedWithRangeInt64(instanceTypeValue *int64, target *IntRangeFilter) bool {
5970
if target == nil {
6071
return true

pkg/selector/comparators_internal_test.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,43 @@ func TestIsSupportedWithRangeUint64_Overflow(t *testing.T) {
191191
h.Assert(t, isSupported == true, "Uint64RangeFilter should match with 0 - MAX target and source 4")
192192
}
193193

194+
// float64
195+
196+
func TestIsSupportedWithFloat64_Supported(t *testing.T) {
197+
isSupported := isSupportedWithFloat64(aws.Float64(0.33), aws.Float64(0.33))
198+
h.Assert(t, isSupported == true, "Float64 comparison should match exactly with 2 decimal places")
199+
}
200+
201+
func TestIsSupportedWithFloat64_SupportedTruncatedDecPlacesExact(t *testing.T) {
202+
isSupported := isSupportedWithFloat64(aws.Float64(0.3322), aws.Float64(0.3322))
203+
h.Assert(t, isSupported == true, "Float64 comparison should match exactly with 4 decimal places")
204+
}
205+
206+
func TestIsSupportedWithFloat64_SupportedTruncatedDecPlaces(t *testing.T) {
207+
isSupported := isSupportedWithFloat64(aws.Float64(0.3399), aws.Float64(0.3311))
208+
h.Assert(t, isSupported == true, "Float64 comparison should match when truncating to 2 decimal places")
209+
}
210+
211+
func TestIsSupportedWithFloat64_Unsupported(t *testing.T) {
212+
isSupported := isSupportedWithFloat64(aws.Float64(0.4), aws.Float64(0.3399))
213+
h.Assert(t, isSupported == false, "Float64 comparison should NOT match")
214+
}
215+
216+
func TestIsSupportedWithFloat64_SourceNil(t *testing.T) {
217+
isSupported := isSupportedWithFloat64(nil, aws.Float64(0.3399))
218+
h.Assert(t, isSupported == false, "Float64 comparison should NOT match with nil source")
219+
}
220+
221+
func TestIsSupportedWithFloat64_TargetNil(t *testing.T) {
222+
isSupported := isSupportedWithFloat64(aws.Float64(0.3399), nil)
223+
h.Assert(t, isSupported == true, "Float64 comparison should match with nil target")
224+
}
225+
226+
func TestIsSupportedWithFloat64_BothNil(t *testing.T) {
227+
isSupported := isSupportedWithFloat64(nil, nil)
228+
h.Assert(t, isSupported == true, "Float64 comparison should match with nil target and source")
229+
}
230+
194231
// bools
195232

196233
func TestSupportSyntaxToBool_Supported(t *testing.T) {

pkg/selector/outputs/outputs.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -201,15 +201,15 @@ func TableOutputWide(instanceTypeInfoSlice []*ec2.InstanceTypeInfo) []string {
201201
headers := []interface{}{
202202
"Instance Type",
203203
"VCPUs",
204-
"Mem (MiB)",
204+
"Mem (GiB)",
205205
"Hypervisor",
206206
"Current Gen",
207207
"Hibernation Support",
208208
"CPU Arch",
209209
"Network Performance",
210210
"ENIs",
211211
"GPUs",
212-
"GPU Mem (MiB)",
212+
"GPU Mem (GiB)",
213213
"GPU Info",
214214
}
215215
separators := []interface{}{}

pkg/selector/selector.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,15 @@ func (itf Selector) executeFilters(filterToInstanceSpecMapping map[string]filter
323323
default:
324324
return false, fmt.Errorf(invalidInstanceSpecTypeMsg)
325325
}
326+
case *float64:
327+
switch iSpec := instanceSpec.(type) {
328+
case *float64:
329+
if !isSupportedWithFloat64(iSpec, filter) {
330+
return false, nil
331+
}
332+
default:
333+
return false, fmt.Errorf(invalidInstanceSpecTypeMsg)
334+
}
326335
default:
327336
return false, fmt.Errorf("No filter handler found for %s", filterDetailsMsg)
328337
}

0 commit comments

Comments
 (0)