fester/backend/metrics/cluster_state.py

42 lines
938 B
Python

class ClusterState:
def __init__(self):
self.nodes = {}
def register_node(self, node_id, node_state):
self.nodes[node_id] = node_state
# -----------------------------
# GLOBAL AGGREGATION (VERY CHEAP)
# -----------------------------
def snapshot(self):
hot_nodes = 0
total_temp = 0
total_cpu = 0
instability_sum = 0
n = len(self.nodes)
if n == 0:
return {}
for node in self.nodes.values():
s = node.snapshot()
total_temp += s["avg_temp"]
total_cpu += s["avg_cpu"]
instability_sum += s["instability"]
if s["avg_temp"] > 80:
hot_nodes += 1
return {
"avg_cluster_temp": total_temp / n,
"avg_cluster_cpu": total_cpu / n,
"instability": instability_sum / n,
"hot_nodes": hot_nodes
}