1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
|
<?php
/* Icinga Web 2 | (c) 2015 Icinga Development Team | GPLv2+ */
namespace Icinga\Module\Monitoring\Web\Navigation\Renderer;
use Exception;
use Icinga\Application\Logger;
use Icinga\Authentication\Auth;
use Icinga\Data\Filter\Filter;
use Icinga\Data\Filterable;
use Icinga\Module\Monitoring\Backend\MonitoringBackend;
use Icinga\Web\Navigation\Renderer\BadgeNavigationItemRenderer;
/**
* Render generic DataView columns as badges in menu items
*
* It is possible to configure the class of the rendered badge as option 'class', the
* columns to fetch using the option 'columns' and the DataView from which the columns
* will be fetched using the option 'dataview'.
*/
class MonitoringBadgeNavigationItemRenderer extends BadgeNavigationItemRenderer
{
/**
* Cached count
*
* @var int
*/
protected $count;
/**
* Caches the responses for all executed summaries
*
* @var array
*/
protected static $summaries = array();
/**
* Accumulates all needed columns for a view to allow fetching the needed columns in
* one single query
*
* @var array
*/
protected static $dataViews = array();
/**
* The dataview referred to by the navigation item
*
* @var string
*/
protected $dataView;
/**
* The columns and titles displayed in the badge
*
* @var array
*/
protected $columns;
/**
* Set the dataview referred to by the navigation item
*
* @param string $dataView
*
* @return $this
*/
public function setDataView($dataView)
{
$this->dataView = $dataView;
return $this;
}
/**
* Return the dataview referred to by the navigation item
*
* @return string
*/
public function getDataView()
{
return $this->dataView;
}
/**
* Set the columns and titles displayed in the badge
*
* @param array $columns
*
* @return $this
*/
public function setColumns(array $columns)
{
$this->columns = $columns;
return $this;
}
/**
* Return the columns and titles displayed in the badge
*
* @return array
*/
public function getColumns()
{
return $this->columns;
}
/**
* Apply a restriction on the given data view
*
* @param string $restriction The name of restriction
* @param Filterable $filterable The filterable to restrict
*
* @return Filterable The filterable
*/
protected static function applyRestriction($restriction, Filterable $filterable)
{
$restrictions = Filter::matchAny();
foreach (Auth::getInstance()->getRestrictions($restriction) as $filter) {
if ($filter === '*') {
$filterable->addFilter(Filter::matchAll());
return $filterable;
}
$restrictions->addFilter(Filter::fromQueryString($filter));
}
$filterable->applyFilter($restrictions);
return $filterable;
}
/**
* Fetch the dataview from the database
*
* @return object
*/
protected function fetchDataView()
{
$summary = MonitoringBackend::instance()->select()->from(
$this->getDataView(),
array_keys($this->getColumns())
);
static::applyRestriction('monitoring/filter/objects', $summary);
return $summary->fetchRow();
}
/**
* {@inheritdoc}
*/
public function getCount()
{
if ($this->count === null) {
try {
$summary = $this->fetchDataView();
} catch (Exception $e) {
Logger::debug($e);
$this->count = 1;
$this->state = static::STATE_UNKNOWN;
$this->title = $e->getMessage();
return $this->count;
}
$count = 0;
$titles = array();
foreach ($this->getColumns() as $column => $title) {
if (isset($summary->$column) && $summary->$column > 0) {
$titles[] = sprintf($title, $summary->$column);
$count += $summary->$column;
}
}
$this->count = $count;
$this->title = implode('. ', $titles);
}
return $this->count;
}
}
|