Skip to content

Commit

Permalink
showing litemage cache statistics on cache management screen
Browse files Browse the repository at this point in the history
  • Loading branch information
litespeedtech committed Jan 9, 2017
1 parent ee1f0de commit da6efee
Show file tree
Hide file tree
Showing 6 changed files with 276 additions and 4 deletions.
146 changes: 146 additions & 0 deletions Block/Backend/Cache/Management.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
<?php
/**
* LiteMage
*
* NOTICE OF LICENSE
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see https://opensource.org/licenses/GPL-3.0 .
*
* @package LiteSpeed_LiteMage
* @copyright Copyright (c) 2017 LiteSpeed Technologies, Inc. (https://www.litespeedtech.com)
* @license https://opensource.org/licenses/GPL-3.0
*/

namespace Litespeed\Litemage\Block\Backend\Cache;

class Management extends \Magento\Backend\Block\Template
{
/**
* @var \Litespeed\Litemage\Model\Config
*/
protected $config;

protected $url;


/**
* @var \Magento\Framework\HTTP\ZendClientFactory
*/
protected $httpClientFactory;
/**
* @param \Magento\Backend\Block\Template\Context $context
* @param \Litespeed\Litemage\Model\Config $config,
* @param array $data
*/
public function __construct(
\Magento\Backend\Block\Template\Context $context,
\Litespeed\Litemage\Model\Config $config,
\Magento\Framework\HTTP\ZendClientFactory $httpClientFactory,
\Magento\Framework\Url $url,
array $data = [])
{
parent::__construct($context, $data);
$this->config = $config;
$this->httpClientFactory = $httpClientFactory;
$this->url = $url;
}

/**
* Get clean cache url
*
* @return string
*/
public function getPurgeUrl($type)
{
if ($type == 'Refresh') {
return $this->getUrl('*/*/cache/index');
}
else {
$types = array('All', 'Tag', 'Url');

if (in_array($type, $types)) {
return $this->getUrl('*/litemageCache/purge' . $type);
}
else {
return $this->getUrl('*/litemageCache/purgeAll');
}
}
}

public function getCacheStatistics()
{
$statUri = '/__LSCACHE/STATS';
$base = $this->url->getBaseUrl();
if ((stripos($base, 'http') !== false) && ($pos = strpos($base, '://'))) {
$pos2 = strpos($base, '/', $pos+ 4);
if ($pos === false) {
$statBase = $base;
}
else {
$statBase = substr($base, 0, $pos2);
if (substr($base, $pos2+1, 1) == '~') {
if ($pos3 = strpos($base, '/', $pos2+1)) {
$statBase = substr($base, 0, $pos3);
}
}
}
}
$statUri = $statBase . $statUri;

$client = $this->httpClientFactory->create();
$client->setUri($statUri);

try {
$response = $client->request() ;
$data = trim($response->getBody());
if ($data{0} !== '{') {
return null;
}

$data1 = json_decode($data, true);
$data2 = array_values($data1);
if (count($data2)) {
$stats = $data2[0];
switch ($stats['LITEMAGE_PLAN']) {
case 11: $stats['plan'] = 'LiteMage Standard';
$stats['plan_desc'] = 'up to 25000 publicly cached objects';
break;
case 3: $stats['plan'] = 'LiteMage Unlimited';
$stats['plan_desc'] = 'unlimited publicly cached objects';
break;
case 9:
default:
$stats['plan'] = 'LiteMage Starter';
$stats['plan_desc'] = 'up to 1500 publicly cached objects';
}
return $stats;
}
} catch ( Exception $e ) {
}
return null;
}


/**
* Check if block can be displayed
*
* @return bool
*/
public function canShowButton()
{
return $this->config->moduleEnabled();
}


}
2 changes: 1 addition & 1 deletion Model/CacheControl.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class CacheControl
{

/**
* @var \Magento\Framework\Module\Dir\Reader
* @var \Litespeed\Litemage\Model\Config
*/
protected $config;

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"magento/framework": "100.0.*|100.1.*|100.2.*"
},
"type": "magento2-module",
"version": "2.0.2",
"version": "2.0.3",
"license": [
"OSL-3.0",
"AFL-3.0"
Expand Down
4 changes: 2 additions & 2 deletions etc/module.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@
* along with this program. If not, see https://opensource.org/licenses/GPL-3.0 .
*
* @package LiteSpeed_LiteMage
* @copyright Copyright (c) 2016 LiteSpeed Technologies, Inc. (https://www.litespeedtech.com)
* @copyright Copyright (c) 2016-2017 LiteSpeed Technologies, Inc. (https://www.litespeedtech.com)
* @license https://opensource.org/licenses/GPL-3.0
*/
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Litespeed_Litemage" setup_version="2.0.1">
<module name="Litespeed_Litemage" setup_version="2.0.3">
<sequence>
<module name="Magento_PageCache"/>
</sequence>
Expand Down
33 changes: 33 additions & 0 deletions view/adminhtml/layout/adminhtml_cache_index.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?xml version="1.0"?>
<!--
/**
* LiteMage
*
* NOTICE OF LICENSE
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see https://opensource.org/licenses/GPL-3.0 .
*
* @package LiteSpeed_LiteMage
* @copyright Copyright (c) 2017 LiteSpeed Technologies, Inc. (https://www.litespeedtech.com)
* @license https://opensource.org/licenses/GPL-3.0
*/
-->
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
<referenceContainer name="content">
<block class="Litespeed\Litemage\Block\Backend\Cache\Management" name="cache.litemage" template="Litespeed_Litemage::system/cache/litemage.phtml"/>
</referenceContainer>
</body>
</page>

93 changes: 93 additions & 0 deletions view/adminhtml/templates/system/cache/litemage.phtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?php
/**
* LiteMage
*
* NOTICE OF LICENSE
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see https://opensource.org/licenses/GPL-3.0 .
*
* @package LiteSpeed_LiteMage
* @copyright Copyright (c) 2017 LiteSpeed Technologies, Inc. (https://www.litespeedtech.com)
* @license https://opensource.org/licenses/GPL-3.0
*/
?>

<?php
if ($block->canShowButton()):
?>
<br>
<div class="additional-cache-management">
<h2>
<span><?php /* @escapeNotVerified */ echo __('LiteMage Cache Management (LiteSpeed Web Server)') ?></span>
</h2>
</div>
<?php


if ($stats = $block->getCacheStatistics()) :
?>

<div><table class="data-grid">
<tr>
<th class="data-grid-th" width="25%"><?php echo __('Current LiteMage Plan');?></th>
<th class="data-grid-th" width="25%"><?php echo __('Public Cache Hits'); ?></th>
<th class="data-grid-th" width="25%"><?php echo __('Full Page Hits'); ?></th>
<th class="data-grid-th" width="25%"><?php echo __('LiteMage Cached Objects'); ?></th>
</tr>
<?php
$severity = $stats['LITEMAGE_LIMITED'] > 0 ? 'grid-severity-critical' : 'grid-severity-notice';
$noteclass = 'notifications-entry-description';
echo '<tr><td><div class="' . $severity . '">' . $stats['plan']
. '</div></td><td>' . $stats['PUB_HITS']
. '<div class="' . $noteclass . '">' . __('Number of requests that hit public cache since last flush all')
. '</div></td><td>' . $stats['FULLPAGE_HITS']
. '<div class="' . $noteclass . '">' . __('Number of requests fully served from cache (no PHP invoked) since last flush all')
. '</div></td><td>' . $stats['LITEMAGE_OBJS']
. '<div class="' . $noteclass . '">' . __('Current publicly cached object count')
. "</div></td></tr>\n";
?>
<tr>
<th class="data-grid-th"><?php echo __('Available Plan Options');?>
</th>
<th class="data-grid-th"><?php echo __('Private Cache Hits'); ?></th>
<th class="data-grid-th"><?php echo __('Partial Page Hits'); ?></th>
<th class="data-grid-th"><?php echo __('Not Cached (Limited by Plan)'); ?></th>
</tr>
<?php
echo '<tr><td>' . __('LiteMage Starter')
. '<div class="' . $noteclass . '">' . __('1,500 publicly cached objects')
. '</div>' . __('LiteMage Standard')
. '<div class="' . $noteclass . '">' . __('25,000 publicly cached objects')
. '</div>' . __('LiteMage Unlimited')
. '<div class="' . $noteclass . '">' . __('unlimited publicly cached objects')
. '</div></td><td>'. $stats['PVT_HITS']
. '<div class="' . $noteclass . '">' . __('Number of requests that hit private cache since last flush all')
. '</div></td><td>'. $stats['PARTIAL_HITS']
. '<div class="' . $noteclass . '">' . __('Number of requests partially served from cache (some data retrieved from Magento) since last flush all')
. '</div></td><td><div class="' . $severity . '">' . $stats['LITEMAGE_LIMITED']
. '</div><div class="' . $noteclass . '">' . __('Number of requests not served from cache due to plan limitation. You can upgrade your LiteMage plan if this value is not zero.')
. "</div></td></tr>\n";
?>
</table>
</div>
<?php
endif;
?>

<?php

endif;

?>

0 comments on commit da6efee

Please sign in to comment.