Completed
Push — master ( 92bd35...fb5c6c )
by Tom
04:10
created

StatusCommand::timeElapsedString()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
rs 10
cc 2
eloc 2
nc 2
nop 2
1
<?php
2
3
namespace N98\Magento\Command\Database;
4
5
use N98\Util\TimeElapsed;
6
7
class StatusCommand extends AbstractShowCommand
8
{
9
    protected $showMethod = 'getGlobalStatus';
10
11
    /**
12
     * Add more important status variables
13
     *
14
     * @var array
15
     */
16
    protected $_importantVars = array(
17
        'Threads_connected' => array(
18
            'desc' => 'Total number of clients that have currently open connections to the server.',
19
        ),
20
        'Created_tmp_disk_tables' => array(
21
            'desc' => 'Number of temporary tables that have been created on disk instead of in-memory. Lower is
22
            better.',
23
        ),
24
        'Handler_read_first' => array(
25
            'desc' => 'Number of times a table handler made a request to read the first row of a table index.',
26
        ),
27
        'Handler_read_rnd_next' => array(
28
            'desc' => 'Number of requests to read the next row in the data file. This value is high if you
29
                are doing a lot of table scans. Generally this suggests that your tables are not properly indexed or
30
                that your queries are not written to take advantage of the indexes you have.',
31
        ),
32
        'Innodb_buffer_pool_wait_free' => array(
33
            'desc' => 'Number of times MySQL has to wait for memory pages to be flushed.',
34
        ),
35
        'Innodb_buffer_pool_pages_dirty' => array(
36
            'desc' => 'Indicates the number of InnoDB buffer pool data pages that have been changed in memory,
37
            but the changes are not yet written (flushed) to the InnoDB data files',
38
        ),
39
        'Key_reads' => array(
40
            'desc' => 'Number of filesystem accesses MySQL performed to fetch database indexes.',
41
        ),
42
        'Max_used_connections' => array(
43
            'desc' => 'Max number of connections MySQL has had open at the same time since the server 
44
            was last restarted.',
45
        ),
46
        'Open_tables' => array(
47
            'desc' => 'Number of tables that are currently open.',
48
        ),
49
        'Select_full_join' => array(
50
            'desc' => 'Number of full joins MySQL has performed to satisfy client queries.',
51
        ),
52
        'Slow_queries' => array(
53
            'desc' => 'Number of queries that have taken longer than usual to execute.',
54
        ),
55
        'Uptime' => array(
56
            'desc' => 'Time since the server was last restarted.',
57
        ),
58
        'Aborted_connects' => array(
59
            'desc' => 'Total number of failed attempts to connect to MySQL.',
60
        ),
61
    );
62
    /**
63
     * @var array
64
     */
65
    protected $_specialFormat = array(
66
        'Uptime' => array('N98\Util\TimeElapsed', 'short'),
67
    );
68
69
    protected function configure()
70
    {
71
        parent::configure();
72
        $this
73
            ->setName('db:status')
74
            ->setDescription('Shows important server status information or custom selected status values');
75
76
        $help = <<<HELP
77
This command is useful to print important server status information about the current database.
78
HELP;
79
        $this->setHelp($help);
80
    }
81
82
    /**
83
     * @param array $outputVars
84
     * @param bool $hasDescription
85
     *
86
     * @return array
87
     */
88
    protected function generateRows(array $outputVars, $hasDescription)
89
    {
90
        $rows = parent::generateRows($outputVars, $hasDescription);
91
92
        if (false === $hasDescription) {
93
            return $rows;
94
        }
95
96
        if (isset($this->_allVariables['Handler_read_rnd_next'])) {
97
            $tableScanRate = (
98
                (
99
                    $this->_allVariables['Handler_read_rnd_next'] +
100
                    $this->_allVariables['Handler_read_rnd']
101
                )
102
                /
103
                (
104
                    $this->_allVariables['Handler_read_rnd_next'] +
105
                    $this->_allVariables['Handler_read_rnd'] +
106
                    $this->_allVariables['Handler_read_first'] +
107
                    $this->_allVariables['Handler_read_next'] +
108
                    $this->_allVariables['Handler_read_key'] +
109
                    $this->_allVariables['Handler_read_prev']
110
                )
111
            );
112
            $rows[] = array(
113
                'Full table scans',
114
                sprintf('%.2f', $tableScanRate * 100) . '%',
115
                $this->formatDesc(
116
                    'HINT: "Handler_read_rnd_next" is reset to zero when reached the value of 2^32 (4G).'
117
                ),
118
            );
119
        }
120
        if (isset($this->_allVariables['Innodb_buffer_pool_read_requests'])) {
121
            $bufferHitRate = $this->_allVariables['Innodb_buffer_pool_read_requests'] /
122
                ($this->_allVariables['Innodb_buffer_pool_read_requests'] +
123
                    $this->_allVariables['Innodb_buffer_pool_reads']);
124
125
            $rows[] = array(
126
                'InnoDB Buffer Pool hit',
127
                sprintf('%.2f', $bufferHitRate * 100) . '%',
128
                $this->formatDesc(
129
                    'An InnoDB Buffer Pool hit ratio below 99.9% is a weak indicator that your InnoDB Buffer Pool 
130
                    could be increased.'
131
                ),
132
            );
133
        }
134
135
        return $rows;
136
    }
137
138
    /**
139
     * @param string $name
140
     *
141
     * @return bool
142
     */
143
    protected function allowRounding($name)
144
    {
145
        $isSize = false !== strpos($name, '_size');
146
147
        return $isSize;
148
    }
149
150
    /**
151
     * borrowed from <http://stackoverflow.com/questions/1416697/>
152
     *
153
     * echo time_elapsed_string('2013-05-01 00:22:35');
154
     * echo time_elapsed_string('@1367367755'); # timestamp input
155
     * echo time_elapsed_string('2013-05-01 00:22:35', true);
156
     *
157
     * @param      $datetime
158
     * @param bool $full
159
     * @deprecated since 1.3.2, use callback "N98\Util\TimeElapsed::short" instead
160
     * @see \N98\Util\TimeElapsed::short()
161
     *
162
     * @return string
163
     */
164
    protected function timeElapsedString($datetime, $full = false)
165
    {
166
        return $full ? TimeElapsed::full($datetime) : TimeElapsed::short($datetime);
167
    }
168
}
169