123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175 |
- <?php
- namespace manager\controllers;
- use app\models\Analytics;
- use Yii;
- class AnalyticsController extends BaseController
- {
- public function actionIndex()
- {
- $model = new Analytics();
- return $this->render('dashboard', ['model'=>$model]);
- }
- public function actionRate()
- {
- $model = new Analytics();
- return $this->render('rate', ['model'=>$model]);
- }
- public function actionAjaxGetlive()
- {
- if (Yii::$app->request->isGet){
- $cache = Yii::$app->cache;
- $metrics = Yii::$app->request->get('metrics');
- $type = Yii::$app->request->get('type');
- $ckey = md5($metrics).$type;
- $model = new Analytics();
- if( $res = $cache->get($ckey) ){
- $data = unserialize($res);
- }else{
- $data = $model ->GetLive($metrics, $type);
- if( $data ){
- $cache->set($ckey, serialize($data), 86400*30);
- }
- }
- if( $data && $type == '2d'){
- $data_s = '';
- $data_cur = 0;
- foreach( $data as $item ){
- $data_s .= ($data_s)?',':'';
- $k = array_key_first($item);
- $k2 = array_key_first($item[$k]);
- $data_s .= $item[$k][$k2]['count'];
- $data_cur = $item[$k][$k2]['count'];
- }
- return json_encode( ['status'=>'ok', 'data'=>$data, 'datas'=>$data_s, 'datal'=>$data_cur] );
- }
- if( $data && $type == '3d'){
- $data_s = '';
- $data_cur = 0;
- return json_encode( ['status'=>'ok', 'data'=>$data, 'datas'=>$data_s, 'datal'=>$data_cur] );
- }
- }
- return json_encode( ['status'=>'err', 'msg'=>'Ошибка получения данных'] );
- }
- public function actionAjaxYglob()
- {
- if (Yii::$app->request->isGet){
- $cache = Yii::$app->cache;
- $dstart = Yii::$app->request->get('dstart');
- $dend = Yii::$app->request->get('dend');
- $model = new Analytics();
- $ckey = "YGetByTime-".md5($dstart.$dend);
- if( $res = $cache->get($ckey) ){
- $data = unserialize($res);
- }else{
- $data = $model ->GetYglob($dstart, $dend);
- if( !$data || isset($data->errors) ){
- return json_encode( ['status'=>'err', 'msg'=>@$data->message] );
- }
- $cache->set($ckey, serialize($data), 86400*30);
- }
- $u_sum = 0;
- $data_u_mid = '';
- $a_all = array();
- $a_max = array();
- $a_sum = array();
- $a_par = array('vi','u','vis', 'bo','time','deep');
- $sweek = $data;
- // var_dump($sweek);
- foreach( $sweek->data[0]->metrics as $key => $item){
- foreach( $item as $el ){
- $a_all[$a_par[$key]] = @isset( $a_all[$a_par[$key]] )?$a_all[$a_par[$key]]:'';
- $a_all[$a_par[$key]] .= ($a_all[$a_par[$key]])?',':'';
- if( $key == 4 ){
- $a_all[$a_par[$key]] .= round($el/60,2);
- }else{
- $a_all[$a_par[$key]] .= round($el,1);
- }
- if( $key == 1 ){
- $data_u_mid .= ($data_u_mid)?',':'';
- $data_u_mid .= round( $el/date( 'd', (strtotime($sweek->time_intervals[$key-1][1])-strtotime($sweek->time_intervals[$key-1][0]))));
- }
- $a_max[$a_par[$key]] = @isset( $a_max[$a_par[$key]] )?$a_max[$a_par[$key]]:0;
- $a_max[$a_par[$key]] = ($el > $a_max[$a_par[$key]])?$el:$a_max[$a_par[$key]];
- $a_sum[$a_par[$key]] = @isset( $a_sum[$a_par[$key]] )?$a_sum[$a_par[$key]]:0;
- $a_sum[$a_par[$key]] = $a_sum[$a_par[$key]]?$a_sum[$a_par[$key]]:0;
- $a_sum[$a_par[$key]] += $el;
- }
- }
- return json_encode( ['status'=>'ok', 'data'=>$a_all, 'max'=>$a_max] );
- }
- return json_encode( ['status'=>'err'] );
- }
- //YGetReferer
- public function actionAjaxYReferer()
- {
- if (Yii::$app->request->isGet){
- $cache = Yii::$app->cache;
- $dstart = Yii::$app->request->get('dstart');
- $dend = Yii::$app->request->get('dend');
- $model = new Analytics();
- $ckey = "YGetByTimeReferer-".md5($dstart.$dend);
- if( $res = $cache->get($ckey) ){
- $data = unserialize($res);
- }else{
- $data = $model ->YGetReferer($dstart, $dend);
- if( !$data || isset($data->errors) ){
- return json_encode( ['status'=>'err', 'msg'=>@$data->message] );
- }
- $cache->set($ckey, serialize($data), 86400*30);
- }
- $u_sum = 0;
- $data_u_mid = '';
- $a_all = array();
- $a_max = array();
- $a_sum = array();
- $a_par = array('vi','u','vis', 'bo','time','deep');
- $sweek = $data;
- return json_encode( ['status'=>'ok', 'data'=>$data->data, 'max'=>$a_max, 'd'=>$data, 'sum'=>$data->totals] );
- }
- return json_encode( ['status'=>'err'] );
- }
- // по дням csv
- public function actionCsv()
- {
- if (Yii::$app->request->isGet){
- $author = Yii::$app->request->get('author');
- $m = Yii::$app->request->get('m');
- $str = $this->renderpartial('csv', ['author'=>$author, 'm'=>$m]);
- Yii::$app->response->format = \yii\web\Response::FORMAT_RAW;
- Yii::$app->response->headers->set('Content-type', ['text/csv']);
- Yii::$app->response->headers->set('Content-Transfer-Encoding', ['binary']);
- header("Pragma: no-cache");
- header("Expires: 0");
- header('Content-Disposition: attachment; filename="'.basename($author.'.csv').'"');
- header('Content-Length: ' . strlen($str));
- return $str;
- }
- return "Нет данных.";
- }
- /**
- * Finds the Authors model based on its primary key value.
- * If the model is not found, a 404 HTTP exception will be thrown.
- * @param int $id id
- * @return Authors the loaded model
- * @throws NotFoundHttpException if the model cannot be found
- */
- protected function findModel($id)
- {
- if (($model = Analytics::findOne(['id' => $id])) !== null) {
- return $model;
- }
- throw new NotFoundHttpException('The requested page does not exist.');
- }
- }
|