src/Controller/Admin/DashboardController.php line 24

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Admin;
  3. use App\Repository\PlayerRepository;
  4. use App\Service\AssetManager;
  5. use App\Service\MemcachedWrapper;
  6. use Doctrine\DBAL\Connection;
  7. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Cache;
  8. use Symfony\Component\HttpFoundation\Request;
  9. use Symfony\Component\Routing\Annotation\Route;
  10. class DashboardController extends AbstractAdminController {
  11.   /**
  12.    * @var MemcachedWrapper
  13.    */
  14.   protected $memc;
  15.   
  16.   /**
  17.    * @Route("/")
  18.    * @Cache(mustRevalidate=true)
  19.    */
  20.   public function index(Request $rPlayerRepository $prepoAssetManager $thmngMemcachedWrapper $memc) {   
  21.     $this->memc $memc;
  22.     $stats $this->getSystemStats($this->session);
  23.     
  24.     if($this->isClientContext()) {
  25.       $client $this->getClient();
  26.       $totalPlayers $prepo->getPlayerCountForClient($client);
  27.       $livePlayers $prepo->getLivePlayersForClient($client);
  28.       $players $prepo->getPlayersWithViewersForClient($client);
  29.       $upcomingPlayers $prepo->getUpcomingPlayersForClient($client);
  30.       
  31.       $totalViewers 0;
  32.       $ids = [];
  33.       foreach($players as $p) {
  34.         $totalViewers += $p['viewers'];
  35.         $ids[$p['slug']] = 1;
  36.       }
  37.       
  38.       foreach($livePlayers as $p) {
  39.         $p['viewers'] = 0;
  40.         $p['live'] = true;
  41.         if(!isset($ids[$p['slug']])) $players[] = $p;
  42.       }
  43.       
  44.       return $this->render(
  45.         'admin/dashboard/client.html.twig'
  46.         [
  47.             'totalPlayers' => $totalPlayers,
  48.             'live' => $livePlayers,
  49.             'players' => $players,
  50.             'viewers' => $totalViewers,
  51.             'thmng' => $thmng,
  52.             'upcomingPlayers' => $upcomingPlayers,
  53.         ]
  54.       );
  55.     } else {
  56.       $livePlayers $prepo->getLivePlayers();
  57.       $players $prepo->getPlayersWithViewers();
  58.       $stats $this->getSystemStats();
  59.       $upcomingPlayers $prepo->getUpcomingPlayers();
  60.       
  61.       $totalViewers 0;
  62.       $ids = [];
  63.       foreach($players as $p) {
  64.         $totalViewers += $p['viewers'];
  65.         $ids[$p['slug']] = 1;
  66.       }
  67.       
  68.       foreach($livePlayers as $p) {
  69.         $p['viewers'] = 0;
  70.         $p['live'] = true;
  71.         if(!isset($ids[$p['slug']])) $players[] = $p;
  72.       }
  73.       
  74.       return $this->render(
  75.         'admin/dashboard/admin.html.twig'
  76.         [
  77.             'live' => $livePlayers,
  78.             'viewers' => $totalViewers,
  79.             'stats' => $stats,
  80.             'players' => $players,
  81.             'thmng' => $thmng,
  82.             'upcomingPlayers' => $upcomingPlayers,
  83.         ]
  84.       );
  85.     }
  86.   }
  87.   
  88.   private function getSystemStats() {
  89.     $this->session->start();
  90.     $stats = [];
  91.     
  92.     $memcstats $this->memc->getStats();
  93.     $stats['memc'] = array_pop($memcstats);
  94.     
  95.     $nginx file_get_contents("http://127.0.0.1/nginx_status");
  96.     if(preg_match('/Active connections:\s*(?P<active>[0-9]+)\s*[^\n]+\s*(?P<accepts>[0-9]+)\s*(?P<handled>[0-9]+)\s*(?P<requests>[0-9]+)\s*\nReading:\s*(?P<reading>[0-9]+)\s*'.
  97.             'Writing:\s*(?P<writing>[0-9]+)\s*Waiting:\s*(?P<waiting>[0-9]+)/m'$nginx$matches)) {
  98.       $stats['nginx'] = array_filter($matches"is_string"ARRAY_FILTER_USE_KEY);
  99.     }
  100.     
  101.     $fpm file_get_contents("http://127.0.0.1/fpm_status");
  102.     $stats['fpm'] = [];
  103.     foreach(explode("\n"$fpm) as $line) {
  104.        $parts explode(':'$line);
  105.        if(!isset($parts[1])) continue;
  106.        $stats['fpm'][str_replace(' ''_'trim($parts[0]))] = trim($parts[1]);
  107.     }
  108.     
  109.     /* @var $conn Connection */
  110.     $conn $this->getDoctrine()->getConnection();
  111.     $q $conn->executeQuery("SHOW GLOBAL STATUS");
  112.     $stats['mysql'] = [];
  113.     while($r $q->fetch()) {
  114.       $stats['mysql'][$r['Variable_name']] = $r['Value'];
  115.     }
  116.     
  117.     
  118.     $stats['load'] = sys_getloadavg();
  119.     
  120.     $stats['time'] = time();
  121.     $oldstats $this->session->get('OMNISTATS');
  122.     
  123.     // Compute diff 
  124.     $diff 0;
  125.     if($oldstats && $stats['time'] != $oldstats['time']) {
  126.       $diff $stats['time'] - $oldstats['time'];
  127.             
  128.       $stats['nginx']['accepts_per_sec'] =  round(($stats['nginx']['accepts'] - $oldstats['nginx']['accepts']) / $diff);
  129.       $stats['nginx']['handled_per_sec'] =  round(($stats['nginx']['handled'] - $oldstats['nginx']['handled']) / $diff);
  130.       $stats['nginx']['requests_per_sec'] =  round(($stats['nginx']['requests'] - $oldstats['nginx']['requests']) / $diff);
  131.       
  132.       $stats['memc']['get_per_sec'] =  round(($stats['memc']['cmd_get'] - $oldstats['memc']['cmd_get']) / $diff);
  133.       $stats['memc']['set_per_sec'] =  round(($stats['memc']['cmd_set'] - $oldstats['memc']['cmd_set']) / $diff);
  134.       $stats['fpm']['accepted_conn_per_sec'] =  round(($stats['fpm']['accepted_conn'] - $oldstats['fpm']['accepted_conn']) / $diff);
  135.       $stats['mysql']['Com_select_per_sec'] =  round(($stats['mysql']['Com_select'] - $oldstats['mysql']['Com_select']) / $diff);
  136.       $stats['mysql']['Com_update_per_sec'] =  round(($stats['mysql']['Com_update'] - $oldstats['mysql']['Com_update']) / $diff);
  137.       $stats['mysql']['Com_insert_per_sec'] =  round(($stats['mysql']['Com_insert'] - $oldstats['mysql']['Com_insert']) / $diff);
  138.       $stats['mysql']['Connections_per_sec'] =  round(($stats['mysql']['Connections'] - $oldstats['mysql']['Connections']) / $diff);
  139.     }
  140.     
  141.     // Save session only if old stats
  142.     if(!$oldstats || $diff 60) {
  143.       $this->session->set("OMNISTATS"$stats);
  144.     }
  145.     
  146.     return $stats;
  147.   }
  148. }