1 | <?php |
||
27 | class Status |
||
28 | { |
||
29 | /** |
||
30 | * @var string The colors of the graphs |
||
31 | */ |
||
32 | const DARK_GREEN = '#16a085'; |
||
33 | const RED = '#e74c3c'; |
||
34 | const GREEN = '#2ecc71'; |
||
35 | |||
36 | /** |
||
37 | * @var \OpCacheGUI\Format\Byte Formatter of byte values |
||
38 | */ |
||
39 | private $byteFormatter; |
||
40 | |||
41 | /** |
||
42 | * @var \OpCacheGUI\I18n\Translator A translator |
||
43 | */ |
||
44 | private $translator; |
||
45 | |||
46 | /** |
||
47 | * @var array The (unfiltered) output of opcache_get_status() |
||
48 | */ |
||
49 | private $statusData; |
||
50 | |||
51 | /** |
||
52 | * Creates instance |
||
53 | * |
||
54 | * @param \OpCacheGUI\Format\Byte $byteFormatter Formatter of byte values |
||
55 | * @param \OpCacheGUI\I18n\Translator $translator A translator |
||
56 | * @param array $statusData The (unfiltered) output of opcache_get_status() |
||
57 | */ |
||
58 | 14 | public function __construct(Byte $byteFormatter, Translator $translator, array $statusData) |
|
64 | |||
65 | /** |
||
66 | * Gets the status info of OpCache |
||
67 | * |
||
68 | * @return array The status info |
||
69 | */ |
||
70 | 1 | public function getStatusInfo() |
|
71 | { |
||
72 | return [ |
||
73 | 1 | 'opcache_enabled' => $this->statusData['opcache_enabled'], |
|
74 | 1 | 'cache_full' => $this->statusData['cache_full'], |
|
75 | 1 | 'restart_pending' => $this->statusData['restart_pending'], |
|
76 | 1 | 'restart_in_progress' => $this->statusData['restart_in_progress'], |
|
77 | ]; |
||
78 | } |
||
79 | |||
80 | /** |
||
81 | * Gets the memory info of OpCache |
||
82 | * |
||
83 | * @return array The memory info |
||
84 | */ |
||
85 | 1 | public function getMemoryInfo() |
|
86 | { |
||
87 | 1 | $memory = $this->statusData['memory_usage']; |
|
88 | |||
89 | return [ |
||
90 | 1 | 'used_memory' => $this->byteFormatter->format($memory['used_memory']), |
|
91 | 1 | 'free_memory' => $this->byteFormatter->format($memory['free_memory']), |
|
92 | 1 | 'wasted_memory' => $this->byteFormatter->format($memory['wasted_memory']), |
|
93 | 1 | 'current_wasted_percentage' => round($memory['current_wasted_percentage'], 2) . '%', |
|
94 | ]; |
||
95 | } |
||
96 | |||
97 | /** |
||
98 | * Gets the memory info formatted to build a graph |
||
99 | * |
||
100 | * @return string JSON encoded memory info |
||
101 | */ |
||
102 | 1 | public function getGraphMemoryInfo() |
|
103 | { |
||
104 | 1 | $memory = $this->statusData['memory_usage']; |
|
105 | |||
106 | 1 | return json_encode([ |
|
107 | [ |
||
108 | 1 | 'value' => $memory['used_memory'], |
|
109 | 1 | 'color' => self::DARK_GREEN, |
|
110 | 1 | 'label' => $this->translator->translate('graph.memory.used'), |
|
111 | ], |
||
112 | [ |
||
113 | 1 | 'value' => $memory['free_memory'], |
|
114 | 1 | 'color' => self::GREEN, |
|
115 | 1 | 'label' => $this->translator->translate('graph.memory.free'), |
|
116 | ], |
||
117 | [ |
||
118 | 1 | 'value' => $memory['wasted_memory'], |
|
119 | 1 | 'color' => self::RED, |
|
120 | 1 | 'label' => $this->translator->translate('graph.memory.wasted'), |
|
121 | ], |
||
122 | ]); |
||
123 | } |
||
124 | |||
125 | /** |
||
126 | * Gets the statistics info |
||
127 | * |
||
128 | * @return array The statistics info |
||
129 | */ |
||
130 | 4 | public function getStatsInfo() |
|
131 | { |
||
132 | 4 | if (!$this->statusData['opcache_enabled']) { |
|
133 | return [ |
||
134 | [ |
||
135 | 1 | 'num_cached_scripts' => 0, |
|
136 | 'num_cached_keys' => 0, |
||
137 | 'max_cached_keys' => 0, |
||
138 | 'hits' => 0, |
||
139 | 'misses' => 0, |
||
140 | 'blacklist_misses' => 0, |
||
141 | 'blacklist_miss_ratio' => 'n/a', |
||
142 | ], |
||
143 | [ |
||
144 | 'opcache_hit_rate' => 'n/a', |
||
145 | 'start_time' => 'n/a', |
||
146 | 'last_restart_time' => 'n/a', |
||
147 | 'oom_restarts' => 'n/a', |
||
148 | 'hash_restarts' => 'n/a', |
||
149 | 'manual_restarts' => 'n/a', |
||
150 | ], |
||
151 | ]; |
||
152 | } |
||
153 | |||
154 | 3 | $stats = $this->statusData['opcache_statistics']; |
|
155 | |||
156 | 3 | $lastRestartTime = null; |
|
157 | |||
158 | 3 | if ($stats['last_restart_time']) { |
|
159 | 2 | $lastRestartTime = (new \DateTime())->setTimestamp($stats['last_restart_time'])->format('H:i:s d-m-Y'); |
|
160 | } |
||
161 | |||
162 | return [ |
||
163 | [ |
||
164 | 3 | 'num_cached_scripts' => $stats['num_cached_scripts'], |
|
165 | 3 | 'num_cached_keys' => $stats['num_cached_keys'], |
|
166 | 3 | 'max_cached_keys' => $stats['max_cached_keys'], |
|
167 | 3 | 'hits' => $stats['hits'], |
|
168 | 3 | 'misses' => $stats['misses'], |
|
169 | 3 | 'blacklist_misses' => $stats['blacklist_misses'], |
|
170 | 3 | 'blacklist_miss_ratio' => round($stats['blacklist_miss_ratio'], 2), |
|
171 | ], |
||
172 | [ |
||
173 | 3 | 'opcache_hit_rate' => round($stats['opcache_hit_rate'], 2) . '%', |
|
174 | 3 | 'start_time' => (new \DateTime())->setTimestamp($stats['start_time'])->format('H:i:s d-m-Y'), |
|
175 | 3 | 'last_restart_time' => $lastRestartTime, |
|
176 | 3 | 'oom_restarts' => $stats['oom_restarts'], |
|
177 | 3 | 'hash_restarts' => $stats['hash_restarts'], |
|
178 | 3 | 'manual_restarts' => $stats['manual_restarts'], |
|
179 | ], |
||
180 | ]; |
||
181 | } |
||
182 | |||
183 | /** |
||
184 | * Gets the key statistics formatted to build a graph |
||
185 | * |
||
186 | * @return string JSON encoded key statistics |
||
187 | */ |
||
188 | 1 | public function getGraphKeyStatsInfo() |
|
189 | { |
||
190 | 1 | $stats = $this->statusData['opcache_statistics']; |
|
191 | |||
192 | 1 | return json_encode([ |
|
193 | [ |
||
194 | 1 | 'value' => $stats['num_cached_scripts'], |
|
195 | 1 | 'color' => self::DARK_GREEN, |
|
196 | 1 | 'label' => $this->translator->translate('graph.keys.scripts'), |
|
197 | ], |
||
198 | [ |
||
199 | 1 | 'value' => $stats['max_cached_keys'] - $stats['num_cached_keys'], |
|
200 | 1 | 'color' => self::GREEN, |
|
201 | 1 | 'label' => $this->translator->translate('graph.keys.free'), |
|
202 | ], |
||
203 | [ |
||
204 | 1 | 'value' => $stats['num_cached_keys'] - $stats['num_cached_scripts'], |
|
205 | 1 | 'color' => self::RED, |
|
206 | 1 | 'label' => $this->translator->translate('graph.keys.wasted'), |
|
207 | ], |
||
208 | ]); |
||
209 | } |
||
210 | |||
211 | /** |
||
212 | * Gets the hit statistics formatted to build a graph |
||
213 | * |
||
214 | * @return string JSON encoded hit statistics |
||
215 | */ |
||
216 | 1 | public function getGraphHitStatsInfo() |
|
217 | { |
||
218 | 1 | $stats = $this->statusData['opcache_statistics']; |
|
219 | |||
220 | 1 | return json_encode([ |
|
221 | [ |
||
222 | 1 | 'value' => $stats['hits'], |
|
223 | 1 | 'color' => self::GREEN, |
|
224 | 1 | 'label' => $this->translator->translate('graph.hits.hits'), |
|
225 | ], |
||
226 | [ |
||
227 | 1 | 'value' => $stats['misses'], |
|
228 | 1 | 'color' => self::RED, |
|
229 | 1 | 'label' => $this->translator->translate('graph.hits.misses'), |
|
230 | ], |
||
231 | [ |
||
232 | 1 | 'value' => $stats['blacklist_misses'], |
|
233 | 1 | 'color' => self::DARK_GREEN, |
|
234 | 1 | 'label' => $this->translator->translate('graph.hits.blacklist'), |
|
235 | ], |
||
236 | ]); |
||
237 | } |
||
238 | |||
239 | /** |
||
240 | * Gets the cached scripts |
||
241 | * |
||
242 | * @return array List of the cached scripts |
||
243 | */ |
||
244 | 5 | public function getCachedScripts() |
|
245 | { |
||
246 | 5 | if (!isset($this->statusData['scripts'])) { |
|
247 | 1 | return []; |
|
248 | } |
||
249 | |||
250 | 4 | $scripts = []; |
|
251 | |||
252 | 4 | foreach ($this->statusData['scripts'] as $script) { |
|
253 | 4 | if (isset($script['timestamp']) && $script['timestamp'] === 0) { |
|
254 | 1 | continue; |
|
255 | } |
||
256 | |||
257 | 3 | $timestamp = 'N/A'; |
|
258 | |||
259 | 3 | if (isset($script['timestamp'])) { |
|
260 | 3 | $timestamp = (new \DateTime())->setTimestamp($script['timestamp'])->format('H:i:s d-m-Y'); |
|
261 | } |
||
262 | |||
263 | 3 | $scripts[] = [ |
|
264 | 3 | 'full_path' => $script['full_path'], |
|
265 | 3 | 'hits' => $script['hits'], |
|
266 | 3 | 'memory_consumption' => $this->byteFormatter->format($script['memory_consumption']), |
|
267 | 3 | 'last_used_timestamp' => (new \DateTime())->setTimestamp($script['last_used_timestamp'])->format('H:i:s d-m-Y'), |
|
268 | 3 | 'timestamp' => $timestamp, |
|
269 | ]; |
||
270 | } |
||
271 | |||
272 | 4 | usort($scripts, [$this, 'sortCachedScripts']); |
|
273 | |||
274 | 4 | return $scripts; |
|
275 | } |
||
276 | |||
277 | /** |
||
278 | * Gets the cached scripts for the overview (with trimmed prefix) |
||
279 | * |
||
280 | * @param \OpCacheGUI\Format\Trimmer $trimmer The prefix trimmer |
||
281 | * |
||
282 | * @return array List of the cached scripts |
||
283 | */ |
||
284 | 1 | public function getCachedScriptsForOverview(Trimmer $trimmer) |
|
288 | |||
289 | /** |
||
290 | * Sorts the lists of cached scripts |
||
291 | * |
||
292 | * @param array $a Array to compare |
||
293 | * @param array $b Array to compare |
||
294 | * |
||
295 | * @return int The direction of the sort |
||
296 | */ |
||
297 | 3 | private function sortCachedScripts(array $a, array $b) |
|
301 | } |
||
302 |