Conditions | 25 |
Paths | 107 |
Total Lines | 135 |
Code Lines | 79 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
122 | public function aggregate(bool $debug = false): array |
||
123 | { |
||
124 | $this->loadMetadata(); |
||
125 | |||
126 | if (!is_dir($this->statdir)) { |
||
127 | throw new Exception('Statistics module: output dir does not exist [' . $this->statdir . ']'); |
||
128 | } |
||
129 | |||
130 | if (!file_exists($this->inputfile)) { |
||
131 | throw new Exception('Statistics module: input file does not exist [' . $this->inputfile . ']'); |
||
132 | } |
||
133 | |||
134 | $file = fopen($this->inputfile, 'r'); |
||
135 | |||
136 | if ($file === false) { |
||
137 | throw new Exception('Statistics module: unable to open file [' . $this->inputfile . ']'); |
||
138 | } |
||
139 | |||
140 | $logparser = new LogParser( |
||
141 | $this->statconfig->getOptionalValue('datestart', 0), |
||
142 | $this->statconfig->getOptionalValue('datelength', 15), |
||
143 | $this->statconfig->getOptionalValue('offsetspan', 44), |
||
144 | ); |
||
145 | $datehandler = [ |
||
146 | 'default' => new DateHandler($this->offset), |
||
147 | 'month' => new DateHandlerMonth($this->offset), |
||
148 | ]; |
||
149 | |||
150 | $notBefore = 0; |
||
151 | $lastRead = 0; |
||
152 | $lastlinehash = '-'; |
||
153 | |||
154 | if (isset($this->metadata)) { |
||
155 | $notBefore = $this->metadata['notBefore']; |
||
156 | $lastlinehash = $this->metadata['lastlinehash']; |
||
157 | } |
||
158 | |||
159 | $lastlogline = 'sdfsdf'; |
||
160 | $lastlineflip = false; |
||
161 | $results = []; |
||
162 | |||
163 | $i = 0; |
||
164 | // Parse through log file, line by line |
||
165 | while (!feof($file)) { |
||
166 | $logline = strval(fgets($file, 4096)); |
||
167 | |||
168 | // Continue if STAT is not found on line |
||
169 | if (!preg_match('/STAT/', $logline)) { |
||
170 | continue; |
||
171 | } |
||
172 | |||
173 | $i++; |
||
174 | $lastlogline = $logline; |
||
175 | |||
176 | // Parse log, and extract epoch time and rest of content. |
||
177 | $epoch = $logparser->parseEpoch($logline); |
||
178 | $content = $logparser->parseContent($logline); |
||
179 | $action = trim($content[5]); |
||
180 | |||
181 | if ($this->fromcmdline && ($i % 10000) == 0) { |
||
182 | echo "Read line " . $i . "\n"; |
||
183 | } |
||
184 | |||
185 | if ($debug) { |
||
186 | echo "----------------------------------------\n"; |
||
187 | echo 'Log line: ' . $logline . "\n"; |
||
188 | echo 'Date parse [' . substr($logline, 0, $this->statconfig->getOptionalValue('datelength', 15)) . |
||
189 | '] to [' . date(DATE_RFC822, $epoch) . ']' . "\n"; |
||
190 | $ret = print_r($content, true); |
||
191 | echo htmlentities($ret); |
||
|
|||
192 | if ($i >= 13) { |
||
193 | exit; |
||
194 | } |
||
195 | } |
||
196 | |||
197 | if ($epoch > $lastRead) { |
||
198 | $lastRead = $epoch; |
||
199 | } |
||
200 | |||
201 | if ($epoch === $notBefore) { |
||
202 | if (!$lastlineflip) { |
||
203 | if (sha1($logline) === $lastlinehash) { |
||
204 | $lastlineflip = true; |
||
205 | } |
||
206 | continue; |
||
207 | } |
||
208 | } |
||
209 | |||
210 | if ($epoch < $notBefore) { |
||
211 | continue; |
||
212 | } |
||
213 | |||
214 | // Iterate all the statrules from config. |
||
215 | foreach ($this->statrules as $rulename => $rule) { |
||
216 | $type = 'aggregate'; |
||
217 | |||
218 | if (array_key_exists('type', $rule)) { |
||
219 | $type = $rule['type']; |
||
220 | } |
||
221 | |||
222 | if ($type !== 'aggregate') { |
||
223 | continue; |
||
224 | } |
||
225 | |||
226 | foreach ($this->timeres as $tres => $tresconfig) { |
||
227 | $dh = 'default'; |
||
228 | if (isset($tresconfig['customDateHandler'])) { |
||
229 | $dh = $tresconfig['customDateHandler']; |
||
230 | } |
||
231 | |||
232 | $timeslot = $datehandler['default']->toSlot($epoch, $tresconfig['slot']); |
||
233 | $fileslot = $datehandler[$dh]->toSlot($epoch, intval($tresconfig['fileslot'])); |
||
234 | |||
235 | if (isset($rule['action']) && ($action !== $rule['action'])) { |
||
236 | continue; |
||
237 | } |
||
238 | |||
239 | $difcol = self::getDifCol($content, $rule['col']); |
||
240 | |||
241 | if (!isset($results[$rulename][$tres][$fileslot][$timeslot]['_'])) { |
||
242 | $results[$rulename][$tres][$fileslot][$timeslot]['_'] = 0; |
||
243 | } |
||
244 | if (!isset($results[$rulename][$tres][$fileslot][$timeslot][$difcol])) { |
||
245 | $results[$rulename][$tres][$fileslot][$timeslot][$difcol] = 0; |
||
246 | } |
||
247 | |||
248 | $results[$rulename][$tres][$fileslot][$timeslot]['_']++; |
||
249 | $results[$rulename][$tres][$fileslot][$timeslot][$difcol]++; |
||
250 | } |
||
251 | } |
||
252 | } |
||
253 | $this->metadata['notBefore'] = $lastRead; |
||
254 | $this->metadata['lastline'] = $lastlogline; |
||
255 | $this->metadata['lastlinehash'] = sha1($lastlogline); |
||
256 | return $results; |
||
257 | } |
||
374 |