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

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

	protected $_display_filters = array(
		'DATE_TIME'=>array(
			array('ORM_TSM::date',array(':value','d-M H:i')),
		),
		'MESSAGE'=>array(
			array('Model_ACTLOG::strsession',array(':value')),
		),
	);

	private $_msgno_ba_objects = array(
		4952=>array('msg'=>'ANE4952I Total number of objects inspected:\s+([0-9,]+).*','id'=>'OBJECT_TOTAL'),
		4953=>array('msg'=>'ANE4953I Total number of objects archived:\s+([0-9,]+).*','id'=>'OBJECT_TOTAL'),
		4954=>array('msg'=>'ANE4954I Total number of objects backed up:\s+([0-9,]+).*','id'=>'OBJECT_BACKUP'),
		4957=>array('msg'=>'ANE4957I Total number of objects deleted:\s+([0-9,]+).*','id'=>'OBJECT_DELETED'),
		4958=>array('msg'=>'ANE4958I Total number of objects updated:\s+([0-9,]+).*','id'=>'OBJECT_UDPATE'),
		4959=>array('msg'=>'ANE4959I Total number of objects failed:\s+([0-9,]+).*','id'=>'OBJECT_FAILED'),
		4960=>array('msg'=>'ANE4960I Total number of objects rebound:\s+([0-9,]+).*','id'=>'OBJECT_REBOUND'),
		4970=>array('msg'=>'ANE4970I Total number of objects expired:\s+([0-9,]+).*','id'=>'OBJECT_EXPIRED'),
		4982=>array('msg'=>'ANE4982I Total objects deduplicated:\s+([0-9,]+).*','id'=>'OJBECT_DEDUPED'),
	);
	private $_msgno_ba_bytes = array(
		4198=>array('msg'=>'ANE4198I Total bytes after deduplication:\s+([0-9,\.]+) ([KMG]B).*','id'=>'BYTES_DEDUPE_AFTER'),
		4961=>array('msg'=>'ANE4961I Total number of bytes transferred:\s+([0-9,\.]+) ([KMG]B).*','id'=>'BYTES_TRANSFER'),
		4975=>array('msg'=>'ANE4975I Total number of bytes processed:\s+([0-9,\.]+) ([KMG]B).*','id'=>'BYTES_PROCESS'),
		4977=>array('msg'=>'ANE4977I Total number of bytes inspected:\s+([0-9,\.]+) ([KMG]B).*','id'=>'BYTES_INSPECT'),
		4984=>array('msg'=>'ANE4984I Total bytes before deduplication:\s+([0-9,\.]+) ([KMG]B).*','id'=>'BYTES_DEDUPE_BEFORE'),
	);
	private $_msgno_ba_transfer = array(
		4963=>array('msg'=>'ANE4963I Data transfer time:\s+([0-9,\.]+).*','id'=>'TRANSFER_SEC'),
		4966=>array('msg'=>'ANE4966I Network data transfer rate:\s+([0-9,\.]+).*','id'=>'TRANSFER_RATE'),
		4967=>array('msg'=>'ANE4967I Aggregate data transfer rate:\s+([0-9,\.]+).*','id'=>'TRANSFER_AGGR'),
		4964=>array('msg'=>'ANE4964I Elapsed processing time:\s+([0-9:]+).*','id'=>'PROCESS_TIME'),
	);
	private $_msgno_ba_reduction = array(
		4968=>array('msg'=>'ANE4968I Objects compressed by:\s+([0-9,\.\-]+).*','id'=>'COMPRESS'),
		4976=>array('msg'=>'ANE4976I Total data reduction ratio:\s+([0-9,\.]+).*','id'=>'REDUCTION'),
		4981=>array('msg'=>'ANE4981I Deduplication reduction:\s+([0-9,\.]+).*','id'=>'DEDUPE'),
	);

	private $_msgno_admin = array(
		405=>array('msg'=>'ANR0405I Session [0-9]+ ended for administrator ([0-9a-zA-Z]+) \(.*\)\..*','id'=>''),
		407=>array('msg'=>'ANR0407I Session [0-9]+ started for administrator ([0-9a-zA-Z]+) \(.*\) \(.*\)\..*','id'=>''),
		1999=>array('msg'=>'ANR1999I QUERY REPLRULE completed successfully','id'=>''),
		2017=>array('msg'=>'ANR2017I Administrator ADMIN issued command: (.*)','id'=>''),
		2034=>array('msg'=>'ANR2034E SELECT: No match found using this criteria.','id'=>''),
		2906=>array('msg'=>'ANR2906E Unexpected SQL literal token - .*.','id'=>''),
		2939=>array('msg'=>'ANR2939E The reference .* contains an unknown SQL table name','id'=>''),
	);

	private $_msgno_expire = array(
		165=>array('msg'=>'ANR0165I Inventory file expiration started processing for node (.*), filespace (.*), copygroup (.*) and object type (.*).','id'=>''),
		166=>array('msg'=>'ANR0166I Inventory file expiration finished processing for node (.*), filespace (.*), copygroup (.*) and object type (.*) with processing statistics: examined ([0-9]+), deleted ([0-9]+), retrying ([0-9]+), and failed ([0-9]+).','id'=>''),
		167=>array('msg'=>'ANR0167I Inventory file expiration process [0-9]+ processed for .* minutes.','id'=>''),
		812=>array('msg'=>'ANR0812I Inventory file expiration process [0-9]+ completed: processed .* nodes, examined .* objects, deleting .* backup objects, .* archive objects, .* DB backup volumes, and .* recovery plan files. .* objects were retried and .* errors were encountered','id'=>''),
		987=>array('msg'=>'ANR0987I Process [0-9]+ for EXPIRE INVENTORY running in the BACKGROUND processed .* items with a completion state of .* at .*','id'=>''),
	);

	private $_msgno_session = array(
		403=>array('msg'=>'ANR0403I Session [0-9]+ ended for node .* \(.*\)','id'=>''),
		406=>array('msg'=>'ANR0406I Session [0-9]+ started for node .* \(.*\) \(.*\)','id'=>''),
		408=>array('msg'=>'ANR0408I Session [0-9]+ started for server .* \(.*\) \(.*\) for configuration management','id'=>''),
		409=>array('msg'=>'ANR0409I Session [0-9]+ ended for server .* \(.*\).','id'=>''),
		482=>array('msg'=>'ANR0482W Session [0-9]+ for node .* \(.*\) terminated - idle for more than .* minutes.','id'=>''),
		511=>array('msg'=>'ANR0511I Session [0-9]+ opened output volume .*','id'=>''),
		514=>array('msg'=>'ANR0514I Session [0-9]+ closed volume .*','id'=>''),
		2507=>array('msg'=>'ANR2507I Schedule .* for domain .* started at .* for node .* completed successfully at .*','id'=>''),
		3157=>array('msg'=>'ANR3157I Configuration refresh started for managed server .*','id'=>''),
		8592=>array('msg'=>'ANR8592I Session [0-9]+ connection is using SSL version TLSV11, cipher specification AES-128 certificate serial number ([0-9:]+)','id'=>''),
		8595=>array('msg'=>'ANR8595I Session to address .* is using SSL version TLSV11, cipher specification AES-128, certificate serial number ([0-9:]+).','id'=>''),
	);

	private function _bamsgs() {
		return $this->_msgno_ba_objects+$this->_msgno_ba_bytes+$this->_msgno_ba_transfer+$this->_msgno_ba_reduction;
	}

	private function _svrmsgs() {
		return $this->_msgno_admin+$this->_msgno_expire+$this->_msgno_session;
	}

	/**
	 * Return the timestamp of the record in the ACTLOG
	 */
	public function FirstRec() {
		return DB::query(Database::SELECT,'SELECT min(DATE_TIME) as DATE_TIME from ACTLOG')->cached(86400)->execute()->get('DATE_TIME');
	}

	/**
	 * Where class to exclude NODE backup messages
	 */
	public function ExcludeBA() {
		return $this->where('MSGNO','NOT IN',array_keys($this->_bamsgs()));
	}

	public function ExcludeSERVER() {
		return $this->where('MSGNO','NOT IN',array_keys($this->_svrmsgs()));
	}

	/**
	 * Extract data from our BA Client messages
	 */
	public function msgtrim() {
		$x = $this->_bamsgs();

		if (! array_key_exists($this->MSGNO,$x))
			return array();

		// @todo Shouldnt need this
		if (empty($x[$this->MSGNO]['id']))
			throw new Kohana_Exception('Message has no ID :id [:msg]',array(':id'=>$this->MSGNO,':msg'=>$this->MESSAGE));

		$y = preg_replace('/'.$x[$this->MSGNO]['msg'].'/',"$1$2",$this->MESSAGE);

		$m = array();
		// Do we have units we need to convert?
		if (preg_match('/^(.*)([KMG]B)$/',$y,$m)) {
			// Our number is in m[1]
			$y = $m[1];

			// Our unit is in m[2], convert to bytes
			switch ($m[2]) {
				case 'GB': $y *= 1024;
				case 'MB': $y *= 1024;
				case 'KB': $y *= 1024;
			}
		}

		// Safety check to make sure we extracted what we wanted.
		if ($y == $this->MESSAGE)
			throw new Kohana_Exception('Unable to extra data from message :id [:msg]',array(':id'=>$this->MSGNO,':msg'=>$this->MESSAGE));

		return array($x[$this->MSGNO]['id']=>str_replace(',','',$y));
	}

	/**
	 * Return this ACTIVITY LOG start time in seconds since epoch
	 */
	public function start() {
		return ORM_TSM::date($this->DATE_TIME,'U');
	}

	public static function strsession($value) {
		return preg_replace('/\s+\(SESSION: .*\)\s*$/','',$value);
	}
}
?>