Completed
Push — develop ( 320c63...92592d )
by Tom
04:57
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('HINT: "Handler_read_rnd_next" is reset to zero when reached the value of 2^32 (4G).'),
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 121 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
116
            );
117
        }
118
        if (isset($this->_allVariables['Innodb_buffer_pool_read_requests'])) {
119
            $bufferHitRate = $this->_allVariables['Innodb_buffer_pool_read_requests'] /
120
                ($this->_allVariables['Innodb_buffer_pool_read_requests'] +
121
                    $this->_allVariables['Innodb_buffer_pool_reads']);
122
123
            $rows[] = array(
124
                'InnoDB Buffer Pool hit',
125
                sprintf('%.2f', $bufferHitRate * 100) . '%',
126
                $this->formatDesc(
127
                    'An InnoDB Buffer Pool hit ratio below 99.9% is a weak indicator that your InnoDB Buffer Pool 
128
                    could be increased.'
129
                ),
130
            );
131
        }
132
        return $rows;
133
    }
134
135
    /**
136
     * @param string $name
137
     *
138
     * @return bool
139
     */
140
    protected function allowRounding($name)
141
    {
142
        $isSize = false !== strpos($name, '_size');
143
        return $isSize;
144
    }
145
146
    /**
147
     * borrowed from <http://stackoverflow.com/questions/1416697/>
148
     *
149
     * echo time_elapsed_string('2013-05-01 00:22:35');
150
     * echo time_elapsed_string('@1367367755'); # timestamp input
151
     * echo time_elapsed_string('2013-05-01 00:22:35', true);
152
     *
153
     * @param      $datetime
154
     * @param bool $full
155
     * @deprecated since 1.3.2, use callback "N98\Util\TimeElapsed::short" instead
156
     * @see \N98\Util\TimeElapsed::short()
157
     *
158
     * @return string
159
     */
160
    protected function timeElapsedString($datetime, $full = false)
161
    {
162
        return $full ? TimeElapsed::full($datetime) : TimeElapsed::short($datetime);
163
    }
164
}
165