123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146 |
- <?php
- /**
- * Created by PhpStorm.
- * User: stanley-king
- * Date: 2018/5/7
- * Time: 下午9:28
- */
- namespace statistics;
- use string_helper;
- class request
- {
- const unknown_type = 0;
- const content_type = 1;
- const time_type = 2;
- const session_type = 3;
- const end_type = 4;
- private $mRecord;
- private $mEnded;
- public function __construct()
- {
- $this->mEnded = false;
- }
- public function add($time,$content)
- {
- $ret = $this->reqtype($time,$content);
- if($ret['type'] == request::content_type)
- {
- if($this->mEnded == false) {
- $this->mEnded = true;
- }
- else {
- $stay_time = $time - $this->mRecord['start_time'];
- $this->mRecord['stay_time'] = $stay_time;
- $this->record();
- $this->mRecord = [];
- $this->mEnded = false;
- }
- $parmas = $ret['data'];
- $this->mRecord['start_time'] = $parmas['start_time'];
- $this->mRecord['act'] = $parmas['act'];
- $this->mRecord['op'] = $parmas['op'];
- $this->mRecord['op'] = $parmas['op'];
- // $this->mRecord['from_act'] = $parmas['from_act'];
- // $this->mRecord['from_op'] = $parmas['from_op'];
- }
- elseif($ret['type'] == request::session_type)
- {
- $this->mRecord['session'] = $ret['data'];
- }
- elseif($ret['type'] == request::time_type)
- {
- $this->mRecord['reqtime'] = $ret['data'];
- }
- else {
- }
- }
- private function record()
- {
- $x = 0;
- }
- private function reqtype($time,$content)
- {
- $content = trim($content);
- if(string_helper::starts_with($content,'content='))
- {
- $content = substr($content,strlen('content='));
- $input = preg_split('/&|=/', $content);
- $params = [];
- for ($i = 0; $i < count($input); ++$i) {
- $key = $input[$i];
- $val = $input[++$i];
- $params[$key] = $val;
- }
- $params['start_time'] = $time;
- return ['type' => request::content_type,'data' => $params];
- }
- elseif(string_helper::starts_with($content,'APP MPHPSESSID ='))
- {
- $content = substr($content,strlen('APP MPHPSESSID ='));
- $content = trim($content);
- return ['type' => request::session_type,'data' => $content];
- }
- elseif(string_helper::starts_with($content,'request time=')) {
- $content = substr($content,strlen('request time='));
- $content = trim($content);
- return ['type' => request::time_type,'data' => $content];
- }
- else {
- return request::unknown_type;
- }
- }
- }
- class stlog
- {
- private $mPath;
- private $mRequestes;
- public function __construct($path)
- {
- $this->mPath = $path;
- $this->mRequestes = [];
- }
- public function daily()
- {
- $file = fopen($this->mPath,'r+');
- while (!feof($file)) {
- $line = fgets($file);
- $this->parse($line);
- }
- fclose($file);
- }
- private function parse($line)
- {
- $command = '/\[(\d*)\s+(.*)\]\s+DEBUG:\s+(.*)/ui';
- $val = preg_match_all($command,$line,$match);
- if($val > 0) {
- $trdid = intval($match[1][0]);
- $time = strtotime($match[2][0]);
- $content = $match[3][0];
- if(!array_key_exists($trdid,$this->mRequestes)) {
- $this->mRequestes[$trdid] = new request();
- }
- $this->mRequestes[$trdid]->add($time,$content);
- }
- }
- }
|