Monitoring: adjust Backend and DataView
Made them use the new filters and interfaces. refs #6418
This commit is contained in:
parent
1c3ab74e80
commit
1677193571
|
@ -7,6 +7,7 @@ namespace Icinga\Module\Monitoring;
|
|||
use Icinga\Exception\ProgrammingError;
|
||||
use Icinga\Data\Selectable;
|
||||
use Icinga\Data\Queryable;
|
||||
use Icinga\Data\ConnectionInterface;
|
||||
|
||||
use Icinga\Application\Config as IcingaConfig;
|
||||
use Icinga\Data\ResourceFactory;
|
||||
|
@ -15,7 +16,7 @@ use Icinga\Exception\ConfigurationError;
|
|||
/**
|
||||
* Data view and query loader tied to a backend type
|
||||
*/
|
||||
class Backend implements Selectable, Queryable
|
||||
class Backend implements Selectable, Queryable, ConnectionInterface
|
||||
{
|
||||
/**
|
||||
* Resource
|
||||
|
@ -90,6 +91,11 @@ class Backend implements Selectable, Queryable
|
|||
return new Backend($resource, $backendConfig->type);
|
||||
}
|
||||
|
||||
public function getResource()
|
||||
{
|
||||
return $this->resource;
|
||||
}
|
||||
|
||||
/**
|
||||
* Backend entry point
|
||||
*
|
||||
|
@ -111,8 +117,7 @@ class Backend implements Selectable, Queryable
|
|||
public function from($viewName, array $columns = null)
|
||||
{
|
||||
$viewClass = $this->resolveDataViewName($viewName);
|
||||
$queryClass = $this->resolveQueryName($viewClass::getQueryName());
|
||||
return new $viewClass(new $queryClass($this->resource), $columns);
|
||||
return new $viewClass($this, $columns);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -132,6 +137,11 @@ class Backend implements Selectable, Queryable
|
|||
return $viewClass;
|
||||
}
|
||||
|
||||
public function getQueryClass($name)
|
||||
{
|
||||
return $this->resolveQueryName($name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Query name to class name resolution
|
||||
*
|
||||
|
@ -149,7 +159,7 @@ class Backend implements Selectable, Queryable
|
|||
. 'Query';
|
||||
if (!class_exists($queryClass)) {
|
||||
throw new ProgrammingError(
|
||||
'Query ' . ucfirst($queryName) . ' does not exist for backend ' . ucfirst($this->type)
|
||||
'Query "' . ucfirst($queryName) . '" does not exist for backend ' . ucfirst($this->type)
|
||||
);
|
||||
}
|
||||
return $queryClass;
|
||||
|
|
|
@ -29,13 +29,16 @@
|
|||
|
||||
namespace Icinga\Module\Monitoring\DataView;
|
||||
|
||||
use Icinga\Data\Filter\Filter;
|
||||
use Icinga\Data\SimpleQuery;
|
||||
use Icinga\Data\Browsable;
|
||||
use Icinga\Data\PivotTable;
|
||||
use Icinga\Data\Sortable;
|
||||
use Icinga\Filter\Filterable;
|
||||
use Icinga\Module\Monitoring\Filter\UrlViewFilter;
|
||||
use Icinga\Data\ConnectionInterface;
|
||||
use Icinga\Data\Filterable;
|
||||
use Icinga\Web\Request;
|
||||
use Icinga\Web\Url;
|
||||
use Icinga\Module\Monitoring\Backend;
|
||||
|
||||
/**
|
||||
* A read-only view of an underlying query
|
||||
|
@ -49,16 +52,22 @@ abstract class DataView implements Browsable, Filterable, Sortable
|
|||
*/
|
||||
private $query;
|
||||
|
||||
protected $filter;
|
||||
|
||||
protected $connection;
|
||||
|
||||
/**
|
||||
* Create a new view
|
||||
*
|
||||
* @param SimpleQuery $query Which backend to query
|
||||
* @param array $columns Select columns
|
||||
*/
|
||||
public function __construct(SimpleQuery $query, array $columns = null)
|
||||
public function __construct(ConnectionInterface $connection, array $columns = null)
|
||||
{
|
||||
$this->query = $query;
|
||||
$this->query->columns($columns === null ? $this->getColumns() : $columns);
|
||||
$this->connection = $connection;
|
||||
$queryClass = $connection->getQueryClass($this->getQueryName());
|
||||
$this->query = new $queryClass($this->connection->getResource(), $columns);
|
||||
$this->filter = Filter::matchAll();
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -75,6 +84,18 @@ abstract class DataView implements Browsable, Filterable, Sortable
|
|||
return $tableName;
|
||||
}
|
||||
|
||||
public function where($condition, $value = null)
|
||||
{
|
||||
$this->filter->addFilter(Filter::where($condition, $value));
|
||||
$this->query->where($condition, $value);
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function dump()
|
||||
{
|
||||
return $this->query->dump();
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve columns provided by this view
|
||||
*
|
||||
|
@ -94,24 +115,30 @@ abstract class DataView implements Browsable, Filterable, Sortable
|
|||
public static function fromRequest($request, array $columns = null)
|
||||
{
|
||||
$view = new static(Backend::createBackend($request->getParam('backend')), $columns);
|
||||
$parser = new UrlViewFilter($view);
|
||||
$view->getQuery()->setFilter($parser->fromRequest($request));
|
||||
$view->applyUrlFilter($request);
|
||||
|
||||
$order = $request->getParam('dir');
|
||||
if ($order !== null) {
|
||||
if (strtolower($order) === 'desc') {
|
||||
$order = self::SORT_DESC;
|
||||
} else {
|
||||
$order = self::SORT_ASC;
|
||||
}
|
||||
}
|
||||
$view->sort(
|
||||
$request->getParam('sort'),
|
||||
$order
|
||||
);
|
||||
return $view;
|
||||
}
|
||||
|
||||
// TODO: This is not the right place for this, move it away
|
||||
protected function applyUrlFilter($request = null)
|
||||
{
|
||||
$url = Url::fromRequest();
|
||||
$limit = $url->shift('limit');
|
||||
$sort = $url->shift('sort');
|
||||
$dir = $url->shift('dir');
|
||||
$page = $url->shift('page');
|
||||
$format = $url->shift('format');
|
||||
$view = $url->shift('view');
|
||||
$view = $url->shift('backend');
|
||||
foreach ($url->getParams() as $k => $v) {
|
||||
$this->where($k, $v);
|
||||
}
|
||||
if ($sort) {
|
||||
$this->order($sort, $dir);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Create view from params
|
||||
*
|
||||
|
@ -126,9 +153,10 @@ abstract class DataView implements Browsable, Filterable, Sortable
|
|||
|
||||
foreach ($params as $key => $value) {
|
||||
if ($view->isValidFilterTarget($key)) {
|
||||
$view->getQuery()->where($key, $value);
|
||||
$view->where($key, $value);
|
||||
}
|
||||
}
|
||||
|
||||
$order = isset($params['order']) ? $params['order'] : null;
|
||||
if ($order !== null) {
|
||||
if (strtolower($order) === 'desc') {
|
||||
|
@ -142,6 +170,7 @@ abstract class DataView implements Browsable, Filterable, Sortable
|
|||
isset($params['sort']) ? $params['sort'] : null,
|
||||
$order
|
||||
);
|
||||
|
||||
return $view;
|
||||
}
|
||||
|
||||
|
@ -163,6 +192,11 @@ abstract class DataView implements Browsable, Filterable, Sortable
|
|||
return array();
|
||||
}
|
||||
|
||||
public function getFilter()
|
||||
{
|
||||
return $this->filter;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a pivot table for the given columns based on the current query
|
||||
*
|
||||
|
@ -278,10 +312,9 @@ abstract class DataView implements Browsable, Filterable, Sortable
|
|||
return $this->query;
|
||||
}
|
||||
|
||||
public function applyFilter()
|
||||
public function applyFilter(Filter $filter)
|
||||
{
|
||||
$this->query->applyFilter();
|
||||
return $this;
|
||||
return $this->addFilter($filter);
|
||||
}
|
||||
|
||||
public function clearFilter()
|
||||
|
@ -290,9 +323,16 @@ abstract class DataView implements Browsable, Filterable, Sortable
|
|||
return $this;
|
||||
}
|
||||
|
||||
public function addFilter($filter)
|
||||
public function setFilter(Filter $filter)
|
||||
{
|
||||
$this->query->addFilter($filter);
|
||||
$this->query->setFilter($filter);
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function addFilter(Filter $filter)
|
||||
{
|
||||
$this->query->addFilter(clone($filter));
|
||||
$this->filter = $filter; // TODO: Hmmmm.... and?
|
||||
return $this;
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue