<commit_before><?php
// Copyright (C) 2010-2012 Combodo SARL
//
//   This program is free software; you can redistribute it and/or modify
//   it under the terms of the GNU General Public License as published by
//   the Free Software Foundation; version 3 of the License.
//
//   This program is distributed in the hope that it will be useful,
//   but WITHOUT ANY WARRANTY; without even the implied warranty of
//   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//   GNU General Public License for more details.
//
//   You should have received a copy of the GNU General Public License
//   along with this program; if not, write to the Free Software
//   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

require_once('backgroundprocess.inc.php');

/**
 * ormStopWatch
 * encapsulate the behavior of a stop watch that will be stored as an attribute of class AttributeStopWatch 
 *
 * @author      <NAME> <<EMAIL>>
 * @author      <NAME> <<EMAIL>>
 * @author      <NAME> <<EMAIL>>
 * @license     http://www.opensource.org/licenses/gpl-3.0.html LGPL
 */


/**
 * ormStopWatch
 * encapsulate the behavior of a stop watch that will be stored as an attribute of class AttributeStopWatch 
 *
 * @package     itopORM
 */
class ormStopWatch
{
	protected $iTimeSpent; // seconds
	protected $iStarted; // unix time (seconds)
	protected $iLastStart; // unix time (seconds)
	protected $iStopped; // unix time (seconds)
	protected $aThresholds;
	
	/**
	 * Constructor
	 */
	public function __construct($iTimeSpent = 0, $iStarted = null, $iLastStart = null, $iStopped = null)
	{
		$this->iTimeSpent = (int) $iTimeSpent;
		$this->iStarted = $iStarted;
		$this->iLastStart = $iLastStart;
		$this->iStopped = $iStopped;

		$this->aThresholds = array();
	}

	/**
	 * Necessary for the triggers
	 */	 	
	public function __toString()
	{
		return (string) $this->iTimeSpent;
	}

	public function DefineThreshold($iPercent, $tDeadline = null, $bPassed = false, $bTriggered = false, $iOverrun = null)
	{
		$this->aThresholds[$iPercent] = array(
			'deadline' => $tDeadline, // unix time (seconds)
			'passed' => $bPassed,
			'triggered' => $bTriggered,
			'overrun' => $iOverrun
		);
	}

	public function MarkThresholdAsTriggered($iPercent)
	{
		$this->aThresholds[$iPercent]['triggered'] = true;
	}

	public function GetTimeSpent()
	{
		return $this->iTimeSpent;
	}

	public function GetStartDate()
	{
		return $this->iStarted;
	}

	public function GetLastStartDate()
	{
		return $this->iLastStart;
	}

	public function GetStopDate()
	{
		return $this->iStopped;
	}

	public function GetThresholdDate($iPercent)
	{
		if (array_key_exists($iPercent, $this->aThresholds))
		{
			return $this->aThresholds[$iPercent]['deadline'];
		}
		else
		{
			return null;
		}
	}

	public function GetOverrun($iPercent)
	{
		if (array_key_exists($iPercent, $this->aThresholds))
		{
			return $this->aThresholds[$iPercent]['overrun'];
		}
		else
		{
			return null;
		}
	}
	public function IsThresholdPassed($iPercent)
	{
		if (array_key_exists($iPercent, $this->aThresholds))
		{
			return $this->aThresholds[$iPercent]['passed'];
		}
		else
		{
			return false;
		}
	}
	public function IsThresholdTriggered($iPercent)
	{
		if (array_key_exists($iPercent, $this->aThresholds))
		{
			return $this->aThresholds[$iPercent]['triggered'];
		}
		else
		{
			return false;
		}
	}

	public function GetAsHTML($oAttDef, $oHostObject = null)
	{
		$aProperties = array();

		$aProperties['States'] = implode(', ', $oAttDef->GetStates());

		if (is_null($this->iLastStart))
		{
			if (is_null($this->iStarted))
			{
				$aProperties['Elapsed'] = 'never started';
			}
			else
			{
				$aProperties['Elapsed'] = $this->iTimeSpent.' s';
			}
		}
		else
		{
			$iElapsedTemp = $this->ComputeDuration($oHostObject, $oAttDef, $this->iLastStart, time());
			$aProperties['Elapsed'] = $this->iTimeSpent.' + '.$iElapsedTemp.' s + <img src="../images/indicator.gif">';
		}

		$aProperties['Started'] = $oAttDef->SecondsToDate($this->iStarted);
		$aProperties['LastStart'] = $oAttDef->SecondsToDate($this->iLastStart);
		$aProperties['Stopped'] = $oAttDef->SecondsToDate($this->iStopped);

		foreach ($this->aThresholds as $iPercent => $aThresholdData)
		{
			$sThresholdDesc = $oAttDef->SecondsToDate($aThresholdData['deadline']);
			if ($aThresholdData['triggered'])
			{
				$sThresholdDesc .= " <b>TRIGGERED</b>";
			}
			if ($aThresholdData['overrun'])
			{
				$sThresholdDesc .= " Overrun:".(int) $aThresholdData['overrun']." sec.";
			}
			$aProperties[$iPercent.'%'] = $sThresholdDesc;
		}
		$sRes = "<TABLE class=\"listResults\">";
		$sRes .= "<TBODY>";
		foreach ($aProperties as $sProperty => $sValue)
		{
			$sRes .= "<TR>";
			$sCell = str_replace("\n", "<br>\n", $sValue);
			$sRes .= "<TD class=\"label\">$sProperty</TD><TD>$sCell</TD>";<commit_msg>Don't perform computations inside GetAsHTML because this may cause an infinite recursion since GetAsHTML is called by ToArgs<commit_after><?php
// Copyright (C) 2010-2012 Combodo SARL
//
//   This program is free software; you can redistribute it and/or modify
//   it under the terms of the GNU General Public License as published by
//   the Free Software Foundation; version 3 of the License.
//
//   This program is distributed in the hope that it will be useful,
//   but WITHOUT ANY WARRANTY; without even the implied warranty of
//   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//   GNU General Public License for more details.
//
//   You should have received a copy of the GNU General Public License
//   along with this program; if not, write to the Free Software
//   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

require_once('backgroundprocess.inc.php');

/**
 * ormStopWatch
 * encapsulate the behavior of a stop watch that will be stored as an attribute of class AttributeStopWatch 
 *
 * @author      <NAME> <<EMAIL>>
 * @author      <NAME> <<EMAIL>>
 * @author      <NAME> <<EMAIL>>
 * @license     http://www.opensource.org/licenses/gpl-3.0.html LGPL
 */


/**
 * ormStopWatch
 * encapsulate the behavior of a stop watch that will be stored as an attribute of class AttributeStopWatch 
 *
 * @package     itopORM
 */
class ormStopWatch
{
	protected $iTimeSpent; // seconds
	protected $iStarted; // unix time (seconds)
	protected $iLastStart; // unix time (seconds)
	protected $iStopped; // unix time (seconds)
	protected $aThresholds;
	
	/**
	 * Constructor
	 */
	public function __construct($iTimeSpent = 0, $iStarted = null, $iLastStart = null, $iStopped = null)
	{
		$this->iTimeSpent = (int) $iTimeSpent;
		$this->iStarted = $iStarted;
		$this->iLastStart = $iLastStart;
		$this->iStopped = $iStopped;

		$this->aThresholds = array();
	}

	/**
	 * Necessary for the triggers
	 */	 	
	public function __toString()
	{
		return (string) $this->iTimeSpent;
	}

	public function DefineThreshold($iPercent, $tDeadline = null, $bPassed = false, $bTriggered = false, $iOverrun = null)
	{
		$this->aThresholds[$iPercent] = array(
			'deadline' => $tDeadline, // unix time (seconds)
			'passed' => $bPassed,
			'triggered' => $bTriggered,
			'overrun' => $iOverrun
		);
	}

	public function MarkThresholdAsTriggered($iPercent)
	{
		$this->aThresholds[$iPercent]['triggered'] = true;
	}

	public function GetTimeSpent()
	{
		return $this->iTimeSpent;
	}

	public function GetStartDate()
	{
		return $this->iStarted;
	}

	public function GetLastStartDate()
	{
		return $this->iLastStart;
	}

	public function GetStopDate()
	{
		return $this->iStopped;
	}

	public function GetThresholdDate($iPercent)
	{
		if (array_key_exists($iPercent, $this->aThresholds))
		{
			return $this->aThresholds[$iPercent]['deadline'];
		}
		else
		{
			return null;
		}
	}

	public function GetOverrun($iPercent)
	{
		if (array_key_exists($iPercent, $this->aThresholds))
		{
			return $this->aThresholds[$iPercent]['overrun'];
		}
		else
		{
			return null;
		}
	}
	public function IsThresholdPassed($iPercent)
	{
		if (array_key_exists($iPercent, $this->aThresholds))
		{
			return $this->aThresholds[$iPercent]['passed'];
		}
		else
		{
			return false;
		}
	}
	public function IsThresholdTriggered($iPercent)
	{
		if (array_key_exists($iPercent, $this->aThresholds))
		{
			return $this->aThresholds[$iPercent]['triggered'];
		}
		else
		{
			return false;
		}
	}

	public function GetAsHTML($oAttDef, $oHostObject = null)
	{
		$aProperties = array();

		$aProperties['States'] = implode(', ', $oAttDef->GetStates());

		if (is_null($this->iLastStart))
		{
			if (is_null($this->iStarted))
			{
				$aProperties['Elapsed'] = 'never started';
			}
			else
			{
				$aProperties['Elapsed'] = $this->iTimeSpent.' s';
			}
		}
		else
		{
			$iElapsedTemp = ''; //$this->ComputeDuration($oHostObject, $oAttDef, $this->iLastStart, time());
			$aProperties['Elapsed'] = $this->iTimeSpent.' + '.$iElapsedTemp.' s + <img src="../images/indicator.gif">';
		}

		$aProperties['Started'] = $oAttDef->SecondsToDate($this->iStarted);
		$aProperties['LastStart'] = $oAttDef->SecondsToDate($this->iLastStart);
		$aProperties['Stopped'] = $oAttDef->SecondsToDate($this->iStopped);

		foreach ($this->aThresholds as $iPercent => $aThresholdData)
		{
			$sThresholdDesc = $oAttDef->SecondsToDate($aThresholdData['deadline']);
			if ($aThresholdData['triggered'])
			{
				$sThresholdDesc .= " <b>TRIGGERED</b>";
			}
			if ($aThresholdData['overrun'])
			{
				$sThresholdDesc .= " Overrun:".(int) $aThresholdData['overrun']." sec.";
			}
			$aProperties[$iPercent.'%'] = $sThresholdDesc;
		}
		$sRes = "<TABLE class=\"listResults\">";
		$sRes .= "<TBODY>";
		foreach ($aProperties as $sProperty => $sValue)
		{
			$sRes .= "<TR>";
			$sCell = str_replace("\n", "<br>\n", $sValue);
			$sRes .= "<TD class=\"label\">$sProperty</TD><TD>$sCell</TD>";
{'fraction_non_alphanumeric': 0.1547703899806537, 'fraction_numerical': 0.005294776499338153, 'mean_word_length': 3.8076358296622614, 'pattern_counts': {'":': 0, '<': 45, '<?xml version=': 0, '>': 125, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4715500', 'n_tokens_mistral': 3721, 'n_tokens_neox': 3343, 'n_words': 788}