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[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 | |||||
249 | /* |
||||
250 | * @param |
||||
251 | */ |
||||
252 | public static function performABTest($tests, $options = []) |
||||
253 | { |
||||
254 | $config = SiteAnalyzer::loadConfig(); |
||||
255 | $pdo = SiteAnalyzer::getPDO($config, $options); |
||||
256 | |||||
257 | $testCounts = Persistence::getCountsFromTest($pdo, $config, $tests); |
||||
258 | $targetCounts = Persistence::getFromByTest($pdo, $config, $tests); |
||||
259 | $result = Statistics::ABtest($testCounts, $targetCounts, $options); |
||||
260 | |||||
261 | return $result; |
||||
262 | } |
||||
263 | |||||
264 | |||||
265 | |||||
266 | /* |
||||
267 | * @param |
||||
268 | */ |
||||
269 | public static function findVisitTimeProfiles($nprofiles, $options = []) |
||||
270 | { |
||||
271 | $config = SiteAnalyzer::loadConfig(); |
||||
272 | $pdo = SiteAnalyzer::getPDO($config, $options); |
||||
273 | $table = OptionsDAO::getHitsWithOptions($pdo, $config); |
||||
274 | |||||
275 | |||||
276 | $data = []; |
||||
277 | foreach ($table as $row) { |
||||
278 | $tmp = getdate($row[1]); |
||||
279 | $data[] = [$tmp['weekday'], $tmp['hours']]; |
||||
280 | } |
||||
281 | |||||
282 | $cdata = new CategoricalDataset($data); |
||||
283 | $cdata.setEncodedFeatures([0, 1]); |
||||
0 ignored issues
–
show
Bug
introduced
by
Loading history...
Are you sure
$cdata of type SiteAnalyzer\CategoricalDataset can be used in concatenation ? Consider adding a __toString() -method.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
284 | $tdata = $cdata.encode(); |
||||
0 ignored issues
–
show
The function
encode was not found. Maybe you did not declare it correctly or list all dependencies?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
285 | /*$clusters = ML::kmeans($tdata, $nprofiles);*/ |
||||
286 | |||||
287 | |||||
288 | //$clusters = ML::kmeans($data, $nprofiles); |
||||
289 | return $tdata; |
||||
290 | } |
||||
291 | } |
||||
292 | |||||
293 |