53 lines
1.1 KiB
Python
53 lines
1.1 KiB
Python
from policy.engine import PolicyEngine
|
|
|
|
|
|
class TargetOptimizer:
|
|
|
|
def __init__(self, nodes, policy_engine):
|
|
self.nodes = nodes
|
|
self.policy = policy_engine
|
|
|
|
def choose(self, action, target):
|
|
|
|
candidates = []
|
|
|
|
for node in self.nodes:
|
|
|
|
if not self._compatible(node, target):
|
|
continue
|
|
|
|
base_score = self._score(node)
|
|
|
|
policy_score = self.policy.evaluate(action, target, node)
|
|
|
|
total = base_score + policy_score
|
|
|
|
candidates.append((node, total))
|
|
|
|
if not candidates:
|
|
raise Exception("No compatible nodes")
|
|
|
|
candidates.sort(key=lambda x: x[1], reverse=True)
|
|
|
|
return candidates[0][0]
|
|
|
|
def _compatible(self, node, target):
|
|
|
|
if node["arch"] != target.arch:
|
|
if target.runtime == "host":
|
|
return False
|
|
|
|
if target.runtime not in node["runtimes"]:
|
|
return False
|
|
|
|
return True
|
|
|
|
def _score(self, node):
|
|
|
|
score = 0
|
|
|
|
score += (100 - node.get("cpu_load", 50))
|
|
score += (100 - node.get("temp", 60))
|
|
|
|
return score
|