Refactor upload_stats_to_grist function in grpc-balancer.py to consolidate server statistics into a single dictionary, improving clarity and maintainability. Update error logging to include exception details.

This commit is contained in:
vvzvlad 2025-01-17 07:20:54 +03:00
parent 8425114abf
commit 77a5f1a071

View File

@ -268,25 +268,26 @@ def select_servers():
def upload_stats_to_grist(update_row):
while True:
try:
stats = {}
stats['failures'] = 0
stats['successes'] = 0
stats['rps'] = 0
total_stats = {
'failures': 0,
'successes': 0,
'rps': 0
}
with STATS_LOCK:
for server in BACKEND_SERVERS:
server_id = server['id']
stats = SERVER_STATS[server_id]
failures = sum(1 for t, success in stats if not success)
successes = len(stats) - failures
stats['failures'] += failures
stats['successes'] += successes
stats['rps'] += len(stats)/STATISTICS_WINDOW.total_seconds()
#logging.info(f"Server {server_id}: {successes} successes, {failures} failures, Weight: {weight:.2f}")
server_stats = SERVER_STATS[server_id]
failures = sum(1 for t, success in server_stats if not success)
successes = len(server_stats) - failures
total_stats['failures'] += failures
total_stats['successes'] += successes
total_stats['rps'] += len(server_stats)/STATISTICS_WINDOW.total_seconds()
health = f"{stats['successes']}/{stats['failures']}/{stats['rps']}"
health = f"{total_stats['successes']}/{total_stats['failures']}/{total_stats['rps']:.2f}"
update_row({"Health": health})
except Exception as e:
logging.error(f"Failed to upload stats to Grist: {e}")
logging.error(f"Failed to upload stats to Grist: {str(e)}")
time.sleep(30)
if __name__ == '__main__':