From d2a87c0f66b814429c422abe3072a82614175fd7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Gro=C3=9F?= Date: Thu, 25 May 2023 10:52:52 +0200 Subject: [PATCH] Refactor threshold magic values --- src/index.js | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/index.js b/src/index.js index 7dd16db..ee3d372 100644 --- a/src/index.js +++ b/src/index.js @@ -18,6 +18,11 @@ const images = { up: "https://cdn.discordapp.com/attachments/1083687175998152714/1110913780268924958/605e7b80-9b97-4792-b903-eaa482dd7f11.jpeg" } +const threshold = { + warn: 5, + down: 10 +} + let consecutiveFailures = 0; // Function to ping the API endpoint @@ -29,7 +34,7 @@ async function pingEndpoint() { // Check if the response status is successful (2xx) if (response.status === 200) { console.log('API endpoint is online.'); - if (consecutiveFailures != 0) { + if (consecutiveFailures >= threshold.warn) { consecutiveFailures = 0; sendNotification("Recovered", "The system has recovered", color='#00FF00', image=images.up); } @@ -49,11 +54,11 @@ async function pingEndpoint() { function handleFailure(errorMessage) { console.error(errorMessage); - if (consecutiveFailures == 10) { + if (consecutiveFailures == threshold.down) { sendNotification("Down", errorMessage, color='#ff0000', image=images.down); return; } - if (consecutiveFailures == 5) { + if (consecutiveFailures == threshold.warn) { sendNotification("Intermittent issues", errorMessage, '#D9B611'); return; }