IndexController.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. <?php
  2. namespace app\controllers;
  3. use Yii;
  4. use yii\filters\AccessControl;
  5. use yii\web\Controller;
  6. use yii\web\Response;
  7. use yii\filters\VerbFilter;
  8. use app\lib\GA;
  9. use app\models\ContentPage;
  10. use app\models\Trafficsource;
  11. use yii\data\Pagination;
  12. class IndexController extends Controller
  13. {
  14. public function actionIndex(){
  15. echo 111;exit;
  16. }
  17. public function actionContentpage(){
  18. $request = Yii::$app->request;
  19. \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
  20. $store = $request->get('store', "supernova");
  21. $limit = $request->get('limit', "10");
  22. $page = $request->get('page', "1");
  23. $page=$page-1>=0?$page-1:0;
  24. $where=[];
  25. if(!$store){
  26. return ['status'=>false,'msg'=>"参数异常"];
  27. }
  28. $where['store']=$store;
  29. $query=ContentPage::find()->where(["store"=>$where]);
  30. $count = $query->count();
  31. $pagination = new Pagination(['totalCount' => $count,'pageSize'=>$limit,'page'=>$page]);
  32. $items = $query->offset($pagination->offset)
  33. ->limit($pagination->limit)
  34. ->orderBy([
  35. 'pageviews'=>SORT_DESC,
  36. 'id'=>SORT_DESC
  37. ])
  38. ->all();
  39. foreach($items as $k=>$v){
  40. $items[$k]['created_at']=date('Y-m-d H:i:s',$v['created_at']);
  41. }
  42. $data=[
  43. 'total'=>$count,
  44. 'items'=>$items,
  45. ];
  46. return [
  47. 'code' => 200,
  48. 'data'=>$data,
  49. 'status'=>true
  50. ];
  51. }
  52. public function actionSource(){
  53. $request = Yii::$app->request;
  54. \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
  55. $store = $request->get('store', "supernova");
  56. $limit = $request->get('limit', "10");
  57. $page = $request->get('page', "1");
  58. $page=$page-1>=0?$page-1:0;
  59. $where=[];
  60. if(!$store){
  61. return ['status'=>false,'msg'=>"参数异常"];
  62. }
  63. $where['store']=$store;
  64. $query=Trafficsource::find()->where(["store"=>$where]);
  65. $count = $query->count();
  66. $pagination = new Pagination(['totalCount' => $count,'pageSize'=>$limit,'page'=>$page]);
  67. $items = $query->offset($pagination->offset)
  68. ->limit($pagination->limit)
  69. ->orderBy([
  70. 'date'=>SORT_DESC,
  71. 'users'=>SORT_DESC
  72. ])
  73. ->all();
  74. foreach($items as $k=>$v){
  75. $items[$k]['created_at']=date('Y-m-d H:i:s',$v['created_at']);
  76. }
  77. $data=[
  78. 'total'=>$count,
  79. 'items'=>$items,
  80. ];
  81. return [
  82. 'code' => 200,
  83. 'data'=>$data,
  84. 'status'=>true
  85. ];
  86. }
  87. public function actionSynsdata(){
  88. $ga=new GA();
  89. $store="supernova";
  90. $request = Yii::$app->request;
  91. $start_day = $request->get('start', date('Y-m-d'),time()-24*3600);
  92. $end_day=$start_day;
  93. $query=ContentPage::find()->where(["store"=>$store,'date'=>$start_day]);
  94. $count = $query->count();
  95. echo $count;exit;
  96. if($count>0){
  97. echo $start_day.$store." ContentPage"."已经执行过,请不要重复执行";
  98. }else{
  99. $contentpages=$ga->contentpages($store,$start_day,$end_day);
  100. $this->addContentpages($contentpages,$store);
  101. }
  102. $query=Trafficsource::find()->where(["store"=>$store,'date'=>$start_day]);
  103. $count = $query->count();
  104. if($count>0){
  105. echo $start_day.$store." Trafficsource"."已经执行过,请不要重复执行";
  106. }else{
  107. $contentpages=$ga->trafficsource($store,$start_day,$end_day);
  108. $this->addSource($contentpages,$store);
  109. }
  110. echo $start_day." 任务完成"."<br/>";
  111. exit;
  112. }
  113. public function addSource($data,$store){
  114. $i=1;
  115. $limit=200;
  116. $modelKey=[
  117. 'date',
  118. 'store',
  119. 'source',
  120. 'channelGrouping',
  121. 'landingPagePath',
  122. 'keyword',
  123. 'socialNetwork',
  124. 'users',
  125. 'newUsers',
  126. 'sessions',
  127. 'bounceRate',
  128. 'pageviewsPerSession',
  129. 'avgSessionDuration',
  130. 'transactionsPerVisit',
  131. 'transactions',
  132. 'transactionRevenue',
  133. 'created_at',
  134. ];
  135. if(!empty($data)){
  136. $list=[];
  137. foreach($data as $k=>$v){
  138. $item=[
  139. date('Y-m-d',strtotime($v['date'])),
  140. $store,
  141. $v['source'],
  142. $v['channelGrouping'],
  143. $v['landingPagePath']!=="(not set)"?$v["landingPagePath"]:"",
  144. $v['keyword']!=="(not set)"?$v['keyword']:"",
  145. $v['socialNetwork']!=="(not set)"?$v['socialNetwork']:"",
  146. $v['users'],
  147. $v['newUsers'],
  148. $v['sessions'],
  149. $v['bounceRate'],
  150. $v['pageviewsPerSession'],
  151. $v['avgSessionDuration'],
  152. $v['transactionsPerVisit'],
  153. $v['transactions'],
  154. $v['transactionRevenue'],
  155. time(),
  156. ];
  157. $list[]=$item;
  158. if($i%$limit==0||$i==count($data)){
  159. \Yii::$app->db->createCommand()->batchInsert(Trafficsource::tableName(), $modelKey, $list)->execute();
  160. $list=[];
  161. echo "addSource 任务完成".$i."个"."<br/>";
  162. }
  163. $i++;
  164. }
  165. echo "addSource 任务全部完成"."<br/>";
  166. }
  167. }
  168. public function addContentpages($contentpages,$store){
  169. $i=1;
  170. $limit=20;
  171. $modelKey=[
  172. 'pagePath',
  173. 'store',
  174. 'date',
  175. 'pageviews',
  176. 'uniquePageviews',
  177. 'avgTimeOnPage',
  178. 'entrances',
  179. 'bounceRate',
  180. 'exitRate',
  181. 'pageValue',
  182. 'created_at',
  183. ];
  184. if(!empty($contentpages)){
  185. $list=[];
  186. foreach($contentpages as $k=>$v){
  187. $item=[
  188. $v['pagePath'],
  189. $store,
  190. date('Y-m-d',strtotime($v['date'])),
  191. $v['pageviews'],
  192. $v['uniquePageviews'],
  193. $v['avgTimeOnPage'],
  194. $v['entrances'],
  195. $v['bounceRate'],
  196. $v['exitRate'],
  197. $v['pageValue'],
  198. time(),
  199. ];
  200. $list[]=$item;
  201. if($i%$limit==0||$i==count($contentpages)){
  202. \Yii::$app->db->createCommand()->batchInsert(ContentPage::tableName(), $modelKey, $list)->execute();
  203. $list=[];
  204. echo "addContentpages 任务完成".$i."个"."<br/>";
  205. }
  206. $i++;
  207. }
  208. echo "addContentpages 任务全部完成"."<br/>";
  209. }
  210. }
  211. }