statistics_helper.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: stanley-king
  5. * Date: 2016/11/2
  6. * Time: 下午2:57
  7. */
  8. require_once (BASE_ROOT_PATH . '/helper/stat_helper.php');
  9. class statistics_helper
  10. {
  11. private static $stInstance;
  12. private $mItems;
  13. private $mOther;
  14. private $mIOSCount;
  15. private $mAndroidCount;
  16. private $mWapCount;
  17. private $mRecordTime;
  18. const interval_time = 600;
  19. public static function instance()
  20. {
  21. if(self::$stInstance == null) {
  22. self::$stInstance = new statistics_helper();
  23. }
  24. return self::$stInstance;
  25. }
  26. private function __construct()
  27. {
  28. $this->reinit();
  29. }
  30. public function add_logs($params)
  31. {
  32. $record_time = $params['star_time'];
  33. if($this->read_data($record_time))
  34. {
  35. $this->mIOSCount += $params['ios_count'];
  36. $this->mAndroidCount += $params['android_count'];
  37. $this->mWapCount += $params['wap_count'];
  38. $this->add_items($params);
  39. $this->add_others($params);
  40. $data = ['function' => $this->mItems,'other' => $this->mOther,
  41. 'ios_count' => $this->mIOSCount,'android_count' => $this->mAndroidCount,'wap_count' => $this->mWapCount];
  42. $stat_call = new \statistics\statcall($record_time);
  43. $stat_call->update($data);
  44. }
  45. }
  46. private function read_data($time)
  47. {
  48. if($time <= 0) return false;
  49. $stat_call = new \statistics\statcall($time);
  50. $record = $stat_call->read();
  51. if($record == false) {
  52. $this->reinit();
  53. } else {
  54. $this->mIOSCount = $record['ios_count'];
  55. $this->mAndroidCount = $record['android_count'];
  56. $this->mWapCount = $record['wap_count'];
  57. $this->mItems = $record['function'];
  58. $this->mOther = $record['other'];
  59. }
  60. return true;
  61. }
  62. private function add_others($params)
  63. {
  64. $others = $params['other'];
  65. foreach ($others as $key => $val) {
  66. $count = $val['count'];
  67. $this->add_other($key,$count);
  68. }
  69. }
  70. private function add_items($params)
  71. {
  72. $acts = $params['function'];
  73. foreach ($acts as $act => $ops)
  74. {
  75. if($this->act($act) == false) continue;
  76. foreach ($ops as $opx => $data)
  77. {
  78. if($this->op($act,$opx) == false) continue;
  79. $oper = &$this->mItems[$act][$opx];
  80. foreach ($data as $key => $value)
  81. {
  82. if($key == 'count')
  83. {
  84. if(empty($oper['count'])) {
  85. $oper['count'] = 0;
  86. }
  87. $oper['count'] += intval($value);
  88. }
  89. }
  90. }
  91. }
  92. }
  93. private function same_day($cur,$record)
  94. {
  95. $left = strtotime(date('Y-m-d',$cur));
  96. $right = strtotime(date('Y-m-d',$record));
  97. return ($left == $right);
  98. }
  99. public function add_call($param)
  100. {
  101. $cur = time();
  102. if($cur >= $this->mRecordTime + self::interval_time || $this->same_day($cur,$this->mRecordTime) == false)
  103. {
  104. $data = ['star_time'=> $this->mRecordTime,'end_time' => time(),
  105. 'function' => $this->mItems,'other' => $this->mOther,
  106. 'ios_count' => $this->mIOSCount,'android_count' => $this->mAndroidCount,'wap_count' => $this->mWapCount];
  107. QueueClient::push("savelog",$data);
  108. $this->reinit();
  109. }
  110. $act = $param['act'];
  111. $op = $param['op'];
  112. if($this->act($act) == false) return;
  113. if($this->op($act,$op) == false) return;
  114. $client_type = $param['client_type'];
  115. if($client_type == 'ios') {
  116. $this->mIOSCount += 1;
  117. }
  118. elseif($client_type == 'android') {
  119. $this->mAndroidCount += 1;
  120. }
  121. else {
  122. $this->mWapCount += 1;
  123. }
  124. $this->add_data($act,$op,$param);
  125. }
  126. private function add_data($act,$op,$param)
  127. {
  128. $oper = &$this->mItems[$act][$op];
  129. if(empty($oper['count'])) {
  130. $oper['count'] = 1;
  131. } else {
  132. $oper['count'] += 1;
  133. }
  134. if($act == 'goods_common') {
  135. return $this->add_goods($op,$param);
  136. }
  137. elseif($act == 'special') {
  138. return $this->add_special($op,$param);
  139. }
  140. elseif($act == 'member_bonus') {
  141. return $this->add_bonus($op,$param);
  142. }
  143. elseif($act == 'search') {
  144. return $this->add_search($op,$param);
  145. }
  146. }
  147. private function add_other($key,$count)
  148. {
  149. if(array_key_exists($key,$this->mOther) == false) {
  150. $this->mOther[$key] = [];
  151. $this->mOther[$key]['count'] = $count;
  152. } else {
  153. $this->mOther[$key]['count'] += $count;
  154. }
  155. return true;
  156. }
  157. private function add_goods($op,$param)
  158. {
  159. if($op != 'index') return false;
  160. $goods_id = intval($param['goods_id']);
  161. if($goods_id > 0) {
  162. $key = 'goods_' . $goods_id;
  163. return $this->add_other($key,1);
  164. } else {
  165. return false;
  166. }
  167. }
  168. private function add_special($op,$param) {
  169. if($op != 'index') return false;
  170. $special_id = intval($param['special_id']);
  171. if($special_id >= 0) {
  172. $key = 'special_' . $special_id;
  173. return $this->add_other($key,1);
  174. } else {
  175. return false;
  176. }
  177. }
  178. private function add_bonus($op,$param)
  179. {
  180. if($op == 'make') {
  181. $key = 'make_bonus';
  182. $count = intval($param['total_num']);
  183. return $this->add_other($key,$count);
  184. }
  185. else {
  186. return false;
  187. }
  188. }
  189. private function add_search($op,$param)
  190. {
  191. if($op == "index")
  192. {
  193. $brand_id = intval($param['brand_id']);
  194. $hot_id = intval($param['hot_id']);
  195. $keyword = trim(urldecode($param['keyword']));
  196. if($brand_id > 0) {
  197. $key = "brand_{$brand_id}";
  198. return $this->add_other($key,1);
  199. }
  200. if($hot_id > 0) {
  201. $key = "hot_{$hot_id}";
  202. return $this->add_other($key,1);
  203. }
  204. if(!empty($keyword)) {
  205. $key = "keyword_{$keyword}";
  206. return $this->add_other($key,1);
  207. }
  208. }
  209. elseif($op == "history")
  210. {
  211. $keyword = trim(urldecode($param['keyword']));
  212. if(!empty($keyword)) {
  213. $key = "keyword_{$keyword}";
  214. return $this->add_other($key,1);
  215. }
  216. }
  217. else {
  218. return false;
  219. }
  220. }
  221. private function act($act)
  222. {
  223. if(empty($act)) return false;
  224. if(isset($this->mItems[$act]) == false) {
  225. $this->mItems[$act] = [];
  226. }
  227. return true;
  228. }
  229. private function op($act,$op) {
  230. if(empty($op)) return false;
  231. if(isset($this->mItems[$act][$op]) == false) {
  232. $this->mItems[$act][$op] = [];
  233. }
  234. return true;
  235. }
  236. private function reinit()
  237. {
  238. $this->mItems = [];
  239. $this->mOther = [];
  240. $this->mWapCount = 0;
  241. $this->mIOSCount = 0;
  242. $this->mAndroidCount = 0;
  243. $this->mRecordTime = time();
  244. }
  245. }