-
Notifications
You must be signed in to change notification settings - Fork 1
/
checkins-trygvis.php
71 lines (65 loc) · 1.58 KB
/
checkins-trygvis.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
<?php
header('Content-Type: application/vnd.collection+json');
require_once('db-connect-string.php');
pg_connect($db_connect_string);
$from = NULL;
$matches = NULL;
if (isset($_GET["from"])) {
$x = preg_match("/^([0-9]{4})-([0-9]{1,2})-([0-9]{1,2})$/", $_GET["from"], $matches);
if($x != 1) {
header("HTTP/1.1 400 Bad request, x=" . $x);
return;
}
$from = $matches[1] . "-" . $matches[2] . "-" . $matches[3];
// header("from: " . $from);
}
$to = NULL;
if (isset($_GET["to"])) {
$x = preg_match("/^([0-9]{4})-([0-9]{1,2})-([0-9]{1,2})$/", $_GET["to"], $matches);
if($x != 1) {
header("HTTP/1.1 400 Bad request, x=" . $x);
return;
}
$to = $matches[1] . "-" . $matches[2] . "-" . $matches[3];
// header("to: " . $to);
}
// TODO: Consider adding queries for searching for certain time intervals instead of loading everything
?>
{ "collection" :
{
"version" : "1.0",
"links" : [],
"items" : [
<?php
$query = "SELECT TO_CHAR(date, 'YYYY-MM-DD') as date, COUNT(DISTINCT(account)) as count FROM checkins WHERE";
if($from) {
$query .= " date >= '$from' AND";
}
if($to) {
$query .= " date < '$to' AND";
}
$query .= " 1=1 GROUP BY TO_CHAR(date, 'YYYY-MM-DD') ORDER BY TO_CHAR(date, 'YYYY-MM-DD')";
$res = pg_query($query);
// header("Query: $query");
$first = TRUE;
while ($row = pg_fetch_assoc($res))
{
if($first) {
$first = FALSE;
}
else {
echo ",";
}
?>
{
"data" : [
{"name" : "date", "value" : "<?=$row['date']?>"},
{"name" : "checkins", "value" : "<?=$row['count']?>"}
]
}
<?php
}
?>
]
}
}