Skip to content

Commit 2b4bf2d

Browse files
committed
Refactor Redis info panels
1 parent 5ba9266 commit 2b4bf2d

File tree

5 files changed

+238
-132
lines changed

5 files changed

+238
-132
lines changed

src/Dashboards/Redis/Compatibility/Cluster/PredisCluster.php

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -84,43 +84,52 @@ public function getKeyType(string $key): string {
8484
* @return array<string, array<string, mixed>>
8585
*/
8686
public function getInfo(?string $option = null, ?array $combine = null): array {
87-
static $info = [];
87+
static $info = null;
8888

89-
$options = ['Server', 'Clients', 'Memory', 'Persistence', 'Stats', 'Replication', 'CPU', 'Cluster', 'Keyspace'];
89+
if ($info !== null) {
90+
return $option !== null ? ($info[strtolower($option)] ?? []) : $info;
91+
}
9092

91-
foreach ($options as $option_name) {
92-
/** @var array<string, array<int, mixed>|array<string, array<int, mixed>>> $combined */
93-
$combined = [];
93+
$aggregated = [];
9494

95-
foreach ($this->nodes as $node) {
96-
/** @var array<string, mixed> $node_info */
97-
$node_info = $node->info()[$option_name];
95+
foreach ($this->nodes as $node) {
96+
try {
97+
$node_section_info = $node->info();
98+
} catch (Exception) {
99+
continue;
100+
}
98101

99-
foreach ($node_info as $key => $value) {
102+
foreach ($node_section_info as $section_name => $section_data) {
103+
$section_name_lower = strtolower((string) $section_name);
104+
foreach ($section_data as $key => $value) {
100105
if (is_array($value)) {
101106
foreach ($value as $sub_key => $sub_val) {
102-
$combined[$key][$sub_key][] = $sub_val;
107+
$aggregated[$section_name_lower][$key][$sub_key][] = $sub_val;
103108
}
104109
} else {
105-
$combined[$key][] = $value;
110+
$aggregated[$section_name_lower][$key][] = $value;
106111
}
107112
}
108113
}
114+
}
109115

110-
foreach ($combined as $key => $values) {
116+
$combined_info = [];
117+
118+
foreach ($aggregated as $section_name => $section_data) {
119+
foreach ($section_data as $key => $values) {
111120
if (is_array(reset($values))) {
112121
foreach ($values as $sub_key => $sub_values) {
113-
$combined[$key][$sub_key] = $this->combineValues($sub_key, $sub_values, $combine);
122+
$combined_info[$section_name][$key][$sub_key] = $this->combineValues($sub_key, $sub_values, $combine);
114123
}
115124
} else {
116-
$combined[$key] = $this->combineValues($key, $values, $combine);
125+
$combined_info[$section_name][$key] = $this->combineValues($key, $values, $combine);
117126
}
118127
}
119-
120-
$info[strtolower($option_name)] = $combined;
121128
}
122129

123-
return $option !== null ? ($info[$option] ?? []) : $info;
130+
$info = $combined_info;
131+
132+
return $option !== null ? ($info[strtolower($option)] ?? []) : $info;
124133
}
125134

126135
/**

src/Dashboards/Redis/Compatibility/Cluster/RedisCluster.php

Lines changed: 37 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -82,47 +82,63 @@ public function getKeyType(string $key): string {
8282
* @param list<string>|null $combine
8383
*
8484
* @return array<string, array<string, mixed>>
85-
*
86-
* @throws RedisClusterException
8785
*/
8886
public function getInfo(?string $option = null, ?array $combine = null): array {
89-
static $info = [];
87+
static $info = null;
88+
89+
if ($info !== null) {
90+
return $option !== null ? ($info[strtolower($option)] ?? []) : $info;
91+
}
9092

91-
$options = ['SERVER', 'CLIENTS', 'MEMORY', 'PERSISTENCE', 'STATS', 'REPLICATION', 'CPU', 'CLUSTER', 'KEYSPACE'];
93+
$section_info = [];
94+
$sections = ['SERVER', 'CLIENTS', 'MEMORY', 'PERSISTENCE', 'STATS', 'REPLICATION', 'CPU', 'CLUSTER', 'KEYSPACE',];
9295

93-
foreach ($options as $option_name) {
94-
/** @var array<string, array<int, mixed>|array<string, array<int, mixed>>> $combined */
95-
$combined = [];
96+
foreach ($sections as $section_name) {
97+
$aggregated_values = [];
9698

9799
foreach ($this->nodes as $node) {
98-
/** @var array<string, mixed> $node_info */
99-
$node_info = $this->info($node, $option_name);
100+
try {
101+
$node_section_info = $this->info($node, $section_name);
102+
103+
if (!is_array($node_section_info)) {
104+
continue;
105+
}
100106

101-
foreach ($node_info as $key => $value) {
102-
if (is_array($value)) {
103-
foreach ($value as $sub_key => $sub_val) {
104-
$combined[$key][$sub_key][] = $sub_val;
107+
foreach ($node_section_info as $key => $value) {
108+
if (is_array($value)) {
109+
foreach ($value as $sub_key => $sub_val) {
110+
$aggregated_values[$key][$sub_key][] = $sub_val;
111+
}
112+
} else {
113+
$aggregated_values[$key][] = $value;
105114
}
106-
} else {
107-
$combined[$key][] = $value;
108115
}
116+
} catch (RedisClusterException) {
117+
continue;
109118
}
110119
}
111120

112-
foreach ($combined as $key => $values) {
121+
if (empty($aggregated_values)) {
122+
continue;
123+
}
124+
125+
$combined_section = [];
126+
127+
foreach ($aggregated_values as $key => $values) {
113128
if (is_array(reset($values))) {
114129
foreach ($values as $sub_key => $sub_values) {
115-
$combined[$key][$sub_key] = $this->combineValues($sub_key, $sub_values, $combine);
130+
$combined_section[$key][$sub_key] = $this->combineValues($sub_key, $sub_values, $combine);
116131
}
117132
} else {
118-
$combined[$key] = $this->combineValues($key, $values, $combine);
133+
$combined_section[$key] = $this->combineValues($key, $values, $combine);
119134
}
120135
}
121-
122-
$info[strtolower($option_name)] = $combined;
136+
$section_info[strtolower($section_name)] = $combined_section;
123137
}
124138

125-
return $option !== null ? ($info[$option] ?? []) : $info;
139+
$info = $section_info;
140+
141+
return $option !== null ? ($info[strtolower($option)] ?? []) : $info;
126142
}
127143

128144
/**

src/Dashboards/Redis/Compatibility/Predis.php

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -69,28 +69,44 @@ public function getKeyType(string $key): string {
6969
* @return array<int|string, mixed>
7070
*/
7171
public function getInfo(?string $option = null): array {
72-
static $array = [];
72+
static $info = null;
7373

74-
$options = ['Server', 'Clients', 'Memory', 'Persistence', 'Stats', 'Replication', 'CPU', 'Cluster', 'Keyspace'];
74+
if ($info !== null) {
75+
return $option !== null ? ($info[strtolower($option)] ?? []) : $info;
76+
}
7577

76-
foreach ($options as $option_name) {
77-
$data = $this->info()[$option_name];
78+
$all_sections = $this->info();
79+
$section_info = [];
7880

79-
if ($option_name === 'Keyspace') {
80-
foreach ($data as $db => $keys_data) {
81-
$keys = [];
82-
foreach ($keys_data as $key_name => $key_value) {
83-
$keys[] = $key_name.'='.$key_value;
84-
}
81+
foreach ($all_sections as $section_name => $section_data) {
82+
$section_name_lower = strtolower((string) $section_name);
83+
84+
if ($section_name_lower === 'keyspace') {
85+
$reformatted_keyspace = [];
86+
87+
if (is_array($section_data)) {
88+
foreach ($section_data as $db => $keys_data_array) {
89+
$key_value_pairs = [];
8590

86-
$data[$db] = implode(',', $keys);
91+
if (is_array($keys_data_array)) {
92+
foreach ($keys_data_array as $key => $value) {
93+
$key_value_pairs[] = $key.'='.$value;
94+
}
95+
}
96+
97+
$reformatted_keyspace[$db] = implode(',', $key_value_pairs);
98+
}
8799
}
100+
101+
$section_data = $reformatted_keyspace;
88102
}
89103

90-
$array[strtolower($option_name)] = $data;
104+
$section_info[$section_name_lower] = $section_data;
91105
}
92106

93-
return $array[$option] ?? $array;
107+
$info = $section_info;
108+
109+
return $option !== null ? ($info[strtolower($option)] ?? []) : $info;
94110
}
95111

96112
/**

src/Dashboards/Redis/Compatibility/Redis.php

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -87,15 +87,32 @@ public function getKeyType(string $key): string {
8787
* @throws RedisException
8888
*/
8989
public function getInfo(?string $option = null): array {
90-
static $info = [];
90+
static $info = null;
91+
92+
if ($info === null) {
93+
$section_info = [];
94+
$sections = ['SERVER', 'CLIENTS', 'MEMORY', 'PERSISTENCE', 'STATS', 'REPLICATION', 'CPU', 'CLUSTER', 'KEYSPACE'];
95+
96+
foreach ($sections as $section) {
97+
try {
98+
$section_data = $this->info($section);
99+
100+
if ($section_data) {
101+
$section_info[strtolower($section)] = $section_data;
102+
}
103+
} catch (RedisException) {
104+
continue;
105+
}
106+
}
91107

92-
$options = ['SERVER', 'CLIENTS', 'MEMORY', 'PERSISTENCE', 'STATS', 'REPLICATION', 'CPU', 'CLUSTER', 'KEYSPACE'];
108+
$info = $section_info;
109+
}
93110

94-
foreach ($options as $option_name) {
95-
$info[strtolower($option_name)] = $this->info($option_name);
111+
if ($option !== null) {
112+
return $info[strtolower($option)] ?? [];
96113
}
97114

98-
return $info[$option] ?? $info;
115+
return $info;
99116
}
100117

101118
/**

0 commit comments

Comments
 (0)