Made embedded colors work logically
This commit is contained in:
@@ -12,6 +12,23 @@ import config
|
|||||||
log = logging.getLogger('healthbot')
|
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):
|
class HealthBot(discord.Client):
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
intents = discord.Intents.default()
|
intents = discord.Intents.default()
|
||||||
@@ -61,8 +78,7 @@ class HealthBot(discord.Client):
|
|||||||
return await checks.run_checks(session, config.CHECK_TIMEOUT)
|
return await checks.run_checks(session, config.CHECK_TIMEOUT)
|
||||||
|
|
||||||
def build_embed(self, results):
|
def build_embed(self, results):
|
||||||
all_up = all(r['up'] for r in results if r.get('critical', True))
|
color = _health_color(results)
|
||||||
color = discord.Color.green() if all_up else discord.Color.red()
|
|
||||||
embed = discord.Embed(
|
embed = discord.Embed(
|
||||||
title='Service Health',
|
title='Service Health',
|
||||||
color=color,
|
color=color,
|
||||||
|
|||||||
Reference in New Issue
Block a user