update_user_active_time.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. <?php
  2. require_once '../path.php';
  3. date_default_timezone_set("UTC");
  4. $logfile = "update_user_active_time_last.txt";
  5. $last = file_get_contents($logfile);
  6. $start = strtotime($last." +1 day");
  7. $end = strtotime($last." +2 day");
  8. $today = strtotime("today");
  9. $dns = "sqlite:"._FILE_DB_USER_ACTIVE_;
  10. $dbh = new PDO($dns, "", "",array(PDO::ATTR_PERSISTENT=>true));
  11. $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
  12. $dns = "sqlite:"._FILE_DB_USER_ACTIVE_INDEX_;
  13. $dbh_index = new PDO($dns, "", "",array(PDO::ATTR_PERSISTENT=>true));
  14. $dbh_index->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
  15. /* 开始一个事务,关闭自动提交 */
  16. $dbh_index->beginTransaction();
  17. $query = "INSERT INTO active_index (user_id, date , duration , hit) VALUES (?, ?, ? , ?)";
  18. $sth_index = $dbh_index->prepare($query);
  19. $runing=$last;
  20. while ($end <= $today) {
  21. $runing=gmdate("Y-m-d",$start);
  22. echo "runing:".$runing."\n";
  23. # do one day
  24. $query = "SELECT user_id, sum(duration) as time , sum(hit) as hit FROM edit WHERE end between ? and ? group by user_id ";
  25. $stmt = $dbh->prepare($query);
  26. $stmt->execute(array($start*1000,$end*1000));
  27. while($result = $stmt->fetch(PDO::FETCH_ASSOC))
  28. {
  29. $user_id = $result["user_id"];
  30. $time = $result["time"];
  31. $hit = $result["hit"];
  32. $sth_index->execute(array($user_id,$start*1000,$time,$hit));
  33. echo "$user_id - $time - $hit \n";
  34. }
  35. $start = $end;
  36. $end = strtotime( gmdate("Y-m-d",$end)." +1 day");
  37. }
  38. $dbh_index->commit();
  39. if (!$sth_index || ($sth_index && $sth_index->errorCode() != 0)) {
  40. /* 识别错误且回滚更改 */
  41. $sth_index->rollBack();
  42. $error = $dbh_index->errorInfo();
  43. echo "error:".$error[2]."\n";
  44. }
  45. echo "run until:".$runing."\n";
  46. echo file_put_contents($logfile,$runing);
  47. ?>