|
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(get_cfg_var("site_analyzer_config"), $pdoProvided); |
|
70
|
|
|
} catch (Exception $e) { |
|
71
|
|
|
try { |
|
72
|
|
|
$config = new Configuration("site-analyzer.ini", $pdoProvided); |
|
73
|
|
|
} catch (Exception $e) { |
|
74
|
|
|
throw new Exception("Configuration file not found. Provide a configuration file in the following directory: ".getcwd()." or configure it on the php.ini file by setting the variable 'site_analyzer_config = filename'"); |
|
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[0]]; |
|
115
|
|
|
$tmp = array_merge($tmp, getdate($row[1])); |
|
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[2], $count)) { |
|
133
|
|
|
$count[$row[2]]++; |
|
134
|
|
|
} else { |
|
135
|
|
|
$count[$row[2]] = 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
|
|
|
$data = Matrix::submatrix($targetCounts, [1, 0, 2]); |
|
175
|
|
|
$data = Matrix::toSquareMatrix($data, 0, 1, 2); |
|
176
|
|
|
$labels = Matrix::arrayToMatrix($data["labels"]); |
|
177
|
|
|
$data = $data["data"]; |
|
178
|
|
|
$data = Matrix::toBinary($data); |
|
179
|
|
|
|
|
180
|
|
|
if (array_key_exists("level", $options)) { |
|
181
|
|
|
$level = $options["level"]; |
|
182
|
|
|
} |
|
183
|
|
|
else { |
|
184
|
|
|
$level = count($labels) - 1; |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
|
|
$result = $data; |
|
188
|
|
|
for ($i=1;$i<$level;$i++) { |
|
189
|
|
|
$tmp = Matrix::multiply($result, $data); |
|
190
|
|
|
$result = Matrix::sum($result, $tmp); |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
$result = Matrix::toBinary($result); |
|
194
|
|
|
return ["labels"=>$labels, "data"=>$result]; |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
/* |
|
198
|
|
|
* @param |
|
199
|
|
|
*/ |
|
200
|
|
|
public static function getPathCountMatrix($options = []) |
|
201
|
|
|
{ |
|
202
|
|
|
|
|
203
|
|
|
$config = SiteAnalyzer::loadConfig(); |
|
204
|
|
|
$pdo = SiteAnalyzer::getPDO($config, $options); |
|
205
|
|
|
|
|
206
|
|
|
$targetCounts = Persistence::findByFrom($pdo, $config); |
|
207
|
|
|
$data = Matrix::submatrix($targetCounts, [1, 0, 2]); |
|
208
|
|
|
$data = Matrix::toSquareMatrix($data, 0, 1, 2); |
|
209
|
|
|
$labels = Matrix::arrayToMatrix($data["labels"]); |
|
210
|
|
|
$data = $data["data"]; |
|
211
|
|
|
$data = Matrix::toBinary($data); |
|
212
|
|
|
|
|
213
|
|
|
if (array_key_exists("level", $options)) { |
|
214
|
|
|
$level = $options["level"]; |
|
215
|
|
|
} |
|
216
|
|
|
else { |
|
217
|
|
|
$level = count($labels) - 1; |
|
218
|
|
|
} |
|
219
|
|
|
|
|
220
|
|
|
$result = $data; |
|
221
|
|
|
for ($i=1;$i<$level;$i++) { |
|
222
|
|
|
$tmp = Matrix::multiply($result, $data); |
|
223
|
|
|
$result = Matrix::sum($result, $tmp); |
|
224
|
|
|
} |
|
225
|
|
|
|
|
226
|
|
|
return ["labels"=>$labels, "data"=>$result]; |
|
227
|
|
|
|
|
228
|
|
|
} |
|
229
|
|
|
|
|
230
|
|
|
/* |
|
231
|
|
|
* @param |
|
232
|
|
|
*/ |
|
233
|
|
|
public static function getTransitionCounts($options = []) |
|
234
|
|
|
{ |
|
235
|
|
|
$config = SiteAnalyzer::loadConfig(); |
|
236
|
|
|
$pdo = SiteAnalyzer::getPDO($config, $options); |
|
237
|
|
|
|
|
238
|
|
|
$targetCounts = Persistence::findByFrom($pdo, $config); |
|
239
|
|
|
|
|
240
|
|
|
$data = Matrix::submatrix($targetCounts, [1, 0, 2]); |
|
241
|
|
|
$data = Matrix::toSquareMatrix($data, 0, 1, 2); |
|
242
|
|
|
$labels = Matrix::arrayToMatrix($data["labels"]); |
|
243
|
|
|
|
|
244
|
|
|
return ["data"=>$data["data"], "labels"=>$labels]; |
|
245
|
|
|
} |
|
246
|
|
|
|
|
247
|
|
|
/* |
|
248
|
|
|
* @param |
|
249
|
|
|
*/ |
|
250
|
|
|
public static function performABTest($tests, $options = []) |
|
251
|
|
|
{ |
|
252
|
|
|
$config = SiteAnalyzer::loadConfig(); |
|
253
|
|
|
$pdo = SiteAnalyzer::getPDO($config, $options); |
|
254
|
|
|
|
|
255
|
|
|
$testCounts = Persistence::getCountsFromTest($pdo, $config, $tests); |
|
256
|
|
|
$targetCounts = Persistence::getFromByTest($pdo, $config, $tests); |
|
257
|
|
|
$result = Statistics::ABtest($testCounts, $targetCounts); |
|
258
|
|
|
|
|
259
|
|
|
return $result; |
|
260
|
|
|
} |
|
261
|
|
|
|
|
262
|
|
|
/* |
|
263
|
|
|
* @param |
|
264
|
|
|
*/ |
|
265
|
|
|
public static function findVisitTimeProfiles($nprofiles, $options = []) |
|
266
|
|
|
{ |
|
267
|
|
|
$config = SiteAnalyzer::loadConfig(); |
|
268
|
|
|
$pdo = SiteAnalyzer::getPDO($config, $options); |
|
269
|
|
|
$table = OptionsDAO::getHitsWithOptions($pdo, $config); |
|
270
|
|
|
|
|
271
|
|
|
|
|
272
|
|
|
$data = []; |
|
273
|
|
|
foreach ($table as $row) { |
|
274
|
|
|
$tmp = getdate($row[1]); |
|
275
|
|
|
$data[] = [$tmp['weekday'], $tmp['hours']]; |
|
276
|
|
|
} |
|
277
|
|
|
|
|
278
|
|
|
|
|
279
|
|
|
$cdata = new CategoricalDataset($data); |
|
280
|
|
|
$cdata->setEncodedFeatures([0, 1]); |
|
281
|
|
|
$tdata = $cdata->encode(); |
|
282
|
|
|
$kmResult = ML::kmeans($tdata, $nprofiles); |
|
283
|
|
|
$resp = []; |
|
284
|
|
|
$resp["clusters"] = $kmResult["clusters"]; |
|
285
|
|
|
$resp["centroids"] = $kmResult["centroids"]; |
|
286
|
|
|
$resp["labels"] = $cdata->getLabelsAsArray(); |
|
287
|
|
|
return $resp; |
|
288
|
|
|
} |
|
289
|
|
|
|
|
290
|
|
|
/* |
|
291
|
|
|
* @param |
|
292
|
|
|
*/ |
|
293
|
|
|
public static function findUserProfiles($nprofiles, $options = []) |
|
294
|
|
|
{ |
|
295
|
|
|
$config = SiteAnalyzer::loadConfig(); |
|
296
|
|
|
$pdo = SiteAnalyzer::getPDO($config, $options); |
|
297
|
|
|
$table = OptionsDAO::getHitsWithOptions($pdo, $config); |
|
298
|
|
|
|
|
299
|
|
|
$data = []; |
|
300
|
|
|
foreach ($table as $row) { |
|
301
|
|
|
$tmp = getdate($row[1]); |
|
302
|
|
|
$data[] = [$row[2], $tmp['weekday'], $tmp['hours']]; |
|
303
|
|
|
} |
|
304
|
|
|
|
|
305
|
|
|
|
|
306
|
|
|
$cdata = new CategoricalDataset($data); |
|
307
|
|
|
$cdata->setEncodedFeatures([0, 1, 2]); |
|
308
|
|
|
$tdata = $cdata->encode(); |
|
309
|
|
|
$kmResult = ML::kmeans($tdata, $nprofiles); |
|
310
|
|
|
$resp = []; |
|
311
|
|
|
$resp["clusters"] = $kmResult["clusters"]; |
|
312
|
|
|
$resp["centroids"] = $kmResult["centroids"]; |
|
313
|
|
|
$resp["labels"] = $cdata->getLabelsAsArray(); |
|
314
|
|
|
return $resp; |
|
315
|
|
|
} |
|
316
|
|
|
|
|
317
|
|
|
} |
|
318
|
|
|
|
|
319
|
|
|
|