Monitoring: adjust Backend and DataView

Made them use the new filters and interfaces.

refs #6418
This commit is contained in:
Thomas Gelf 2014-06-06 07:05:21 +00:00
parent 1c3ab74e80
commit 1677193571
2 changed files with 79 additions and 29 deletions

View File

@ -7,6 +7,7 @@ namespace Icinga\Module\Monitoring;
use Icinga\Exception\ProgrammingError; use Icinga\Exception\ProgrammingError;
use Icinga\Data\Selectable; use Icinga\Data\Selectable;
use Icinga\Data\Queryable; use Icinga\Data\Queryable;
use Icinga\Data\ConnectionInterface;
use Icinga\Application\Config as IcingaConfig; use Icinga\Application\Config as IcingaConfig;
use Icinga\Data\ResourceFactory; use Icinga\Data\ResourceFactory;
@ -15,7 +16,7 @@ use Icinga\Exception\ConfigurationError;
/** /**
* Data view and query loader tied to a backend type * Data view and query loader tied to a backend type
*/ */
class Backend implements Selectable, Queryable class Backend implements Selectable, Queryable, ConnectionInterface
{ {
/** /**
* Resource * Resource
@ -90,6 +91,11 @@ class Backend implements Selectable, Queryable
return new Backend($resource, $backendConfig->type); return new Backend($resource, $backendConfig->type);
} }
public function getResource()
{
return $this->resource;
}
/** /**
* Backend entry point * Backend entry point
* *
@ -111,8 +117,7 @@ class Backend implements Selectable, Queryable
public function from($viewName, array $columns = null) public function from($viewName, array $columns = null)
{ {
$viewClass = $this->resolveDataViewName($viewName); $viewClass = $this->resolveDataViewName($viewName);
$queryClass = $this->resolveQueryName($viewClass::getQueryName()); return new $viewClass($this, $columns);
return new $viewClass(new $queryClass($this->resource), $columns);
} }
/** /**
@ -132,6 +137,11 @@ class Backend implements Selectable, Queryable
return $viewClass; return $viewClass;
} }
public function getQueryClass($name)
{
return $this->resolveQueryName($name);
}
/** /**
* Query name to class name resolution * Query name to class name resolution
* *
@ -149,7 +159,7 @@ class Backend implements Selectable, Queryable
. 'Query'; . 'Query';
if (!class_exists($queryClass)) { if (!class_exists($queryClass)) {
throw new ProgrammingError( 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; return $queryClass;

View File

@ -29,13 +29,16 @@
namespace Icinga\Module\Monitoring\DataView; namespace Icinga\Module\Monitoring\DataView;
use Icinga\Data\Filter\Filter;
use Icinga\Data\SimpleQuery; use Icinga\Data\SimpleQuery;
use Icinga\Data\Browsable; use Icinga\Data\Browsable;
use Icinga\Data\PivotTable; use Icinga\Data\PivotTable;
use Icinga\Data\Sortable; use Icinga\Data\Sortable;
use Icinga\Filter\Filterable; use Icinga\Data\ConnectionInterface;
use Icinga\Module\Monitoring\Filter\UrlViewFilter; use Icinga\Data\Filterable;
use Icinga\Web\Request; use Icinga\Web\Request;
use Icinga\Web\Url;
use Icinga\Module\Monitoring\Backend;
/** /**
* A read-only view of an underlying query * A read-only view of an underlying query
@ -49,16 +52,22 @@ abstract class DataView implements Browsable, Filterable, Sortable
*/ */
private $query; private $query;
protected $filter;
protected $connection;
/** /**
* Create a new view * Create a new view
* *
* @param SimpleQuery $query Which backend to query * @param SimpleQuery $query Which backend to query
* @param array $columns Select columns * @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->connection = $connection;
$this->query->columns($columns === null ? $this->getColumns() : $columns); $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; 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 * 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) public static function fromRequest($request, array $columns = null)
{ {
$view = new static(Backend::createBackend($request->getParam('backend')), $columns); $view = new static(Backend::createBackend($request->getParam('backend')), $columns);
$parser = new UrlViewFilter($view); $view->applyUrlFilter($request);
$view->getQuery()->setFilter($parser->fromRequest($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; 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 * Create view from params
* *
@ -126,9 +153,10 @@ abstract class DataView implements Browsable, Filterable, Sortable
foreach ($params as $key => $value) { foreach ($params as $key => $value) {
if ($view->isValidFilterTarget($key)) { if ($view->isValidFilterTarget($key)) {
$view->getQuery()->where($key, $value); $view->where($key, $value);
} }
} }
$order = isset($params['order']) ? $params['order'] : null; $order = isset($params['order']) ? $params['order'] : null;
if ($order !== null) { if ($order !== null) {
if (strtolower($order) === 'desc') { if (strtolower($order) === 'desc') {
@ -142,6 +170,7 @@ abstract class DataView implements Browsable, Filterable, Sortable
isset($params['sort']) ? $params['sort'] : null, isset($params['sort']) ? $params['sort'] : null,
$order $order
); );
return $view; return $view;
} }
@ -163,6 +192,11 @@ abstract class DataView implements Browsable, Filterable, Sortable
return array(); return array();
} }
public function getFilter()
{
return $this->filter;
}
/** /**
* Return a pivot table for the given columns based on the current query * 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; return $this->query;
} }
public function applyFilter() public function applyFilter(Filter $filter)
{ {
$this->query->applyFilter(); return $this->addFilter($filter);
return $this;
} }
public function clearFilter() public function clearFilter()
@ -290,9 +323,16 @@ abstract class DataView implements Browsable, Filterable, Sortable
return $this; 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; return $this;
} }