1 | <?php |
||
33 | class StatisticsRepository |
||
34 | { |
||
35 | /** |
||
36 | * Fetches must popular search keys words from the table tx_solr_statistics |
||
37 | * |
||
38 | * @param int $rootPageId |
||
39 | * @param int $days number of days of history to query |
||
40 | * @param int $limit |
||
41 | * @return mixed |
||
42 | */ |
||
43 | 1 | public function getSearchStatistics($rootPageId, $days = 30, $limit = 10) |
|
44 | { |
||
45 | 1 | $now = time(); |
|
46 | 1 | $timeStart = (int) ($now - 86400 * intval($days)); // 86400 seconds/day |
|
47 | 1 | $rootPageId = (int) $rootPageId; |
|
48 | 1 | $limit = (int) $limit; |
|
49 | |||
50 | 1 | $statisticsRows = (array)$this->getDatabase()->exec_SELECTgetRows( |
|
51 | 1 | 'keywords, count(keywords) as count, num_found as hits', |
|
52 | 1 | 'tx_solr_statistics', |
|
53 | 1 | 'tstamp > ' . $timeStart . ' AND root_pid = ' . $rootPageId, |
|
54 | 1 | 'keywords, num_found', |
|
55 | 1 | 'count DESC, hits DESC, keywords ASC', |
|
56 | $limit |
||
57 | 1 | ); |
|
58 | |||
59 | 1 | $statisticsRows = $this->mergeRowsWithSameKeyword($statisticsRows); |
|
60 | |||
61 | 1 | $sumCount = $statisticsRows['sumCount']; |
|
62 | 1 | foreach ($statisticsRows as $statisticsRow) { |
|
63 | $sumCount += $statisticsRow['count']; |
||
64 | 1 | } |
|
65 | |||
66 | 1 | $statisticsRows = array_map(function ($row) use ($sumCount) { |
|
67 | $row['percent'] = $row['count'] * 100 / $sumCount; |
||
68 | |||
69 | return $row; |
||
70 | 1 | }, $statisticsRows); |
|
71 | |||
72 | 1 | return $statisticsRows; |
|
73 | } |
||
74 | |||
75 | /** |
||
76 | * Find Top search keywords with results |
||
77 | * |
||
78 | * @param int $rootPageId |
||
79 | * @param int $days number of days of history to query |
||
80 | * @param int $limit |
||
81 | * @return array |
||
82 | */ |
||
83 | 1 | public function getTopKeyWordsWithHits($rootPageId, $days = 30, $limit = 10) |
|
87 | |||
88 | /** |
||
89 | * Find Top search keywords without results |
||
90 | * |
||
91 | * @param int $rootPageId |
||
92 | * @param int $days number of days of history to query |
||
93 | * @param int $limit |
||
94 | * @return array |
||
95 | */ |
||
96 | 2 | public function getTopKeyWordsWithoutHits($rootPageId, $days = 30, $limit = 10) |
|
100 | |||
101 | /** |
||
102 | * Find Top search keywords with or without results |
||
103 | * |
||
104 | * @param int $rootPageId |
||
105 | * @param int $days number of days of history to query |
||
106 | * @param int $limit |
||
107 | * @param bool $withoutHits |
||
108 | * @return array |
||
109 | */ |
||
110 | 3 | protected function getTopKeyWordsWithOrWithoutHits($rootPageId, $days = 30, $limit, $withoutHits) |
|
139 | |||
140 | /** |
||
141 | * This method groups rows with the same term and different count and hits |
||
142 | * and calculates the average. |
||
143 | * |
||
144 | * @param array $statisticsRows |
||
145 | * @return array |
||
146 | */ |
||
147 | 4 | protected function mergeRowsWithSameKeyword(array $statisticsRows) |
|
169 | |||
170 | /** |
||
171 | * Get number of queries over time |
||
172 | * |
||
173 | * @param int $rootPageId |
||
174 | * @param int $days number of days of history to query |
||
175 | * @param int $bucketSeconds Seconds per bucket |
||
176 | * @return array [labels, data] |
||
177 | */ |
||
178 | public function getQueriesOverTime($rootPageId, $days = 30, $bucketSeconds = 3600) |
||
193 | |||
194 | /** |
||
195 | * This method is used to get an average value from merged statistic rows. |
||
196 | * |
||
197 | * @param array $mergedRow |
||
198 | * @param array $statisticsRow |
||
199 | * @param string $fieldName |
||
200 | * @return float|int |
||
201 | */ |
||
202 | 2 | protected function getAverageFromField(array &$mergedRow, array $statisticsRow, $fieldName) |
|
216 | |||
217 | /** |
||
218 | * @return \TYPO3\CMS\Core\Database\DatabaseConnection |
||
219 | */ |
||
220 | 4 | protected function getDatabase() |
|
224 | } |
||
225 |