<commit_before>package leader

import (
	"fmt"
	"log"
	"sort"

	"github.com/chrislusf/glow/resource"
)

func (tl *TeamLeader) allocate(req *resource.AllocationRequest) (result *resource.AllocationResult) {
	result = &resource.AllocationResult{}
	dc, err := tl.findDataCenter(req)
	if err != nil {
		result.Error = err.Error()
		return
	}

	allocations := tl.findServers(dc, req)

	result.Allocations = allocations
	return
}

func (tl *TeamLeader) allocateServersOnRack(dc *resource.DataCenter, rack *resource.Rack, requests []*resource.ComputeRequest) (
	allocated []resource.Allocation, remainingRequests []*resource.ComputeRequest) {
	for _, agent := range rack.Agents {
		available := agent.Resource.Minus(agent.Allocated)
		hasAllocation := true
		for available.GreaterThanZero() && hasAllocation {
			hasAllocation = false
			for _, request := range requests {
				if available.Covers(request.ComputeResource) {
					allocated = append(allocated, resource.Allocation{
						Location:  agent.Location,
						Allocated: request.ComputeResource,
					})
					agent.Allocated = agent.Allocated.Plus(request.ComputeResource)
					rack.Allocated = rack.Allocated.Plus(request.ComputeResource)
					dc.Allocated = dc.Allocated.Plus(request.ComputeResource)
					tl.LeaderResource.Topology.Allocated = tl.LeaderResource.Topology.Allocated.Plus(request.ComputeResource)
					available = available.Minus(request.ComputeResource)
					hasAllocation = true
				} else {
					remainingRequests = append(remainingRequests, request)
				}
			}
		}
	}
	return
}

func (tl *TeamLeader) findServers(dc *resource.DataCenter, req *resource.AllocationRequest) (ret []resource.Allocation) {
	// sort racks by unallocated resources
	racks := make([]*resource.Rack, 0, len(dc.Racks))
	for _, rack := range dc.Racks {
		racks = append(racks, rack)
	}<commit_msg>properly assign servers<commit_after>package leader

import (
	"fmt"
	"log"
	"sort"

	"github.com/chrislusf/glow/resource"
)

func (tl *TeamLeader) allocate(req *resource.AllocationRequest) (result *resource.AllocationResult) {
	result = &resource.AllocationResult{}
	dc, err := tl.findDataCenter(req)
	if err != nil {
		result.Error = err.Error()
		return
	}

	allocations := tl.findServers(dc, req)

	result.Allocations = allocations
	return
}

func (tl *TeamLeader) allocateServersOnRack(dc *resource.DataCenter, rack *resource.Rack, requests []*resource.ComputeRequest) (
	allocated []resource.Allocation, remainingRequests []*resource.ComputeRequest) {
	var j = -1
	for _, agent := range rack.Agents {
		if j >= len(requests) {
			break
		}
		available := agent.Resource.Minus(agent.Allocated)
		hasAllocation := true
		for available.GreaterThanZero() && hasAllocation && j < len(requests) {
			hasAllocation = false

			j++
			if j >= len(requests) {
				break
			}
			request := requests[j]

			fmt.Printf("available %v, requested %v\n", available, request.ComputeResource)
			if available.Covers(request.ComputeResource) {
				allocated = append(allocated, resource.Allocation{
					Location:  agent.Location,
					Allocated: request.ComputeResource,
				})
				agent.Allocated = agent.Allocated.Plus(request.ComputeResource)
				rack.Allocated = rack.Allocated.Plus(request.ComputeResource)
				dc.Allocated = dc.Allocated.Plus(request.ComputeResource)
				tl.LeaderResource.Topology.Allocated = tl.LeaderResource.Topology.Allocated.Plus(request.ComputeResource)
				available = available.Minus(request.ComputeResource)
				hasAllocation = true
			} else {
				remainingRequests = append(remainingRequests, request)
			}
		}
	}
	return
}

func (tl *TeamLeader) findServers(dc *resource.DataCenter, req *resource.AllocationRequest) (ret []resource.Allocation) {
	// sort racks by unallocated resources
	racks := make([]*resource.Rack, 0, len(dc.Racks))
	for _, rack := range dc.Racks {
		racks = append(racks, rack)
	}
{'fraction_non_alphanumeric': 0.12047569803516028, 'fraction_numerical': 0.0007755946225439503, 'mean_word_length': 4.418767507002801, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1082763', 'n_tokens_mistral': 1383, 'n_tokens_neox': 1259, 'n_words': 244}