-
Notifications
You must be signed in to change notification settings - Fork 107
Instance sorting #138
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
Instance sorting #138
Changes from all commits
6c596cd
8ea3f10
ef7b781
6c5ce45
5035033
27a434d
f0fcb1b
36bbfb3
60fdce7
2871abf
244789b
bdf0a3e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,11 @@ | ||
package main | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
|
||
"github.com/aws/amazon-ec2-instance-selector/v2/pkg/bytequantity" | ||
"github.com/aws/amazon-ec2-instance-selector/v2/pkg/cli/sorter" | ||
"github.com/aws/amazon-ec2-instance-selector/v2/pkg/selector" | ||
"github.com/aws/amazon-ec2-instance-selector/v2/pkg/selector/outputs" | ||
"github.com/aws/aws-sdk-go/aws" | ||
|
@@ -38,33 +40,71 @@ func main() { | |
// when creating the Filter struct | ||
cpuArch := "x86_64" | ||
|
||
priceRange := selector.Float64RangeFilter{ | ||
LowerBound: 0, | ||
UpperBound: 100, | ||
} | ||
|
||
// Create a Filter struct with criteria you would like to filter | ||
// The full struct definition can be found here for all of the supported filters: | ||
// https://github.com/aws/amazon-ec2-instance-selector/blob/main/pkg/selector/types.go | ||
filters := selector.Filters{ | ||
VCpusRange: &vcpusRange, | ||
MemoryRange: &memoryRange, | ||
CPUArchitecture: &cpuArch, | ||
PricePerHour: &priceRange, | ||
} | ||
|
||
// Pass the Filter struct to the FilteredInstanceTypes function of your | ||
// selector instance to get a list of filtered instance types and their details | ||
// selector instance to get a list of filtered instance types and their details. | ||
instanceTypesSlice, err := instanceSelector.FilterInstanceTypes(filters) | ||
if err != nil { | ||
fmt.Printf("Oh no, there was an error getting instance types: %v", err) | ||
return | ||
} | ||
|
||
// Pass in your list of instance type details to the appropriate output function | ||
// in order to format the instance types as printable strings. | ||
maxResults := 100 | ||
// Truncate results and format them for output with your desired formatting function. | ||
// All formatting functions can be found here: | ||
// https://github.com/aws/amazon-ec2-instance-selector/blob/main/pkg/selector/outputs/outputs.go | ||
// Examples of formatted outputs can be found here: | ||
// https://github.com/aws/amazon-ec2-instance-selector#examples | ||
maxResults := 10 | ||
instanceTypesSlice, _, err = outputs.TruncateResults(&maxResults, instanceTypesSlice) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think we really need this for outputs. It's trivial to truncate a slice. It's not clear why this returns 3 values too. What error could be returned from a truncation? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
That is a good point. I am going to move
The 3 values are the truncated list, the number of truncated items, and an error. The error exists to catch invalid There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For this type of func, I'd drop the For example,
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh that is a very good idea. I'll change the output of |
||
if err != nil { | ||
fmt.Printf("Oh no, there was an error truncating instnace types: %v", err) | ||
fmt.Printf("Oh no, there was an error truncating instance types: %v", err) | ||
return | ||
} | ||
instanceTypes := outputs.SimpleInstanceTypeOutput(instanceTypesSlice) | ||
|
||
instanceTypesSlice[3].OnDemandPricePerHour = nil | ||
instanceTypesSlice[8].OnDemandPricePerHour = nil | ||
|
||
// Pass in the list of instance type details to the SortInstanceTypes function | ||
// if you wish to sort the instances based on set filters. | ||
sortFilter := "on-demand-price" | ||
sortDirection := "descending" | ||
sortedTypes, err := instanceSelector.SortInstanceTypes(instanceTypesSlice, &sortFilter, &sortDirection) | ||
if err != nil { | ||
fmt.Printf("Oh no, there was an error sorting instance types: %v", err) | ||
return | ||
} | ||
instanceTypes := outputs.TableOutputWide(sortedTypes) | ||
|
||
jsonTypeIndent, err := json.MarshalIndent(instanceTypesSlice[0], "", " ") | ||
fmt.Println(string(jsonTypeIndent)) | ||
fmt.Println() | ||
|
||
// Print the returned instance types slice | ||
fmt.Println(instanceTypes) | ||
|
||
// TODO: remove this. This is for testing purposes | ||
fmt.Println() | ||
|
||
// jsonType, err := json.Marshal(instanceTypesSlice[0]) | ||
// fmt.Println(string(jsonType) + "\n") | ||
|
||
sorter, err := sorter.NewSorter(instanceTypesSlice, "$.OndemandPricePerHour", sortDirection) | ||
sorter.Sort() | ||
types := sorter.InstanceTypes() | ||
str := outputs.TableOutputWide(types) | ||
fmt.Printf(str[0]) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: what do you think about changing this to
sort-by
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah I agree with this change.
sort-by
makes more intuitive sense for what the purpose of the flag is.