<?php
namespace App\Controller\Admin;
use App\Repository\PlayerRepository;
use App\Service\AssetManager;
use App\Service\MemcachedWrapper;
use Doctrine\DBAL\Connection;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Cache;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
class DashboardController extends AbstractAdminController {
/**
* @var MemcachedWrapper
*/
protected $memc;
/**
* @Route("/")
* @Cache(mustRevalidate=true)
*/
public function index(Request $r, PlayerRepository $prepo, AssetManager $thmng, MemcachedWrapper $memc) {
$this->memc = $memc;
$stats = $this->getSystemStats($this->session);
if($this->isClientContext()) {
$client = $this->getClient();
$totalPlayers = $prepo->getPlayerCountForClient($client);
$livePlayers = $prepo->getLivePlayersForClient($client);
$players = $prepo->getPlayersWithViewersForClient($client);
$upcomingPlayers = $prepo->getUpcomingPlayersForClient($client);
$totalViewers = 0;
$ids = [];
foreach($players as $p) {
$totalViewers += $p['viewers'];
$ids[$p['slug']] = 1;
}
foreach($livePlayers as $p) {
$p['viewers'] = 0;
$p['live'] = true;
if(!isset($ids[$p['slug']])) $players[] = $p;
}
return $this->render(
'admin/dashboard/client.html.twig',
[
'totalPlayers' => $totalPlayers,
'live' => $livePlayers,
'players' => $players,
'viewers' => $totalViewers,
'thmng' => $thmng,
'upcomingPlayers' => $upcomingPlayers,
]
);
} else {
$livePlayers = $prepo->getLivePlayers();
$players = $prepo->getPlayersWithViewers();
$stats = $this->getSystemStats();
$upcomingPlayers = $prepo->getUpcomingPlayers();
$totalViewers = 0;
$ids = [];
foreach($players as $p) {
$totalViewers += $p['viewers'];
$ids[$p['slug']] = 1;
}
foreach($livePlayers as $p) {
$p['viewers'] = 0;
$p['live'] = true;
if(!isset($ids[$p['slug']])) $players[] = $p;
}
return $this->render(
'admin/dashboard/admin.html.twig',
[
'live' => $livePlayers,
'viewers' => $totalViewers,
'stats' => $stats,
'players' => $players,
'thmng' => $thmng,
'upcomingPlayers' => $upcomingPlayers,
]
);
}
}
private function getSystemStats() {
$this->session->start();
$stats = [];
$memcstats = $this->memc->getStats();
$stats['memc'] = array_pop($memcstats);
$nginx = file_get_contents("http://127.0.0.1/nginx_status");
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*'.
'Writing:\s*(?P<writing>[0-9]+)\s*Waiting:\s*(?P<waiting>[0-9]+)/m', $nginx, $matches)) {
$stats['nginx'] = array_filter($matches, "is_string", ARRAY_FILTER_USE_KEY);
}
$fpm = file_get_contents("http://127.0.0.1/fpm_status");
$stats['fpm'] = [];
foreach(explode("\n", $fpm) as $line) {
$parts = explode(':', $line);
if(!isset($parts[1])) continue;
$stats['fpm'][str_replace(' ', '_', trim($parts[0]))] = trim($parts[1]);
}
/* @var $conn Connection */
$conn = $this->getDoctrine()->getConnection();
$q = $conn->executeQuery("SHOW GLOBAL STATUS");
$stats['mysql'] = [];
while($r = $q->fetch()) {
$stats['mysql'][$r['Variable_name']] = $r['Value'];
}
$stats['load'] = sys_getloadavg();
$stats['time'] = time();
$oldstats = $this->session->get('OMNISTATS');
// Compute diff
$diff = 0;
if($oldstats && $stats['time'] != $oldstats['time']) {
$diff = $stats['time'] - $oldstats['time'];
$stats['nginx']['accepts_per_sec'] = round(($stats['nginx']['accepts'] - $oldstats['nginx']['accepts']) / $diff);
$stats['nginx']['handled_per_sec'] = round(($stats['nginx']['handled'] - $oldstats['nginx']['handled']) / $diff);
$stats['nginx']['requests_per_sec'] = round(($stats['nginx']['requests'] - $oldstats['nginx']['requests']) / $diff);
$stats['memc']['get_per_sec'] = round(($stats['memc']['cmd_get'] - $oldstats['memc']['cmd_get']) / $diff);
$stats['memc']['set_per_sec'] = round(($stats['memc']['cmd_set'] - $oldstats['memc']['cmd_set']) / $diff);
$stats['fpm']['accepted_conn_per_sec'] = round(($stats['fpm']['accepted_conn'] - $oldstats['fpm']['accepted_conn']) / $diff);
$stats['mysql']['Com_select_per_sec'] = round(($stats['mysql']['Com_select'] - $oldstats['mysql']['Com_select']) / $diff);
$stats['mysql']['Com_update_per_sec'] = round(($stats['mysql']['Com_update'] - $oldstats['mysql']['Com_update']) / $diff);
$stats['mysql']['Com_insert_per_sec'] = round(($stats['mysql']['Com_insert'] - $oldstats['mysql']['Com_insert']) / $diff);
$stats['mysql']['Connections_per_sec'] = round(($stats['mysql']['Connections'] - $oldstats['mysql']['Connections']) / $diff);
}
// Save session only if old stats
if(!$oldstats || $diff > 60) {
$this->session->set("OMNISTATS", $stats);
}
return $stats;
}
}