123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230 |
- <?php
- /**
- * Created by PhpStorm.
- * User: stanley-king
- * Date: 2016/11/2
- * Time: 下午2:57
- */
- class statistics_helper
- {
- private static $stInstance;
- private $mItems;
- private $mOther;
- private $mRecordTime;
- private $mSaveTime;
- const interval_time = 1800;
- public static function instance()
- {
- if(self::$stInstance == null) {
- self::$stInstance = new statistics_helper();
- }
- return self::$stInstance;
- }
- private function __construct()
- {
- $this->mItems = [];
- $this->mOther = [];
- $this->reset_time();
- }
- public function __destroy()
- {
- $this->save();
- }
- public function add_logs($params)
- {
- $this->add_items($params);
- $this->add_others($params);
- if($this->expired()) {
- $this->save();
- }
- }
- private function add_others($params)
- {
- $others = $params['other'];
- foreach ($others as $key => $count) {
- $this->add_other($key,$count);
- }
- }
- private function add_items($params)
- {
- $acts = $params['function'];
- foreach ($acts as $act => $ops)
- {
- if($this->act($act) == false) continue;
- foreach ($ops as $opx => $data)
- {
- if($this->op($act,$opx) == false) continue;
- $oper = &$this->mItems[$act][$opx];
- foreach ($data as $key => $value)
- {
- if($key == 'count')
- {
- if(empty($oper['count'])) {
- $oper['count'] = 0;
- }
- $oper['count'] += intval($value);
- }
- }
- }
- }
- }
- private function expired()
- {
- return (time() >= $this->mSaveTime);
- }
- private function reset_time()
- {
- $this->mRecordTime = time();
- $day = new DateTime();
- $day->setTimestamp($this->mRecordTime);
- $day->setTime(0,0,0);
- $morning = $day->getTimestamp();
- $noon = $morning + 12 * 60 * 60;
- $midnight = $day->getTimestamp() + 24 * 60 * 60;
- if(time() < $noon) {
- $this->mSaveTime = $noon;
- } else {
- $this->mSaveTime = $midnight;
- }
- }
- private function save()
- {
- $pid = posix_getpid();
- $date = date('Ymd-H',time());
- $file = BASE_DATA_PATH . '/log/' . "{$date}-{$pid}.txt";
- $data = array('star_time'=> $this->mRecordTime,'end_time' => time(),'function' => $this->mItems,'other' => $this->mOther);
- $data = json_encode($data);
- file_put_contents($file,$data);
- $this->mOther = [];
- $this->mItems = [];
- $this->reset_time();
- }
- public function send_queue()
- {
- $data = array('star_time'=> $this->mRecordTime,'end_time' => time(),'function' => $this->mItems,'other' => $this->mOther);
- QueueClient::push("savelog",$data);
- $this->mRecordTime = time();
- $this->mOther = [];
- $this->mItems = [];
- }
- public function add_call($param)
- {
- if(time() >= $this->mRecordTime + self::interval_time) {
- $this->send_queue();
- }
- $act = $param['act'];
- $op = $param['op'];
- if($this->act($act) == false) return;
- if($this->op($act,$op) == false) return;
- $this->add_data($act,$op,$param);
- }
- private function add_data($act,$op,$param)
- {
- $oper = &$this->mItems[$act][$op];
- if(empty($oper['count'])) {
- $oper['count'] = 1;
- } else {
- $oper['count'] += 1;
- }
- if($act == 'goods_common') {
- return $this->add_goods($op,$param);
- }
- if($act == 'special') {
- return $this->add_special($op,$param);
- }
- if($act == 'member_bonus') {
- return$this->add_bonus($op,$param);
- }
- }
- private function add_other($key,$count) {
- if(isset($this->mOther[$key]) == false) {
- $this->mOther[$key] = [];
- $this->mOther[$key]['count'] = $count;
- } else {
- $this->mOther[$key]['count'] += $count;
- }
- return true;
- }
- private function add_goods($op,$param)
- {
- if($op != 'index') return false;
- $common_id = intval($param['goods_commonid']);
- $goods_id = intval($param['goods_id']);
- if($common_id > 0 && $goods_id > 0) {
- $key = 'goods_' . $goods_id;
- return $this->add_other($key,1);
- } else {
- return false;
- }
- }
- private function add_special($op,$param) {
- if($op != 'index') return false;
- $special_id = intval($param['special_id']);
- if($special_id > 0) {
- $key = 'special_' . $special_id;
- return $this->add_other($key,1);
- } else {
- return false;
- }
- }
- private function add_bonus($op,$param)
- {
- if($op == 'make') {
- $key = 'make_bonus';
- $count = intval($param['total_num']);
- return $this->add_other($key,$count);
- }
- else {
- return false;
- }
- }
- private function act($act)
- {
- if(empty($act)) return false;
- if(isset($this->mItems[$act]) == false) {
- $this->mItems[$act] = [];
- }
- return true;
- }
- private function op($act,$op) {
- if(empty($op)) return false;
- if(isset($this->mItems[$act][$op]) == false) {
- $this->mItems[$act][$op] = [];
- }
- return true;
- }
- }
|