monitoring/timeline: should benefit from new filter

Different changes have been applied:

* Allow integer unix timestamps as parameters for timestamp columns
* Remove alias-rewriting from Url class
* Remove all traces of raw_timestamp
* Use new filters
This commit is contained in:
Thomas Gelf 2014-06-21 03:09:40 +02:00
parent 78193137f0
commit b3cdeee35d
12 changed files with 45 additions and 100 deletions

View File

@ -165,6 +165,25 @@ class DbQuery extends SimpleQuery
return preg_replace('/\*/', '%', $value); return preg_replace('/\*/', '%', $value);
} }
protected function valueToTimestamp($value)
{
// We consider integers as valid timestamps. Does not work for URL params
if (ctype_digit($value)) {
return $value;
}
$value = strtotime($value);
if (! $value) {
/*
NOTE: It's too late to throw exceptions, we might finish in __toString
throw new \Exception(sprintf(
'"%s" is not a valid time expression',
$value
));
*/
}
return $value;
}
protected function timestampForSql($value) protected function timestampForSql($value)
{ {
// TODO: do this db-aware // TODO: do this db-aware
@ -174,7 +193,7 @@ class DbQuery extends SimpleQuery
public function whereToSql($col, $sign, $expression) public function whereToSql($col, $sign, $expression)
{ {
if ($this->isTimestamp($col)) { if ($this->isTimestamp($col)) {
$expression = strtotime($expression); $expression = $this->valueToTimestamp($expression);
} }
if (is_array($expression) && $sign === '=') { if (is_array($expression) && $sign === '=') {
// TODO: Should we support this? Doesn't work for blub* // TODO: Should we support this? Doesn't work for blub*

View File

@ -51,13 +51,6 @@ class Url
*/ */
protected $params; protected $params;
/**
* An array to map aliases to valid parameters
*
* @var array
*/
protected $aliases = array();
/** /**
* The site anchor after the '#' * The site anchor after the '#'
* *
@ -215,33 +208,6 @@ class Url
return preg_replace('/#.*$/', '', $url); return preg_replace('/#.*$/', '', $url);
} }
/**
* Set the array to be used to map aliases to valid parameters
* TODO: Kill this
*
* @param array $aliases The array to be used (alias => param)
*
* @return self
*/
public function setAliases(array $aliases)
{
$this->aliases = $aliases;
return $this;
}
/**
* Return the parameter for the given alias
* TODO: Kill this
*
* @param string $alias The alias to translate
*
* @return string The parameter name
*/
public function translateAlias($alias)
{
return array_key_exists($alias, $this->aliases) ? $this->aliases[$alias] : $alias;
}
/** /**
* Overwrite the baseUrl * Overwrite the baseUrl
* *

View File

@ -9,13 +9,13 @@ use Icinga\Web\Url;
use Icinga\Util\Format; use Icinga\Util\Format;
use Icinga\Application\Config; use Icinga\Application\Config;
use Icinga\Util\DateTimeFactory; use Icinga\Util\DateTimeFactory;
use Icinga\Web\Controller\ActionController; use Icinga\Module\Monitoring\Controller;
use Icinga\Module\Monitoring\Timeline\TimeLine; use Icinga\Module\Monitoring\Timeline\TimeLine;
use Icinga\Module\Monitoring\Timeline\TimeRange; use Icinga\Module\Monitoring\Timeline\TimeRange;
use Icinga\Module\Monitoring\Web\Widget\TimelineIntervalBox; use Icinga\Module\Monitoring\Web\Widget\TimelineIntervalBox;
use Icinga\Module\Monitoring\DataView\EventHistory as EventHistoryView; use Icinga\Module\Monitoring\DataView\EventHistory as EventHistoryView;
class Monitoring_TimelineController extends ActionController class Monitoring_TimelineController extends Controller
{ {
public function indexAction() public function indexAction()
{ {
@ -23,23 +23,13 @@ class Monitoring_TimelineController extends ActionController
$this->setupIntervalBox(); $this->setupIntervalBox();
list($displayRange, $forecastRange) = $this->buildTimeRanges(); list($displayRange, $forecastRange) = $this->buildTimeRanges();
$detailUrl = Url::fromPath( $detailUrl = Url::fromPath('monitoring/list/eventhistory');
'/monitoring/list/eventhistory',
Url::fromRequest()->getParams()
)->setAliases(
array(
'name' => 'type',
'end' => 'raw_timestamp>',
'start' => 'raw_timestamp<'
)
)->remove(array('start', 'end', 'extend', 'interval'));
$timeline = new TimeLine( $timeline = new TimeLine(
EventHistoryView::fromRequest( $this->backend->select()->from('eventHistory',
$this->getRequest(),
array( array(
'name' => 'type', 'name' => 'type',
'time' => 'raw_timestamp' 'time' => 'timestamp'
) )
), ),
array( array(

View File

@ -32,8 +32,8 @@ $firstRow = !$beingExtended;
<a href="<?= $this->href( <a href="<?= $this->href(
'/monitoring/list/eventhistory', '/monitoring/list/eventhistory',
array( array(
'raw_timestamp<' => $timeInfo[0]->start->getTimestamp(), 'timestamp<' => $timeInfo[0]->start->getTimestamp(),
'raw_timestamp>' => $timeInfo[0]->end->getTimestamp() 'timestamp>' => $timeInfo[0]->end->getTimestamp()
) )
); ?>"> ); ?>">
<?= $timeInfo[0]->end->format($intervalFormat); ?> <?= $timeInfo[0]->end->format($intervalFormat); ?>
@ -69,9 +69,9 @@ $extrapolatedCircleWidth = $timeline->getExtrapolatedCircleWidth($timeInfo[1][$g
$circleWidth $circleWidth
); ?>" href="<?= $timeInfo[1][$groupName]->getDetailUrl()->overwriteParams( ); ?>" href="<?= $timeInfo[1][$groupName]->getDetailUrl()->overwriteParams(
array( array(
'start' => $timeInfo[0]->start->getTimestamp(), 'timestamp<' => $timeInfo[0]->start->getTimestamp(),
'end' => $timeInfo[0]->end->getTimestamp(), 'timestamp>' => $timeInfo[0]->end->getTimestamp(),
'name' => $groupName 'type' => $groupName
) )
); ?>" title="<?= $timeInfo[1][$groupName]->getValue(); ?> <?= $labelAndColor['label']; ?>"></a> ); ?>" title="<?= $timeInfo[1][$groupName]->getValue(); ?> <?= $labelAndColor['label']; ?>"></a>
</div> </div>

View File

@ -29,7 +29,7 @@ class CommentdeletionhistoryQuery extends IdoQuery
public function whereToSql($col, $sign, $expression) public function whereToSql($col, $sign, $expression)
{ {
if ($col === 'UNIX_TIMESTAMP(h.deletion_time)') { if ($col === 'UNIX_TIMESTAMP(h.deletion_time)') {
return 'h.deletion_time ' . $sign . ' ' . $this->timestampForSql(strtotime($expression)); return 'h.deletion_time ' . $sign . ' ' . $this->timestampForSql($this->valueToTimestamp($expression));
} else { } else {
return parent::whereToSql($col, $sign, $expression); return parent::whereToSql($col, $sign, $expression);
} }

View File

@ -29,7 +29,7 @@ class CommenthistoryQuery extends IdoQuery
public function whereToSql($col, $sign, $expression) public function whereToSql($col, $sign, $expression)
{ {
if ($col === 'UNIX_TIMESTAMP(h.comment_time)') { if ($col === 'UNIX_TIMESTAMP(h.comment_time)') {
return 'h.comment_time ' . $sign . ' ' . $this->timestampForSql(strtotime($expression)); return 'h.comment_time ' . $sign . ' ' . $this->timestampForSql($this->valueToTimestamp($expression));
} else { } else {
return parent::whereToSql($col, $sign, $expression); return parent::whereToSql($col, $sign, $expression);
} }

View File

@ -29,7 +29,7 @@ class DowntimeendhistoryQuery extends IdoQuery
public function whereToSql($col, $sign, $expression) public function whereToSql($col, $sign, $expression)
{ {
if ($col === 'UNIX_TIMESTAMP(h.actual_end_time)') { if ($col === 'UNIX_TIMESTAMP(h.actual_end_time)') {
return 'h.actual_end_time ' . $sign . ' ' . $this->timestampForSql(strtotime($expression)); return 'h.actual_end_time ' . $sign . ' ' . $this->timestampForSql($this->valueToTimestamp($expression));
} else { } else {
return parent::whereToSql($col, $sign, $expression); return parent::whereToSql($col, $sign, $expression);
} }

View File

@ -29,7 +29,7 @@ class DowntimestarthistoryQuery extends IdoQuery
public function whereToSql($col, $sign, $expression) public function whereToSql($col, $sign, $expression)
{ {
if ($col === 'UNIX_TIMESTAMP(h.actual_start_time)') { if ($col === 'UNIX_TIMESTAMP(h.actual_start_time)') {
return 'h.actual_start_time ' . $sign . ' ' . $this->timestampForSql(strtotime($expression)); return 'h.actual_start_time ' . $sign . ' ' . $this->timestampForSql($this->valueToTimestamp($expression));
} else { } else {
return parent::whereToSql($col, $sign, $expression); return parent::whereToSql($col, $sign, $expression);
} }

View File

@ -29,7 +29,7 @@ class NotificationhistoryQuery extends IdoQuery
public function whereToSql($col, $sign, $expression) public function whereToSql($col, $sign, $expression)
{ {
if ($col === 'UNIX_TIMESTAMP(n.start_time)') { if ($col === 'UNIX_TIMESTAMP(n.start_time)') {
return 'n.start_time ' . $sign . ' ' . $this->timestampForSql(strtotime($expression)); return 'n.start_time ' . $sign . ' ' . $this->timestampForSql($this->valueToTimestamp($expression));
} else { } else {
return parent::whereToSql($col, $sign, $expression); return parent::whereToSql($col, $sign, $expression);
} }

View File

@ -29,7 +29,7 @@ class StatehistoryQuery extends IdoQuery
public function whereToSql($col, $sign, $expression) public function whereToSql($col, $sign, $expression)
{ {
if ($col === 'UNIX_TIMESTAMP(sh.state_time)') { if ($col === 'UNIX_TIMESTAMP(sh.state_time)') {
return 'sh.state_time ' . $sign . ' ' . $this->timestampForSql(strtotime($expression)); return 'sh.state_time ' . $sign . ' ' . $this->timestampForSql($this->valueToTimestamp($expression));
} else { } else {
return parent::whereToSql($col, $sign, $expression); return parent::whereToSql($col, $sign, $expression);
} }

View File

@ -8,6 +8,7 @@ use \DateTime;
use \Exception; use \Exception;
use \ArrayIterator; use \ArrayIterator;
use \IteratorAggregate; use \IteratorAggregate;
use Icinga\Data\Filter\Filter;
use Icinga\Web\Hook; use Icinga\Web\Hook;
use Icinga\Web\Session; use Icinga\Web\Session;
use Icinga\Web\Session\SessionNamespace; use Icinga\Web\Session\SessionNamespace;
@ -344,15 +345,14 @@ class TimeLine implements IteratorAggregate
} }
} }
$query = $this->dataview->getQuery(); $query = $this->dataview;
$queryColumns = $query->getColumns(); $filter = Filter::matchAll(
$query->where( Filter::where('type', array_keys($this->identifiers)),
$query->isValidFilterTarget('name') ? 'name' : $queryColumns['name'], Filter::expression('timestamp', '<=', $this->displayRange->getStart()->getTimestamp()),
array_keys($this->identifiers) Filter::expression('timestamp', '>', $this->displayRange->getEnd()->getTimestamp())
)->where('raw_timestamp <= ?', $this->displayRange->getStart()->getTimestamp()) );
->where('raw_timestamp > ?', $this->forecastRange->getEnd()->getTimestamp()); $query->applyFilter($filter);
return array_merge($query->getQuery()->fetchAll(), $hookResults);
return array_merge($query->fetchAll(), $hookResults);
} }
/** /**

View File

@ -144,36 +144,6 @@ class UrlTest extends BaseTestCase
); );
} }
/**
* @depends testWhetherFromPathProperlyRecognizesAndDecodesQueryParameters
*/
public function testWhetherTranslateAliasTranslatesKnownAliases()
{
$url = Url::fromPath('/my/test/url.html');
$url->setAliases(array('foo' => 'bar'));
$this->assertEquals(
'bar',
$url->translateAlias('foo'),
'Url::translateAlias does not translate a known alias'
);
}
/**
* @depends testWhetherFromPathProperlyRecognizesAndDecodesQueryParameters
*/
public function testWhetherTranslateAliasDoesNotTranslateUnknownAliases()
{
$url = Url::fromPath('/my/test/url.html');
$url->setAliases(array('foo' => 'bar'));
$this->assertEquals(
'fo',
$url->translateAlias('fo'),
'Url::translateAlias does translate an unknown alias'
);
}
/** /**
* @depends testWhetherFromPathProperlyRecognizesAndDecodesQueryParameters * @depends testWhetherFromPathProperlyRecognizesAndDecodesQueryParameters
*/ */