Skip to content

Commit 00ca214

Browse files
committed
Fixes
1 parent 3f1dcf5 commit 00ca214

File tree

14 files changed

+73
-60
lines changed

14 files changed

+73
-60
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ To customize the configuration, do not edit `config.dist.php` directly, but copy
5151

5252
## Updating
5353

54-
Replace all files and delete the `tmp` folder (this folder contains only compiled Twig templates).
54+
Replace all files and delete the `tmp` folder (this folder contains compiled Twig templates and metrics DB files).
5555

5656
## Environment variables
5757

index.php

Lines changed: 4 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
use Composer\InstalledVersions;
1010
use RobiNN\Pca\Admin;
1111
use RobiNN\Pca\Config;
12-
use RobiNN\Pca\Http;
1312
use RobiNN\Pca\Template;
1413

1514
// Always display errors
@@ -37,40 +36,12 @@
3736
autoload($path);
3837
}
3938

39+
$auth = false;
40+
4041
if (is_callable(Config::get('auth'))) {
4142
Config::get('auth')();
4243
$auth = true;
4344
}
4445

45-
$tpl = new Template();
46-
$admin = new Admin($tpl);
47-
48-
$nav = array_map(static fn ($d_dashboard): array => $d_dashboard->dashboardInfo(), $admin->dashboards);
49-
50-
$current = $admin->currentDashboard();
51-
$dashboard = $admin->getDashboard($current);
52-
$info = $dashboard->dashboardInfo();
53-
54-
$tpl->addGlobal('current', $current);
55-
56-
if (isset($_GET['ajax'])) {
57-
echo $dashboard->ajax();
58-
} else {
59-
if (isset($info['colors'])) {
60-
$colors = '';
61-
62-
foreach ((array) $info['colors'] as $key => $color) {
63-
$colors .= '--color-primary-'.$key.':'.$color.';';
64-
}
65-
}
66-
67-
echo $tpl->render('layout', [
68-
'colors' => $colors ?? null,
69-
'site_title' => $info['title'],
70-
'nav' => $nav,
71-
'logout_url' => isset($auth) ? Http::queryString([], ['logout' => 'yes']) : null,
72-
'version' => Admin::VERSION,
73-
'repo' => 'https://github.com/RobiNN1/phpCacheAdmin',
74-
'dashboard' => $dashboard->dashboard(),
75-
]);
76-
}
46+
$template = new Template();
47+
echo (new Admin($template))->render($auth);

src/Admin.php

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ class Admin {
1616
/**
1717
* @var array<string, DashboardInterface>
1818
*/
19-
public array $dashboards = [];
19+
private array $dashboards = [];
2020

21-
public function __construct(Template $template) {
21+
public function __construct(private readonly Template $template) {
2222
foreach (Config::get('dashboards', []) as $class) {
2323
if (is_subclass_of($class, DashboardInterface::class) && $class::check()) {
2424
$dashboard = new $class($template);
@@ -28,13 +28,45 @@ public function __construct(Template $template) {
2828
}
2929
}
3030

31-
public function getDashboard(string $dashboard): DashboardInterface {
31+
private function getDashboard(string $dashboard): DashboardInterface {
3232
return $this->dashboards[$dashboard];
3333
}
3434

35-
public function currentDashboard(): string {
35+
private function currentDashboard(): string {
3636
$current = Http::get('dashboard', '');
3737

3838
return array_key_exists($current, $this->dashboards) ? $current : array_key_first($this->dashboards);
3939
}
40+
41+
public function render(bool $auth): string {
42+
$nav = array_map(static fn ($d_dashboard): array => $d_dashboard->dashboardInfo(), $this->dashboards);
43+
44+
$current = $this->currentDashboard();
45+
$dashboard = $this->getDashboard($current);
46+
$info = $dashboard->dashboardInfo();
47+
48+
$this->template->addGlobal('current', $current);
49+
50+
if (isset($_GET['ajax'])) {
51+
return $dashboard->ajax();
52+
}
53+
54+
$colors = '';
55+
56+
if (isset($info['colors'])) {
57+
foreach ((array) $info['colors'] as $key => $color) {
58+
$colors .= '--color-primary-'.$key.':'.$color.';';
59+
}
60+
}
61+
62+
return $this->template->render('layout', [
63+
'colors' => $colors,
64+
'site_title' => $info['title'],
65+
'nav' => $nav,
66+
'logout_url' => $auth ? Http::queryString([], ['logout' => 'yes']) : null,
67+
'version' => self::VERSION,
68+
'repo' => 'https://github.com/RobiNN1/phpCacheAdmin',
69+
'dashboard' => $dashboard->dashboard(),
70+
]);
71+
}
4072
}

src/Dashboards/Memcached/MemcachedMetrics.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,10 @@
1515
use RobiNN\Pca\Http;
1616

1717
class MemcachedMetrics {
18-
private PDO $pdo;
18+
private readonly PDO $pdo;
1919

2020
private const RATE_COMMANDS = ['get', 'set', 'delete', 'incr', 'decr', 'cas', 'touch', 'flush'];
21+
2122
private const HIT_RATE_COMMANDS = ['get', 'delete', 'incr', 'decr', 'cas', 'touch'];
2223

2324
/**
@@ -54,7 +55,7 @@ public function __construct(private readonly PHPMem $memcached, array $servers,
5455
public function collectAndRespond(): string {
5556
$stats = $this->memcached->getServerStats();
5657

57-
if (empty($stats)) {
58+
if ($stats === []) {
5859
throw new MemcachedException('Failed to retrieve Memcached stats.');
5960
}
6061

@@ -113,7 +114,8 @@ private function calculateMetrics(array $stats): array {
113114
$command_rates['request_rate_overall'] = array_sum($command_rates);
114115

115116
$hit_rates = [];
116-
$total_hits = $total_misses = 0;
117+
$total_hits = 0;
118+
$total_misses = 0;
117119

118120
foreach (self::HIT_RATE_COMMANDS as $cmd) {
119121
$hit_rates['hit_rate_'.$cmd] = $calculate_hit_rate($stats, $cmd);
@@ -154,7 +156,7 @@ private function calculateMetrics(array $stats): array {
154156
private function insertMetrics(array $metrics): void {
155157
$columns = implode(', ', array_keys($metrics));
156158
$placeholders = rtrim(str_repeat('?, ', count($metrics)), ', ');
157-
$sql = "INSERT INTO metrics ($columns) VALUES ($placeholders)";
159+
$sql = sprintf('INSERT INTO metrics (%s) VALUES (%s)', $columns, $placeholders);
158160

159161
$stmt = $this->pdo->prepare($sql);
160162
$stmt->execute(array_values($metrics));
@@ -169,6 +171,7 @@ private function fetchRecentMetrics(): array {
169171
$stmt = $this->pdo->prepare('SELECT * FROM metrics ORDER BY id DESC LIMIT :limit');
170172
$stmt->bindValue(':limit', $max_data_points_to_return, PDO::PARAM_INT);
171173
$stmt->execute();
174+
172175
$results = $stmt->fetchAll(PDO::FETCH_ASSOC);
173176

174177
return array_reverse($results);

src/Dashboards/Memcached/MemcachedTrait.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ private function getPanelsData(bool $command_stats = false): array {
6868
];
6969

7070
if ($command_stats) {
71-
$stats = array_merge($stats, $this->commandsStatsData($info));
71+
return array_merge($stats, $this->commandsStatsData($info));
7272
}
7373

7474
return $stats;

src/Dashboards/Memcached/PHPMem.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,7 @@ public function getKeys(): array {
217217
if (isset($seen_keys[$key_name])) {
218218
continue;
219219
}
220+
220221
$seen_keys[$key_name] = true;
221222

222223
$exp = ((int) $item[3] === 0) ? -1 : (int) $item[3];
@@ -448,6 +449,7 @@ private function streamConnection(string $command, string $command_name): string
448449
if ($this->checkCommandEnd($buffer)) {
449450
break;
450451
}
452+
451453
continue;
452454
}
453455

@@ -461,6 +463,7 @@ private function streamConnection(string $command, string $command_name): string
461463
if ($this->checkCommandEnd($buffer)) {
462464
break;
463465
}
466+
464467
continue;
465468
}
466469

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -437,7 +437,7 @@ public function getSlowlog(int $count): ?array {
437437
}
438438
}
439439

440-
usort($all_logs, static fn ($a, $b): int => $b[1] <=> $a[1]);
440+
usort($all_logs, static fn (array $a, array $b): int => $b[1] <=> $a[1]);
441441

442442
return $all_logs;
443443
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@ public function getSlowlog(int $count): ?array {
337337
}
338338
}
339339

340-
usort($all_logs, static fn ($a, $b): int => $b[1] <=> $a[1]);
340+
usort($all_logs, static fn (array $a, array $b): int => $b[1] <=> $a[1]);
341341

342342
return $all_logs;
343343
}

src/Dashboards/Redis/Compatibility/Predis.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,8 +172,10 @@ public function pipelineKeys(array $keys): array {
172172

173173
foreach ($keys as $i => $key) {
174174
$result = $results[$i] ?? null;
175-
176-
if (!is_array($result) || count($result) < 3) {
175+
if (!is_array($result)) {
176+
continue;
177+
}
178+
if (count($result) < 3) {
177179
continue;
178180
}
179181

src/Dashboards/Redis/Compatibility/Redis.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,8 +186,10 @@ public function pipelineKeys(array $keys): array {
186186

187187
foreach ($keys as $i => $key) {
188188
$result = $results[$i] ?? null;
189-
190-
if (!is_array($result) || count($result) < 3) {
189+
if (!is_array($result)) {
190+
continue;
191+
}
192+
if (count($result) < 3) {
191193
continue;
192194
}
193195

0 commit comments

Comments
 (0)