Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
fryckbos committed Feb 11, 2016
2 parents 82a979b + 252e23e commit f1c7a7b
Show file tree
Hide file tree
Showing 17 changed files with 765 additions and 219 deletions.
58 changes: 58 additions & 0 deletions app/code/community/CoScale/Monitor/Helper/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,63 @@
*/
class CoScale_Monitor_Helper_Data extends Mage_Core_Helper_Abstract
{
protected $debug = false;
protected $timer = array();

public function isEnabled()
{
if (!Mage::getStoreConfig('system/coscale_monitor/enabled')) {
return false;
}

$resource = Mage::getSingleton('core/resource')->getConnection('core_write');

try {
$tables = Mage::getConfig()->getNode('global/models/coscale_monitor_resource/entities')->asArray();

foreach ($tables as $id => $data) {
$metricTable = $resource->getTableName($data['table']);
if (! $resource->isTableExists($metricTable)) {
return false;
}
}
} catch (Exception $ex) {
return false;
}
return true;
}

public function writeDebug($msg)
{
if ($this->debug) {
Mage::log($msg, Zend_Log::DEBUG, 'coscale-collect.log', true);
}
}

public function debugStart($name)
{
$this->writeDebug('Start: ' . $name);
$this->timer[$name] = microtime(true);
}

public function debugEnd($name)
{
$startTime = isset($this->timer[$name]) ? $this->timer[$name]:microtime(true);
$duration = microtime(true)-$startTime;

$this->writeDebug('Completed: ' . $name . ' (' . number_format($duration, 6) . 's)');
}

public function debugEndError($name, $ex)
{
$startTime = isset($this->timer[$name]) ? $this->timer[$name]:microtime(true);
$duration = microtime(true)-$startTime;

$this->writeDebug('Error: ' . $name . ' (' . number_format($duration, 6) . 's) [' . $ex->getMessage() . ']');
}

public function enableDebug()
{
$this->debug = true;
}
}
83 changes: 83 additions & 0 deletions app/code/community/CoScale/Monitor/Model/Event/Observer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php

class CoScale_Monitor_Model_Event_Observer
{
public function generate(Varien_Event_Observer $observer)
{
$event = $observer->getEvent();

/** @var CoScale_Monitor_Helper_Data $logger */
$logger = $event->getLogger();
/** @var CoScale_Monitor_Model_Output_Generator $output */
$output = $event->getOutput();

// Get Events Collection
try {
$logger->debugStart('Events Collection');

$collection = Mage::getModel('coscale_monitor/event')->getCollection();
/** @var CoScale_Monitor_Model_Event $event */
foreach ($collection as $event) {
$output->addEvent(
array(
'type' => $event->getTypeGroup(),
'message' => $event->getDescription(),
'data' => array_merge(
array('originator' => $event->getSource()),
unserialize($event->getEventData())
),
'start_time' => (int)(time() - $event->getTimestampStart()),
'stop_time' => (int)($event->getTimestampEnd() != 0 ? (time() - $event->getTimestampEnd()) : 0),
)
);

$event->delete();
if ($event->getState() != $event::STATE_ENABLED) {
//$event->delete();
}
}
$logger->debugEnd('Events Collection');
} catch (Exception $ex) {
$logger->debugEndError('Events Collection', $ex);
}

// Get Cronjob Collection
try {
$logger->debugStart('Cronjob Collection');

$endDateTime = date('U');
$collection = Mage::getModel('cron/schedule')->getCollection()
->addFieldToFilter('finished_at', array('from' => date('Y-m-d H:i:s', $endDateTime-65)))
->setOrder('finished_at', 'DESC');
/** @var Mage_Cron_Model_Schedule $event */
foreach ($collection as $cron) {
$output['events'][] = array(
'type' => CoScale_Monitor_Model_Event::GROUP_CRON,
'message' => $cron->getJobCode(),
'status' => $cron->getStatus(),
'start_time' => (int)(time() - strtotime($cron->getExecutedAt())),
'stop_time' => (int)(time() - strtotime($cron->getFinishedAt())),
);
}
$logger->debugEnd('Cronjob Collection');
} catch (Exception $ex) {
$logger->debugEndError('Events Collection', $ex);
}

try {
$logger->debugStart('Maintenance Flag');
if (file_exists(Mage::getBaseDir('base') . DS . 'maintenance.flag')) {
$output['events'][] = array(
'type' => CoScale_Monitor_Model_Event::GROUP_ADMIN,
'message' => 'Maintenance mode enabled',
'start_time' => 0,
'stop_time' => 0,
);
}
$logger->debugEnd('Maintenance Flag');
} catch (Exception $ex) {
$logger->debugEndError('Maintenance Flag', $ex);
}

}
}
26 changes: 24 additions & 2 deletions app/code/community/CoScale/Monitor/Model/Metric/Abstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class CoScale_Monitor_Model_Metric_Abstract
{
protected $_metric = false;
protected $_metricData = array();
protected $_helper = false;

protected $_metricType = CoScale_Monitor_Model_Metric::TYPE_APPLICATION;

Expand All @@ -20,6 +21,7 @@ class CoScale_Monitor_Model_Metric_Abstract
public function __construct()
{
$this->_metric = Mage::getModel('coscale_monitor/metric');
$this->_helper = Mage::helper('coscale_monitor');
$this->_contruct();
}

Expand Down Expand Up @@ -74,14 +76,34 @@ protected function setMetric($action, $key, $store, $value, $unit = false, $type
}
}

/**
* @param $key
* @param $store
* @return int
*/
protected function getMetric($key, $store)
{
if (!$data = $this->getMetricData($key, $store)) {
return 0;
}
return $data->getValue();
}

/**
* @param $key
* @param $store
* @return $data|bool
*/
protected function getMetricData($key, $store)
{
/** @var CoScale_Monitor_Model_Metric $data */
$data = $this->_metric->loadByKey($key, $store);

if (!$data->getId()) {
return 0;
return false;
}
return $data->getValue();
return $data;
}


}
7 changes: 7 additions & 0 deletions app/code/community/CoScale/Monitor/Model/Metric/Customer.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ public function _contruct()
*/
public function addNew(Varien_Event_Observer $observer)
{
if (!$this->_helper->isEnabled()) {
return;
}

/** @var Mage_Customer_Model_Customer $customer */
$customer = $observer->getEvent()->getCustomer();

Expand All @@ -58,6 +62,9 @@ public function addNew(Varien_Event_Observer $observer)
*/
public function dailyCron()
{
if (!$this->_helper->isEnabled()) {
return;
}
$this->updateTotalCount();
}

Expand Down
32 changes: 32 additions & 0 deletions app/code/community/CoScale/Monitor/Model/Metric/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,38 @@
*/
class CoScale_Monitor_Model_Metric_File extends CoScale_Monitor_Model_Metric_Abstract
{
public function generate(Varien_Event_Observer $observer)
{
$event = $observer->getEvent();

/** @var CoScale_Monitor_Helper_Data $logger */
$logger = $event->getLogger();
/** @var CoScale_Monitor_Model_Output_Generator $output */
$output = $event->getOutput();

// Get Error reports
try {
$logger->debugStart('Error Reports');

$output->addMetric($this->getErrorReports());

$logger->debugEnd('Error Reports');
} catch (Exception $ex) {
$logger->debugEndError('Error Reports', $ex);
}

// Get Logfiles (name and size)
try {
$logger->debugStart('Logfiles');

foreach ($this->getLogFiles() as $data) {
$output->addMetric($data);
}
$logger->debugEnd('Logfiles');
} catch (Exception $ex) {
$logger->debugEndError('Logfiles', $ex);
}
}

/**
* Get amount of reports in var/report
Expand Down
42 changes: 42 additions & 0 deletions app/code/community/CoScale/Monitor/Model/Metric/Observer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

class CoScale_Monitor_Model_Metric_Observer
{
public function generate(Varien_Event_Observer $observer)
{
$event = $observer->getEvent();

/** @var CoScale_Monitor_Helper_Data $logger */
$logger = $event->getLogger();
/** @var CoScale_Monitor_Model_Output_Generator $output */
$output = $event->getOutput();

// Get Metrics Collection
try {
$logger->debugStart('Metrics Collection');

$metricOrderDelete = Mage::getSingleton('coscale_monitor/metric_order');
$collection = Mage::getModel('coscale_monitor/metric')->getCollection();
/** @var CoScale_Monitor_Model_Metric $metric */
foreach ($collection as $metric) {
$output->addMetric(
array(
'name' => $metric->getName(),
'unit' => $metric->getUnit(),
'value' => (float)$metric->getValue(),
'store_id' => (int)$metric->getStoreId(),
'type' => $metric->getTypeText()
)
);

// Check if metric need to be reset after collection
if ($metricOrderDelete->resetOnCollect($metric->getKey())) {
$metric->delete();
}
}
$logger->debugEnd('Metrics Collection');
} catch (Exception $ex) {
$logger->debugEndError('Metrics Collection', $ex);
}
}
}
Loading

0 comments on commit f1c7a7b

Please sign in to comment.