RunLog.class.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. <?php
  2. namespace Cas\Controller\CmdLine;
  3. use Cas\Dao\LotteryPvUvLog;
  4. use KIF\Core\Config;
  5. use Cas\Dao\LotteryUserParticipateLog;
  6. /**
  7. * 分析前一天 /log/Pass_log 访问日志
  8. *
  9. * php index.php -cCmdLine_RunLog -aRun
  10. */
  11. class RunLog extends Controller {
  12. public function doDefault() {
  13. }
  14. /**
  15. * 跑出前一天的活动的 IP PV UV
  16. * php /export/manager/v2/v2_cas/www/index.php -cIndex -asetuser
  17. */
  18. public function doRun() {
  19. $objDLotteryPvUvLog = new LotteryPvUvLog ();
  20. # 获取昨天目录下 所有的
  21. $Y = date('Y',strtotime('-1 day today'));
  22. $m = date('m',strtotime('-1 day today'));
  23. $dir =Config::getInstance ()->get ( 'Log_Path' ).DIRECTORY_SEPARATOR.$Y.DIRECTORY_SEPARATOR.$m.DIRECTORY_SEPARATOR;
  24. $list = scandir($dir);
  25. $thisList = array();
  26. $name = 'Pass_log_'.date('Y_m_d',strtotime('-1 day today'));
  27. foreach($list as $data){
  28. if(strpos($data, $name) !== false){
  29. $thisList[$data] = $dir.$data;
  30. }
  31. }
  32. $allData = array();
  33. $eventsArr = array();
  34. $tmpEventsIp = array();
  35. $tmpEventsUV = array();
  36. foreach($thisList as $thisDir){
  37. $string = file_get_contents($thisDir);
  38. $d = explode("\n", $string);
  39. foreach($d as $key=> $s){
  40. if($s){
  41. $arr = unserialize($s);
  42. if(isset($eventsArr[$arr['event_id']]['pv'])){
  43. $eventsArr[$arr['event_id']]['pv'] ++;
  44. }else{
  45. $eventsArr[$arr['event_id']]['pv'] = 1;
  46. }
  47. $tmpEventsIp[$arr['event_id']][$arr['ip']] = $arr['ip'];
  48. $tmpEventsUV[$arr['event_id']][$arr['ip'].$arr['user_agent']] = $arr['ip'].$arr['user_agent'];
  49. $allData[] = $arr;
  50. }
  51. }
  52. }
  53. # PV
  54. # IP
  55. foreach($tmpEventsIp as $events_id => $ips){
  56. $eventsArr[$events_id]['ip'] = count($ips);
  57. }
  58. # UV
  59. foreach($tmpEventsUV as $events_id => $vps){
  60. $eventsArr[$events_id]['uv'] = count($vps);
  61. }
  62. # 用户参与数
  63. $objLotteryLog = new LotteryUserParticipateLog();
  64. foreach($eventsArr as $events_id => $data){
  65. $info = array();
  66. $time = strtotime('-1 day today');
  67. /*
  68. $start = $this->maketime(date('Y-m-d 00:00:00', $time));
  69. $end = $time;
  70. # 参与人数
  71. $info ['user_num'] = $objLotteryLog->getUserParticipateLogNumGroupByUidDate ($events_id, $start, $end);
  72. */
  73. $info ['events_id'] = $events_id ;
  74. $info ['pv_num'] = $data['pv'] ;
  75. $info ['uv_num'] = $data['uv'] ;
  76. $info ['ip_num'] = $data['ip'] ;
  77. $info ['date'] = $time;
  78. $objDLotteryPvUvLog->add ( $info ,LotteryPvUvLog::PARAM_CREATE_ACTION_ONDUPLICATE);
  79. }
  80. }
  81. /**
  82. * 跑出今天的 IP PV UV
  83. * php /export/manager/v2/v2_cas/www/index.php -cIndex -asetuser
  84. */
  85. public function doToday() {
  86. $objDLotteryPvUvLog = new LotteryPvUvLog ();
  87. # 获取今天目录下
  88. $Y = date('Y');
  89. $m = date('m');
  90. $dir =Config::getInstance ()->get ( 'Log_Path' ).DIRECTORY_SEPARATOR.$Y.DIRECTORY_SEPARATOR.$m.DIRECTORY_SEPARATOR;
  91. $list = scandir($dir);
  92. $thisList = array();
  93. $name = 'Pass_log_'.date('Y_m_d');
  94. foreach($list as $data){
  95. if(strpos($data, $name) !== false){
  96. $thisList[$data] = $dir.$data;
  97. }
  98. }
  99. $allData = array();
  100. $eventsArr = array();
  101. $tmpEventsIp = array();
  102. $tmpEventsUV = array();
  103. foreach($thisList as $thisDir){
  104. $string = file_get_contents($thisDir);
  105. $d = explode("\n", $string);
  106. foreach($d as $key=> $s){
  107. if($s){
  108. $arr = unserialize($s);
  109. if(isset($eventsArr[$arr['event_id']]['pv'])){
  110. $eventsArr[$arr['event_id']]['pv'] ++;
  111. }else{
  112. $eventsArr[$arr['event_id']]['pv'] = 1;
  113. }
  114. $tmpEventsIp[$arr['event_id']][$arr['ip']] = $arr['ip'];
  115. $tmpEventsUV[$arr['event_id']][$arr['ip'].$arr['user_agent']] = $arr['ip'].$arr['user_agent'];
  116. $allData[] = $arr;
  117. }
  118. }
  119. }
  120. # PV
  121. # IP
  122. foreach($tmpEventsIp as $events_id => $ips){
  123. $eventsArr[$events_id]['ip'] = count($ips);
  124. }
  125. # UV
  126. foreach($tmpEventsUV as $events_id => $vps){
  127. $eventsArr[$events_id]['uv'] = count($vps);
  128. }
  129. # 用户参与数
  130. $objLotteryLog = new LotteryUserParticipateLog();
  131. foreach($eventsArr as $events_id => $data){
  132. $info = array();
  133. $time = date();
  134. $start = $this->maketime(date('Y-m-d 00:00:00', $time));
  135. $end = $time;
  136. # 参与人数
  137. $info ['user_num'] = $objLotteryLog->getUserParticipateLogNumGroupByUidDate ($events_id, $start, $end);
  138. $info ['events_id'] = $events_id ;
  139. $info ['pv_num'] = $data['pv'] ;
  140. $info ['uv_num'] = $data['uv'] ;
  141. $info ['ip_num'] = $data['ip'] ;
  142. $info ['date'] = $time;
  143. $objDLotteryPvUvLog->add ( $info ,LotteryPvUvLog::PARAM_CREATE_ACTION_ONDUPLICATE);
  144. }
  145. }
  146. public function maketime($v)
  147. {
  148. if (!$v) {
  149. return '';
  150. }
  151. if (is_numeric($v)) {
  152. return $v;
  153. }
  154. if (is_array($v)) {
  155. $v = $v[1];
  156. }
  157. if (strstr($v, ' ')) {
  158. $t = explode(' ', $v);
  159. $v = $t[0];
  160. $s = explode(':', $t[1]);
  161. } else {
  162. $s = array(0, 0, 0);
  163. }
  164. if (!isset($s[1])) {
  165. $s[1] = 0;
  166. }
  167. if (!isset($s[2])) {
  168. $s[2] = 0;
  169. }
  170. if (strstr($v, '-')) {
  171. $t = explode('-', $v);
  172. } elseif (strstr($v, '/')) {
  173. $u = explode('/', $v);
  174. $t[0] = $u[2];
  175. $t[1] = $u[0];
  176. $t[2] = $u[1];
  177. }
  178. if (!isset($t)) {
  179. $t = array(0, 0, 0);
  180. }
  181. if (!isset($t[1])) {
  182. $t[1] = 0;
  183. }
  184. if (!isset($t[2])) {
  185. $t[2] = 0;
  186. }
  187. $v = mktime($s[0], $s[1], $s[2], $t[1], $t[2], $t[0]);
  188. return $v;
  189. }
  190. }