|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* |
|
4
|
|
|
* (c) Ruben Dorado <[email protected]> |
|
5
|
|
|
* |
|
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
7
|
|
|
* file that was distributed with this source code. |
|
8
|
|
|
*/ |
|
9
|
|
|
namespace SiteAnalyzer; |
|
10
|
|
|
|
|
11
|
|
|
use Exception; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* class SiteAnalyzer |
|
15
|
|
|
* |
|
16
|
|
|
* @package SiteAnalyzer |
|
17
|
|
|
* @author Ruben Dorado <[email protected]> |
|
18
|
|
|
* @copyright 2018 Ruben Dorado |
|
19
|
|
|
* @license http://www.opensource.org/licenses/MIT The MIT License |
|
20
|
|
|
*/ |
|
21
|
|
|
class SiteAnalyzer |
|
22
|
|
|
{ |
|
23
|
|
|
|
|
24
|
|
|
/* |
|
25
|
|
|
* @param |
|
26
|
|
|
*/ |
|
27
|
|
|
public static function count($options = []) |
|
28
|
|
|
{ |
|
29
|
|
|
$config = SiteAnalyzer::loadConfig(array_key_exists('pdo', $options)); |
|
30
|
|
|
|
|
31
|
|
|
if (array_key_exists('pdo', $options)) { |
|
32
|
|
|
$pdo = $options['pdo']; |
|
33
|
|
|
} else { |
|
34
|
|
|
$pdo = Persistence::getPDO($config); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
try { |
|
38
|
|
|
return Persistence::updateCount($pdo, $config, $options); |
|
39
|
|
|
} catch (Exception $e) { |
|
40
|
|
|
try { |
|
41
|
|
|
Persistence::crateDatabase($pdo, $config); |
|
42
|
|
|
return Persistence::updateCount($pdo, $config, $options); |
|
43
|
|
|
} catch (Exception $e) { |
|
44
|
|
|
throw new Exception("Site Analyzer could connect to the database.".$e->getMessage()); |
|
45
|
|
|
}; |
|
46
|
|
|
|
|
47
|
|
|
}; |
|
48
|
|
|
|
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/* |
|
52
|
|
|
* @param $format string, one of [php-array, xml, json, txt-csv] |
|
53
|
|
|
*/ |
|
54
|
|
|
public static function resetDatabase($options = []) |
|
55
|
|
|
{ |
|
56
|
|
|
$config = SiteAnalyzer::loadConfig(); |
|
57
|
|
|
$pdo = SiteAnalyzer::getPDO($config, $options); |
|
58
|
|
|
|
|
59
|
|
|
Persistence::deleteDatabase($pdo, $config); |
|
60
|
|
|
Persistence::crateDatabase($pdo, $config); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/* |
|
64
|
|
|
* @param $format string, one of [php-array, xml, json, txt-csv] |
|
65
|
|
|
*/ |
|
66
|
|
|
public static function loadConfig($pdoProvided = FALSE) |
|
67
|
|
|
{ |
|
68
|
|
|
try { |
|
69
|
|
|
$config = new Configuration("../../../../site-analyzer.ini", $pdoProvided); |
|
70
|
|
|
} catch (Exception $e) { |
|
71
|
|
|
try { |
|
72
|
|
|
$config = new Configuration("site-analyzer.ini", $pdoProvided); |
|
73
|
|
|
} catch (Exception $e) { |
|
74
|
|
|
throw new Exception("Config file not found."); |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
return $config; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/* |
|
81
|
|
|
* @param |
|
82
|
|
|
*/ |
|
83
|
|
|
public static function getPDO($config, $options) |
|
84
|
|
|
{ |
|
85
|
|
|
if (array_key_exists("pdo",$options)) { |
|
86
|
|
|
return $options["pdo"]; |
|
87
|
|
|
} |
|
88
|
|
|
return Persistence::getPDO($config); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/* |
|
92
|
|
|
* @param $format string, one of [php-array, xml, json, txt-csv] |
|
93
|
|
|
*/ |
|
94
|
|
|
public static function getStats($options = []) |
|
95
|
|
|
{ |
|
96
|
|
|
$config = SiteAnalyzer::loadConfig(); |
|
97
|
|
|
$pdo = SiteAnalyzer::getPDO($config, $options); |
|
98
|
|
|
|
|
99
|
|
|
$data = Persistence::getCounts($pdo, $config); |
|
100
|
|
|
return $data; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/* |
|
104
|
|
|
* @param |
|
105
|
|
|
*/ |
|
106
|
|
|
public static function groupHitsByTime($options = []) |
|
107
|
|
|
{ |
|
108
|
|
|
$config = SiteAnalyzer::loadConfig(); |
|
109
|
|
|
$pdo = SiteAnalyzer::getPDO($config, $options); |
|
110
|
|
|
|
|
111
|
|
|
$data = OptionsDAO::getHitsWithOptions($pdo, $config); |
|
112
|
|
|
$resp = []; |
|
113
|
|
|
foreach ($data as $row) { |
|
114
|
|
|
$tmp = [$row['id']]; |
|
115
|
|
|
$tmp = array_merge($tmp, getdate($row['time'])); |
|
116
|
|
|
$resp[] = $tmp; |
|
117
|
|
|
} |
|
118
|
|
|
return $resp; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/* |
|
122
|
|
|
* @param |
|
123
|
|
|
*/ |
|
124
|
|
|
public static function groupHitsByUser($options = []) |
|
125
|
|
|
{ |
|
126
|
|
|
$config = SiteAnalyzer::loadConfig(); |
|
127
|
|
|
$pdo = SiteAnalyzer::getPDO($config, $options); |
|
128
|
|
|
|
|
129
|
|
|
$data = OptionsDAO::getHitsWithOptions($pdo, $config); |
|
130
|
|
|
$count = []; |
|
131
|
|
|
foreach ($data as $row) { |
|
132
|
|
|
if (array_key_exists($row['user'], $count)) { |
|
133
|
|
|
$count[$row['user']]++; |
|
134
|
|
|
} else { |
|
135
|
|
|
$count[$row['user']] = 1; |
|
136
|
|
|
} |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
$resp = []; |
|
140
|
|
|
foreach ($count as $user => $count) { |
|
141
|
|
|
$resp[] = [$user, $count]; |
|
142
|
|
|
} |
|
143
|
|
|
return $resp; |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
/* |
|
147
|
|
|
* @param $format string, one of [php-array, xml, json, txt-csv] |
|
148
|
|
|
*/ |
|
149
|
|
|
public static function transform($data, $format) |
|
150
|
|
|
{ |
|
151
|
|
|
if ($format=="html") { |
|
152
|
|
|
$resp = "<table style='border-collapse: collapse;border: 1px solid black;'>"; |
|
153
|
|
|
foreach ($data as $row) { |
|
154
|
|
|
$resp .= "<tr style='border: 1px solid black;'>"; |
|
155
|
|
|
foreach ($row as $cell) { |
|
156
|
|
|
$resp .= "<td style='border: 1px solid black;'>$cell</td>"; |
|
157
|
|
|
} |
|
158
|
|
|
$resp .= "</tr>"; |
|
159
|
|
|
} |
|
160
|
|
|
return $resp."</table>"; |
|
161
|
|
|
} |
|
162
|
|
|
return $data; |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
/* |
|
166
|
|
|
* @param |
|
167
|
|
|
*/ |
|
168
|
|
|
public static function getTransitionMatrix($options = []) |
|
169
|
|
|
{ |
|
170
|
|
|
$config = SiteAnalyzer::loadConfig(); |
|
171
|
|
|
$pdo = SiteAnalyzer::getPDO($config, $options); |
|
172
|
|
|
|
|
173
|
|
|
$targetCounts = Persistence::findByFrom($pdo, $config); |
|
174
|
|
|
|
|
175
|
|
|
$data = Matrix::submatrix($targetCounts, [1, 0, 2]); |
|
176
|
|
|
$data = Matrix::toSquareMatrix($data, 0, 1, 2); |
|
177
|
|
|
$labels = Matrix::arrayToMatrix($data["labels"]); |
|
178
|
|
|
$data = $data["data"]; |
|
179
|
|
|
$data = Matrix::toBinary($data); |
|
180
|
|
|
|
|
181
|
|
|
if (array_key_exists("level", $options)) { |
|
182
|
|
|
$level = $options["level"]; |
|
183
|
|
|
} |
|
184
|
|
|
else { |
|
185
|
|
|
$level = count($labels) - 1; |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
|
$result = $data; |
|
189
|
|
|
for ($i=1;$i<$level;$i++) { |
|
190
|
|
|
$tmp = Matrix::multiply($result, $data); |
|
191
|
|
|
$result = Matrix::sum($result, $tmp); |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
|
|
$result = Matrix::toBinary($result); |
|
195
|
|
|
return ["labels"=>$labels, "data"=>$result]; |
|
196
|
|
|
} |
|
197
|
|
|
|
|
198
|
|
|
/* |
|
199
|
|
|
* @param |
|
200
|
|
|
*/ |
|
201
|
|
|
public static function getTransitionCounts($options = []) |
|
202
|
|
|
{ |
|
203
|
|
|
$config = SiteAnalyzer::loadConfig(); |
|
204
|
|
|
$pdo = SiteAnalyzer::getPDO($config, $options); |
|
205
|
|
|
|
|
206
|
|
|
$targetCounts = Persistence::findByFrom($pdo, $config); |
|
207
|
|
|
|
|
208
|
|
|
$data = Matrix::submatrix($targetCounts, [1, 0, 2]); |
|
209
|
|
|
$data = Matrix::toSquareMatrix($data, 0, 1, 2); |
|
210
|
|
|
$labels = Matrix::arrayToMatrix($data["labels"]); |
|
211
|
|
|
|
|
212
|
|
|
return ["data"=>$data["data"], "labels"=>$labels]; |
|
213
|
|
|
} |
|
214
|
|
|
|
|
215
|
|
|
/* |
|
216
|
|
|
* @param |
|
217
|
|
|
*/ |
|
218
|
|
|
public static function performABTest($tests, $options) |
|
219
|
|
|
{ |
|
220
|
|
|
$config = SiteAnalyzer::loadConfig(); |
|
221
|
|
|
$pdo = SiteAnalyzer::getPDO($config, $options); |
|
222
|
|
|
|
|
223
|
|
|
$testIds = getTestIds($tests); |
|
|
|
|
|
|
224
|
|
|
$testCounts = Persistence::getCountsByIds($pdo, $testIds); |
|
|
|
|
|
|
225
|
|
|
$targetCounts = Persistence::getFromByIds($pdo, $pairs); |
|
|
|
|
|
|
226
|
|
|
$result = Statistics::ABtest($testCounts, $targetCounts, $options); |
|
|
|
|
|
|
227
|
|
|
|
|
228
|
|
|
return $result; |
|
229
|
|
|
} |
|
230
|
|
|
|
|
231
|
|
|
/* |
|
232
|
|
|
* @param |
|
233
|
|
|
*/ |
|
234
|
|
|
public static function findVisitTimeProfiles($nprofiles, $options) |
|
235
|
|
|
{ |
|
236
|
|
|
$config = SiteAnalyzer::loadConfig(); |
|
237
|
|
|
$pdo = SiteAnalyzer::getPDO($config, $pdo); |
|
|
|
|
|
|
238
|
|
|
$data = Persistence::getOptions($pdo, $options); |
|
|
|
|
|
|
239
|
|
|
$data = Matrix::submatrix($data, [1]); |
|
240
|
|
|
$clusters = ML::kmeans($data, $nprofiles); |
|
|
|
|
|
|
241
|
|
|
return $clusters; |
|
242
|
|
|
} |
|
243
|
|
|
} |
|
244
|
|
|
|
|
245
|
|
|
|