_getLastSave()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 9.7666
c 0
b 0
f 0
cc 3
nc 3
nop 0
1
<?php
2
/**
3
 * Redis Management Module
4
 *
5
 * PHP Version 5
6
 *
7
 * @category  Steverobbins
8
 * @package   Steverobbins_Redismanager
9
 * @author    Steve Robbins <[email protected]>
10
 * @copyright 2014 Steve Robbins
11
 * @license   http://creativecommons.org/licenses/by/3.0/deed.en_US Creative Commons Attribution 3.0 Unported License
12
 * @link      https://github.com/steverobbins/Magento-Redismanager
13
 */
14
15
/**
16
 * Main block for display configured redis services in admin
17
 *
18
 * @category  Steverobbins
19
 * @package   Steverobbins_Redismanager
20
 * @author    Steve Robbins <[email protected]>
21
 * @copyright 2014 Steve Robbins
22
 * @license   http://creativecommons.org/licenses/by/3.0/deed.en_US Creative Commons Attribution 3.0 Unported License
23
 * @link      https://github.com/steverobbins/Magento-Redismanager
24
 */
25
class Steverobbins_Redismanager_Block_Adminhtml_Manager
26
    extends Mage_Adminhtml_Block_Template
27
{
28
    const DEFAULT_MISSING_STRING = 'N/A';
29
30
    /**
31
     * Cached array of info from a redis instance
32
     *
33
     * @var array
34
     */
35
    protected $_info;
36
37
    /**
38
     * Build multidimensional array of servers by host:port
39
     *
40
     * @return array
41
     */
42
    public function getSortedServices()
43
    {
44
        $helper = $this->helper('redismanager');
45
        $sorted = array();
46
        foreach ($helper->getServices() as $id => $service) {
47
            $hostPort = $service['host'] . ':' . $service['port'];
48
            if (!isset($sorted[$hostPort])) {
49
                $client = $helper->getRedisInstance(
50
                    $service['host'],
51
                    $service['port'],
52
                    $service['password'],
53
                    $service['db']
54
                );
55
                $sorted[$hostPort] = $this->_getSortedService($service, $id, $client);
56
                continue;
57
            }
58
            $client = $helper->getRedisInstance(
59
                $service['host'],
60
                $service['port'],
61
                $service['password'],
62
                $service['db']
63
            );
64
            $sorted[$hostPort]['services'][$id] = array(
65
                'name' => $service['name'],
66
                'db' => $service['db'],
67
                'keys' => count($client->getRedis()->keys('*'))
68
            );
69
        }
70
        return $sorted;
71
    }
72
73
    /**
74
     * Get a formatted array of data from the redis info
75
     *
76
     * @param  array              $service
77
     * @param  integer            $id
78
     * @param  Zend_Cache_Backend $client
79
     *
80
     * @return array
81
     */
82
    protected function _getSortedService(array $service, $id, Zend_Cache_Backend $client)
83
    {
84
        $this->_info = $client->getRedis()->info();
85
        return array(
86
            'host' => $service['host'],
87
            'port' => $service['port'],
88
            'uptime' => $this->_getUptime(),
89
            'connections' => $this->_getInfo('connected_clients'),
90
            'memory' => $this->_getMemory(),
91
            'role' => $this->_getInfo('role') . $this->_getSlaves(),
92
            'lastsave' => $this->_getLastSave(),
93
            'services' => array(
94
                $id => array(
95
                    'name' => $service['name'],
96
                    'db' => $service['db'],
97
                    'keys' => count($client->getRedis()->keys('*'))
98
                )
99
            )
100
        );
101
    }
102
103
    /**
104
     * Get the uptime for this service
105
     *
106
     * @return string
107
     */
108
    protected function _getUptime()
109
    {
110
        $uptime = $this->_getInfo('uptime_in_seconds', false);
111
        if (!$uptime) {
112
            return $this->__(self::DEFAULT_MISSING_STRING);
113
        }
114
        return $this->__(
115
            '%s days, %s hours, %s minutes, %s seconds',
116
            floor($uptime / 86400),
117
            floor($uptime / 3600) % 24,
118
            floor($uptime / 60) % 60,
119
            floor($uptime % 60)
120
        );
121
    }
122
123
    /**
124
     * Get the memory usage
125
     *
126
     * @return string
127
     */
128
    protected function _getMemory()
129
    {
130
        $used = $this->_getInfo('used_memory_human', false);
131
        $peak = $this->_getInfo('used_memory_peak_human', false);
132
        if (!$used || !$peak) {
133
            return $this->__(self::DEFAULT_MISSING_STRING);
134
        }
135
        return $used . ' / ' . $peak;
136
    }
137
138
    /**
139
     * Get any connected slaves
140
     *
141
     * @return string
142
     */
143
    protected function _getSlaves()
144
    {
145
        $slaves = $this->_getInfo('connected_slaves', false);
146
        if (!$slaves) {
147
            return '';
148
        }
149
        return $this->__(' (%s slaves)', $slaves);
150
    }
151
152
    /**
153
     * Get the last save timestamp
154
     *
155
     * @return string
156
     */
157
    protected function _getLastSave()
158
    {
159
        $lastSave = $this->_getInfo('rdb_last_save_time', false);
160
        if (!$lastSave) {
161
            return $this->__(self::DEFAULT_MISSING_STRING);
162
        }
163
        try {
164
            return $this->helper('core')->formatTime(
165
                Mage::getSingleton('core/date')->timestamp($lastSave),
166
                Mage_Core_Model_Locale::FORMAT_TYPE_LONG
167
            );
168
        } catch (Exception $e) {
169
            return '';
170
        }
171
    }
172
173
    /**
174
     * Get information from the redis client
175
     *
176
     * @param string $key
177
     * @param mixed  $ifMissing
178
     *
179
     * @return mixed
180
     */
181
    protected function _getInfo($key, $ifMissing = self::DEFAULT_MISSING_STRING)
182
    {
183
        if (isset($this->_info[$key])) {
184
            return $this->_info[$key];
185
        }
186
        return is_string($ifMissing) ? $this->__($ifMissing) : $ifMissing;
187
    }
188
}
189