Skip to content

Commit

Permalink
version 0.13.0
Browse files Browse the repository at this point in the history
  • Loading branch information
fryckbos committed Aug 1, 2016
1 parent f1c7a7b commit c97bdb4
Show file tree
Hide file tree
Showing 12 changed files with 313 additions and 52 deletions.
22 changes: 18 additions & 4 deletions app/code/community/CoScale/Monitor/Helper/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,31 @@ class CoScale_Monitor_Helper_Data extends Mage_Core_Helper_Abstract
{
protected $debug = false;
protected $timer = array();
protected $logs = array();

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

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

try {
$this->debugStart('Module checking');
$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)) {
if (! $connection->isTableExists(Mage::getConfig()->getTablePrefix().$metricTable)) {
$this->debugEndError('Module checking', new Exception('Table ' . $metricTable . ' not found!'));
return false;
}
}
$this->debugEnd('Module checking');
} catch (Exception $ex) {
$this->debugEndError('Module checking', $ex);
return false;
}
return true;
Expand Down Expand Up @@ -61,12 +67,20 @@ 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() . ']');
$msg = 'Error: ' . $name . ' (' . number_format($duration, 6) . 's) [' . $ex->getMessage() . ']';
array_push($this->logs, $msg);
$this->writeDebug($msg);
}

public function enableDebug()
{
$this->debug = true;
}

public function getLogs()
{
$tmp_logs = implode(", ", $this->logs);
$this->logs = array();
return $tmp_logs;
}
}
28 changes: 16 additions & 12 deletions app/code/community/CoScale/Monitor/Model/Event/Observer.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public function generate(Varien_Event_Observer $observer)
'message' => $event->getDescription(),
'data' => array_merge(
array('originator' => $event->getSource()),
unserialize($event->getEventData())
$event->getEventData()
),
'start_time' => (int)(time() - $event->getTimestampStart()),
'stop_time' => (int)($event->getTimestampEnd() != 0 ? (time() - $event->getTimestampEnd()) : 0),
Expand All @@ -51,12 +51,14 @@ public function generate(Varien_Event_Observer $observer)
->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())),
$output->addEvent(
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');
Expand All @@ -67,11 +69,13 @@ public function generate(Varien_Event_Observer $observer)
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,
$output->addEvent(
array(
'type' => CoScale_Monitor_Model_Event::GROUP_ADMIN,
'message' => 'Maintenance mode enabled',
'start_time' => 0,
'stop_time' => 0,
)
);
}
$logger->debugEnd('Maintenance Flag');
Expand Down
31 changes: 28 additions & 3 deletions app/code/community/CoScale/Monitor/Model/Metric.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ class CoScale_Monitor_Model_Metric extends Mage_Core_Model_Abstract
*/
const TYPE_APPLICATION = 2;

const CALC_DIFFERENCE = 1;
const CALC_INSTANT = 2;
const CALC_AVERAGE = 3;

/**
* Construct the metric model
*/
Expand All @@ -57,16 +61,18 @@ protected function _construct()
* @param string $description A longer description of the metric
* @param mixed $value The value to set
* @param string $unit The unit the value is saved in
* @param int $calctype Calculation Type
*
* @throws Exception
*/
public function updateMetric($key, $store, $type, $name, $description, $value, $unit)
public function updateMetric($key, $store, $type, $name, $description, $value, $unit, $calctype = 0)
{
$this->loadByKey($key, $store);

$this->setKey($key)
->setStoreId($store)
->setType($type)
->setCalculationType($calctype)
->setName($name)
->setDescription($description)
->setValue($value)
Expand All @@ -89,12 +95,13 @@ public function updateMetric($key, $store, $type, $name, $description, $value, $
* @param string $description A longer description of the metric
* @param mixed $value The value to set
* @param string $unit The unit the value is saved in
* @param int $calctype Calculation Type
*/
public function incrementMetric($key, $store, $type, $name, $description, $value, $unit)
public function incrementMetric($key, $store, $type, $name, $description, $value, $unit, $calctype = 0)
{
$this->loadByKey($key, $store);

$this->updateMetric($key, $store, $type, $name, $description, ($this->getValue() + $value), $unit);
$this->updateMetric($key, $store, $type, $name, $description, ($this->getValue() + $value), $unit, $calctype);
}

/**
Expand Down Expand Up @@ -131,6 +138,24 @@ public function loadByKey($key, $store)
return $this;
}

public function getCalculationTypeCode()
{
switch ($this->getCalculationType()) {
case self::CALC_INSTANT:
$code = 'Instant';
break;
case self::CALC_AVERAGE:
$code = 'Average';
break;
case self::CALC_DIFFERENCE:
$code = 'Difference';
break;
default:
$code = false;
}
return $code;
}

/**
* Set some defaults before saving an event to the database
*
Expand Down
11 changes: 9 additions & 2 deletions app/code/community/CoScale/Monitor/Model/Metric/Abstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,18 @@ protected function setMetric($action, $key, $store, $value, $unit = false, $type
if (!$name || !$descr || !$unit) {
throw new Exception('Invalid metric data supplied');
}
$calctype = 0;
if (isset($this->_metricData[$key]['calctype'])) {
$calctype = $this->_metricData[$key]['calctype'];
}

if ($action == self::ACTION_UPDATE) {
$this->_metric->updateMetric($key, $store, $type, $name, $descr, $value, $unit);
$this->_metric->updateMetric($key, $store, $type, $name, $descr, $value, $unit, $calctype);
} else {
$this->_metric->incrementMetric($key, $store, $type, $name, $descr, $value, $unit);
$this->_metric->incrementMetric($key, $store, $type, $name, $descr, $value, $unit, $calctype);
if (isset($this->_metricData[$key]['combine']) && $this->_metricData[$key]['combine']) {
$this->_metric->incrementMetric($key, 0, $type, $name, $descr, $value, $unit, $calctype);
}
}
}

Expand Down
18 changes: 10 additions & 8 deletions app/code/community/CoScale/Monitor/Model/Metric/Observer.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,17 @@ public function generate(Varien_Event_Observer $observer)
$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()
)
$data = array(
'name' => $metric->getName(),
'unit' => $metric->getUnit(),
'value' => (float)$metric->getValue(),
'store_id' => (int)$metric->getStoreId(),
'type' => $metric->getTypeText()
);
if ($code = $metric->getCalculationTypeCode()) {
$data['calctype'] = $code;
}
$output->addMetric($data);

// Check if metric need to be reset after collection
if ($metricOrderDelete->resetOnCollect($metric->getKey())) {
Expand Down
Loading

0 comments on commit c97bdb4

Please sign in to comment.