|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * This file is part of the phpCacheAdmin. |
| 4 | + * Copyright (c) Róbert Kelčák (https://kelcak.com/) |
| 5 | + */ |
| 6 | + |
| 7 | +declare(strict_types=1); |
| 8 | + |
| 9 | +namespace RobiNN\Pca\Dashboards\Redis\Compatibility; |
| 10 | + |
| 11 | +use Exception; |
| 12 | + |
| 13 | +trait RedisExtra { |
| 14 | + /** |
| 15 | + * @return array<int, string> |
| 16 | + */ |
| 17 | + public function getInfoSections(): array { |
| 18 | + return [ |
| 19 | + 'server', 'clients', 'memory', 'persistence', 'threads', 'stats', 'replication', 'cpu', |
| 20 | + 'commandstats', 'latencystats', 'cluster', 'keyspace', 'errorstats', |
| 21 | + ]; |
| 22 | + } |
| 23 | + |
| 24 | + /** |
| 25 | + * @return array<int, array<string, mixed>> |
| 26 | + */ |
| 27 | + public function parseSectionData(string $section): array { |
| 28 | + $data = $this->getInfo($section); |
| 29 | + |
| 30 | + return array_map(static function ($value) { |
| 31 | + if (is_array($value)) { |
| 32 | + return $value; |
| 33 | + } |
| 34 | + |
| 35 | + parse_str(str_replace(',', '&', $value), $parsed); |
| 36 | + |
| 37 | + return $parsed; |
| 38 | + }, $data); |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * @throws Exception |
| 43 | + */ |
| 44 | + public function jsonGet(string $key): string { |
| 45 | + return $this->rawCommand('JSON.GET', $key); |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * @throws Exception |
| 50 | + */ |
| 51 | + public function jsonSet(string $key, mixed $value): bool { |
| 52 | + $raw = $this->rawCommand('JSON.SET', $key, '$', $value); |
| 53 | + |
| 54 | + return $raw === true || $raw === 'OK'; |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * @return array<int, array<string, int|string>> |
| 59 | + * |
| 60 | + * @throws Exception |
| 61 | + */ |
| 62 | + public function getModules(): array { |
| 63 | + static $modules = []; |
| 64 | + |
| 65 | + try { |
| 66 | + $list = $this->rawCommand('MODULE', 'LIST'); // require Redis >= 4.0 |
| 67 | + } catch (Exception) { |
| 68 | + return []; |
| 69 | + } |
| 70 | + |
| 71 | + if (!is_array($list) || $list === []) { |
| 72 | + return []; |
| 73 | + } |
| 74 | + |
| 75 | + foreach ($list as $module) { |
| 76 | + $modules[] = [ |
| 77 | + $module[0] => $module[1], // name |
| 78 | + $module[2] => $module[3], // version |
| 79 | + ]; |
| 80 | + } |
| 81 | + |
| 82 | + return $modules; |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * @throws Exception |
| 87 | + */ |
| 88 | + public function checkModule(string $module): bool { |
| 89 | + return in_array($module, array_column($this->getModules(), 'name'), true); |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * Combine info values into a single value in a cluster. |
| 94 | + * |
| 95 | + * @param list<mixed> $values |
| 96 | + * @param list<string>|null $combine |
| 97 | + */ |
| 98 | + private function combineValues(string $key, array $values, ?array $combine): mixed { |
| 99 | + $unique = array_unique($values); |
| 100 | + |
| 101 | + if (count($unique) === 1) { |
| 102 | + return $unique[0]; |
| 103 | + } |
| 104 | + |
| 105 | + $numeric = array_filter($values, is_numeric(...)); |
| 106 | + |
| 107 | + if ($combine && in_array($key, $combine, true) && count($numeric) === count($values)) { |
| 108 | + return array_sum($values); |
| 109 | + } |
| 110 | + |
| 111 | + if ($key === 'mem_fragmentation_ratio' && count($numeric) === count($values)) { |
| 112 | + return round(array_sum($values) / count($values), 2); |
| 113 | + } |
| 114 | + |
| 115 | + if ($key === 'used_memory_peak' && count($numeric) === count($values)) { |
| 116 | + return max($values); |
| 117 | + } |
| 118 | + |
| 119 | + return $values; |
| 120 | + } |
| 121 | +} |
0 commit comments