Added a generic task sorter utility. This allows for an array of tasks to be sorted based on any resource. The possible resources by which an array of tasks can be sorted currently are listed in def/sortingCriteria.go. One can add more to this if required.
This commit is contained in:
parent
9b9dc73269
commit
f29b7f51a9
2 changed files with 44 additions and 0 deletions
29
def/sortingCriteria.go
Normal file
29
def/sortingCriteria.go
Normal file
|
@ -0,0 +1,29 @@
|
|||
package def
|
||||
|
||||
// Creating an enumeration of the resources in def.Task.
|
||||
var TaskResourceNames []string
|
||||
|
||||
type SortCriteria int
|
||||
|
||||
// Map a task's resource name to the sorting criteria (an integer corresponding to the enumeration).
|
||||
func resourceToSortCriteria(resourceName string) SortCriteria {
|
||||
// Appending resourceName to TaskResourceNames.
|
||||
TaskResourceNames = append(TaskResourceNames, resourceName)
|
||||
|
||||
// Considering index of resource in TaskResourceNames to be the int mapping.
|
||||
return SortCriteria(len(TaskResourceNames) - 1)
|
||||
}
|
||||
|
||||
func (sc SortCriteria) String() string {
|
||||
return TaskResourceNames[int(sc)]
|
||||
}
|
||||
|
||||
// Possible Sorting Criteria
|
||||
// Note: the value of the string passed as argument to resourceToSortCriteria() should be the same (case-sensitive)
|
||||
// as the name of the of the corresponding resource in the struct.
|
||||
var (
|
||||
CPU = resourceToSortCriteria("CPU")
|
||||
RAM = resourceToSortCriteria("RAM")
|
||||
Watts = resourceToSortCriteria("Watts")
|
||||
Instances = resourceToSortCriteria("Instances")
|
||||
)
|
|
@ -4,6 +4,7 @@ import (
|
|||
"github.com/mdesenfants/gokmeans"
|
||||
"sort"
|
||||
"log"
|
||||
"reflect"
|
||||
)
|
||||
|
||||
// Information about a cluster of tasks
|
||||
|
@ -114,3 +115,17 @@ func labelAndOrder(clusters map[int][]Task, numberOfClusters int, taskObservatio
|
|||
})
|
||||
return sizedClusters
|
||||
}
|
||||
|
||||
// Generic Task Sorter.
|
||||
// Be able to sort an array of tasks based on any of the tasks' resources.
|
||||
|
||||
// Retrieve a sorter (same signature as 'Less' function in sort.Interface) for the given sorting criteria.
|
||||
func TaskSorter(sc SortCriteria, tasks []Task) func (i, j int) bool {
|
||||
return func (i, j int) bool {
|
||||
taskIFields := reflect.Indirect(reflect.ValueOf(tasks[i]))
|
||||
tasksJFields := reflect.Indirect(reflect.ValueOf(tasks[j]))
|
||||
resourceI := taskIFields.FieldByName(sc.String()).Float()
|
||||
resourceJ := tasksJFields.FieldByName(sc.String()).Float()
|
||||
return resourceI <= resourceJ
|
||||
}
|
||||
}
|
||||
|
|
Reference in a new issue