AnalyticsController.php 5.5 KB

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