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

/**
 *
 * @package    PTA
 * @subpackage Server Status
 * @category   Models
 * @author     Deon George
 * @copyright  (c) 2010 phpTSMadmin Development Team
 * @license    http://phptsmadmin.sf.net/license.html
 */
class Model_STATUS extends ORM_TSM {
	protected $_table_name = 'STATUS';
	protected $_primary_key = 'SERVER_NAME';
	protected $_sorting = array(
		'SERVER_NAME'=>'ASC',
	);

	/**
	 * Get all the ACTIVITIY LOG for this SERVER
	 */
	private function _actlog() {
		Log::instance()->add(LOG::DEBUG,'ENTER :method',array(':method'=>__METHOD__));

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

		if (is_null($result = Cache::instance($c)->get($k))) {
			// We cant load all records here, like we do with the others, there is too much data!
			// Due to the volume of data that could be retrieved, we'll only get the last tsmserveractlog days
			$result = ORM::factory('ACTLOG')
				->ExcludeSERVER()
				->where('ORIGINATOR','IN',array('SERVER'))
				->and_where('DATE_TIME','>=',date('Y-m-d H:00:00',time()-3600*Kohana::$config->load('config')->tsmserveractlog))
				->find_all();

			// @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;
	}

	public function activity() {
		return $this->_actlog();
	}
}
?>
