<?php defined('SYSPATH') or die('No direct access allowed.');

/**
 *
 * @package    PTA
 * @subpackage File Spaces
 * @category   Models
 * @author     Deon George
 * @copyright  (c) 2010 phpTSMadmin Development Team
 * @license    http://phptsmadmin.sf.net/license.html
 */
class Model_FILESPACE extends ORM_TSM {
	protected $_table_name = 'FILESPACES';
	protected $_primary_key = 'FILESPACE_NAME'; // We need a primary key to detect that the object is loaded.
	protected $_sorting = array(
		'NODE_NAME'=>'ASC',
		'FILESPACE_NAME'=>'ASC',
	);

	protected $_has_one = array(
		'NODE'=>array('foreign_key'=>'NODE_NAME','far_key'=>'NODE_NAME'),
	);

	protected $_tsm = array(
		'db2'=>array(
			'_primary_key'=>'FSNAME',
			'_sorting'=>array(
				'NODEID'=>'ASC',
				'FSNAME'=>'ASC',
			),
			'_has_one'=>array(
				'NODE'=>array('foreign_key'=>'NODEID','far_key'=>'NODEID'),
			),
			'translate'=>array(
				'FILESPACE_NAME'=>'FSNAME',
				'BACKUP_END'=>'BACKSTART',
				'PCT_UTIL'=>NULL,
			),
		),
	);

	protected $_display_filters = array(
		'BACKUP_END'=>array(
			array('ORM_TSM::date',array(':value','d-M-Y')),
		),
	);

	/**
	 * Get all the OCCUPANCY for this FILESYSTEM on this NODE
	 */
	private function _occupancy() {
		Log::instance()->add(LOG::DEBUG,'ENTER :method',array(':method'=>__METHOD__));

		$k = sprintf('%s-%s-%s',__METHOD__,$this->NODE_NAME,$this->FILESPACE_NAME);
		$c = Kohana::$config->load('config')->cache;

		if (is_null($result = Cache::instance($c)->get($k))) {
			$result = array();

			foreach ($this->NODE->occupancy() as $oo)
				if ($oo->FILESPACE_NAME == $this->FILESPACE_NAME)
					array_push($result,$oo);

			// @todo Cache time should be configurble
			Cache::instance($c)->set($k,$result,300);
		}

		Log::instance()->add(LOG::DEBUG,'EXIT :method',array(':method'=>__METHOD__));
		return $result;
	}

	/**
	 * Get all the VOLUEMUSAGE for this FILESYSTEM on this NODE
	 */
	private function _volumeusage() {
		Log::instance()->add(LOG::DEBUG,'ENTER :method',array(':method'=>__METHOD__));

		$k = sprintf('%s-%s-%s',__METHOD__,$this->NODE_NAME,$this->FILESPACE_NAME);
		$c = Kohana::$config->load('config')->cache;

		if (is_null($result = Cache::instance($c)->get($k))) {
			$result = array();

			foreach ($this->NODE->volumeusage() as $vuo)
				if ($vuo->FILESPACE_NAME == $this->FILESPACE_NAME)
					array_push($result,$vuo);

			// @todo Cache time should be configurble
			Cache::instance($c)->set($k,$result,300);
		}

		Log::instance()->add(LOG::DEBUG,'EXIT :method',array(':method'=>__METHOD__));
		return $result;
	}

	/**
	 * Return the data that this FILESYSTEM has in a STORAGE POOL
	 * @param $pool is STORAGE POOL NAME
	 * @param $metric is metric of the storpage pool, eg: NUM_FILES
	 */
	private function data_bypool($pool,$metric) {
		Log::instance()->add(LOG::DEBUG,'ENTER :method',array(':method'=>__METHOD__));

		$k = sprintf('%s-%s-%s-%s-%s',__METHOD__,$this->NODE_NAME,$this->FILESPACE_NAME,$pool,$metric);
		$c = Kohana::$config->load('config')->cache;

		if (is_null($result = Cache::instance($c)->get($k))) {
			$result = 0;

			foreach ($this->_occupancy() as $oo)
				if ($oo->STGPOOL_NAME == $pool)
					$result += $oo->{$metric};

			// @todo Cache time should be configurble
			Cache::instance($c)->set($k,$result,300);
		}

		Log::instance()->add(LOG::DEBUG,'EXIT :method',array(':method'=>__METHOD__));
		return $result;
	}

	/**
	 * Return the data that this FILESYSTEM has in a STORAGE POOL
	 * @param $pool is STORAGE POOL NAME
	 * @param $metric is metric of the storpage pool, eg: NUM_FILES
	 * @param $type is Bkup/Arch/SpMg
	 */
	private function data_bypoolbybtype($pool,$metric,$type) {
		Log::instance()->add(LOG::DEBUG,'ENTER :method',array(':method'=>__METHOD__));

		$k = sprintf('%s-%s-%s-%s-%s-%s',__METHOD__,$this->NODE_NAME,$this->FILESPACE_NAME,$pool,$metric,$type);
		$c = Kohana::$config->load('config')->cache;

		if (is_null($result = Cache::instance($c)->get($k))) {
			$result = 0;

			foreach ($this->_occupancy() as $oo)
				if ($oo->STGPOOL_NAME == $pool AND $oo->TYPE == $type)
					$result += $oo->{$metric};

			// @todo Cache time should be configurble
			Cache::instance($c)->set($k,$result,300);
		}

		Log::instance()->add(LOG::DEBUG,'EXIT :method',array(':method'=>__METHOD__));
		return $result;
	}

	/**
	 * Return the LOGICAL_MB that this FILESYSTEM has in a STORAGE POOL
	 * @param $pool is STORAGE POOL NAME
	 */
	public function logmb_bypool($pool) {
		Log::instance()->add(LOG::DEBUG,'FLYBY :method',array(':method'=>__METHOD__));
		return $this->data_bypool($pool,'LOGICAL_MB');
	}

	/**
	 * Return the LOGICAL_MB that this FILESYSTEM has in a STORAGE POOL
	 * @param $pool is STORAGE POOL NAME
	 * @param $type is Bkup/Arch/SpMg
	 */
	public function logmb_bypoolbybtype($pool,$type) {
		Log::instance()->add(LOG::DEBUG,'FLYBY :method',array(':method'=>__METHOD__));
		return $this->data_bypoolbybtype($pool,'LOGICAL_MB',$type);
	}

	public function utilsation() {
		return $this->CAPACITY*($this->PCT_UTIL/100);
	}

	/**
	 * Return the VOLUMES that this FILESYSTEM uses for this NODE
	 * @param $pool is STORAGE POOL NAME
	 */
	public function vols_bypool($pool) {
		Log::instance()->add(LOG::DEBUG,'ENTER :method',array(':method'=>__METHOD__));

		$k = sprintf('%s-%s-%s-%s',__METHOD__,$this->NODE_NAME,$this->FILESPACE_NAME,$pool);
		$c = Kohana::$config->load('config')->cache;

		if (is_null($result = Cache::instance($c)->get($k))) {
			$x = $result = array();

			foreach ($this->_volumeusage() as $vuo)
				if ($vuo->STGPOOL_NAME == $pool AND ! in_array($vuo->VOLUME_NAME,$x)) {
					array_push($result,$vuo);
					array_push($x,$vuo->VOLUME_NAME);
				}

			// @todo Cache time should be configurble
			Cache::instance($c)->set($k,$result,300);
		}

		Log::instance()->add(LOG::DEBUG,'EXIT :method',array(':method'=>__METHOD__));
		return $result;
	}

	/**
	 * Return the VOLUMES that this FILESYSTEM uses for this NODE by POOL and BACKUP TYPE
	 * @param $pool is STORAGE POOL NAME
	 * @param $type is BACKUP/ARCHIVE/SPACE MANAGED
	 */
	public function vols_bypoolbyctype($pool,$type) {
		$x = $result = array();

		foreach ($this->vols_bypool($pool) as $vuo)
			if ($vuo->COPY_TYPE == $type AND ! in_array($vuo->VOLUME_NAME,$x)) {
				array_push($result,$vuo);
				array_push($x,$vuo->VOLUME_NAME);
			}

		return $result;
	}
}
?>
