index.php 3.42 KB
Newer Older
1
<?php
2
require_once "load.php";
3 4 5 6 7 8

$results = dbw_query($db_conn,"SELECT * FROM GROUPS"); //All groups

while ($group = dbw_fetch_array($db_conn,$results)){ //Know name of all groups
	$groups[$group['ID_G']] =  $group['name'];
}
JKA Network's avatar
JKA Network committed
9
unset($results); //Free mem
10 11 12 13 14 15 16

$results = dbw_query($db_conn,"SELECT * FROM CHECKS ORDER BY ID_G"); //All checks

while ($onechk = dbw_fetch_array($db_conn,$results)){
	$idchk = $onechk['ID_C'];
	$chks[$idchk] = $onechk; //First array data
	$chks[$idchk]['nameGroup'] = $groups[$onechk['ID_G']]; //Know group of this check
JKA Network's avatar
JKA Network committed
17
	$ID_TC = $chks[$idchk]['ID_TC']; //Type of Check
18

JKA Network's avatar
JKA Network committed
19
	switch ($ID_TC){
20
		// Case 3 (Visits) is numeric, not status
21 22
		case 1: //Ping 
		case 2: //HttpCode
23 24
		case 4: //MySQL
		case 5: //JSON GET|POST
25

JKA Network's avatar
JKA Network committed
26 27
			//To enter or not, check if has history (Instead of count > 0, only see if there is any timestamp, is faster)
			if (dbw_query_fetch_array($db_conn, "SELECT `timestamp` FROM CHKHIST WHERE ID_C = '$idchk' LIMIT 1")[0] != 0){
28
				
JKA Network's avatar
JKA Network committed
29
				$consul = dbw_query_fetch_array($db_conn, "SELECT `code`,`timestamp` FROM CHKHIST WHERE ID_C = '$idchk' ORDER BY `timestamp` DESC LIMIT 1");
30
				$chks[$idchk]['dateLastChk'] = date('d/m H:i',$consul['timestamp']);
31

32 33
				$chks[$idchk]['failedLastChk'] = $consul['code'];

34
				$lastErr = dbw_query_fetch_array($db_conn, "SELECT MAX(`timestamp`) FROM CHKHIST WHERE ID_C = '$idchk' AND code != 0")[0]; //Record last error, for showing if there are any.
35 36 37 38
				if ($lastErr != false){
					$chks[$idchk]['dateLastErr'] = date('d/m H:i',$lastErr);
				}

Kevin Puertas Ruiz's avatar
Kevin Puertas Ruiz committed
39
				$chks[$idchk]['nameCheck'] = arrayTypeChk()[$ID_TC]; //To send array name and not the word
40 41 42 43

				if ($onechk['manStatus'] != ''){
					$chks[$idchk]['uptime'] = 255;//255 = Maintenance I suppose, not implemented
				}else{
44
					$chks[$idchk]['uptime'] = getUptime($idchk);
45
				}
46
				list($chks[$idchk]['statusText'],$chks[$idchk]['statusColor']) = getStatus($idchk);
JKA Network's avatar
JKA Network committed
47 48
			}else{ //If nothing collected yet
				$chks[$idchk]['uptime'] = -1; // -1 is nothing in twig page
49 50 51 52 53
			}

		break;

		case 3: //Visits
JKA Network's avatar
JKA Network committed
54
			$visitsSite = dbw_query($db_conn, "SELECT * FROM (SELECT * FROM VISITS WHERE ID_C = '$idchk' ORDER BY `date` DESC LIMIT 0,5) sub ORDER BY `date` ASC"); //Subquery for reorder lastest days in reverse
55 56 57 58 59 60 61 62 63 64 65 66 67
			//Create array from last 5 days
			while ($visitsDay = dbw_fetch_array($db_conn, $visitsSite)){
				$dateArray[] = $visitsDay['date'];
				$visitsArray[] = $visitsDay['visits'];
				$uniqueVisitsArray[] = $visitsDay['uniqueVisits'];
			}
			$chks[$idchk]['dateArray'] = implode(',',$dateArray);
			$chks[$idchk]['visitsArray'] = implode(',',$visitsArray);
		break;
	}

}

JKA Network's avatar
JKA Network committed
68
//Here load "News"
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86

$incidents = dbw_query($db_conn,"SELECT * FROM NEWS ORDER BY ID_N DESC LIMIT 20");
while ($incident = dbw_fetch_array($db_conn,$incidents)){
	$ID_N = $incident['ID_N'];
	$incs[$ID_N]['day'] = date('j/n/Y', $incident['timestamp']);
	require_once "lib/parsedown.php"; //Import parsedown (once)
		
	$incs[$ID_N]['sentBy'] = $incident['sentBy'];
	if ($incident['sentBy'] != NULL && substr($incident['sentBy'],0,1) == "S"){ // Sent by a page 
		$pageinc = substr($incident['sentBy'],1);
		$pageinc = dbw_query_fetch_array($db_conn,"SELECT name FROM CHECKS WHERE ID_C = '$pageinc'")[0];
		$incs[$ID_N]['sentBy'] = $pageinc;
	}
	$incs[$ID_N]['text'] = Parsedown::instance()
			->setMarkupEscaped(true) # escapes markup (HTML)
			->text($incident['text']);
	$incs[$ID_N]['ID_N'] = $incident['ID_N'];
}
Kevin Puertas Ruiz's avatar
Kevin Puertas Ruiz committed
87
	renderPage('indexpage.twig',array('checks' => $chks, 'news' => $incs)); //Render
JKA Network's avatar
JKA Network committed
88
?>