stlog.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. $x = 0;
  60. }
  61. private function reqtype($time,$content)
  62. {
  63. $content = trim($content);
  64. if(string_helper::starts_with($content,'content='))
  65. {
  66. $content = substr($content,strlen('content='));
  67. $input = preg_split('/&|=/', $content);
  68. $params = [];
  69. for ($i = 0; $i < count($input); ++$i) {
  70. $key = $input[$i];
  71. $val = $input[++$i];
  72. $params[$key] = $val;
  73. }
  74. $params['start_time'] = $time;
  75. return ['type' => request::content_type,'data' => $params];
  76. }
  77. elseif(string_helper::starts_with($content,'APP MPHPSESSID ='))
  78. {
  79. $content = substr($content,strlen('APP MPHPSESSID ='));
  80. $content = trim($content);
  81. return ['type' => request::session_type,'data' => $content];
  82. }
  83. elseif(string_helper::starts_with($content,'request time=')) {
  84. $content = substr($content,strlen('request time='));
  85. $content = trim($content);
  86. return ['type' => request::time_type,'data' => $content];
  87. }
  88. else {
  89. return request::unknown_type;
  90. }
  91. }
  92. }
  93. class stlog
  94. {
  95. private $mPath;
  96. private $mRequestes;
  97. public function __construct($path)
  98. {
  99. $this->mPath = $path;
  100. $this->mRequestes = [];
  101. }
  102. public function daily()
  103. {
  104. $file = fopen($this->mPath,'r+');
  105. while (!feof($file)) {
  106. $line = fgets($file);
  107. $this->parse($line);
  108. }
  109. fclose($file);
  110. }
  111. private function parse($line)
  112. {
  113. $command = '/\[(\d*)\s+(.*)\]\s+DEBUG:\s+(.*)/ui';
  114. $val = preg_match_all($command,$line,$match);
  115. if($val > 0) {
  116. $trdid = intval($match[1][0]);
  117. $time = strtotime($match[2][0]);
  118. $content = $match[3][0];
  119. if(!array_key_exists($trdid,$this->mRequestes)) {
  120. $this->mRequestes[$trdid] = new request();
  121. }
  122. $this->mRequestes[$trdid]->add($time,$content);
  123. }
  124. }
  125. }