AnalyticsController.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. <?php
  2. namespace manager\controllers;
  3. use app\models\Analytics;
  4. use Yii;
  5. class AnalyticsController extends BaseController
  6. {
  7. public function actionIndex()
  8. {
  9. $model = new Analytics();
  10. return $this->render('dashboard', ['model'=>$model]);
  11. }
  12. public function actionRate()
  13. {
  14. $model = new Analytics();
  15. return $this->render('rate', ['model'=>$model]);
  16. }
  17. public function actionAjaxGetlive()
  18. {
  19. if (Yii::$app->request->isGet){
  20. $cache = Yii::$app->cache;
  21. $metrics = Yii::$app->request->get('metrics');
  22. $type = Yii::$app->request->get('type');
  23. $ckey = md5($metrics).$type;
  24. $model = new Analytics();
  25. if( $res = $cache->get($ckey) ){
  26. $data = unserialize($res);
  27. }else{
  28. $data = $model ->GetLive($metrics, $type);
  29. if( $data ){
  30. $cache->set($ckey, serialize($data), 86400*30);
  31. }
  32. }
  33. if( $data && $type == '2d'){
  34. $data_s = '';
  35. $data_cur = 0;
  36. foreach( $data as $item ){
  37. $data_s .= ($data_s)?',':'';
  38. $k = array_key_first($item);
  39. $k2 = array_key_first($item[$k]);
  40. $data_s .= $item[$k][$k2]['count'];
  41. $data_cur = $item[$k][$k2]['count'];
  42. }
  43. return json_encode( ['status'=>'ok', 'data'=>$data, 'datas'=>$data_s, 'datal'=>$data_cur] );
  44. }
  45. if( $data && $type == '3d'){
  46. $data_s = '';
  47. $data_cur = 0;
  48. return json_encode( ['status'=>'ok', 'data'=>$data, 'datas'=>$data_s, 'datal'=>$data_cur] );
  49. }
  50. }
  51. return json_encode( ['status'=>'err', 'msg'=>'Ошибка получения данных'] );
  52. }
  53. public function actionAjaxYglob()
  54. {
  55. if (Yii::$app->request->isGet){
  56. $cache = Yii::$app->cache;
  57. $dstart = Yii::$app->request->get('dstart');
  58. $dend = Yii::$app->request->get('dend');
  59. $model = new Analytics();
  60. $ckey = "YGetByTime-".md5($dstart.$dend);
  61. if( $res = $cache->get($ckey) ){
  62. $data = unserialize($res);
  63. }else{
  64. $data = $model ->GetYglob($dstart, $dend);
  65. if( !$data || isset($data->errors) ){
  66. return json_encode( ['status'=>'err', 'msg'=>@$data->message] );
  67. }
  68. $cache->set($ckey, serialize($data), 86400*30);
  69. }
  70. $u_sum = 0;
  71. $data_u_mid = '';
  72. $a_all = array();
  73. $a_max = array();
  74. $a_sum = array();
  75. $a_par = array('vi','u','vis', 'bo','time','deep');
  76. $sweek = $data;
  77. // var_dump($sweek);
  78. foreach( $sweek->data[0]->metrics as $key => $item){
  79. foreach( $item as $el ){
  80. $a_all[$a_par[$key]] = @isset( $a_all[$a_par[$key]] )?$a_all[$a_par[$key]]:'';
  81. $a_all[$a_par[$key]] .= ($a_all[$a_par[$key]])?',':'';
  82. if( $key == 4 ){
  83. $a_all[$a_par[$key]] .= round($el/60,2);
  84. }else{
  85. $a_all[$a_par[$key]] .= round($el,1);
  86. }
  87. if( $key == 1 ){
  88. $data_u_mid .= ($data_u_mid)?',':'';
  89. $data_u_mid .= round( $el/date( 'd', (strtotime($sweek->time_intervals[$key-1][1])-strtotime($sweek->time_intervals[$key-1][0]))));
  90. }
  91. $a_max[$a_par[$key]] = @isset( $a_max[$a_par[$key]] )?$a_max[$a_par[$key]]:0;
  92. $a_max[$a_par[$key]] = ($el > $a_max[$a_par[$key]])?$el:$a_max[$a_par[$key]];
  93. $a_sum[$a_par[$key]] = @isset( $a_sum[$a_par[$key]] )?$a_sum[$a_par[$key]]:0;
  94. $a_sum[$a_par[$key]] = $a_sum[$a_par[$key]]?$a_sum[$a_par[$key]]:0;
  95. $a_sum[$a_par[$key]] += $el;
  96. }
  97. }
  98. return json_encode( ['status'=>'ok', 'data'=>$a_all, 'max'=>$a_max] );
  99. }
  100. return json_encode( ['status'=>'err'] );
  101. }
  102. //YGetReferer
  103. public function actionAjaxYReferer()
  104. {
  105. if (Yii::$app->request->isGet){
  106. $cache = Yii::$app->cache;
  107. $dstart = Yii::$app->request->get('dstart');
  108. $dend = Yii::$app->request->get('dend');
  109. $model = new Analytics();
  110. $ckey = "YGetByTimeReferer-".md5($dstart.$dend);
  111. if( $res = $cache->get($ckey) ){
  112. $data = unserialize($res);
  113. }else{
  114. $data = $model ->YGetReferer($dstart, $dend);
  115. if( !$data || isset($data->errors) ){
  116. return json_encode( ['status'=>'err', 'msg'=>@$data->message] );
  117. }
  118. $cache->set($ckey, serialize($data), 86400*30);
  119. }
  120. $u_sum = 0;
  121. $data_u_mid = '';
  122. $a_all = array();
  123. $a_max = array();
  124. $a_sum = array();
  125. $a_par = array('vi','u','vis', 'bo','time','deep');
  126. $sweek = $data;
  127. return json_encode( ['status'=>'ok', 'data'=>$data->data, 'max'=>$a_max, 'd'=>$data, 'sum'=>$data->totals] );
  128. }
  129. return json_encode( ['status'=>'err'] );
  130. }
  131. // по дням csv
  132. public function actionCsv()
  133. {
  134. if (Yii::$app->request->isGet){
  135. $author = Yii::$app->request->get('author');
  136. $m = Yii::$app->request->get('m');
  137. $str = $this->renderpartial('csv', ['author'=>$author, 'm'=>$m]);
  138. Yii::$app->response->format = \yii\web\Response::FORMAT_RAW;
  139. Yii::$app->response->headers->set('Content-type', ['text/csv']);
  140. Yii::$app->response->headers->set('Content-Transfer-Encoding', ['binary']);
  141. header("Pragma: no-cache");
  142. header("Expires: 0");
  143. header('Content-Disposition: attachment; filename="'.basename($author.'.csv').'"');
  144. header('Content-Length: ' . strlen($str));
  145. return $str;
  146. }
  147. return "Нет данных.";
  148. }
  149. /**
  150. * Finds the Authors model based on its primary key value.
  151. * If the model is not found, a 404 HTTP exception will be thrown.
  152. * @param int $id id
  153. * @return Authors the loaded model
  154. * @throws NotFoundHttpException if the model cannot be found
  155. */
  156. protected function findModel($id)
  157. {
  158. if (($model = Analytics::findOne(['id' => $id])) !== null) {
  159. return $model;
  160. }
  161. throw new NotFoundHttpException('The requested page does not exist.');
  162. }
  163. }