36 lines
832 B
Python
36 lines
832 B
Python
from backend.nodes.state_model import NodeStateRegistry
|
|
|
|
|
|
def choose_best_node(nodes, action, registry: NodeStateRegistry = None):
|
|
|
|
best_node = None
|
|
best_score = -999999
|
|
|
|
for node in nodes:
|
|
|
|
state = registry.get(node["name"]) if registry else None
|
|
|
|
cpu_load = state.cpu_load if state else node.get("cpu_load", 0)
|
|
temp = state.temp if state else node.get("temp", 0)
|
|
|
|
score = 0
|
|
score += (100 - cpu_load)
|
|
score -= temp
|
|
|
|
if node.get("policy") == "preferred":
|
|
score += 20
|
|
|
|
if node.get("policy") == "avoid":
|
|
score -= 50
|
|
|
|
if state:
|
|
score -= state.instability * 10
|
|
|
|
node["_computed_score"] = score
|
|
|
|
if score > best_score:
|
|
best_score = score
|
|
best_node = node
|
|
|
|
return best_node
|