stlog.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: stanley-king
  5. * Date: 2018/5/7
  6. * Time: 下午9:28
  7. */
  8. namespace statistics;
  9. use string_helper;
  10. class request
  11. {
  12. const unknown_type = 0;
  13. const content_type = 1;
  14. const time_type = 2;
  15. const session_type = 3;
  16. const end_type = 4;
  17. private $mRecord;
  18. private $mEnded;
  19. public function __construct()
  20. {
  21. $this->mEnded = false;
  22. }
  23. public function add($time,$content)
  24. {
  25. $ret = $this->reqtype($time,$content);
  26. if($ret['type'] == request::content_type)
  27. {
  28. if($this->mEnded == false) {
  29. $this->mEnded = true;
  30. }
  31. else {
  32. $stay_time = $time - $this->mRecord['start_time'];
  33. $this->mRecord['stay_time'] = $stay_time;
  34. $this->record();
  35. $this->mRecord = [];
  36. $this->mEnded = false;
  37. }
  38. $parmas = $ret['data'];
  39. $this->mRecord['start_time'] = $parmas['start_time'];
  40. $this->mRecord['act'] = $parmas['act'];
  41. $this->mRecord['op'] = $parmas['op'];
  42. $this->mRecord['op'] = $parmas['op'];
  43. // $this->mRecord['from_act'] = $parmas['from_act'];
  44. // $this->mRecord['from_op'] = $parmas['from_op'];
  45. }
  46. elseif($ret['type'] == request::session_type)
  47. {
  48. $this->mRecord['session'] = $ret['data'];
  49. }
  50. elseif($ret['type'] == request::time_type)
  51. {
  52. $this->mRecord['reqtime'] = $ret['data'];
  53. }
  54. else {
  55. }
  56. }
  57. private function record()
  58. {
  59. }
  60. private function reqtype($time,$content)
  61. {
  62. $content = trim($content);
  63. if(string_helper::starts_with($content,'content='))
  64. {
  65. $content = substr($content,strlen('content='));
  66. $input = preg_split('/&|=/', $content);
  67. $params = [];
  68. for ($i = 0; $i < count($input); ++$i) {
  69. $key = $input[$i];
  70. $val = $input[++$i];
  71. $params[$key] = $val;
  72. }
  73. $params['start_time'] = $time;
  74. return ['type' => request::content_type,'data' => $params];
  75. }
  76. elseif(string_helper::starts_with($content,'APP MPHPSESSID ='))
  77. {
  78. $content = substr($content,strlen('APP MPHPSESSID ='));
  79. $content = trim($content);
  80. return ['type' => request::session_type,'data' => $content];
  81. }
  82. elseif(string_helper::starts_with($content,'request time=')) {
  83. $content = substr($content,strlen('request time='));
  84. $content = trim($content);
  85. return ['type' => request::time_type,'data' => $content];
  86. }
  87. else {
  88. return request::unknown_type;
  89. }
  90. }
  91. }
  92. class stlog
  93. {
  94. private $mPath;
  95. private $mRequestes;
  96. public function __construct($path)
  97. {
  98. $this->mPath = $path;
  99. $this->mRequestes = [];
  100. }
  101. public function daily()
  102. {
  103. $file = fopen($this->mPath,'r+');
  104. while (!feof($file)) {
  105. $line = fgets($file);
  106. $this->parse($line);
  107. }
  108. fclose($file);
  109. }
  110. private function parse($line)
  111. {
  112. $command = '/\[(\d*)\s+(.*)\]\s+DEBUG:\s+(.*)/ui';
  113. $val = preg_match_all($command,$line,$match);
  114. if($val > 0) {
  115. $trdid = intval($match[1][0]);
  116. $time = strtotime($match[2][0]);
  117. $content = $match[3][0];
  118. if(!array_key_exists($trdid,$this->mRequestes)) {
  119. $this->mRequestes[$trdid] = new request();
  120. }
  121. $this->mRequestes[$trdid]->add($time,$content);
  122. }
  123. }
  124. }