Skip to content
This repository has been archived by the owner on Jul 6, 2020. It is now read-only.

dashboard view #6

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
Open
226 changes: 215 additions & 11 deletions app/src/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,213 @@ public function __construct(Application $app)
*/
public function index(Request $request, Response $response, array $args)
{
$layout = $request->query->get('layout', 'grid');
in_array($layout, ['grid', 'list']) or $layout = 'grid';
$layout = $request->query->get('layout', 'dashboard');
$filter = $request->query->get('filter', 'none');
$filter_value = $request->query->get('filter_value', 'none');
$sort = $request->query->get('sort', 'none');

$instances = $this->app['manager']->getAll();

$allProcesses = [];
$hosts = [];
$groups = [];
$unreachableHosts = [];
$notRunning = 0;
$states = [];

//Building datas for layout
foreach ($instances as $key => $value) {
if($value->isConnected()) {
$processes = $value->getAllProcessInfo();
foreach ($processes as $process) {
$process['host'] = $key;
$allProcesses[] = $process;
array_key_exists($process['host'], $hosts) ? $hosts[$process['host']] += 1 : $hosts[$process['host']] = 1 ;
array_key_exists($process['group'], $groups) ? $groups[$process['group']] += 1 : $groups[$process['group']] = 1;
array_key_exists($process['statename'], $states) ? $states[$process['statename']] += 1 : $states[$process['statename']] = 1;
}
}
else {
$unreachableHosts[] = $key;
}
}

ksort($hosts);
ksort($groups);

switch($layout) {
case 'grid':
break;
case 'list':
break;
case 'dashboard':
break;
case 'global':
switch($filter) {
case 'group':
$allProcesses = $this->filter_processes($filter, $filter_value, $allProcesses);
break;
case 'host':
$allProcesses = $this->filter_processes($filter, $filter_value, $allProcesses);
break;
case 'statename':
$allProcesses = $this->filter_processes($filter, $filter_value, $allProcesses);
break;
}

switch($sort) {
case 'group':
usort($allProcesses, array($this,'group_sort'));
break;
case 'host':
usort($allProcesses, array($this,'host_sort'));
break;
case 'state':
usort($allProcesses, array($this,'state_sort'));
break;
case 'start':
usort($allProcesses, array($this,'start_sort'));
break;
}

break;
default:
$layout = 'grid';
break;
}

$template = $this->app['twig']->loadTemplate(sprintf('layout/%s.twig', $layout));
$response->setContent($template->render(['instances' => $this->app['manager']->getAll()]));

$response->setContent($template->render([
'instances' => $instances,
'hosts' => $hosts,
'allProcesses' => $allProcesses,
'states' => $states,
'filter' => $filter,
'filter_value' => $filter_value,
'unreachableHosts' => $unreachableHosts,
'groups' => $groups
]));

return $response;
}

private function group_sort($a, $b) {
return strcmp($a["group"], $b["group"]);
}

private function host_sort($a, $b) {
return strcmp($a["host"], $b["host"]);
}

private function start_sort($a, $b) {
return $a["start"] > $b["start"];
}

private function state_sort($a, $b) {
return $a["state"] > $b["state"];
}

private function filter_processes($key, $value, $processes) {
if ($value == 'all') return $processes;
$tokeep = [];
foreach ($processes as $process) {
//TODO key exist ?
if ($process[$key] == $value) {
$tokeep[] = $process;
}
}
return $tokeep;
}

/**
* Starts all processes with specific group
*
* @param Request $request
* @param Response $response
* @param array $args
*
* @return Response
*/
public function startGroup(Request $request, Response $response, array $args)
{
$group = $args['group'];

$instances = $this->app['manager']->getAll();

foreach ($instances as $name => $instance) {
if($instance->isConnected()) {
$processes = $instance->getAllProcessInfo();
foreach ($processes as $process) {
if ($process['group'] == $group || $group == 'all') {
$instance->startProcess($process['group'] . ':' . $process['name'], false);
}
}
}
}

return new RedirectResponse("/?layout=global&filter=group&filter_value=$group");
}

/**
* Restarts all processes with specific group
*
* @param Request $request
* @param Response $response
* @param array $args
*
* @return Response
*/
public function restartGroup(Request $request, Response $response, array $args)
{
$group = $args['group'];

$instances = $this->app['manager']->getAll();

foreach ($instances as $name => $instance) {
if($instance->isConnected()) {
$processes = $instance->getAllProcessInfo();
foreach ($processes as $process) {
if ($process['group'] == $group || $group == 'all') {
$instance->stopProcess($process['group'] . ':' . $process['name'], true);
$instance->startProcess($process['group'] . ':' . $process['name'], false);
}
}
}
}

return new RedirectResponse("/?layout=global&filter=group&filter_value=$group");
}

/**
* Stops all processes with specific group
*
* @param Request $request
* @param Response $response
* @param array $args
*
* @return Response
*/
public function stopGroup(Request $request, Response $response, array $args)
{
$group = $args['group'];

$instances = $this->app['manager']->getAll();

foreach ($instances as $name => $instance) {
if($instance->isConnected()) {
$processes = $instance->getAllProcessInfo();
foreach ($processes as $process) {
if ($process['group'] == $group || $group == 'all') {
$instance->stopProcess($process['group'] . ':' . $process['name'], false);
}
}
}
}

return new RedirectResponse("/?layout=global&filter=group&filter_value=$group");
}

/**
* Starts all processes in an instance
*
Expand All @@ -67,11 +265,13 @@ public function index(Request $request, Response $response, array $args)
*/
public function startAll(Request $request, Response $response, array $args)
{
$instance = $this->app['manager']->get($args['instance']);
$host = $args['instance'];

$instance = $this->app['manager']->get($host);

$instance->startAllProcesses(false);

return new RedirectResponse('/');
return new RedirectResponse("/?layout=global&filter=host&filter_value=$host");
}

/**
Expand All @@ -85,12 +285,14 @@ public function startAll(Request $request, Response $response, array $args)
*/
public function restartAll(Request $request, Response $response, array $args)
{
$instance = $this->app['manager']->get($args['instance']);
$host = $args['instance'];

$instance->stopAllProcesses(false);
$instance = $this->app['manager']->get($host);

$instance->stopAllProcesses(true);
$instance->startAllProcesses(false);

return new RedirectResponse('/');
return new RedirectResponse("/?layout=global&filter=host&filter_value=$host");
}

/**
Expand All @@ -104,11 +306,13 @@ public function restartAll(Request $request, Response $response, array $args)
*/
public function stopAll(Request $request, Response $response, array $args)
{
$instance = $this->app['manager']->get($args['instance']);
$host = $args['instance'];

$instance = $this->app['manager']->get($host);

$instance->stopAllProcesses(false);

return new RedirectResponse('/');
return new RedirectResponse("/?layout=global&filter=host&filter_value=$host");
}

/**
Expand Down Expand Up @@ -142,7 +346,7 @@ public function restartProcess(Request $request, Response $response, array $args
{
$instance = $this->app['manager']->get($args['instance']);

$instance->stopProcess($args['process'], false);
$instance->stopProcess($args['process'], true);
$instance->startProcess($args['process'], false);

return new RedirectResponse('/');
Expand Down
5 changes: 3 additions & 2 deletions app/src/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,9 @@ public function get($instance)

$config = $this->config[$instance];
$transport = new StreamSocketTransport;

$transport->setHeader('Authorization', 'Basic '.base64_encode(sprintf('%s:%s', $config['username'], $config['password'])));
if (array_key_exists('username', $config) && array_key_exists('username', $config)) {
$transport->setHeader('Authorization', 'Basic '.base64_encode(sprintf('%s:%s', $config['username'], $config['password'])));
}

$client = new Client($config['url'], $transport);
$connector = new XmlRpc($client);
Expand Down
3 changes: 3 additions & 0 deletions app/views/actions_filter.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<a href="/{{ filter }}/start/{{ filter_value }}" title="Start all" data-toggle="tooltip"><span class="glyphicon glyphicon-play text-success"></span></a>
<a href="/{{ filter }}/restart/{{ filter_value }}" title="Restart all" data-toggle="tooltip"><span class="glyphicon glyphicon-refresh"></span></a>
<a href="/{{ filter }}/stop/{{ filter_value }}" title="Stop all" data-toggle="tooltip"><span class="glyphicon glyphicon-stop text-danger"></span></a>
56 changes: 56 additions & 0 deletions app/views/layout/dashboard.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
{% extends 'template.twig' %}

{% block content %}
{% if unreachableHosts is not empty %}
<div class="row">
<div class="col-lg-offset-2 col-lg-8">
<div class="alert alert-danger" role="alert">
One or more host unreachable:
<ul>
{% for host in unreachableHosts %}
<li>{{ host }}</li>
{% endfor %}
</ul>
</div>
</div>
</div>
{% endif %}
<div class="row">
<div class="col-lg-offset-2 col-lg-4">
<div class="panel panel-default">
<h3>Total processes <span class="label label-default">{{ allProcesses|length }}</span></h3>
<a href="/?layout=global&filter=group&amp;filter_value=all">View all processes</a>
</div>
</div>
<div class="col-lg-4">
<div class="panel panel-default">
{% for state, count in states %}
<h3>{{ state }} <a href="/?layout=global&filter=statename&amp;filter_value={{ state }}"><span class="label label-default">{{ count }}</span></a></h3>
{% endfor %}
</div>
</div>
</div>
<div class="row">
<div class="col-lg-offset-2 col-lg-4">
<div class="panel panel-default">
<h3>Hosts <span class="label label-default">{{ hosts|length }}</span></h3>
<ul>
{% for host,number in hosts %}
<li><span class="label label-default">{{ number }}</span> <a href="/?layout=global&amp;filter=host&amp;filter_value={{host}}">{{ host }}</a></li>
{% endfor %}
</ul>
</div>
</div>
<div class="col-lg-4">
<div class="panel panel-default">
<h3>Groups <span class="label label-default">{{ groups|length }}</span></h3>
<ul>
{% for group,number in groups %}
<li><span class="label label-default">{{ number }}</span> <a href="/?layout=global&amp;filter=group&amp;filter_value={{ group }}">{{ group }}</a></li>
{% endfor %}
</ul>
</div>
</div>
</div>
{% endblock %}

40 changes: 40 additions & 0 deletions app/views/layout/global.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
{% extends 'template.twig' %}

{% block content %}
{% if ( filter != none ) and ( ( filter == 'host' ) or ( filter == 'group' ) ) %}
<div class="panel-heading">
<strong>{{ filter_value }}</strong> | {{ allProcesses|length|pluralize('process') }}
<span>
{% include 'actions_filter.twig' %}
</span>
</div>
{% endif %}
<div class="row">
<div class="col-lg-12">
<div class="panel panel-default">
<table class="table table-striped table-bordered">
<tr>
<th>Host</th>
<th>Group</th>
<th>Name</th>
<th>State</th>
<th>Start</th>
<th>Controls</th>
</tr>
{% for process in allProcesses %}
<tr>
<td class="col-lg-2">{{ process.host }}</td>
<td class="col-lg-3">{{ process.group }}</td>
<td class="col-lg-3">{{ process.name }}</td>
<td class="col-lg-1"><span class="label label-{{ process.statename|state }}">{{ process.statename }}</span></td>
<td class="col-lg-1">{{ process|state_ago }}</td>
<td class="col-lg-1">{% include 'actions.twig' with {'instance': process.host, 'process': process.name, 'group': process.group} only %}</td>
</tr>
{% else %}
<tr><td><i>No processes</i></td></tr>
{% endfor %}
</table>
</div>
</div>
</div>
{% endblock %}
2 changes: 1 addition & 1 deletion app/views/template.twig
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="#">Supervisor Monitor</a>
<a class="navbar-brand" href="/">Supervisor Monitor</a>
</div>
<div id="navbar" class="collapse navbar-collapse navbar-right">
<ul class="nav navbar-nav">
Expand Down
Loading