<commit_before><?php

/**
 * Class Sheep_Debug_Model_Observer
 *
 * @category Sheep
 * @package  Sheep_Debug
 * @license  Copyright: <NAME>, 2016, All Rights reserved.
 * @link     https://piratesheep.com
 *
 *
 * TODO: clarify stages where request info's data is updated and when should be changed..
 * TODO: what do we generate when saving is not enabled !? Do we use persist data in cache for 30-60 minutes?
 */
class Sheep_Debug_Model_Observer
{
    // This is initialised as soon as app is setup
    protected $canCapture = true;

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

    /**
     * Returns request info model associated to current request.
     *
     * @return Sheep_Debug_Model_RequestInfo
     */
    public function getRequestInfo()
    {
        return Mage::getSingleton('sheep_debug/requestInfo');
    }


    public function startProfiling(Mage_Core_Controller_Request_Http $httpRequest)
    {
        // Register shutdown function
        register_shutdown_function(array($this, 'shutdown'));

        // Now we can properly initialise canCapture based on configuration
        $this->canCapture = Mage::helper('sheep_debug')->canCapture();

        $requestInfo = $this->getRequestInfo();
        $requestInfo->setStoreId(Mage::app()->getStore()->getId());
        $requestInfo->setIp(Mage::helper('core/http')->getRemoteAddr());
        $requestInfo->setRequestPath($httpRequest->getOriginalPathInfo());
        $requestInfo->setDate(date('Y-m-d H:i:s'));
        $requestInfo->initLogging();
    }


    /**
     * Executed after response is send. Now is safe to capture properties like response code, request time or peak memory usage.
     *
     * @param $response
     */
    public function completeProfiling(Mage_Core_Controller_Response_Http $response)
    {
        $requestInfo = $this->getRequestInfo();
        $helper = Mage::helper('sheep_debug');

        // update query information
        $requestInfo->prepareQueries();

        // capture log ranges
        $requestInfo->getLogging()->endRequest();

        // save rendering time
        $requestInfo->setRenderingTime(Sheep_Debug_Model_Block::getTotalRenderingTime());

        // first tentative to save response code
        $requestInfo->getController()->addResponseInfo($response);
        $requestInfo->setPeakMemory($helper->getMemoryUsage());
        $requestInfo->setTime($helper->getCurrentScriptDuration());
    }


    /**
     * This represents a shutdown callback that allows us to safely save our request info
     */
    public function shutdown()
    {
        $requestInfo = $this->getRequestInfo();
        $helper = Mage::helper('sheep_debug');

        // Last chance to update request profile info
        $requestInfo->setPeakMemory($helper->getMemoryUsage());
        $requestInfo->setTime($helper->getCurrentScriptDuration());
        $requestInfo->setResponseCode(http_response_code());

        $this->saveProfiling();
    }


    /**
     * Saves request info model.
     *
     * @throws Exception
     */
    public function saveProfiling()
    {
        if (!Mage::helper('sheep_debug')->canPersist()) {
            return;
        }

        $this->getRequestInfo()->save();
    }

    /**
     * Listens to controller_front_init_before event. An event that we can consider the start of the
     * request profiling.
     * @param Varien_Event_Observer $observer
     */
    public function onControllerFrontInitBefore(Varien_Event_Observer $observer)
    {
        /** @var Mage_Core_Controller_Varien_Front $front */<commit_msg>Don't try to persist request profile that were not captured<commit_after><?php

/**
 * Class Sheep_Debug_Model_Observer
 *
 * @category Sheep
 * @package  Sheep_Debug
 * @license  Copyright: Pirate Sheep, 2016, All Rights reserved.
 * @link     https://piratesheep.com
 *
 *
 * TODO: clarify stages where request info's data is updated and when should be changed..
 * TODO: what do we generate when saving is not enabled !? Do we use persist data in cache for 30-60 minutes?
 */
class Sheep_Debug_Model_Observer
{
    // This is initialised as soon as app is setup
    protected $canCapture = true;

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

    /**
     * Returns request info model associated to current request.
     *
     * @return Sheep_Debug_Model_RequestInfo
     */
    public function getRequestInfo()
    {
        return Mage::getSingleton('sheep_debug/requestInfo');
    }


    public function startProfiling(Mage_Core_Controller_Request_Http $httpRequest)
    {
        // Register shutdown function
        register_shutdown_function(array($this, 'shutdown'));

        // Now we can properly initialise canCapture based on configuration
        $this->canCapture = Mage::helper('sheep_debug')->canCapture();

        $requestInfo = $this->getRequestInfo();
        $requestInfo->setStoreId(Mage::app()->getStore()->getId());
        $requestInfo->setIp(Mage::helper('core/http')->getRemoteAddr());
        $requestInfo->setRequestPath($httpRequest->getOriginalPathInfo());
        $requestInfo->setDate(date('Y-m-d H:i:s'));
        $requestInfo->initLogging();
    }


    /**
     * Executed after response is send. Now is safe to capture properties like response code, request time or peak memory usage.
     *
     * @param $response
     */
    public function completeProfiling(Mage_Core_Controller_Response_Http $response)
    {
        $requestInfo = $this->getRequestInfo();
        $helper = Mage::helper('sheep_debug');

        // update query information
        $requestInfo->prepareQueries();

        // capture log ranges
        $requestInfo->getLogging()->endRequest();

        // save rendering time
        $requestInfo->setRenderingTime(Sheep_Debug_Model_Block::getTotalRenderingTime());

        // first tentative to save response code
        $requestInfo->getController()->addResponseInfo($response);
        $requestInfo->setPeakMemory($helper->getMemoryUsage());
        $requestInfo->setTime($helper->getCurrentScriptDuration());
    }


    /**
     * This represents a shutdown callback that allows us to safely save our request info
     */
    public function shutdown()
    {
        $requestInfo = $this->getRequestInfo();
        $helper = Mage::helper('sheep_debug');

        // Last chance to update request profile info
        $requestInfo->setPeakMemory($helper->getMemoryUsage());
        $requestInfo->setTime($helper->getCurrentScriptDuration());
        $requestInfo->setResponseCode(http_response_code());

        $this->saveProfiling();
    }


    /**
     * Saves request info model.
     *
     * @throws Exception
     */
    public function saveProfiling()
    {
        if (!$this->canCapture() || !Mage::helper('sheep_debug')->canPersist()) {
            return;
        }

        $this->getRequestInfo()->save();
    }

    /**
     * Listens to controller_front_init_before event. An event that we can consider the start of the
     * request profiling.
     * @param Varien_Event_Observer $observer
     */
    public function onControllerFrontInitBefore(Varien_Event_Observer $observer)
    {
        /** @var Mage_Core_Controller_Varien_Front $front */
{'fraction_non_alphanumeric': 0.1155011013215859, 'fraction_numerical': 0.0022026431718061676, 'mean_word_length': 2.956971677559913, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 73, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, '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': '4984890', 'n_tokens_mistral': 2008, 'n_tokens_neox': 1924, 'n_words': 479}