Last active
August 8, 2018 22:04
-
-
Save brunohq/b565c35d96ae255d4b7d to your computer and use it in GitHub Desktop.
Geckboard Dashboard
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
date_default_timezone_set('Europe/London'); | |
$now = date('F j, g:i:s a'); | |
$entity = "Anonymous"; | |
if(isset($_GET["entity"])) $entity = trim($_GET["entity"]); | |
$action = "visit"; | |
if(isset($_GET["action"])) $action = trim($_GET["action"]); | |
$ip = $_SERVER['REMOTE_ADDR']; | |
$config = loadConfigs(); | |
$actions = loadActions(); | |
// Add this new action to the list | |
$redirectURL = "https://brunohq.geckoboard.com/dashboards/FD334E3E37055EDB"; | |
$newAction = array(); | |
$newAction["entity"] = $entity; | |
if ($action == "open_dashboard") { | |
$newAction["action"] = "Opened dashboard"; | |
} else if ($action == "open_twitter") { | |
$newAction["action"] = "Checked Bruno's Twitter account"; | |
$redirectURL = "https://twitter.com/brunohq"; | |
} else if ($action == "open_linkedin") { | |
$newAction["action"] = "Visited Bruno's Linkedin profile"; | |
$redirectURL = "https://www.linkedin.com/in/brunohq"; | |
} else if ($action == "open_github") { | |
$newAction["action"] = "Checked dashboard code and css on Github"; | |
$redirectURL = "https://github.com/brunohq"; | |
} else { | |
$newAction["action"] = "Saw a random cat GIF"; | |
$redirectURL = "http://gph.is/1qBlj6R"; | |
} | |
$newAction["date"] = $now; | |
$newAction["ip"] = $ip; | |
$actions[] = $newAction; | |
if(!isset($_GET["incognito"])) saveActions($actions); // skip save if running on incognito mode | |
// Compose data for List and Map widgets | |
$dataList = array(); | |
$dataMapPoints = array(); | |
$dataLines = array (); | |
$firstDay = date("Y-m-d", strtotime($actions[0]["date"])); | |
$date = $firstDay; | |
$lineLuis = array(); | |
$lineBruno = array(); | |
while (strtotime($date) <= strtotime($now)) { | |
$lineLuis[$date] = 0; | |
$lineBruno[$date] = 0; | |
$date = date ("Y-m-d", strtotime("+1 day", strtotime($date))); | |
} | |
foreach ($actions as $action) { | |
$dataListElem = array(); | |
$dataMap_elem = array(); | |
$dataListElem["title"]["text"] = $action["action"]; | |
$dataListElem["label"]["name"] = $action["entity"]; | |
$dataListElem["description"] = $action["date"]; | |
$date = date("Y-m-d", strtotime($action["date"])); | |
if ($action["entity"] == "Bruno") { | |
$lineBruno[$date] = $lineBruno[$date] + 1; | |
$color = "#3EB4F9"; | |
} elseif ($action["entity"] == "Luis") { | |
$lineLuis[$date] = $lineLuis[$date] + 1; | |
$color = "#90c564"; | |
} else { | |
$color = "#ff2015"; | |
} | |
$dataListElem["label"]["color"] = $color; | |
$dataList[] = $dataListElem; | |
if (array_key_exists("ip", $action)) { | |
$pid = null; | |
foreach ($dataMapPoints as $key => $point) { | |
if ($point["ip"] == $action["ip"]) { | |
$pid = $key; | |
break; | |
} | |
} | |
if(is_null($pid)) { // add new geo point to the list | |
$dataMapPoints[] = array( | |
"ip" => $action["ip"], | |
"color" => $color, | |
"size" => 3 | |
); | |
} else { // increase size of geo point | |
$dataMapPoints[$pid]["size"] += 1; | |
} | |
} | |
} | |
$dataMap["points"]["point"] = $dataMapPoints; | |
$dataLine["series"] = array(); | |
$dataLine["series"][0] = array(); | |
$dataLine["series"][0]["name"] = "Bruno"; | |
$dataLine["series"][0]["data"] = array_values($lineBruno); | |
$dataLine["series"][1] = array(); | |
$dataLine["series"][1]["name"] = "Luis"; | |
$dataLine["series"][1]["data"] = array_values($lineLuis); | |
$dataLine["y_axis"]["format"] = "decimal"; | |
$dataLine["x_axis"]["labels"] = array_keys($lineLuis); | |
$dataLine["x_axis"]["type"] = "datetime"; | |
// Push all data to widgets | |
$json = array(); | |
$json["api_key"] = $config["api_key"]; | |
$json["data"] = array_reverse($dataList); | |
push($config["push_url"] . $config["widgets"]["list"], $json); | |
$json["data"] = $dataMap; | |
push($config["push_url"] . $config["widgets"]["map"], $json); | |
$json["data"] = $dataLine; | |
push($config["push_url"] . $config["widgets"]["line"], $json); | |
//Redirect to the final destination | |
header( 'Location: ' . $redirectURL); | |
function loadConfigs() { | |
$file = "configs.json"; | |
$config = json_decode(file_get_contents($file), true); | |
return $config ; | |
} | |
function loadActions() { | |
$file = "actions.json"; | |
$actions = json_decode(file_get_contents($file), true); | |
return $actions; | |
} | |
function saveActions($data) { | |
$file = "actions.json"; | |
$r = file_put_contents($file, json_encode($data)); | |
} | |
function push($url, $data) { | |
$ch = curl_init($url); | |
$payload = json_encode( $data ); | |
curl_setopt( $ch, CURLOPT_POSTFIELDS, $payload ); | |
curl_setopt( $ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json')); | |
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true ); | |
$result = curl_exec($ch); | |
curl_close($ch); | |
} | |
function getPHTopPosts($access_token, $endpoint, $date){ | |
$params=array(); | |
$params['day'] = date('Y-m-d',strtotime($date)); | |
$params['access_token'] = $access_token; | |
$ch = curl_init(); | |
$query = http_build_query($params); | |
curl_setopt($ch,CURLOPT_URL,$endpoint . '?' . $query); | |
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true); | |
$result=curl_exec($ch); | |
curl_close($ch); | |
return json_decode($result, true); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment