statistics_helper.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: stanley-king
  5. * Date: 2016/11/2
  6. * Time: 下午2:57
  7. */
  8. class statistics_helper
  9. {
  10. private static $stInstance;
  11. private $mItems;
  12. private $mOther;
  13. private $mRecordTime;
  14. public static function instance()
  15. {
  16. if(self::$stInstance == null) {
  17. self::$stInstance = new statistics_helper();
  18. }
  19. return self::$stInstance;
  20. }
  21. private function __construct()
  22. {
  23. $this->mItems = [];
  24. $this->mOther = [];
  25. $this->mRecordTime = time();
  26. }
  27. public function __destroy()
  28. {
  29. $this->save();
  30. }
  31. private function expired()
  32. {
  33. $day = new DateTime();
  34. $day->setTimestamp($this->mRecordTime);
  35. $day->setTime(0,0,0);
  36. $morning = $day->getTimestamp();
  37. $time_stamp = $morning + 12 * 60 * 60;
  38. if(time() > $time_stamp) {
  39. $time_stamp = $day->getTimestamp() + 24 * 60 * 60;
  40. }
  41. return (time() >= $time_stamp);
  42. }
  43. private function save()
  44. {
  45. $pid = posix_getpid();
  46. $date = date('Ymd-H',time());
  47. $file = BASE_DATA_PATH . '/log/' . "{$date}-{$pid}.txt";
  48. $data = array('star_time'=> $this->mRecordTime,'end_time' => time(),'function' => $this->mItems,'other' => $this->mOther);
  49. $data = json_encode($data);
  50. file_put_contents($file,$data);
  51. $this->mRecordTime = time();
  52. $this->mOther = [];
  53. $this->mItems = [];
  54. }
  55. public function add_call($param)
  56. {
  57. if($this->expired()) {
  58. $this->save();
  59. }
  60. $act = $param['act'];
  61. $op = $param['op'];
  62. if($this->act($act) == false) return;
  63. if($this->op($act,$op) == false) return;
  64. $this->add_data($act,$op,$param);
  65. }
  66. private function add_data($act,$op,$param)
  67. {
  68. $oper = &$this->mItems[$act][$op];
  69. if(empty($oper['count'])) {
  70. $oper['count'] = 1;
  71. } else {
  72. $oper['count'] += 1;
  73. }
  74. if($act == 'goods_common') {
  75. return $this->add_goods($op,$param);
  76. }
  77. if($act == 'special') {
  78. return $this->add_special($op,$param);
  79. }
  80. if($act == 'member_bonus') {
  81. return$this->add_bonus($op,$param);
  82. }
  83. }
  84. private function add_other($key,$count) {
  85. if(array_key_exists($key,$this->mOther) == false) {
  86. $this->mOther[$key] = [];
  87. $this->mOther[$key]['count'] = $count;
  88. } else {
  89. $this->mOther[$key]['count'] += $count;
  90. }
  91. return true;
  92. }
  93. private function add_goods($op,$param)
  94. {
  95. if($op != 'index') return false;
  96. $common_id = intval($param['goods_commonid']);
  97. $goods_id = intval($param['goods_id']);
  98. if($common_id > 0 && $goods_id > 0) {
  99. $key = 'goods_' . $goods_id;
  100. return $this->add_other($key,1);
  101. } else {
  102. return false;
  103. }
  104. }
  105. private function add_special($op,$param) {
  106. if($op != 'index') return false;
  107. $special_id = intval($param['special_id']);
  108. if($special_id > 0) {
  109. $key = 'special_' . $special_id;
  110. return $this->add_other($key,1);
  111. } else {
  112. return false;
  113. }
  114. }
  115. private function add_bonus($op,$param)
  116. {
  117. if($op == 'make') {
  118. $key = 'make_bonus';
  119. $count = intval($param['total_num']);
  120. return $this->add_other($key,$count);
  121. }
  122. else {
  123. return false;
  124. }
  125. }
  126. private function act($act)
  127. {
  128. if(empty($act)) return false;
  129. if(array_key_exists($act,$this->mItems) == false) {
  130. $this->mItems[$act] = [];
  131. }
  132. return true;
  133. }
  134. private function op($act,$op) {
  135. if(empty($op)) return false;
  136. if(array_key_exists($op,$this->mItems[$act]) == false) {
  137. $act[$op] = [];
  138. }
  139. return true;
  140. }
  141. }