statistics_helper.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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. private $mSaveTime;
  15. const interval_time = 1800;
  16. public static function instance()
  17. {
  18. if(self::$stInstance == null) {
  19. self::$stInstance = new statistics_helper();
  20. }
  21. return self::$stInstance;
  22. }
  23. private function __construct()
  24. {
  25. $this->mItems = [];
  26. $this->mOther = [];
  27. $this->reset_time();
  28. }
  29. public function __destroy()
  30. {
  31. $this->save();
  32. }
  33. public function add_logs($params)
  34. {
  35. $this->add_items($params);
  36. $this->add_others($params);
  37. if($this->expired()) {
  38. $this->save();
  39. }
  40. }
  41. private function add_others($params)
  42. {
  43. $others = $params['other'];
  44. foreach ($others as $key => $count) {
  45. $this->add_other($key,$count);
  46. }
  47. }
  48. private function add_items($params)
  49. {
  50. $acts = $params['function'];
  51. foreach ($acts as $act => $ops)
  52. {
  53. if($this->act($act) == false) continue;
  54. foreach ($ops as $opx => $data)
  55. {
  56. if($this->op($act,$opx) == false) continue;
  57. $oper = &$this->mItems[$act][$opx];
  58. foreach ($data as $key => $value)
  59. {
  60. if($key == 'count')
  61. {
  62. if(empty($oper['count'])) {
  63. $oper['count'] = 0;
  64. }
  65. $oper['count'] += intval($value);
  66. }
  67. }
  68. }
  69. }
  70. }
  71. private function expired()
  72. {
  73. return (time() >= $this->mSaveTime);
  74. }
  75. private function reset_time()
  76. {
  77. $this->mRecordTime = time();
  78. $day = new DateTime();
  79. $day->setTimestamp($this->mRecordTime);
  80. $day->setTime(0,0,0);
  81. $morning = $day->getTimestamp();
  82. $noon = $morning + 12 * 60 * 60;
  83. $midnight = $day->getTimestamp() + 24 * 60 * 60;
  84. if(time() < $noon) {
  85. $this->mSaveTime = $noon;
  86. } else {
  87. $this->mSaveTime = $midnight;
  88. }
  89. }
  90. private function save()
  91. {
  92. $pid = posix_getpid();
  93. $date = date('Ymd-H',time());
  94. $file = BASE_DATA_PATH . '/log/' . "{$date}-{$pid}.txt";
  95. $data = array('star_time'=> $this->mRecordTime,'end_time' => time(),'function' => $this->mItems,'other' => $this->mOther);
  96. $data = json_encode($data);
  97. file_put_contents($file,$data);
  98. $this->mOther = [];
  99. $this->mItems = [];
  100. $this->reset_time();
  101. }
  102. public function send_queue()
  103. {
  104. $data = array('star_time'=> $this->mRecordTime,'end_time' => time(),'function' => $this->mItems,'other' => $this->mOther);
  105. QueueClient::push("savelog",$data);
  106. $this->mRecordTime = time();
  107. $this->mOther = [];
  108. $this->mItems = [];
  109. }
  110. public function add_call($param)
  111. {
  112. if(time() >= $this->mRecordTime + self::interval_time) {
  113. $this->send_queue();
  114. }
  115. $act = $param['act'];
  116. $op = $param['op'];
  117. if($this->act($act) == false) return;
  118. if($this->op($act,$op) == false) return;
  119. $this->add_data($act,$op,$param);
  120. }
  121. private function add_data($act,$op,$param)
  122. {
  123. $oper = &$this->mItems[$act][$op];
  124. if(empty($oper['count'])) {
  125. $oper['count'] = 1;
  126. } else {
  127. $oper['count'] += 1;
  128. }
  129. if($act == 'goods_common') {
  130. return $this->add_goods($op,$param);
  131. }
  132. if($act == 'special') {
  133. return $this->add_special($op,$param);
  134. }
  135. if($act == 'member_bonus') {
  136. return$this->add_bonus($op,$param);
  137. }
  138. }
  139. private function add_other($key,$count) {
  140. if(isset($this->mOther[$key]) == false) {
  141. $this->mOther[$key] = [];
  142. $this->mOther[$key]['count'] = $count;
  143. } else {
  144. $this->mOther[$key]['count'] += $count;
  145. }
  146. return true;
  147. }
  148. private function add_goods($op,$param)
  149. {
  150. if($op != 'index') return false;
  151. $common_id = intval($param['goods_commonid']);
  152. $goods_id = intval($param['goods_id']);
  153. if($common_id > 0 && $goods_id > 0) {
  154. $key = 'goods_' . $goods_id;
  155. return $this->add_other($key,1);
  156. } else {
  157. return false;
  158. }
  159. }
  160. private function add_special($op,$param) {
  161. if($op != 'index') return false;
  162. $special_id = intval($param['special_id']);
  163. if($special_id > 0) {
  164. $key = 'special_' . $special_id;
  165. return $this->add_other($key,1);
  166. } else {
  167. return false;
  168. }
  169. }
  170. private function add_bonus($op,$param)
  171. {
  172. if($op == 'make') {
  173. $key = 'make_bonus';
  174. $count = intval($param['total_num']);
  175. return $this->add_other($key,$count);
  176. }
  177. else {
  178. return false;
  179. }
  180. }
  181. private function act($act)
  182. {
  183. if(empty($act)) return false;
  184. if(isset($this->mItems[$act]) == false) {
  185. $this->mItems[$act] = [];
  186. }
  187. return true;
  188. }
  189. private function op($act,$op) {
  190. if(empty($op)) return false;
  191. if(isset($this->mItems[$act][$op]) == false) {
  192. $this->mItems[$act][$op] = [];
  193. }
  194. return true;
  195. }
  196. }