MemcacheMonitor::humanreadable()   D
last analyzed

Complexity

Conditions 19
Paths 19

Size

Total Lines 46
Code Lines 41

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 19
eloc 41
nc 19
nop 1
dl 0
loc 46
rs 4.5166
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\Module\memcacheMonitor;
6
7
use SimpleSAML\Configuration;
8
use SimpleSAML\Locale\Translate;
9
use SimpleSAML\Memcache;
10
use SimpleSAML\XHTML\Template;
11
12
/**
13
 * Handles interactions with SSP's memcacheMonitor system.
14
 */
15
class MemcacheMonitor
16
{
17
    /**
18
     * The configuration that holds the memcache configuration
19
     * @var \SimpleSAML\Configuration
20
     */
21
    private Configuration $config;
22
23
    /**
24
     * An associative array with keys matching the stats, and values pointing to the formatting function for that key
25
     * @var array
26
     */
27
    private array $formats = [
28
        'bytes' => [self::class, 'humanreadable'],
29
        'bytes_read' => [self::class, 'humanreadable'],
30
        'bytes_written' => [self::class, 'humanreadable'],
31
        'limit_maxbytes' => [self::class, 'humanreadable'],
32
        'time' => [self::class, 'tdate'],
33
        'uptime' => [self::class, 'hours'],
34
    ];
35
36
37
    /**
38
     * @param \SimpleSAML\Configuration $config The configuration to use.
39
     */
40
    public function __construct(Configuration $config)
41
    {
42
        $this->config = $config;
43
    }
44
45
46
    /**
47
     * Retrieve stats, render them into a human readable page
48
     */
49
    public function renderStats(): Template
50
    {
51
        $statsraw = Memcache::getStats();
52
        $stats = $statsraw;
53
54
        foreach ($stats as $key => &$entry) {
55
            if (array_key_exists($key, $this->formats)) {
56
                $func = $this->formats[$key];
57
                foreach ($entry as $k => $val) {
58
                    $entry[$k] = call_user_func($func, $val);
59
                }
60
            }
61
        }
62
63
        $t = new Template($this->config, 'memcacheMonitor:memcachestat.twig');
64
        $rowTitles = [
65
            'accepting_conns' => Translate::noop('{memcacheMonitor:memcachestat:accepting_conns}'),
66
            'auth_cmds' => Translate::noop('{memcacheMonitor:memcachestat:auth_cmds}'),
67
            'auth_errors' => Translate::noop('{memcacheMonitor:memcachestat:auth_errors}'),
68
            'bytes' => Translate::noop('{memcacheMonitor:memcachestat:bytes}'),
69
            'bytes_read' => Translate::noop('{memcacheMonitor:memcachestat:bytes_read}'),
70
            'bytes_written' => Translate::noop('{memcacheMonitor:memcachestat:bytes_written}'),
71
            'cas_badval' => Translate::noop('{memcacheMonitor:memcachestat:cas_badval}'),
72
            'cas_hits' => Translate::noop('{memcacheMonitor:memcachestat:cas_hits}'),
73
            'cas_misses' => Translate::noop('{memcacheMonitor:memcachestat:cas_misses}'),
74
            'cmd_flush' => Translate::noop('{memcacheMonitor:memcachestat:cmd_flush}'),
75
            'cmd_get' => Translate::noop('{memcacheMonitor:memcachestat:cmd_get}'),
76
            'cmd_set' => Translate::noop('{memcacheMonitor:memcachestat:cmd_set}'),
77
            'cmd_touch' => Translate::noop('{memcacheMonitor:memcachestat:cmd_touch}'),
78
            'connection_structures' => Translate::noop('{memcacheMonitor:memcachestat:connection_structures}'),
79
            'conn_yields' => Translate::noop('{memcacheMonitor:memcachestat:conn_yields}'),
80
            'curr_connections' => Translate::noop('{memcacheMonitor:memcachestat:curr_connections}'),
81
            'curr_items' => Translate::noop('{memcacheMonitor:memcachestat:curr_items}'),
82
            'decr_hits' => Translate::noop('{memcacheMonitor:memcachestat:decr_hits}'),
83
            'decr_misses' => Translate::noop('{memcacheMonitor:memcachestat:decr_misses}'),
84
            'delete_hits' => Translate::noop('{memcacheMonitor:memcachestat:delete_hits}'),
85
            'delete_misses' => Translate::noop('{memcacheMonitor:memcachestat:delete_misses}'),
86
            'expired_unfetched' => Translate::noop('{memcacheMonitor:memcachestat:expired_unfetched}'),
87
            'evicted_unfetched' => Translate::noop('{memcacheMonitor:memcachestat:evicted_unfetched}'),
88
            'evictions' => Translate::noop('{memcacheMonitor:memcachestat:evictions}'),
89
            'get_hits' => Translate::noop('{memcacheMonitor:memcachestat:get_hits}'),
90
            'get_misses' => Translate::noop('{memcacheMonitor:memcachestat:get_misses}'),
91
            'hash_bytes' => Translate::noop('{memcacheMonitor:memcachestat:hash_bytes}'),
92
            'hash_is_expanding' => Translate::noop('{memcacheMonitor:memcachestat:hash_is_expanding}'),
93
            'hash_power_level' => Translate::noop('{memcacheMonitor:memcachestat:hash_power_level}'),
94
            'incr_hits' => Translate::noop('{memcacheMonitor:memcachestat:incr_hits}'),
95
            'incr_misses' => Translate::noop('{memcacheMonitor:memcachestat:incr_misses}'),
96
            'libevent' => Translate::noop('{memcacheMonitor:memcachestat:libevent}'),
97
            'limit_maxbytes' => Translate::noop('{memcacheMonitor:memcachestat:limit_maxbytes}'),
98
            'listen_disabled_num' => Translate::noop('{memcacheMonitor:memcachestat:listen_disabled_num}'),
99
            'pid' => Translate::noop('{memcacheMonitor:memcachestat:pid}'),
100
            'pointer_size' => Translate::noop('{memcacheMonitor:memcachestat:pointer_size}'),
101
            'reclaimed' => Translate::noop('{memcacheMonitor:memcachestat:reclaimed}'),
102
            'reserved_fds' => Translate::noop('{memcacheMonitor:memcachestat:reserved_fds}'),
103
            'rusage_system' => Translate::noop('{memcacheMonitor:memcachestat:rusage_system}'),
104
            'rusage_user' => Translate::noop('{memcacheMonitor:memcachestat:rusage_user}'),
105
            'threads' => Translate::noop('{memcacheMonitor:memcachestat:threads}'),
106
            'time' => Translate::noop('{memcacheMonitor:memcachestat:time}'),
107
            'total_connections' => Translate::noop('{memcacheMonitor:memcachestat:total_connections}'),
108
            'total_items' => Translate::noop('{memcacheMonitor:memcachestat:total_items}'),
109
            'touch_hits' => Translate::noop('{memcacheMonitor:memcachestat:touch_hits}'),
110
            'touch_misses' => Translate::noop('{memcacheMonitor:memcachestat:touch_misses}'),
111
            'uptime' => Translate::noop('{memcacheMonitor:memcachestat:uptime}'),
112
            'version' => Translate::noop('{memcacheMonitor:memcachestat:version}'),
113
        ];
114
115
        // Identify column headings
116
        $colTitles = [];
117
        foreach ($stats as $rowTitle => $rowData) {
118
            foreach ($rowData as $colTitle => $foo) {
119
                if (!in_array($colTitle, $colTitles, true)) {
120
                    $colTitles[] = $colTitle;
121
                }
122
            }
123
        }
124
125
        if (array_key_exists('bytes', $statsraw) && array_key_exists('limit_maxbytes', $statsraw)) {
126
            $usage = [];
127
            $maxpix = 400;
128
            foreach ($statsraw['bytes'] as $key => $row_data) {
129
                $pix = floor($statsraw['bytes'][$key] * $maxpix / $statsraw['limit_maxbytes'][$key]);
130
                $usage[$key] = $pix . 'px';
131
            }
132
            $t->data['maxpix'] = $maxpix . 'px';
133
            $t->data['usage'] = $usage;
134
        }
135
136
        $t->data['title'] = 'Memcache stats';
137
        $t->data['rowTitles'] = $rowTitles;
138
        $t->data['colTitles'] = $colTitles;
139
        $t->data['statsraw'] = $statsraw;
140
        $t->data['table'] = $stats;
141
142
        return $t;
143
    }
144
145
146
    /**
147
     * @param int $input
148
     * @return string
149
     */
150
    private function tdate(int $input): string
151
    {
152
        return date(DATE_RFC822, $input);
153
    }
154
155
156
    /**
157
     * @param int $input
158
     * @return string
159
     */
160
    private function hours(int $input): string
161
    {
162
        if ($input < 60) {
163
            return number_format($input, 2) . ' sec';
164
        }
165
166
        if ($input < 60 * 60) {
167
            return number_format(($input / 60), 2) . ' min';
168
        }
169
170
        if ($input < 24 * 60 * 60) {
171
            return number_format(($input / (60 * 60)), 2) . ' hours';
172
        }
173
174
        return number_format($input / (24 * 60 * 60), 2) . ' days';
175
    }
176
177
178
    /**
179
     * @param int $input
180
     * @return string
181
     */
182
    private function humanreadable(int $input): string
183
    {
184
        $output = "";
185
        $input = abs($input);
186
187
        if ($input >= (1024 * 1024 * 1024 * 1024 * 1024 * 1024 * 1024 * 100)) {
188
            $output = sprintf("%5ldEi", $input / (1024 * 1024 * 1024 * 1024 * 1024 * 1024));
189
        } elseif ($input >= (1024 * 1024 * 1024 * 1024 * 1024 * 1024 * 10)) {
190
            $output = sprintf("%5.1fEi", $input / (1024.0 * 1024.0 * 1024.0 * 1024.0 * 1024.0 * 1024.0));
191
        } elseif ($input >= (1024 * 1024 * 1024 * 1024 * 1024 * 1024)) {
192
            $output = sprintf("%5.2fEi", $input / (1024.0 * 1024.0 * 1024.0 * 1024.0 * 1024.0 * 1024.0));
193
        } elseif ($input >= (1024 * 1024 * 1024 * 1024 * 1024 * 100)) {
194
            $output = sprintf("%5ldPi", $input / (1024 * 1024 * 1024 * 1024 * 1024));
195
        } elseif ($input >= (1024 * 1024 * 1024 * 1024 * 1024 * 10)) {
196
            $output = sprintf("%5.1fPi", $input / (1024.0 * 1024.0 * 1024.0 * 1024.0 * 1024.0));
197
        } elseif ($input >= (1024 * 1024 * 1024 * 1024 * 1024)) {
198
            $output = sprintf("%5.2fPi", $input / (1024.0 * 1024.0 * 1024.0 * 1024.0 * 1024.0));
199
        } elseif ($input >= (1024 * 1024 * 1024 * 1024 * 100)) {
200
            $output = sprintf("%5ldTi", $input / (1024 * 1024 * 1024 * 1024));
201
        } elseif ($input >= (1024 * 1024 * 1024 * 1024 * 10)) {
202
            $output = sprintf("%5.1fTi", $input / (1024.0 * 1024.0 * 1024.0 * 1024.0));
203
        } elseif ($input >= (1024 * 1024 * 1024 * 1024)) {
204
            $output = sprintf("%5.2fTi", $input / (1024.0 * 1024.0 * 1024.0 * 1024.0));
205
        } elseif ($input >= (1024 * 1024 * 1024 * 100)) {
206
            $output = sprintf("%5ldGi", $input / (1024 * 1024 * 1024));
207
        } elseif ($input >= (1024 * 1024 * 1024 * 10)) {
208
            $output = sprintf("%5.1fGi", $input / (1024.0 * 1024.0 * 1024.0));
209
        } elseif ($input >= (1024 * 1024 * 1024)) {
210
            $output = sprintf("%5.2fGi", $input / (1024.0 * 1024.0 * 1024.0));
211
        } elseif ($input >= (1024 * 1024 * 100)) {
212
            $output = sprintf("%5ldMi", $input / (1024 * 1024));
213
        } elseif ($input >= (1024 * 1024 * 10)) {
214
            $output = sprintf("%5.1fM", $input / (1024.0 * 1024.0));
215
        } elseif ($input >= (1024 * 1024)) {
216
            $output = sprintf("%5.2fMi", $input / (1024.0 * 1024.0));
217
        } elseif ($input >= (1024 * 100)) {
218
            $output = sprintf("%5ldKi", $input / 1024);
219
        } elseif ($input >= (1024 * 10)) {
220
            $output = sprintf("%5.1fKi", $input / 1024.0);
221
        } elseif ($input >= (1024)) {
222
            $output = sprintf("%5.2fKi", $input / 1024.0);
223
        } else {
224
            $output = sprintf("%5ld", $input);
225
        }
226
227
        return $output;
228
    }
229
}
230