Total Complexity | 143 |
Total Lines | 689 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like ZPushTop often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use ZPushTop, and based on these observations, apply Extract Interface, too.
1 | #!/usr/bin/env php |
||
62 | class ZPushTop { |
||
63 | // show options |
||
64 | const SHOW_DEFAULT = 0; |
||
65 | const SHOW_ACTIVE_ONLY = 1; |
||
66 | const SHOW_UNKNOWN_ONLY = 2; |
||
67 | const SHOW_TERM_DEFAULT_TIME = 5; // 5 secs |
||
68 | |||
69 | private $topCollector; |
||
70 | private $starttime; |
||
71 | private $action; |
||
72 | private $filter; |
||
73 | private $status; |
||
74 | private $statusexpire; |
||
75 | private $wide; |
||
76 | private $wasEnabled; |
||
77 | private $terminate; |
||
78 | private $scrSize; |
||
79 | private $pingInterval; |
||
80 | private $showPush; |
||
81 | private $showTermSec; |
||
82 | |||
83 | private $linesActive = array(); |
||
84 | private $linesOpen = array(); |
||
85 | private $linesUnknown = array(); |
||
86 | private $linesTerm = array(); |
||
87 | private $pushConn = 0; |
||
88 | private $activeConn = array(); |
||
89 | private $activeHosts = array(); |
||
90 | private $activeUsers = array(); |
||
91 | private $activeDevices = array(); |
||
92 | |||
93 | /** |
||
94 | * Constructor |
||
95 | * |
||
96 | * @access public |
||
97 | */ |
||
98 | public function __construct() { |
||
99 | $this->starttime = time(); |
||
100 | $this->currenttime = time(); |
||
|
|||
101 | $this->action = ""; |
||
102 | $this->filter = false; |
||
103 | $this->status = false; |
||
104 | $this->statusexpire = 0; |
||
105 | $this->helpexpire = 0; |
||
106 | $this->doingTail = false; |
||
107 | $this->wide = false; |
||
108 | $this->terminate = false; |
||
109 | $this->showPush = true; |
||
110 | $this->showOption = self::SHOW_DEFAULT; |
||
111 | $this->showTermSec = self::SHOW_TERM_DEFAULT_TIME; |
||
112 | $this->scrSize = array('width' => 80, 'height' => 24); |
||
113 | $this->pingInterval = (defined('PING_INTERVAL') && PING_INTERVAL > 0) ? PING_INTERVAL : 12; |
||
114 | |||
115 | // get a TopCollector |
||
116 | $this->topCollector = new TopCollector(); |
||
117 | } |
||
118 | |||
119 | /** |
||
120 | * Requests data from the running grommunio-sync processes |
||
121 | * |
||
122 | * @access private |
||
123 | * @return |
||
124 | */ |
||
125 | private function initialize() { |
||
126 | // request feedback from active processes |
||
127 | $this->wasEnabled = $this->topCollector->CollectData(); |
||
128 | |||
129 | // remove obsolete data |
||
130 | $this->topCollector->ClearLatest(true); |
||
131 | |||
132 | // start with default colours |
||
133 | $this->scrDefaultColors(); |
||
134 | } |
||
135 | |||
136 | /** |
||
137 | * Main loop of grommunio-sync-top |
||
138 | * Runs until termination is requested |
||
139 | * |
||
140 | * @access public |
||
141 | * @return |
||
142 | */ |
||
143 | public function run() { |
||
144 | $this->initialize(); |
||
145 | |||
146 | do { |
||
147 | $this->currenttime = time(); |
||
148 | |||
149 | // see if shared memory is active |
||
150 | if (!$this->IsAvailable()) |
||
151 | $this->terminate = true; |
||
152 | |||
153 | // active processes should continue sending data |
||
154 | $this->topCollector->CollectData(); |
||
155 | |||
156 | // get and process data from processes |
||
157 | $this->topCollector->ClearLatest(); |
||
158 | $topdata = $this->topCollector->ReadLatest(); |
||
159 | $this->processData($topdata); |
||
160 | |||
161 | // clear screen |
||
162 | $this->scrClear(); |
||
163 | |||
164 | // check if screen size changed |
||
165 | $s = $this->scrGetSize(); |
||
166 | if ($this->scrSize['width'] != $s['width']) { |
||
167 | if ($s['width'] > 180) |
||
168 | $this->wide = true; |
||
169 | else |
||
170 | $this->wide = false; |
||
171 | } |
||
172 | $this->scrSize = $s; |
||
173 | |||
174 | // print overview |
||
175 | $this->scrOverview(); |
||
176 | |||
177 | // wait for user input |
||
178 | $this->readLineProcess(); |
||
179 | } |
||
180 | while($this->terminate != true); |
||
181 | } |
||
182 | |||
183 | /** |
||
184 | * Indicates if TopCollector is available collecting data |
||
185 | * |
||
186 | * @access public |
||
187 | * @return boolean |
||
188 | */ |
||
189 | public function IsAvailable() { |
||
190 | if (defined('TOPCOLLECTOR_DISABLED') && constant('TOPCOLLECTOR_DISABLED') === true) { |
||
191 | return false; |
||
192 | } |
||
193 | return $this->topCollector->IsActive(); |
||
194 | } |
||
195 | |||
196 | /** |
||
197 | * Processes data written by the running processes |
||
198 | * |
||
199 | * @param array $data |
||
200 | * |
||
201 | * @access private |
||
202 | * @return |
||
203 | */ |
||
204 | private function processData($data) { |
||
278 | } |
||
279 | |||
280 | /** |
||
281 | * Prints data to the terminal |
||
282 | * |
||
283 | * @access private |
||
284 | * @return |
||
285 | */ |
||
286 | private function scrOverview() { |
||
287 | $linesAvail = $this->scrSize['height'] - 8; |
||
288 | $lc = 1; |
||
289 | $this->scrPrintAt($lc,0, "\033[1mgrommunio-sync-top live statistics\033[0m\t\t\t\t\t". @strftime("%d/%m/%Y %T")."\n"); $lc++; |
||
290 | |||
291 | $this->scrPrintAt($lc,0, sprintf("Open connections: %d\t\t\t\tUsers:\t %d\tgrommunio-sync: %s ",count($this->activeConn),count($this->activeUsers), $this->getVersion())); $lc++; |
||
292 | $this->scrPrintAt($lc,0, sprintf("Push connections: %d\t\t\t\tDevices: %d\tPHP-MAPI: %s", $this->pushConn, count($this->activeDevices),phpversion("mapi"))); $lc++; |
||
293 | $this->scrPrintAt($lc,0, sprintf(" Hosts:\t %d", count($this->activeHosts))); $lc++; |
||
294 | $lc++; |
||
295 | |||
296 | $this->scrPrintAt($lc,0, "\033[4m". $this->getLine(array('pid'=>'PID', 'ip'=>'IP', 'user'=>'USER', 'command'=>'COMMAND', 'time'=>'TIME', 'devagent'=>'AGENT', 'devid'=>'DEVID', 'addinfo'=>'Additional Information')). str_repeat(" ",20)."\033[0m"); $lc++; |
||
297 | |||
298 | // print help text if requested |
||
299 | $hl = 0; |
||
300 | if ($this->helpexpire > $this->currenttime) { |
||
301 | $help = $this->scrHelp(); |
||
302 | $linesAvail -= count($help); |
||
303 | $hl = $this->scrSize['height'] - count($help) -1; |
||
304 | foreach ($help as $h) { |
||
305 | $this->scrPrintAt($hl,0, $h); |
||
306 | $hl++; |
||
307 | } |
||
308 | } |
||
309 | |||
310 | $toPrintActive = $linesAvail; |
||
311 | $toPrintOpen = $linesAvail; |
||
312 | $toPrintUnknown = $linesAvail; |
||
313 | $toPrintTerm = $linesAvail; |
||
314 | |||
315 | // default view: show all unknown, no terminated and half active+open |
||
316 | if (count($this->linesActive) + count($this->linesOpen) + count($this->linesUnknown) > $linesAvail) { |
||
317 | $toPrintUnknown = count($this->linesUnknown); |
||
318 | $toPrintActive = count($this->linesActive); |
||
319 | $toPrintOpen = $linesAvail-$toPrintUnknown-$toPrintActive; |
||
320 | $toPrintTerm = 0; |
||
321 | } |
||
322 | |||
323 | if ($this->showOption == self::SHOW_ACTIVE_ONLY) { |
||
324 | $toPrintActive = $linesAvail; |
||
325 | $toPrintOpen = 0; |
||
326 | $toPrintUnknown = 0; |
||
327 | $toPrintTerm = 0; |
||
328 | } |
||
329 | |||
330 | if ($this->showOption == self::SHOW_UNKNOWN_ONLY) { |
||
331 | $toPrintActive = 0; |
||
332 | $toPrintOpen = 0; |
||
333 | $toPrintUnknown = $linesAvail; |
||
334 | $toPrintTerm = 0; |
||
335 | } |
||
336 | |||
337 | $linesprinted = 0; |
||
338 | foreach ($this->linesActive as $time=>$l) { |
||
339 | if ($linesprinted >= $toPrintActive) |
||
340 | break; |
||
341 | |||
342 | $this->scrPrintAt($lc,0, "\033[01m" . $this->getLine($l) ."\033[0m"); |
||
343 | $lc++; |
||
344 | $linesprinted++; |
||
345 | } |
||
346 | |||
347 | $linesprinted = 0; |
||
348 | foreach ($this->linesOpen as $time=>$l) { |
||
349 | if ($linesprinted >= $toPrintOpen) |
||
350 | break; |
||
351 | |||
352 | $this->scrPrintAt($lc,0, $this->getLine($l)); |
||
353 | $lc++; |
||
354 | $linesprinted++; |
||
355 | } |
||
356 | |||
357 | $linesprinted = 0; |
||
358 | foreach ($this->linesUnknown as $time=>$l) { |
||
359 | if ($linesprinted >= $toPrintUnknown) |
||
360 | break; |
||
361 | |||
362 | $color = "0;31m"; |
||
363 | if ($l['push'] == false && $time - $l["start"] > 30) |
||
364 | $color = "1;31m"; |
||
365 | $this->scrPrintAt($lc,0, "\033[0". $color . $this->getLine($l) ."\033[0m"); |
||
366 | $lc++; |
||
367 | $linesprinted++; |
||
368 | } |
||
369 | |||
370 | if ($toPrintTerm > 0) |
||
371 | $toPrintTerm = $linesAvail - $lc +6; |
||
372 | |||
373 | $linesprinted = 0; |
||
374 | foreach ($this->linesTerm as $time=>$l){ |
||
375 | if ($linesprinted >= $toPrintTerm) |
||
376 | break; |
||
377 | |||
378 | $this->scrPrintAt($lc,0, "\033[01;30m" . $this->getLine($l) ."\033[0m"); |
||
379 | $lc++; |
||
380 | $linesprinted++; |
||
381 | } |
||
382 | |||
383 | // add the lines used when displaying the help text |
||
384 | $lc += $hl; |
||
385 | $this->scrPrintAt($lc,0, "\033[K"); $lc++; |
||
386 | $this->scrPrintAt($lc,0, "Colorscheme: \033[01mActive \033[0mOpen \033[01;31mUnknown \033[01;30mTerminated\033[0m"); |
||
387 | |||
388 | // remove old status |
||
389 | if ($this->statusexpire < $this->currenttime) |
||
390 | $this->status = false; |
||
391 | |||
392 | // show request information and help command |
||
393 | if ($this->starttime + 6 > $this->currenttime) { |
||
394 | $this->status = sprintf("Requesting information (takes up to %dsecs)", $this->pingInterval). str_repeat(".", ($this->currenttime-$this->starttime)) . " type \033[01;31mh\033[00;31m or \033[01;31mhelp\033[00;31m for usage instructions"; |
||
395 | $this->statusexpire = $this->currenttime+1; |
||
396 | } |
||
397 | |||
398 | |||
399 | $str = ""; |
||
400 | if (! $this->showPush) |
||
401 | $str .= "\033[00;32mPush: \033[01;32mNo\033[0m "; |
||
402 | |||
403 | if ($this->showOption == self::SHOW_ACTIVE_ONLY) |
||
404 | $str .= "\033[01;32mActive only\033[0m "; |
||
405 | |||
406 | if ($this->showOption == self::SHOW_UNKNOWN_ONLY) |
||
407 | $str .= "\033[01;32mUnknown only\033[0m "; |
||
408 | |||
409 | if ($this->showTermSec != self::SHOW_TERM_DEFAULT_TIME) |
||
410 | $str .= "\033[01;32mTerminated: ". $this->showTermSec. "s\033[0m "; |
||
411 | |||
412 | if ($this->filter !== false || ($this->status !== false && $this->statusexpire > $this->currenttime)) { |
||
413 | // print filter in green |
||
414 | if ($this->filter !== false) |
||
415 | $str .= "\033[00;32mFilter: \033[01;32m$this->filter\033[0m "; |
||
416 | // print status in red |
||
417 | if ($this->status !== false) |
||
418 | $str .= "\033[00;31m$this->status\033[0m"; |
||
419 | } |
||
420 | $this->scrPrintAt(5,0, $str); |
||
421 | |||
422 | $this->scrPrintAt(4,0,"Action: \033[01m".$this->action . "\033[0m"); |
||
423 | } |
||
424 | |||
425 | /** |
||
426 | * Waits for a keystroke and processes the requested command |
||
427 | * |
||
428 | * @access private |
||
429 | * @return |
||
430 | */ |
||
431 | private function readLineProcess() { |
||
557 | } |
||
558 | } |
||
559 | } |
||
560 | |||
561 | /** |
||
562 | * Signal handler function |
||
563 | * |
||
564 | * @param int $signo signal number |
||
565 | * |
||
566 | * @access public |
||
567 | * @return |
||
568 | */ |
||
569 | public function SignalHandler($signo) { |
||
570 | // don't terminate if the signal was sent by terminating tail |
||
571 | if (!$this->doingTail) { |
||
572 | $this->topCollector->CollectData(true); |
||
573 | $this->topCollector->ClearLatest(true); |
||
574 | $this->terminate = true; |
||
575 | } |
||
576 | } |
||
577 | |||
578 | /** |
||
579 | * Returns usage instructions |
||
580 | * |
||
581 | * @return string |
||
582 | * @access public |
||
583 | */ |
||
584 | public function UsageInstructions() { |
||
585 | $help = "Usage:\n\tgrommunio-sync-top.php\n\n" . |
||
586 | " grommunio-sync-top is a live top-like overview of what grommunio-sync is doing. It does not have specific command line options.\n\n". |
||
587 | " When grommunio-sync-top is running you can specify certain actions and options which can be executed (listed below).\n". |
||
588 | " This help information can also be shown inside grommunio-sync-top by hitting 'help' or 'h'.\n\n"; |
||
589 | $scrhelp = $this->scrHelp(); |
||
590 | unset($scrhelp[0]); |
||
591 | |||
592 | $help .= implode("\n", $scrhelp); |
||
593 | $help .= "\n\n"; |
||
594 | return $help; |
||
595 | } |
||
596 | |||
597 | |||
598 | /** |
||
599 | * Prints a 'help' text at the end of the page |
||
600 | * |
||
601 | * @access private |
||
602 | * @return array with help lines |
||
603 | */ |
||
604 | private function scrHelp() { |
||
605 | $h = array(); |
||
606 | $secs = $this->helpexpire - $this->currenttime; |
||
607 | $h[] = "Actions supported by grommunio-sync-top (help page still displayed for ".$secs."secs)"; |
||
608 | $h[] = " ".$this->scrAsBold("Action")."\t\t".$this->scrAsBold("Comment"); |
||
609 | $h[] = " ".$this->scrAsBold("h")." or ".$this->scrAsBold("help")."\t\tDisplays this information."; |
||
610 | $h[] = " ".$this->scrAsBold("q").", ".$this->scrAsBold("quit")." or ".$this->scrAsBold(":q")."\t\tExits grommunio-sync-top."; |
||
611 | $h[] = " ".$this->scrAsBold("w")." or ".$this->scrAsBold("wide")."\t\tTries not to truncate data. Automatically done if more than 180 columns available."; |
||
612 | $h[] = " ".$this->scrAsBold("f:VAL")." or ".$this->scrAsBold("filter:VAL")."\tOnly display connections which contain VAL. This value is case-insensitive."; |
||
613 | $h[] = " ".$this->scrAsBold("f:")." or ".$this->scrAsBold("filter:")."\t\tWithout a search word: resets the filter."; |
||
614 | $h[] = " ".$this->scrAsBold("l:STR")." or ".$this->scrAsBold("log:STR")."\tIssues 'less +G' on the logfile, after grepping on the optional STR."; |
||
615 | $h[] = " ".$this->scrAsBold("t:STR")." or ".$this->scrAsBold("tail:STR")."\tIssues 'tail -f' on the logfile, grepping for optional STR."; |
||
616 | $h[] = " ".$this->scrAsBold("e:STR")." or ".$this->scrAsBold("error:STR")."\tIssues 'tail -f' on the error logfile, grepping for optional STR."; |
||
617 | $h[] = " ".$this->scrAsBold("r")." or ".$this->scrAsBold("reset")."\t\tResets 'wide' or 'filter'."; |
||
618 | $h[] = " ".$this->scrAsBold("o:")." or ".$this->scrAsBold("option:")."\t\tSets display options. Valid options specified below"; |
||
619 | $h[] = " ".$this->scrAsBold(" p")." or ".$this->scrAsBold("push")."\t\tLists/not lists active and open push connections."; |
||
620 | $h[] = " ".$this->scrAsBold(" a")." or ".$this->scrAsBold("action")."\t\tLists only active connections."; |
||
621 | $h[] = " ".$this->scrAsBold(" u")." or ".$this->scrAsBold("unknown")."\tLists only unknown connections."; |
||
622 | $h[] = " ".$this->scrAsBold(" 10")." or ".$this->scrAsBold("20")."\t\tLists terminated connections for 10 or 20 seconds. Any other number can be used."; |
||
623 | $h[] = " ".$this->scrAsBold(" d")." or ".$this->scrAsBold("default")."\tUses default options"; |
||
624 | |||
625 | return $h; |
||
626 | } |
||
627 | |||
628 | /** |
||
629 | * Encapsulates string with different color escape characters |
||
630 | * |
||
631 | * @param string $text |
||
632 | * |
||
633 | * @access private |
||
634 | * @return string same text as bold |
||
635 | */ |
||
636 | private function scrAsBold($text) { |
||
637 | return "\033[01m" . $text ."\033[0m"; |
||
638 | } |
||
639 | |||
640 | /** |
||
641 | * Prints one line of precessed data |
||
642 | * |
||
643 | * @param array $l line information |
||
644 | * |
||
645 | * @access private |
||
646 | * @return string |
||
647 | */ |
||
648 | private function getLine($l) { |
||
649 | if ($this->wide === true) |
||
650 | return sprintf("%s%s%s%s%s%s%s%s", $this->ptStr($l['pid'],6), $this->ptStr($l['ip'],16), $this->ptStr($l['user'],24), $this->ptStr($l['command'],16), $this->ptStr($this->sec2min($l['time']),8), $this->ptStr($l['devagent'],28), $this->ptStr($l['devid'],33, true), $l['addinfo']); |
||
651 | else |
||
652 | return sprintf("%s%s%s%s%s%s%s%s", $this->ptStr($l['pid'],6), $this->ptStr($l['ip'],16), $this->ptStr($l['user'],8), $this->ptStr($l['command'],8), $this->ptStr($this->sec2min($l['time']),6), $this->ptStr($l['devagent'],20), $this->ptStr($l['devid'],12, true), $l['addinfo']); |
||
653 | } |
||
654 | |||
655 | /** |
||
656 | * Pads and trims string |
||
657 | * |
||
658 | * @param string $str to be trimmed/padded |
||
659 | * @param int $size characters to be considered |
||
660 | * @param boolean $cutmiddle (optional) indicates where to long information should |
||
661 | * be trimmed of, false means at the end |
||
662 | * |
||
663 | * @access private |
||
664 | * @return string |
||
665 | */ |
||
666 | private function ptStr($str, $size, $cutmiddle = false) { |
||
667 | if (strlen($str) < $size) |
||
668 | return str_pad($str, $size); |
||
669 | else if ($cutmiddle == true) { |
||
670 | $cut = ($size-2)/2; |
||
671 | return $this->ptStr(substr($str,0,$cut) ."..". substr($str,(-1)*($cut-1)), $size); |
||
672 | } |
||
673 | else { |
||
674 | return substr($str,0,$size-3).".. "; |
||
675 | } |
||
676 | } |
||
677 | |||
678 | /** |
||
679 | * Tries to discover the size of the current terminal |
||
680 | * |
||
681 | * @access private |
||
682 | * @return array 'width' and 'height' as keys |
||
683 | */ |
||
684 | private function scrGetSize() { |
||
685 | $tty = strtolower(exec('stty -a | fgrep columns')); |
||
686 | if (preg_match_all("/rows.([0-9]+);.columns.([0-9]+);/", $tty, $output) || |
||
687 | preg_match_all("/([0-9]+).rows;.([0-9]+).columns;/", $tty, $output)) |
||
688 | return array('width' => $output[2][0], 'height' => $output[1][0]); |
||
689 | |||
690 | return array('width' => 80, 'height' => 24); |
||
691 | } |
||
692 | |||
693 | /** |
||
694 | * Returns the version of the current grommunio-sync installation |
||
695 | * |
||
696 | * @access private |
||
697 | * @return string |
||
698 | */ |
||
699 | private function getVersion() { |
||
701 | } |
||
702 | |||
703 | /** |
||
704 | * Converts seconds in MM:SS |
||
705 | * |
||
706 | * @param int $s seconds |
||
707 | * |
||
708 | * @access private |
||
709 | * @return string |
||
710 | */ |
||
711 | private function sec2min($s) { |
||
712 | if (!is_int($s)) |
||
713 | return $s; |
||
714 | return sprintf("%02.2d:%02.2d", floor($s/60), $s%60); |
||
715 | } |
||
716 | |||
717 | /** |
||
718 | * Resets the default colors of the terminal |
||
719 | * |
||
720 | * @access private |
||
721 | * @return |
||
722 | */ |
||
723 | private function scrDefaultColors() { |
||
725 | } |
||
726 | |||
727 | /** |
||
728 | * Clears screen of the terminal |
||
729 | * |
||
730 | * @param array $data |
||
731 | * |
||
732 | * @access private |
||
733 | * @return |
||
734 | */ |
||
735 | public function scrClear() { |
||
737 | } |
||
738 | |||
739 | /** |
||
740 | * Prints a text at a specific screen/terminal coordinates |
||
741 | * |
||
742 | * @param int $row row number |
||
743 | * @param int $col column number |
||
744 | * @param string $text to be printed |
||
745 | * |
||
746 | * @access private |
||
747 | * @return |
||
748 | */ |
||
749 | private function scrPrintAt($row, $col, $text="") { |
||
751 | } |
||
752 | |||
753 | } |
||
754 |