1 | <?php |
||
34 | class DbHandler extends AbstractProcessingHandler |
||
35 | { |
||
36 | /** |
||
37 | * @var Db |
||
38 | */ |
||
39 | protected $db; |
||
40 | |||
41 | /** |
||
42 | * @var string |
||
43 | */ |
||
44 | protected $table; |
||
45 | |||
46 | /** |
||
47 | * @var array |
||
48 | */ |
||
49 | protected $additionalFields; |
||
50 | |||
51 | /** |
||
52 | * @var int |
||
53 | */ |
||
54 | protected $maxEntries; |
||
55 | |||
56 | /** |
||
57 | * @var int |
||
58 | */ |
||
59 | protected $cleanDivisor = 100; |
||
60 | |||
61 | /** |
||
62 | * Default probability of table being cleaned (1/100 = 1%). |
||
63 | * |
||
64 | * @var int |
||
65 | */ |
||
66 | protected $cleanProbability = 1; |
||
67 | |||
68 | /** |
||
69 | * @param Db $db |
||
70 | * @param int $maxEntries |
||
71 | * @param array $additionalFields |
||
72 | * @param int $level |
||
73 | * @param bool $bubble |
||
74 | * @param string $table |
||
75 | */ |
||
76 | 5 | public function __construct( |
|
91 | |||
92 | /** |
||
93 | * {@inheritdoc} |
||
94 | */ |
||
95 | 5 | protected function processRecord(array $record) |
|
96 | { |
||
97 | 5 | $record = parent::processRecord($record); |
|
98 | |||
99 | 5 | $record['additionalFieldsData'] = []; |
|
100 | 5 | foreach ($this->additionalFields as $field) { |
|
101 | 5 | foreach (['context', 'extra'] as $sourceKey) { |
|
102 | 5 | if (isset($record[$sourceKey][$field])) { |
|
103 | 1 | $record['additionalFieldsData'][$field] = $record[$sourceKey][$field]; |
|
104 | 5 | unset($record[$sourceKey][$field]); |
|
105 | |||
106 | } |
||
107 | } |
||
108 | } |
||
109 | |||
110 | 5 | return $record; |
|
111 | } |
||
112 | |||
113 | /** |
||
114 | * {@inheritdoc} |
||
115 | */ |
||
116 | 5 | protected function write(array $record) |
|
128 | |||
129 | /** |
||
130 | * @param string $channel |
||
131 | */ |
||
132 | 5 | protected function clean($channel) |
|
133 | { |
||
134 | 5 | if ($this->maxEntries && mt_rand(1, $this->cleanDivisor) <= $this->cleanProbability) { |
|
135 | 2 | $currentCount = $this->getChannelEntriesCount($channel); |
|
136 | 2 | if ($currentCount > $this->maxEntries) { |
|
137 | 1 | $entriesToDelete = $currentCount - $this->maxEntries; |
|
138 | 1 | $this->deleteXOldestChannelEntries($channel, $entriesToDelete); |
|
139 | } |
||
140 | } |
||
141 | 5 | } |
|
142 | |||
143 | /** |
||
144 | * @param string $channel |
||
145 | * @return int |
||
146 | */ |
||
147 | 2 | protected function getChannelEntriesCount($channel) |
|
148 | { |
||
149 | 2 | return $this->db->fetchOne( |
|
150 | 2 | 'SELECT COUNT(*) FROM `' . $this->table . '` WHERE `channel` = ?', |
|
151 | 2 | [$channel] |
|
152 | ); |
||
153 | } |
||
154 | |||
155 | /** |
||
156 | * @param $channel |
||
157 | * @param $entriesToDelete |
||
158 | */ |
||
159 | 1 | protected function deleteXOldestChannelEntries($channel, $entriesToDelete) |
|
160 | { |
||
161 | 1 | $this->db->exec( |
|
162 | sprintf( |
||
163 | 1 | 'DELETE FROM `%s` WHERE `channel` = ? ORDER BY `time` ASC LIMIT %d', |
|
164 | 1 | $this->table, |
|
165 | 1 | (int) $entriesToDelete |
|
166 | ), |
||
167 | 1 | [$channel] |
|
168 | ); |
||
169 | 1 | } |
|
170 | |||
171 | /** |
||
172 | * @param Logger $logger |
||
173 | */ |
||
174 | public function clear(Logger $logger) |
||
178 | |||
179 | /** |
||
180 | * {@inheritdoc} |
||
181 | */ |
||
182 | 5 | protected function getDefaultFormatter() |
|
186 | |||
187 | /** |
||
188 | * @see setCleanProbability() |
||
189 | * @param int $cleanDivisor |
||
190 | */ |
||
191 | 1 | public function setCleanDivisor($cleanDivisor) |
|
195 | |||
196 | /** |
||
197 | * Sets the probability that log table is cleaned on log write. |
||
198 | * |
||
199 | * The clean probability together with clean divisor is used to calculate the probability. |
||
200 | * With a clean probability of 1 and a divisor of 100, there's a 1% chance (1/100) the table |
||
201 | * will be cleaned. |
||
202 | * |
||
203 | * @param int $cleanProbability |
||
204 | */ |
||
205 | 3 | public function setCleanProbability($cleanProbability) |
|
209 | } |
||
210 |