forked from jonbloom/traffic-report
-
Notifications
You must be signed in to change notification settings - Fork 0
/
traffic.php
224 lines (163 loc) · 6.4 KB
/
traffic.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
<?
//if no query has been submitted, get averages for entire database
if (!isset($_GET["submit"])) {
$request = array(
"mode" => "average"
);
$feedback = "";
} else {
$request = array(
"mode" => "average"
);
$feedback = "";
//check for filter data-yes this is messy
if (isset($_GET['days'])){
if (isset($_GET['days']['include'])){
$request["include"] = array();
for ($i = 0; $i < count($_GET['days']['include']['begin']); $i++){
if (!($_GET['days']['include']['begin'][$i] == "" || $_GET['days']['include']['begin'][$i] == "")){
$request["include"][] = array($_GET['days']['include']['begin'][$i], $_GET['days']['include']['end'][$i]);
$feedback .= "Data between dates:" . $_GET['days']['include']['begin'][$i] . " and " . $_GET['days']['include']['end'][$i] . " included<br>";
}
}
}
if (isset($_GET['days']['exclude'])){
$request["exclude"] = array();
for ($i = 0; $i < count($_GET['days']['exclude']['begin']); $i++){
if (!($_GET['days']['exclude']['begin'][$i] == "" || $_GET['days']['exclude']['begin'][$i] == "")){
$request["exclude"][] = array($_GET['days']['exclude']['begin'][$i], $_GET['days']['exclude']['end'][$i]);
$feedback .= "Data between dates:" . $_GET['days']['exclude']['begin'][$i] . " and " . $_GET['days']['exclude']['end'][$i] . " excluded<br>";
}
}
}
}
if (isset($_GET['hours'])){
if (isset($_GET['hours']['include'])){
$request["hoursInclude"] = array();
for ($i = 0; $i < count($_GET['hours']['include']['begin']); $i++){
if (!($_GET['hours']['include']['begin'][$i] == "" || $_GET['hours']['include']['begin'][$i] == "")){
$request["hoursInclude"][] = array($_GET['hours']['include']['begin'][$i], $_GET['hours']['include']['end'][$i]);
$feedback .= "Data between hours:" . $_GET['hours']['include']['begin'][$i] . " and " . $_GET['hours']['include']['end'][$i] . " (24-hour clock) included<br>";
}
}
}
if (isset($_GET['hours']['exclude'])){
$request["hoursExclude"] = array();
for ($i = 0; $i < count($_GET['hours']['exclude']['begin']); $i++){
if (!($_GET['hours']['exclude']['begin'][$i] == "" || $_GET['hours']['exclude']['begin'][$i] == "")){
$request["hoursExclude"][] = array($_GET['hours']['exclude']['begin'][$i], $_GET['hours']['exclude']['end'][$i]);
$feedback .= "Data between hours:" . $_GET['hours']['exclude']['begin'][$i] . " and " . $_GET['hours']['exclude']['end'][$i] . " (24 hour clock) excluded<br>";
}
}
}
}
//and now that we are done with that business...
}
$json = json_encode($request);
$curl = curl_init("https://prod.library.gvsu.edu/trafficapi/calculate/");
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $json);
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'Content-Length: ' . strlen($json)));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_VERBOSE, 1);
curl_setopt($curl, CURLOPT_HEADER, 1);
$response = curl_exec($curl);
$header_size = curl_getinfo($curl, CURLINFO_HEADER_SIZE);
$code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
$header = substr($response, 0, $header_size);
$body = substr($response, $header_size);
curl_close ($curl);
if ($code != "200") {
echo "Could Not get traffic data, API returned: " . $header;
die();
}
$results = json_decode($body, JSON_OBJECT_AS_ARRAY);
if (gettype($results) == "array") {
//format for display
$printable = array();
foreach ($results as $result) {
$print = true;
$toolTip = '<div class="chart-tooltip"><span class="area-title">' . $result["name"] . '</span><br><span class="area-avg-value">' . $result["average"] . '</span></div>';
$printable[] = array($result["space"], (float) $result["average"], $toolTip);
}
$printable = json_encode($printable);
//print_r($printable);
} else {
$print = false;
$feedback = "No Entries found for your search parameters.";
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="">
<meta name="author" content="">
<link rel="icon" href="../../favicon.ico">
<title>Library Traffic Data Dashboard</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap-theme.min.css">
<link rel="stylesheet" type="text/css" href="css/styles.css">
<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
<script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
<![endif]-->
</head>
<body>
<?php include 'nav.php';?>
<div class="container" id="main">
<form>
<h2>Average Traffic by Area</h2>
<div class="row">
<div class="col-xs-12 col-sm-12 col-md-12 col-lg-12">
<div id="chart"></div>
<P style="font-size: 20pt" id="filters"></P>
</div>
</div>
<?php
include 'filters.php';
?>
</form>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/flot/0.8.3/jquery.flot.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/js/bootstrap.min.js"></script>
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript" src="js/scripts.js"></script>
<script type="text/javascript">
var data;
function drawChart() {
var message = document.getElementById('filters');
<? if ($feedback) {
echo 'message.innerHTML = "' . $feedback . '";';
} ?>
data = new google.visualization.DataTable();
data.addColumn('string', 'Space ID');
data.addColumn('number', 'Average Traffic', {role: 'annotationtext'});
data.addColumn({type: 'string', role: 'tooltip', p:{html: true}});
<? if ($print) {echo "data.addRows($printable);";} ?>
var options = {
height: 500,
vAxis:{
viewWindow: {
min: 0,
max: 5
},
title: 'Traffic Level'
},
hAxis:{
title: 'Area'
},
tooltip:{
isHtml: true
}
};
var chart = new google.visualization.ColumnChart(document.getElementById('chart'));
<? if ($print) { echo "chart.draw(data, options);";} ?>
return false;
}
</script>
</body>
</html>