Browse Source

Added ability to have more than one api token for healthchecks.io

causefx 7 years ago
parent
commit
a829522acd
2 changed files with 21 additions and 12 deletions
  1. 20 12
      api/functions/homepage-connect-functions.php
  2. 1 0
      js/functions.js

+ 20 - 12
api/functions/homepage-connect-functions.php

@@ -81,19 +81,27 @@ function healthChecksTags($tags)
 function getHealthChecks($tags = null)
 {
 	if ($GLOBALS['homepageHealthChecksEnabled'] && !empty($GLOBALS['healthChecksToken']) && qualifyRequest($GLOBALS['homepageHealthChecksAuth'])) {
+		$api['content']['checks'] = array();
 		$tags = ($tags) ? healthChecksTags($tags) : '';
-		$url = 'https://healthchecks.io/api/v1/checks/' . $tags;
-		try {
-			$headers = array('X-Api-Key' => $GLOBALS['healthChecksToken']);
-			$options = (localURL($url)) ? array('verify' => false) : array();
-			$response = Requests::get($url, $headers, $options);
-			if ($response->success) {
-				$api['content'] = json_decode($response->body, true);
-			}
-		} catch (Requests_Exception $e) {
-			writeLog('error', 'HealthChecks Connect Function - Error: ' . $e->getMessage(), 'SYSTEM');
-		};
-		$api['content'] = isset($api['content']) ? $api['content'] : false;
+		$healthChecks = explode(',', $GLOBALS['healthChecksToken']);
+		foreach ($healthChecks as $token) {
+			$url = 'https://healthchecks.io/api/v1/checks/' . $tags;
+			try {
+				$headers = array('X-Api-Key' => $token);
+				$options = (localURL($url)) ? array('verify' => false) : array();
+				$response = Requests::get($url, $headers, $options);
+				if ($response->success) {
+					$healthResults = json_decode($response->body, true);
+					$api['content']['checks'] = array_merge($api['content']['checks'], $healthResults['checks']);
+				}
+			} catch (Requests_Exception $e) {
+				writeLog('error', 'HealthChecks Connect Function - Error: ' . $e->getMessage(), 'SYSTEM');
+			};
+		}
+		usort($api['content']['checks'], function ($a, $b) {
+			return $a['status'] <=> $b['status'];
+		});
+		$api['content']['checks'] = isset($api['content']['checks']) ? $api['content']['checks'] : false;
 		return $api;
 	}
 	return false;

+ 1 - 0
js/functions.js

@@ -5325,6 +5325,7 @@ function buildCalendarMetadata(array){
 	return metadata;
 }
 function buildHealthChecks(array){
+    if(array === false){ return ''; }
     var checks = (typeof array.content.checks !== 'undefined') ? array.content.checks.length : false;
     return (checks) ? `
 	<div id="allHealthChecks">