mirror of
https://github.com/Icinga/icingaweb2-module-businessprocess.git
synced 2026-02-18 02:12:24 -05:00
Simulation: refactor the whole class
First step, more to come. Single node simulations should be moved to a dedicated class of course fixes #88 fixes #89
This commit is contained in:
parent
5c84ae3051
commit
60d043cb18
4 changed files with 155 additions and 32 deletions
|
|
@ -21,6 +21,7 @@ class NodeController extends Controller
|
|||
$name = $this->params->get('name');
|
||||
$this->addTitle($this->translate('Business Impact (%s)'), $name);
|
||||
|
||||
$simulation = Simulation::fromSession($this->session());
|
||||
foreach ($this->storage()->listProcessNames() as $configName) {
|
||||
$config = $this->storage()->loadProcess($configName);
|
||||
|
||||
|
|
@ -38,7 +39,6 @@ class NodeController extends Controller
|
|||
}
|
||||
|
||||
MonitoringState::apply($config);
|
||||
$simulation = new Simulation($config, $this->session());
|
||||
$config->applySimulation($simulation);
|
||||
|
||||
foreach ($config->getNode($name)->getPaths() as $path) {
|
||||
|
|
|
|||
|
|
@ -175,7 +175,7 @@ class ProcessController extends Controller
|
|||
|
||||
protected function handleSimulations(BpConfig $bp)
|
||||
{
|
||||
$simulation = new Simulation($bp, $this->session());
|
||||
$simulation = Simulation::fromSession($this->session());
|
||||
|
||||
if ($this->params->get('dismissSimulations')) {
|
||||
Notification::success(
|
||||
|
|
@ -221,7 +221,7 @@ class ProcessController extends Controller
|
|||
} elseif ($action === 'simulation') {
|
||||
$form = $this->loadForm('simulation')
|
||||
->setNode($bp->getNode($this->params->get('simulationnode')))
|
||||
->setSimulation(new Simulation($bp, $this->session()))
|
||||
->setSimulation(Simulation::fromSession($this->session()))
|
||||
->handleRequest();
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -7,70 +7,133 @@ use Icinga\Web\Session\SessionNamespace;
|
|||
|
||||
class Simulation
|
||||
{
|
||||
const DEFAULT_SESSION_KEY = 'bp-simulations';
|
||||
|
||||
/**
|
||||
* @var SessionNamespace
|
||||
*/
|
||||
protected $session;
|
||||
|
||||
/**
|
||||
* @var BpConfig
|
||||
*/
|
||||
protected $bp;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $key;
|
||||
protected $sessionKey;
|
||||
|
||||
/**
|
||||
* @var
|
||||
* @var array
|
||||
*/
|
||||
protected $simulations;
|
||||
protected $simulations = array();
|
||||
|
||||
public function __construct(BpConfig $bp, SessionNamespace $session)
|
||||
{
|
||||
$this->bp = $bp;
|
||||
$this->session = $session;
|
||||
$this->key = 'simulations.' . $bp->getName();
|
||||
}
|
||||
|
||||
public function simulations()
|
||||
{
|
||||
if ($this->simulations === null) {
|
||||
$this->simulations = $this->fetchSimulations();
|
||||
}
|
||||
|
||||
return $this->simulations;
|
||||
}
|
||||
|
||||
protected function setSimulations($simulations)
|
||||
/**
|
||||
* Simulation constructor.
|
||||
* @param array $simulations
|
||||
*/
|
||||
public function __construct(array $simulations = array())
|
||||
{
|
||||
$this->simulations = $simulations;
|
||||
$this->session->set($this->key, $simulations);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $simulations
|
||||
* @return static
|
||||
*/
|
||||
public static function create(array $simulations = array())
|
||||
{
|
||||
return new static($simulations);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param SessionNamespace $session
|
||||
* @param null $sessionKey
|
||||
* @return $this
|
||||
*/
|
||||
public static function fromSession(SessionNamespace $session, $sessionKey = null)
|
||||
{
|
||||
return static::create()
|
||||
->persistToSession($session)
|
||||
->setSessionKey($sessionKey);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $key
|
||||
* @return $this
|
||||
*/
|
||||
public function setSessionKey($key = null)
|
||||
{
|
||||
if ($key === null) {
|
||||
$this->sessionKey = Simulation::DEFAULT_SESSION_KEY;
|
||||
} else {
|
||||
$this->sessionKey = $key;
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
protected function fetchSimulations()
|
||||
/**
|
||||
* @param SessionNamespace $session
|
||||
* @return $this
|
||||
*/
|
||||
public function persistToSession(SessionNamespace $session)
|
||||
{
|
||||
return $this->session->get($this->key, array());
|
||||
$this->session = $session;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function simulations()
|
||||
{
|
||||
return $this->simulations;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $simulations
|
||||
* @return $this
|
||||
*/
|
||||
protected function setSimulations($simulations)
|
||||
{
|
||||
$this->simulations = $simulations;
|
||||
if ($this->session !== null) {
|
||||
$this->session->set($this->sessionKey, $simulations);
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return $this
|
||||
*/
|
||||
public function clear()
|
||||
{
|
||||
$this->simulations = array();
|
||||
$this->session->set($this->key, array());
|
||||
if ($this->session !== null) {
|
||||
$this->session->set($this->sessionKey, array());
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public function count()
|
||||
{
|
||||
return count($this->simulations());
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function isEmpty()
|
||||
{
|
||||
return $this->count() === 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $node
|
||||
* @param $properties
|
||||
*/
|
||||
public function set($node, $properties)
|
||||
{
|
||||
$simulations = $this->simulations();
|
||||
|
|
@ -78,12 +141,21 @@ class Simulation
|
|||
$this->setSimulations($simulations);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $name
|
||||
* @return bool
|
||||
*/
|
||||
public function hasNode($name)
|
||||
{
|
||||
$simulations = $this->simulations();
|
||||
return array_key_exists($name, $simulations);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $name
|
||||
* @return mixed
|
||||
* @throws ProgrammingError
|
||||
*/
|
||||
public function getNode($name)
|
||||
{
|
||||
$simulations = $this->simulations();
|
||||
|
|
@ -93,6 +165,10 @@ class Simulation
|
|||
return $simulations[$name];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $node
|
||||
* @return bool
|
||||
*/
|
||||
public function remove($node)
|
||||
{
|
||||
$simulations = $this->simulations();
|
||||
|
|
|
|||
47
test/php/library/Businessprocess/SimulationTest.php
Normal file
47
test/php/library/Businessprocess/SimulationTest.php
Normal file
|
|
@ -0,0 +1,47 @@
|
|||
<?php
|
||||
|
||||
namespace Tests\Icinga\Module\Businessprocess;
|
||||
|
||||
use Icinga\Module\Businessprocess\Simulation;
|
||||
use Icinga\Module\Businessprocess\Test\BaseTestCase;
|
||||
|
||||
class SimulationTest extends BaseTestCase
|
||||
{
|
||||
public function testSimulationInstantiation()
|
||||
{
|
||||
$class = 'Icinga\\Module\\Businessprocess\\Simulation';
|
||||
$this->assertInstanceOf(
|
||||
$class,
|
||||
Simulation::create()
|
||||
);
|
||||
}
|
||||
|
||||
public function testAppliedSimulation()
|
||||
{
|
||||
$data = (object) array(
|
||||
'state' => 0,
|
||||
'acknowledged' => false,
|
||||
'in_downtime' => false
|
||||
);
|
||||
$config = $this->makeInstance()->loadProcess('simple_with-header');
|
||||
$simulation = Simulation::create(array(
|
||||
'host1;Hoststatus' => $data
|
||||
));
|
||||
$parent = $config->getBpNode('singleHost');
|
||||
|
||||
$config->applySimulation($simulation);
|
||||
$this->assertEquals(
|
||||
'OK',
|
||||
$parent->getStateName()
|
||||
);
|
||||
|
||||
$parent->clearState();
|
||||
$data->state = 1;
|
||||
$simulation->set('host1;Hoststatus', $data);
|
||||
$config->applySimulation($simulation);
|
||||
$this->assertEquals(
|
||||
'CRITICAL',
|
||||
$parent->getStateName()
|
||||
);
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue