Made embedded colors work logically
This commit is contained in:
@@ -12,6 +12,23 @@ import config
|
||||
log = logging.getLogger('healthbot')
|
||||
|
||||
|
||||
def _health_color(results):
|
||||
"""Embed color on a green -> yellow -> red gradient based on the share of
|
||||
critical services that are down (0% down = green, 50% = yellow, 100% = red)."""
|
||||
critical = [r for r in results if r.get('critical', True)]
|
||||
if not critical:
|
||||
return discord.Color.from_rgb(0, 255, 0)
|
||||
down = sum(1 for r in critical if not r['up'])
|
||||
ratio = down / len(critical)
|
||||
if ratio <= 0.5:
|
||||
t = ratio / 0.5
|
||||
red, green, blue = round(255 * t), 255, 0
|
||||
else:
|
||||
t = (ratio - 0.5) / 0.5
|
||||
red, green, blue = 255, round(255 * (1 - t)), 0
|
||||
return discord.Color.from_rgb(red, green, blue)
|
||||
|
||||
|
||||
class HealthBot(discord.Client):
|
||||
def __init__(self):
|
||||
intents = discord.Intents.default()
|
||||
@@ -61,8 +78,7 @@ class HealthBot(discord.Client):
|
||||
return await checks.run_checks(session, config.CHECK_TIMEOUT)
|
||||
|
||||
def build_embed(self, results):
|
||||
all_up = all(r['up'] for r in results if r.get('critical', True))
|
||||
color = discord.Color.green() if all_up else discord.Color.red()
|
||||
color = _health_color(results)
|
||||
embed = discord.Embed(
|
||||
title='Service Health',
|
||||
color=color,
|
||||
|
||||
Reference in New Issue
Block a user