This repository was archived by the owner on Jun 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstatistics.php
More file actions
131 lines (114 loc) · 4.48 KB
/
Copy pathstatistics.php
File metadata and controls
131 lines (114 loc) · 4.48 KB
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
<?php
require_once '../model/Config.php';
require_once '../model/DB.php';
require_once '../model/Statistics.php';
require_once '../model/Response.php';
try {
$writeDB = DB::connectWriteDB();
} catch (PDOException $e) {
error_log("Exception: " . $e->getMessage(), 0);
$response = new Response();
$response->setHttpStatusCode(500);
$response->setSuccess(false);
$response->addMessage("Error: connection to database could not be established.");
$response->send();
exit();
}
try {
if ($_SERVER['REQUEST_METHOD'] === 'GET') {
if (!isset($_GET['id'])) {
$response = new Response();
$response->setHttpStatusCode(400);
$response->setSuccess(false);
$response->addMessage("Error: request did not contain an account id.");
$response->send();
exit();
}
include('authenticate.php');
$statistics = new Statistics();
$statistics->location()->setID(intval($_GET['id']));
$query_id = $statistics->location()->getID();
$query = $writeDB->prepare("SELECT weekday(arr) as `id`, COUNT(DISTINCT DATE(`arr`)) as F, COUNT(*) as `N` FROM `contacts` WHERE account_id = :id AND DATE(`arr`) < DATE(NOW()) GROUP BY weekday(arr)");
$query->bindParam(':id', $query_id, PDO::PARAM_STR);
$query->execute();
$row_count = $query->rowCount();
if ($row_count > 0) {
$statistics->importDay($query->fetchAll(PDO::FETCH_ASSOC));
}
$query = $writeDB->prepare("SELECT HOUR(arr) as `id`, COUNT(*) AS `N` FROM `contacts` WHERE account_id = :id AND DATE(`arr`) < DATE(NOW()) GROUP BY hour(arr)");
$query->bindParam(':id', $query_id, PDO::PARAM_STR);
$query->execute();
$row_count = $query->rowCount();
if ($row_count > 0) {
$statistics->importHour($query->fetchAll(PDO::FETCH_ASSOC));
}
$query = $writeDB->prepare("SELECT (SUM( CASE WHEN n > 1 THEN n ELSE 0 END) / SUM(n)) AS `p` FROM (SELECT `phone`, COUNT(`phone`) AS `n` FROM `contacts` WHERE `account_id` = :id GROUP BY `phone`) AS temp");
$query->bindParam(':id', $query_id, PDO::PARAM_STR);
$query->execute();
$row_count = $query->rowCount();
if ($row_count > 0) {
$row = $query->fetch(PDO::FETCH_ASSOC);
$data = is_null($row['p']) ? 0 : $row['p'];
$statistics->setReturn($data);
}
$query = $writeDB->prepare("SELECT COUNT(*) AS `n` FROM `contacts` WHERE `account_id` = :id AND DATE(`arr`) = DATE(NOW())");
$query->bindParam(':id', $query_id, PDO::PARAM_STR);
$query->execute();
$row_count = $query->rowCount();
if ($row_count > 0) {
$row = $query->fetch(PDO::FETCH_ASSOC);
$data = is_null($row['n']) ? 0 : $row['n'];
$statistics->setToday($data);
}
$query = $writeDB->prepare("SELECT YEAR(`datetime`) as `year`, MONTH(`datetime`) as `month`, COUNT(*) AS `n` FROM actions WHERE account_id=:id GROUP BY `year`, `month` ORDER BY id DESC");
$query->bindParam(":id", $query_id, PDO::PARAM_STR);
$query->execute();
$row_count = $query->rowCount();
if ($row_count > 0) {
$statistics->importAPIStats($query->fetchAll(PDO::FETCH_ASSOC));
}
$response = new Response();
$response->setHttpStatusCode(200);
$response->setSuccess(true);
$response->setData([
"byDay" => $statistics->getTimeStats(Statistics::DAY),
"byHour" => $statistics->getTimeStats(Statistics::HOUR),
"return" => $statistics->getReturnStats(),
"today" => $statistics->getTodayCount(),
"api" => $statistics->getAPIStats()
]);
Config::RegisterAPIAccess($statistics->location()->getID(), "statistics");
$response->send();
exit();
} else {
$response = new Response();
$response->setHttpStatusCode(405);
$response->setSuccess(false);
$response->addMessage("Error: request method not permitted on the statistics endpoint.");
$response->send();
exit();
}
} catch (PDOException $e) {
error_log("Exception: " . $e->getMessage());
$response = new Response();
$response->setHttpStatusCode(500);
$response->setSuccess(false);
$response->addMessage("Error: database error.");
$response->send();
exit();
} catch (APIException $e) {
$response = new Response();
$response->setHttpStatusCode(400);
$response->setSuccess(false);
$response->addMessage("API Error: " . $e->getMessage());
$response->send();
exit();
} catch (Error $e) {
error_log("Exception: " . $e->getMessage());
$response = new Response();
$response->setHttpStatusCode(500);
$response->setSuccess(false);
$response->addMessage("Unknown error.");
$response->send();
exit();
}