Optimizations. Supporting new debug mode + wildcard-style ignores

This commit is contained in:
Andy Miller
2014-09-07 09:02:39 -06:00
parent a48b5132c3
commit 5a68b1c634
3 changed files with 52 additions and 19 deletions

View File

@@ -158,6 +158,7 @@ class AdminPlugin extends Plugin
public function onShutdown() public function onShutdown()
{ {
echo '<span style="color:red">system.debugger.shutdown.close_connection = false</span>';
if ($this->config->get('plugins.admin.popularity.enabled')) { if ($this->config->get('plugins.admin.popularity.enabled')) {
require_once PLUGINS_DIR . 'admin/classes/popularity.php'; require_once PLUGINS_DIR . 'admin/classes/popularity.php';
$popularity = new Popularity(); $popularity = new Popularity();

View File

@@ -2,5 +2,5 @@ enabled: true
route: '/admin' route: '/admin'
popularity: popularity:
enabled: true enabled: true
ignore: ['/admin','/test'] ignore: ['/test/*','/modular']
theme: grav theme: grav

View File

@@ -21,6 +21,9 @@ class Popularity
protected $data_path; protected $data_path;
protected $data_file; protected $data_file;
const MONTHLY_FILE = 'monthly.json';
const TOTALS_FILE = 'totals.json';
public function __construct() public function __construct()
{ {
$this->data_path = LOG_DIR . 'popularity'; $this->data_path = LOG_DIR . 'popularity';
@@ -30,24 +33,62 @@ class Popularity
public function trackHit() public function trackHit()
{ {
$page = self::$grav['page']; $page = self::$grav['page'];
$config = self::$grav['config'];
$relative_url = str_replace($config->get('system.base_url_relative'), '', $page->url());
// make sure this is not an error or an ignored route // Don't track error pages or pages that have no route
if ($page->template() == 'error' || if ($page->template() == 'error' || !$page->route()) {
in_array($page->route(), (array) self::$grav['config']->get('plugins.admin.popularity.ignore'))) {
return; return;
} }
$url = $page->url(); // Make sure no 'widcard-style' ignore matches this url
$data_filepath = $this->data_path.'/'.$this->data_file; foreach ((array) self::$grav['config']->get('plugins.admin.popularity.ignore') as $ignore) {
if (fnmatch($ignore, $relative_url)) {
return;
}
}
// Used more than once, so make a variable!
$monthly_file = $this->data_path.'/'.self::MONTHLY_FILE;
$totals_file = $this->data_path.'/'.self::TOTALS_FILE;
// initial creation if it doesn't exist // initial creation if it doesn't exist
if (!file_exists($this->data_path)) { if (!file_exists($this->data_path)) {
mkdir($this->data_path); mkdir($this->data_path);
file_put_contents($data_filepath, array()); file_put_contents($monthly_file, array());
file_put_contents($totals_file, array());
} }
// Get the JSON data // Update the data we want to track
$data = (array) @json_decode(file_get_contents($data_filepath), true); $this->updateMonthly($monthly_file);
$this->updateTotals($totals_file, $relative_url);
}
public function flushData($weeks = 52)
{
// flush data older than 1 year
}
protected function updateMonthly($path)
{
$data = (array) @json_decode(file_get_contents($path), true);
$month_year = date('m-Y');
if (array_key_exists($month_year, $data)) {
$data[$month_year] = intval($data[$month_year]) + 1;
} else {
$data[$month_year] = 1;
}
file_put_contents($path, json_encode($data));
}
protected function updateTotals($path, $url)
{
$data = (array) @json_decode(file_get_contents($path), true);
if (array_key_exists($url, $data)) { if (array_key_exists($url, $data)) {
$data[$url] = intval($data[$url]) + 1; $data[$url] = intval($data[$url]) + 1;
@@ -55,15 +96,6 @@ class Popularity
$data[$url] = 1; $data[$url] = 1;
} }
// Store the JSON data again file_put_contents($path, json_encode($data));
file_put_contents($data_filepath, json_encode($data));
}
function flushData($weeks = 52)
{
// flush data older than 1 year
} }
} }