Completed
Pull Request — master (#177)
by
unknown
04:33
created

StatusCommand   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 179
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 13
c 1
b 0
f 0
lcom 1
cbo 2
dl 0
loc 179
rs 10

4 Methods

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

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
158
        $diff->d -= $diff->w * 7;
159
160
        $string = array(
161
            'y' => 'year',
162
            'm' => 'month',
163
            'w' => 'week',
164
            'd' => 'day',
165
            'h' => 'hour',
166
            'i' => 'minute',
167
            's' => 'second',
168
        );
169
        foreach ($string as $k => &$v) {
170
            if ($diff->$k) {
171
                $v = $diff->$k . ' ' . $v . ($diff->$k > 1 ? 's' : '');
172
            } else {
173
                unset($string[$k]);
174
            }
175
        }
176
177
        if (!$full) {
178
            $string = array_slice($string, 0, 1);
179
        }
180
181
        return $string ? implode(', ', $string) . ' ago' : 'just now';
182
    }
183
}
184