Db/Housekeeping: prepare centralized housekeeping

This commit is contained in:
Thomas Gelf 2016-02-27 12:55:37 +01:00
parent a41f146679
commit 86f8830bcf
2 changed files with 74 additions and 5 deletions

View File

@ -0,0 +1,74 @@
<?php
namespace Icinga\Module\Director\Db;
use Icinga\Module\Director\Db;
class Housekeeping
{
/**
* @var Db
*/
protected $connection;
/**
* @var \Zend_Db_Adapter_Abstract
*/
protected $db;
/**
* @var int
*/
protected $version;
public function __construct(Db $connection)
{
$this->connection = $connection;
$this->db = $connection->getDbAdapter();
}
public function getTaskSummary()
{
return array(
'oldUndeployedConfigs' => array(
'title' => N_('Undeployed configurations'),
'count' => $this->countOldUndeployedConfigs()
),
'unusedFiles' => array(
'title' => N_('Unused rendered files'),
'count' => $this->countUnusedFiles()
)
);
}
public function getPendingTasks()
{
}
public function countOldUndeployedConfigs()
{
$sql = 'SELECT COUNT(*) FROM director_generated_config c'
. ' LEFT JOIN director_deployment_log d ON c.checksum = d.config_checksum'
. ' WHERE d.config_checksum IS NULL';
return $this->db->fetchOne($sql);
}
public function countUnusedFiles()
{
$sql = 'SELECT COUNT(*) FROM director_generated_file f'
. ' LEFT JOIN director_generated_config_file cf ON f.checksum = cf.file_checksum'
. ' WHERE cf.file_checksum IS NULL';
return $this->db->fetchOne($sql);
}
public function wipeUnusedFiles()
{
$sql = 'DELETE f FROM director_generated_file f'
. ' LEFT JOIN director_generated_config_file cf ON f.checksum = cf.file_checksum'
. ' WHERE cf.file_checksum IS NULL';
return $this->db->exec($sql);
}
}

View File

@ -568,9 +568,4 @@ class IcingaConfig
return $this->lastActivityChecksum;
}
// TODO: wipe unused files
// DELETE f FROM director_generated_file f
// LEFT JOIN director_generated_config_file cf ON f.checksum = cf.file_checksum
// WHERE cf.file_checksum IS NULL;
}