From e63784b0073a3548432712667fc2a084ceae3f27 Mon Sep 17 00:00:00 2001 From: Pradyumna Kaushik Date: Sat, 28 Jan 2017 19:32:54 -0500 Subject: [PATCH] Created utilities/offerUtils that can can hold all the utility functions for Offers. --- utilities/offerUtils/offerUtils.go | 40 ++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 utilities/offerUtils/offerUtils.go diff --git a/utilities/offerUtils/offerUtils.go b/utilities/offerUtils/offerUtils.go new file mode 100644 index 0000000..fc930f9 --- /dev/null +++ b/utilities/offerUtils/offerUtils.go @@ -0,0 +1,40 @@ +package offerUtils + +import ( + mesos "github.com/mesos/mesos-go/mesosproto" +) + +func OfferAgg(offer *mesos.Offer) (float64, float64, float64) { + var cpus, mem, watts float64 + + for _, resource := range offer.Resources { + switch resource.GetName() { + case "cpus": + cpus += *resource.GetScalar().Value + case "mem": + mem += *resource.GetScalar().Value + case "watts": + watts += *resource.GetScalar().Value + } + } + + return cpus, mem, watts +} + +type OffersSorter []*mesos.Offer + +func (offersSorter OffersSorter) Len() int { + return len(offersSorter) +} + +func (offersSorter OffersSorter) Swap(i, j int) { + offersSorter[i], offersSorter[j] = offersSorter[j], offersSorter[i] +} + +func (offersSorter OffersSorter) Less(i, j int) bool { + // getting CPU resource availability of offersSorter[i] + cpu1, _, _ := OfferAgg(offersSorter[i]) + // getting CPU resource availability of offersSorter[j] + cpu2, _, _ := OfferAgg(offersSorter[j]) + return cpu1 <= cpu2 +}