Advertisement
Guest User

Untitled

a guest
Oct 24th, 2016
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 14.05 KB | None | 0 0
  1. <?php
  2.  
  3. namespace app\controllers;
  4.  
  5. use Yii;
  6. use yii\filters\AccessControl;
  7. use yii\web\Controller;
  8. use yii\filters\VerbFilter;
  9. use app\models\LoginForm;
  10. use app\models\ContactForm;
  11. use app\models\CDR;
  12. use app\models\CDRSearch;
  13.  
  14. class SiteController extends Controller
  15. {
  16.     /**
  17.      * @inheritdoc
  18.      */
  19.     public function behaviors()
  20.     {
  21.         return [
  22.             'access' => [
  23.                 'class' => AccessControl::className(),
  24.                 //'only' => ['logout'],
  25.                 'rules' => [
  26.                     [
  27.                         'actions' => ['logout'],
  28.                         'allow' => true,
  29.                         'roles' => ['@'],
  30.                     ],
  31.                     [
  32.                         'actions' => ['index', 'daily-quality', 'search', 'weekly-quality'],
  33.                         'allow' => true,
  34.                         'roles' => ['?'],
  35.                     ],
  36.                 ],
  37.             ],
  38.             'verbs' => [
  39.                 'class' => VerbFilter::className(),
  40.                 'actions' => [
  41.                     'logout' => ['post'],
  42.                 ],
  43.             ],
  44.         ];
  45.     }
  46.  
  47.     /**
  48.      * @inheritdoc
  49.      */
  50.     public function actions()
  51.     {
  52.         return [
  53.             'error' => [
  54.                 'class' => 'yii\web\ErrorAction',
  55.             ],
  56.             'captcha' => [
  57.                 'class' => 'yii\captcha\CaptchaAction',
  58.                 'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
  59.             ],
  60.         ];
  61.     }
  62.  
  63.     /**
  64.      * Displays homepage.
  65.      *
  66.      * @return string
  67.      */
  68.     public function actionIndex($month=null)
  69.     {
  70.        
  71.         $chartTitle = '';
  72.         $chartLabels = [];
  73.         $chartData = [];
  74.        
  75.         $time = time();
  76.         $setMonth = date('m');
  77.        
  78.         $searchModel = new CDRSearch();
  79.         $params = Yii::$app->request->queryParams;
  80.        
  81.         if(!is_null($month))
  82.         {
  83.             $time = mktime(date("H"), date("i"), date("s") , $month);
  84.             $setMonth = $month;        
  85.             //$params[$searchModel->formName()]['dateTimeOrigination'] = $time;
  86.         }
  87.        
  88.         $chartTitle = Yii::t('app', 'Calls Quality for {month}: Delay >150ms, Jitter>40ms and Packet loss > 20%', ['month'=>date('F, Y', $time)]);
  89.        
  90.         $query = CDR::find();
  91.        
  92.         $query->innerJoinWith('cMRs')
  93.         ->where([ '>', 'CMR.jitter',40])
  94.         ->andwhere(['>', 'CMR.latency', 150])
  95.         ->andwhere(['>', 'round(CMR.numberPacketsLost/CMR.numberPacketsSent*100)', 20]) ;
  96.                  
  97.         $dataProvider = $searchModel->search($query, $params, $setMonth);
  98.         //echo '<pre>'; print_r($dataProvider->getModels());  echo '</pre>';  die();  
  99.        
  100.         $rows = (new \yii\db\Query())
  101.             ->select(['AVG(CMR.jitter) AS jitter','AVG(CMR.latency) AS latency','AVG(round(CMR.numberPacketsLost/CMR.numberPacketsSent*100)) AS packetslost', 'FROM_UNIXTIME(dateTimeOrigination, "%d %M %Y") thedate'])
  102.             ->from('CDR')
  103.             ->innerJoin('CMR', 'CDR.globalCallID_callId=CMR.globalCallID_callId')
  104.             ->where(['MONTH(FROM_UNIXTIME(dateTimeOrigination))'=>$setMonth])
  105.             ->andwhere([ '>', 'jitter',40])
  106.             ->andwhere(['>', 'latency', 150])
  107.             ->andwhere(['>', 'round(CMR.numberPacketsLost/CMR.numberPacketsSent*100)', 20])
  108.             ->groupBy('DATE(FROM_UNIXTIME(dateTimeOrigination))')
  109.             ->all();
  110.        
  111.         $ipData = [];
  112.         //place holder
  113.         $ipData[-1]='Clear the Search';
  114.         $encodedIps = Yii::$app->db->createCommand('SELECT DISTINCT origIpAddr FROM CDR')->queryColumn();
  115.         foreach($encodedIps as $ip)
  116.         {
  117.             $ipData[$ip] = Yii::$app->converter->decimalToIP($ip);
  118.         }
  119.  
  120.         $chartData['jitter'] = [];
  121.         $chartData['latency'] = [];
  122.         $chartData['packetslost'] = [];
  123.        
  124.        foreach($rows as $row)
  125.        {
  126.             $chartLabels[] = $row['thedate'];
  127.             $chartData['jitter'][] = $row['jitter'];
  128.             $chartData['latency'][] = $row['latency'];
  129.             $chartData['packetslost'][] = $row['packetslost'];
  130.        }
  131.        
  132.         $dataset =  [
  133.             ['title'=>'Jitter','data'=>$chartData['jitter']],
  134.             ['title'=>'Delay','data'=>$chartData['latency']],
  135.             ['title'=>'Packets Loss','data'=>$chartData['packetslost']],
  136.         ];
  137.        
  138.         return $this->render('index', [
  139.             'searchModel' => $searchModel,
  140.             'dataProvider' => $dataProvider,
  141.             'chartTitle' => $chartTitle,
  142.             'chartLabels' => $chartLabels,
  143.             'dataset' => $dataset,
  144.             'ipData' => $ipData,
  145.             'time' => $time,
  146.         ]);
  147.     }
  148.    
  149.    
  150.     public function actionDailyQuality($day, $month, $year)
  151.     {
  152.         $chartTitle = '';
  153.         $chartLabels = [];
  154.         $chartData = [];
  155.        
  156.         $searchModel = new CDRSearch();
  157.         $params = Yii::$app->request->queryParams;  
  158.        
  159.         $time = mktime(0, 0, 0, $month, $day, $year);
  160.         //$params[$searchModel->formName()]['dateTimeOrigination'] = $time;
  161.        
  162.         $chartTitle = Yii::t('app', 'Calls Quality for {month}: : Delay >150ms, Jitter>40ms and Packet loss > 20%', ['month'=>date('F d, Y', $time)]);
  163.        
  164.         $query = CDR::find();
  165.        
  166.         $query->innerJoinWith('cMRs')
  167.         ->where([ '>', 'jitter',40])
  168.         ->andwhere(['>', 'latency', 150])
  169.         ->andwhere(['>', 'round(CMR.numberPacketsLost/CMR.numberPacketsSent*100)', 20]) ;
  170.        
  171.         $dataProvider = $searchModel->search($query, $params, $month, $year, $day);
  172.            
  173.         $rows = Yii::$app->db->createCommand('SELECT `values` AS call_hour,DATE_FORMAT(FROM_UNIXTIME(CDR.dateTimeOrigination), "%kH") as label_hour, COALESCE(AVG(CMR.jitter), 0) AS jitter, COALESCE(AVG(CMR.latency), 0) AS latency, COALESCE(AVG(round(CMR.numberPacketsLost/CMR.numberPacketsSent*100)), 0) AS numberPacketsLost FROM CDR INNER JOIN CMR ON CMR.globalCallID_callId = CDR.globalCallID_callId RIGHT JOIN Reference ON HOUR(FROM_UNIXTIME(CDR.dateTimeOrigination))=`values`  AND MONTH(FROM_UNIXTIME(CDR.dateTimeOrigination))=:month AND YEAR(FROM_UNIXTIME(CDR.dateTimeOrigination))=:year AND DAY(FROM_UNIXTIME(CDR.dateTimeOrigination))=:day AND jitter>40 AND latency>150 AND round(CMR.numberPacketsLost/CMR.numberPacketsSent*100)>20 GROUP BY call_hour ORDER BY call_hour ASC')
  174.                ->bindValues([':month'=>$month, ':year'=>$year, ':day'=>$day])
  175.                ->queryAll();
  176.            
  177.           // echo '<pre>'; print_r($rows);  echo '</pre>';  die();
  178.        
  179.         $ipData = [];
  180.         //place holder
  181.         $ipData[-1]='Clear the Search';
  182.         $encodedIps = Yii::$app->db->createCommand('SELECT DISTINCT origIpAddr FROM CDR')->queryColumn();
  183.         foreach($encodedIps as $ip)
  184.         {
  185.             $ipData[$ip] = Yii::$app->converter->decimalToIP($ip);
  186.         }
  187.        
  188.         //data sets
  189.         $chartLabels = [];
  190.         $jitter = [];
  191.         $latency = [];
  192.         $numberPacketsLost = [];
  193.        
  194.        foreach($rows as $row)
  195.        {
  196.             $chartLabels[] = $row['label_hour'];
  197.             $jitter[] = $row['jitter'];
  198.             $latency[] = $row['latency'];
  199.             $numberPacketsLost[] = $row['numberPacketsLost'];  
  200.        }
  201.         //set up data
  202.         $datasets = [
  203.             //['title'=>'Call ID','data'=>$callId],
  204.             ['title'=>'Jitter','data'=>$jitter],
  205.             ['title'=>'Latency','data'=>$latency],
  206.             ['title'=>'Packets Lost','data'=>$numberPacketsLost],
  207.         ];  
  208.        
  209.        
  210.         return $this->render('daily', [
  211.             'searchModel' => $searchModel,
  212.             'dataProvider' => $dataProvider,
  213.             'chartTitle' => $chartTitle,
  214.             'chartLabels' => $chartLabels,
  215.             'chartData' => $datasets,
  216.             'ipData' => $ipData,
  217.             'time' => $time
  218.         ]);
  219.     }
  220.    
  221.     public function actionWeeklyQuality($week=null, $year=null)
  222.     {
  223.         $chartTitle = '';
  224.         $chartLabels = [];
  225.         $chartData = [];
  226.        
  227.         $week = is_null($week) ? date('W') : $week;
  228.         $year = is_null($year) ? date('Y') : $year;
  229.                
  230.        
  231.         $startTimeOfWeek = strtotime($year.'W'.str_pad($week, 2, 0, STR_PAD_LEFT));
  232.         $endTimeOfWeek = strtotime($year.'W'.str_pad($week, 2, 0, STR_PAD_LEFT).' +6 days');
  233.        
  234.         $startOfWeek = date("M d", $startTimeOfWeek);
  235.         $endOfWeek = date("d", $endTimeOfWeek);
  236.         $chartTitle = Yii::t('app', 'Calls Quality for week {n} ({week})', ['n'=>$week, 'week'=>$startOfWeek.' - '.$endOfWeek]);
  237.        
  238.         //echo "month:".date('n', $startTimeOfWeek).", year:".date('Y', $startTimeOfWeek).", start:".date('d', $startTimeOfWeek).", end:".date('d', $endTimeOfWeek);
  239.         //die();
  240.            
  241.         $rows = Yii::$app->db->createCommand('SELECT `values` AS days,COALESCE(DATE_FORMAT(FROM_UNIXTIME(CDR.dateTimeOrigination), "%d"), -1) as label_day, COALESCE(AVG(CMR.jitter), 0) AS jitter, COALESCE(AVG(CMR.latency), 0) AS latency, COALESCE(AVG(round(CMR.numberPacketsLost/CMR.numberPacketsSent*100)), 0) AS numberPacketsLost FROM CDR INNER JOIN CMR ON CMR.globalCallID_callId = CDR.globalCallID_callId RIGHT JOIN Reference ON WEEKDAY(FROM_UNIXTIME(CDR.dateTimeOrigination))=`values`  AND MONTH(FROM_UNIXTIME(CDR.dateTimeOrigination))=:month AND YEAR(FROM_UNIXTIME(CDR.dateTimeOrigination))=:year AND DAY(FROM_UNIXTIME(CDR.dateTimeOrigination))>= :start AND DAY(FROM_UNIXTIME(CDR.dateTimeOrigination))<=:end WHERE Reference.`values`<7 GROUP BY days ORDER BY days ASC')
  242.                ->bindValues([':month'=>intval(date('n', $startTimeOfWeek)), ':year'=>intval(date('Y', $startTimeOfWeek)), ':start'=>intval(date('d', $startTimeOfWeek)), ':end'=>intval(date('d', $endTimeOfWeek))])
  243.                ->queryAll();
  244.                
  245.         //print_r($rows); die();
  246.        
  247.         //normalize arrays
  248.         $rowsNormalized = [];
  249.         foreach($rows as $row)
  250.         {
  251.             $rowChanged = $row;
  252.             if($rowChanged['label_day']==-1)
  253.             {
  254.                 $dayTime = strtotime($year.'W'.str_pad($week, 2, 0, STR_PAD_LEFT).' +'.$rowChanged['days'].' days');
  255.                 $rowChanged['label_day'] = date('d', $dayTime);
  256.             }
  257.             $rowsNormalized[] = $rowChanged;
  258.         }
  259.            
  260.         //echo '<pre>'; print_r($rowsNormalized);  echo '</pre>';  die();
  261.        
  262.         $ipData = [];
  263.         //place holder
  264.         $ipData[-1]='Clear the Search';
  265.         $encodedIps = Yii::$app->db->createCommand('SELECT DISTINCT origIpAddr FROM CDR')->queryColumn();
  266.         foreach($encodedIps as $ip)
  267.         {
  268.             $ipData[$ip] = Yii::$app->converter->decimalToIP($ip);
  269.         }
  270.        
  271.         //data sets
  272.         $chartLabels = [];
  273.         $jitter = [];
  274.         $latency = [];
  275.         $numberPacketsLost = [];
  276.        
  277.        foreach($rowsNormalized as $row)
  278.        {
  279.             $chartLabels[] = $row['label_day'];
  280.             $jitter[] = $row['jitter'];
  281.             $latency[] = $row['latency'];
  282.             $numberPacketsLost[] = $row['numberPacketsLost'];  
  283.        }
  284.         //set up data
  285.         $datasets = [
  286.             //['title'=>'Call ID','data'=>$callId],
  287.             [
  288.                 'title'=>'Jitter',
  289.                 'data'=>$jitter,
  290.                 'fillColor' =>'rgba(37,116,168,0.2)',
  291.                 'strokeColor' =>'rgba(37,116,168,1)',
  292.                 'pointColor' =>'rgba(37,116,168,1)',
  293.                 'pointStrokeColor' =>'rgba(255,255,255,1.00)',
  294.             ],
  295.             [
  296.                 'title'=>'Latency','data'=>$latency,                
  297.                 'fillColor' =>'rgba(204,46,59,0.2)',
  298.                 'strokeColor' =>'rgba(204,46,59,1)',
  299.                 'pointColor' =>'rgba(204,46,59,1)',
  300.                 'pointStrokeColor' =>'rgba(255,255,255,1.00)',
  301.             ],
  302.             [
  303.                 'title'=>'Packets Lost',
  304.                 'data'=>$numberPacketsLost,
  305.                 'fillColor' =>'rgba(80,124,75,0.2)'
  306.             ],
  307.         ];  
  308.        
  309.        
  310.         return $this->render('weekly', [
  311.             'chartTitle' => $chartTitle,
  312.             'chartLabels' => $chartLabels,
  313.             'chartData' => $datasets,
  314.             'ipData' => $ipData,
  315.             'time' => $startTimeOfWeek
  316.         ]);
  317.     }
  318.    
  319.     public function actionSearch()
  320.     {
  321.         $searchModel = new CDRSearch();
  322.        
  323.         $dataProvider = $searchModel->search(null, Yii::$app->request->queryParams, date('m'));
  324.  
  325.         return $this->render('search', [
  326.             'searchModel' => $searchModel,
  327.             'dataProvider' => $dataProvider,
  328.         ]);
  329.     }
  330.  
  331.     /**
  332.      * Login action.
  333.      *
  334.      * @return string
  335.      */
  336.     public function actionLogin()
  337.     {
  338.         if (!Yii::$app->user->isGuest) {
  339.             return $this->goHome();
  340.         }
  341.  
  342.         $model = new LoginForm();
  343.         if ($model->load(Yii::$app->request->post()) && $model->login()) {
  344.             return $this->goBack();
  345.         }
  346.         return $this->render('login', [
  347.             'model' => $model,
  348.         ]);
  349.     }
  350.  
  351.     /**
  352.      * Logout action.
  353.      *
  354.      * @return string
  355.      */
  356.     public function actionLogout()
  357.     {
  358.         Yii::$app->user->logout();
  359.  
  360.         return $this->goHome();
  361.     }
  362.  
  363.     /**
  364.      * Displays contact page.
  365.      *
  366.      * @return string
  367.      */
  368.     public function actionContact()
  369.     {
  370.         $model = new ContactForm();
  371.         if ($model->load(Yii::$app->request->post()) && $model->contact(Yii::$app->params['adminEmail'])) {
  372.             Yii::$app->session->setFlash('contactFormSubmitted');
  373.  
  374.             return $this->refresh();
  375.         }
  376.         return $this->render('contact', [
  377.             'model' => $model,
  378.         ]);
  379.     }
  380.  
  381.     /**
  382.      * Displays about page.
  383.      *
  384.      * @return string
  385.      */
  386.     public function actionAbout()
  387.     {
  388.         return $this->render('about');
  389.     }
  390. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement