Skip to content

Commit

Permalink
Merge pull request #15 from mpchadwick/release/v0.3.0
Browse files Browse the repository at this point in the history
Release v0.3.0 -> Master
  • Loading branch information
mpchadwick authored Jun 28, 2016
2 parents c25a657 + 7487bf4 commit 29fecb6
Show file tree
Hide file tree
Showing 9 changed files with 109 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ public function handleControllerFrontSendResponseBefore(Varien_Event_Observer $o
);

$factory = Mage::getModel('mpchadwick_pagecachehitrate/trackerFactory');
foreach ($trackers->asArray() as $data) {
foreach ($trackers->asArray() as $alias => $data) {
$tracker = $factory->build($data['class']);
$tracker->track('RequestResponse', $params);
$tracker->track('RequestResponse', $params, $alias);

// Track any container misses for a partial cache response
$trackContainerMisses = (string)Mage::getConfig()->getNode(self::XML_PATH_TRACK_CONTAINER_MISSES);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ public function extractContent($content)
);

$factory = new Mpchadwick_PageCacheHitRate_Model_TrackerFactory;
foreach ($trackers->asArray() as $data) {
foreach ($trackers->asArray() as $alias => $data) {
$tracker = $factory->build($data['class']);
$tracker->track('RequestResponse', $params);
$tracker->track('RequestResponse', $params, $alias);
}

return $content;
Expand Down
14 changes: 14 additions & 0 deletions app/code/community/Mpchadwick/PageCacheHitRate/Model/SystemLog.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

class Mpchadwick_PageCacheHitRate_Model_SystemLog
{
public function log($message, $level)
{
Mage::log($message, $level, $this->logFile(), true);
}

protected function logFile()
{
return 'mpchadwick_pagecachehitrate_system_' . date('Y_m_d') . '.log';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,21 @@

abstract class Mpchadwick_PageCacheHitRate_Model_Tracker_Abstract
{
abstract protected function _track($type, array $args, $alias);

public function track($type, array $args, $alias)
{
$config = new Mpchadwick_PageCacheHitRate_Model_Config;
$strip = $config->get('trackers/' . $alias . '/strip');
if ($strip) {
foreach ($strip->asArray() as $key => $val) {
unset($args[$key]);
}
}

$this->_track($type, $args, $alias);
}

public function trackContainerMisses($params)
{
$containers = Mage::registry('cached_page_containers');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class Mpchadwick_PageCacheHitRate_Model_Tracker_File
{
protected $file = 'mpchadwick_pagecachehitrate_';

public function track($type, array $args)
protected function _track($type, array $args, $alias)
{
Mage::log(json_encode($args), null, $this->file($type), true);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class Mpchadwick_PageCacheHitRate_Model_Tracker_NewRelic
extends Mpchadwick_PageCacheHitRate_Model_Tracker_Abstract
implements Mpchadwick_PageCacheHitRate_Model_TrackerInterface
{
public function track($type, array $args)
protected function _track($type, array $args, $alias)
{
if (!function_exists('newrelic_record_custom_event')) {
Mage::logException(new Exception('You are using the New Relic tracker, but don\'t have the agent set up properly'));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php

class Mpchadwick_PageCacheHitRate_Model_Tracker_Redis
extends Mpchadwick_PageCacheHitRate_Model_Tracker_Abstract
implements Mpchadwick_PageCacheHitRate_Model_TrackerInterface
{
const KEY_PREFIX = 'mpchadwick_pagecachehitrate_';

/** @var Credis_Client */
protected $redis;

/** @var Mpchadwick_PageCacheHitRate_Model_Config */
protected $config;

public function __construct()
{
$this->config = new Mpchadwick_PageCacheHitRate_Model_Config;
$this->logger = new Mpchadwick_PageCacheHitRate_Model_SystemLog;
}

protected function _track($type, array $args, $alias)
{
$result = $this->setupConnection($alias);
if (!$result) {
// Bail. setUpConnection handled the error already
return;
}

$this->redis->hIncrBy(
self::KEY_PREFIX . $type,
$this->field($args),
1
);
}

protected function setupConnection($alias)
{
if (!is_null($this->redis)) {
return;
}

$prefix = 'trackers/' . $alias . '/';
$server = $this->config->get($prefix . 'server');
$port = $this->config->get($prefix . 'port');
$db = $this->config->get($prefix) . 'database';

if (!$server || !$port || !$db) {
$this->logger->log('Missing parameters for creating Redis connection', Zend_Log::ERR);
return false;
}

try {
$this->redis = new Credis_Client(
$this->config->get($prefix . 'server'),
$this->config->get($prefix . 'port')
);
$this->redis->connect();
$this->redis->select($this->config->get($prefix . 'database'));
} catch (Exception $e) {
$this->logger->log('Could not establish Redis connection', Zend_Log::ERR);
return false;
}

return true;
}

protected function field($args)
{
ksort($args);
return http_build_query($args);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@

interface Mpchadwick_PageCacheHitRate_Model_TrackerInterface
{
public function track($type, array $args);
public function track($type, array $args, $alias);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<config>
<modules>
<Mpchadwick_PageCacheHitRate>
<version>0.2.0</version>
<version>0.3.0</version>
</Mpchadwick_PageCacheHitRate>
</modules>
<global>
Expand Down

0 comments on commit 29fecb6

Please sign in to comment.