DowntimeendhistoryQuery: Use subqueries to fetch host and service downtimes
refs #9009
This commit is contained in:
parent
dadb3853d7
commit
c35be4023b
|
@ -3,49 +3,151 @@
|
||||||
|
|
||||||
namespace Icinga\Module\Monitoring\Backend\Ido\Query;
|
namespace Icinga\Module\Monitoring\Backend\Ido\Query;
|
||||||
|
|
||||||
|
use Zend_Db_Expr;
|
||||||
|
use Zend_Db_Select;
|
||||||
|
use Icinga\Data\Filter\Filter;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Query for host and service downtime end history records
|
||||||
|
*/
|
||||||
class DowntimeendhistoryQuery extends IdoQuery
|
class DowntimeendhistoryQuery extends IdoQuery
|
||||||
{
|
{
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
protected $columnMap = array(
|
protected $columnMap = array(
|
||||||
'downtimehistory' => array(
|
'downtimehistory' => array(
|
||||||
'state_time' => 'h.actual_end_time',
|
'object_type' => 'deh.object_type'
|
||||||
'timestamp' => 'UNIX_TIMESTAMP(h.actual_end_time)',
|
),
|
||||||
'raw_timestamp' => 'h.actual_end_time',
|
'history' => array(
|
||||||
'object_id' => 'h.object_id',
|
'type' => 'deh.type',
|
||||||
'type' => "('dt_end')",
|
'timestamp' => 'deh.timestamp',
|
||||||
'state' => '(NULL)',
|
'object_id' => 'deh.object_id',
|
||||||
'state_type' => '(NULL)',
|
'state' => 'deh.state',
|
||||||
'output' => "('[' || h.author_name || '] ' || h.comment_data)",
|
'output' => 'deh.output'
|
||||||
'attempt' => '(NULL)',
|
),
|
||||||
'max_attempts' => '(NULL)',
|
'hosts' => array(
|
||||||
|
'host_display_name' => 'deh.host_display_name',
|
||||||
'host' => 'o.name1 COLLATE latin1_general_ci',
|
'host_name' => 'deh.host_name'
|
||||||
'service' => 'o.name2 COLLATE latin1_general_ci',
|
),
|
||||||
'host_name' => 'o.name1',
|
'services' => array(
|
||||||
'service_description' => 'o.name2',
|
'service_description' => 'deh.service_description',
|
||||||
'object_type' => "CASE WHEN o.objecttype_id = 1 THEN 'host' ELSE 'service' END"
|
'service_display_name' => 'deh.service_display_name',
|
||||||
|
'service_host_name' => 'deh.service_host_name'
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
|
|
||||||
public function whereToSql($col, $sign, $expression)
|
/**
|
||||||
{
|
* The union
|
||||||
if ($col === 'UNIX_TIMESTAMP(h.actual_end_time)') {
|
*
|
||||||
return 'h.actual_end_time ' . $sign . ' ' . $this->timestampForSql($this->valueToTimestamp($expression));
|
* @var Zend_Db_Select
|
||||||
} else {
|
*/
|
||||||
return parent::whereToSql($col, $sign, $expression);
|
protected $downtimeEndHistoryQuery;
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Subqueries used for the downtime end history query
|
||||||
|
*
|
||||||
|
* @var IdoQuery[]
|
||||||
|
*/
|
||||||
|
protected $subQueries = array();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Whether to additionally select all history columns
|
||||||
|
*
|
||||||
|
* @var bool
|
||||||
|
*/
|
||||||
|
protected $fetchHistoryColumns = false;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
protected function joinBaseTables()
|
protected function joinBaseTables()
|
||||||
{
|
{
|
||||||
|
$this->downtimeEndHistoryQuery = $this->db->select();
|
||||||
$this->select->from(
|
$this->select->from(
|
||||||
array('o' => $this->prefix . 'objects'),
|
array('deh' => $this->downtimeEndHistoryQuery),
|
||||||
array()
|
array()
|
||||||
)->join(
|
);
|
||||||
array('h' => $this->prefix . 'downtimehistory'),
|
$this->joinedVirtualTables['downtimehistory'] = true;
|
||||||
'o.' . $this->object_id . ' = h.' . $this->object_id . ' AND o.is_active = 1',
|
}
|
||||||
array()
|
|
||||||
)->where('h.actual_end_time > ?', '1970-01-02 00:00:00');
|
/**
|
||||||
$this->joinedVirtualTables = array('downtimehistory' => true);
|
* Join history related columns and tables
|
||||||
|
*/
|
||||||
|
protected function joinHistory()
|
||||||
|
{
|
||||||
|
// TODO: Ensure that one is selecting the history columns first...
|
||||||
|
$this->fetchHistoryColumns = true;
|
||||||
|
$this->requireVirtualTable('hosts');
|
||||||
|
$this->requireVirtualTable('services');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Join hosts
|
||||||
|
*/
|
||||||
|
protected function joinHosts()
|
||||||
|
{
|
||||||
|
$columns = array_keys(
|
||||||
|
$this->columnMap['downtimehistory'] + $this->columnMap['hosts']
|
||||||
|
);
|
||||||
|
foreach ($this->columnMap['services'] as $column => $_) {
|
||||||
|
$columns[$column] = new Zend_Db_Expr('NULL');
|
||||||
|
}
|
||||||
|
if ($this->fetchHistoryColumns) {
|
||||||
|
$columns = array_merge($columns, array_keys($this->columnMap['history']));
|
||||||
|
}
|
||||||
|
$hosts = $this->createSubQuery('Hostdowntimeendhistory', $columns);
|
||||||
|
$this->subQueries[] = $hosts;
|
||||||
|
$this->downtimeEndHistoryQuery->union(array($hosts), Zend_Db_Select::SQL_UNION_ALL);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Join services
|
||||||
|
*/
|
||||||
|
protected function joinServices()
|
||||||
|
{
|
||||||
|
$columns = array_keys(
|
||||||
|
$this->columnMap['downtimehistory'] + $this->columnMap['hosts'] + $this->columnMap['services']
|
||||||
|
);
|
||||||
|
if ($this->fetchHistoryColumns) {
|
||||||
|
$columns = array_merge($columns, array_keys($this->columnMap['history']));
|
||||||
|
}
|
||||||
|
$services = $this->createSubQuery('Servicedowntimeendhistory', $columns);
|
||||||
|
$this->subQueries[] = $services;
|
||||||
|
$this->downtimeEndHistoryQuery->union(array($services), Zend_Db_Select::SQL_UNION_ALL);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
public function order($columnOrAlias, $dir = null)
|
||||||
|
{
|
||||||
|
foreach ($this->subQueries as $sub) {
|
||||||
|
$sub->requireColumn($columnOrAlias);
|
||||||
|
}
|
||||||
|
return parent::order($columnOrAlias, $dir);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
public function where($condition, $value = null)
|
||||||
|
{
|
||||||
|
$this->requireColumn($condition);
|
||||||
|
foreach ($this->subQueries as $sub) {
|
||||||
|
$sub->where($condition, $value);
|
||||||
|
}
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
public function addFilter(Filter $filter)
|
||||||
|
{
|
||||||
|
foreach ($this->subQueries as $sub) {
|
||||||
|
$sub->applyFilter(clone $filter);
|
||||||
|
}
|
||||||
|
return $this;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue