Skip to content

Commit a37c617

Browse files
committed
Add data auto refresh for mem command stats
1 parent 125e0a6 commit a37c617

File tree

5 files changed

+88
-74
lines changed

5 files changed

+88
-74
lines changed

assets/js/scripts.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ const update_panel_data = (panel_element, key, value) => {
188188

189189
if (Array.isArray(value)) {
190190
element.textContent = value[0];
191-
const progress_element = document.getElementById(key + '_progress');
191+
const progress_element = panel_element.querySelector(`[data-progress="${key}"]`);
192192
if (progress_element) {
193193
update_progress_bar(progress_element, value[1]);
194194
}

config.dist.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,5 +136,5 @@
136136
'decimal-sep' => ',',
137137
'thousands-sep' => ' ',
138138
'list-view' => 'table', // table/tree - default key list view
139-
'panelrefresh' => 2000, // In ms, refresh interval for panels - default 2000
139+
'panelrefresh' => 30000, // In ms, refresh interval for panels - default 30000 (30s)
140140
];

src/Dashboards/Memcached/MemcachedDashboard.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ public function ajax(): string {
9090
$this->memcached = $this->connect($this->servers[$this->current_server]);
9191

9292
if (isset($_GET['panels'])) {
93-
return Helpers::getPanelsJson($this->getPanelsData());
93+
return Helpers::getPanelsJson($this->getPanelsData(Http::get('tab') === 'commands_stats'));
9494
}
9595

9696
if (isset($_GET['deleteall'])) {

src/Dashboards/Memcached/MemcachedTrait.php

Lines changed: 84 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ trait MemcachedTrait {
1919
/**
2020
* @return array<int|string, mixed>
2121
*/
22-
private function getPanelsData(): array {
22+
private function getPanelsData(bool $command_stats = false): array {
2323
try {
2424
if (class_exists(PHPMem::class)) {
2525
$title = 'PHPMem v'.PHPMem::VERSION;
@@ -29,7 +29,7 @@ private function getPanelsData(): array {
2929

3030
$memory_usage = ($info['limit_maxbytes'] > 0) ? round(($info['bytes'] / $info['limit_maxbytes']) * 100, 2) : 0;
3131

32-
return [
32+
$stats = [
3333
[
3434
'title' => $title ?? null,
3535
'moreinfo' => true,
@@ -66,6 +66,12 @@ private function getPanelsData(): array {
6666
],
6767
],
6868
];
69+
70+
if ($command_stats) {
71+
$stats = array_merge($stats, $this->commandsStatsData($info));
72+
}
73+
74+
return $stats;
6975
} catch (MemcachedException $e) {
7076
return ['error' => $e->getMessage()];
7177
}
@@ -313,82 +319,90 @@ private function keysTreeView(array $keys): array {
313319
return $tree;
314320
}
315321

316-
private function commandsStats(): string {
317-
try {
318-
$info = $this->memcached->getServerStats();
319-
320-
$rate = (static fn (int $hits, int $total): float => $hits !== 0 ? round(($hits / $total) * 100, 2) : 0);
321-
322-
$get_hit_rate = $rate($info['get_hits'], $info['cmd_get']);
323-
$delete_hit_rate = $rate($info['delete_hits'], $info['delete_hits'] + $info['delete_misses']);
324-
$incr_hit_rate = $rate($info['incr_hits'], $info['incr_hits'] + $info['incr_misses']);
325-
$decr_hit_rate = $rate($info['decr_hits'], $info['decr_hits'] + $info['decr_misses']);
326-
$cas_hit_rate = $rate($info['cas_hits'], $info['cas_hits'] + $info['cas_misses']);
327-
$touch_hit_rate = $rate($info['touch_hits'], $info['cmd_touch']);
328-
329-
$commands = [
330-
[
331-
'title' => 'get',
332-
'data' => [
333-
'Hits' => Format::number($info['get_hits']),
334-
'Misses' => Format::number($info['get_misses']),
335-
['Hit Rate', $get_hit_rate.'%', $get_hit_rate, 'higher'],
336-
],
322+
/**
323+
* @param array<int|string, mixed> $info
324+
*
325+
* @return array<int|string, mixed>
326+
*/
327+
private function commandsStatsData(array $info): array {
328+
$rate = (static fn (int $hits, int $total): float => $hits !== 0 ? round(($hits / $total) * 100, 2) : 0);
329+
330+
$get_hit_rate = $rate($info['get_hits'], $info['cmd_get']);
331+
$delete_hit_rate = $rate($info['delete_hits'], $info['delete_hits'] + $info['delete_misses']);
332+
$incr_hit_rate = $rate($info['incr_hits'], $info['incr_hits'] + $info['incr_misses']);
333+
$decr_hit_rate = $rate($info['decr_hits'], $info['decr_hits'] + $info['decr_misses']);
334+
$cas_hit_rate = $rate($info['cas_hits'], $info['cas_hits'] + $info['cas_misses']);
335+
$touch_hit_rate = $rate($info['touch_hits'], $info['cmd_touch']);
336+
337+
return [
338+
[
339+
'title' => 'get',
340+
'data' => [
341+
'Hits' => Format::number($info['get_hits']),
342+
'Misses' => Format::number($info['get_misses']),
343+
['Hit Rate', $get_hit_rate.'%', $get_hit_rate, 'higher'],
337344
],
338-
[
339-
'title' => 'delete',
340-
'data' => [
341-
'Hits' => Format::number($info['delete_hits']),
342-
'Misses' => Format::number($info['delete_misses']),
343-
['Hit Rate', $delete_hit_rate.'%', $delete_hit_rate, 'higher'],
344-
],
345+
],
346+
[
347+
'title' => 'delete',
348+
'data' => [
349+
'Hits' => Format::number($info['delete_hits']),
350+
'Misses' => Format::number($info['delete_misses']),
351+
['Hit Rate', $delete_hit_rate.'%', $delete_hit_rate, 'higher'],
345352
],
346-
[
347-
'title' => 'incr',
348-
'data' => [
349-
'Hits' => Format::number($info['incr_hits']),
350-
'Misses' => Format::number($info['incr_misses']),
351-
['Hit Rate', $incr_hit_rate.'%', $incr_hit_rate, 'higher'],
352-
],
353+
],
354+
[
355+
'title' => 'incr',
356+
'data' => [
357+
'Hits' => Format::number($info['incr_hits']),
358+
'Misses' => Format::number($info['incr_misses']),
359+
['Hit Rate', $incr_hit_rate.'%', $incr_hit_rate, 'higher'],
353360
],
354-
[
355-
'title' => 'decr',
356-
'data' => [
357-
'Hits' => Format::number($info['decr_hits']),
358-
'Misses' => Format::number($info['decr_misses']),
359-
['Hit Rate', $decr_hit_rate.'%', $decr_hit_rate, 'higher'],
360-
],
361+
],
362+
[
363+
'title' => 'decr',
364+
'data' => [
365+
'Hits' => Format::number($info['decr_hits']),
366+
'Misses' => Format::number($info['decr_misses']),
367+
['Hit Rate', $decr_hit_rate.'%', $decr_hit_rate, 'higher'],
361368
],
362-
[
363-
'title' => 'touch',
364-
'data' => [
365-
'Hits' => Format::number($info['touch_hits']),
366-
'Misses' => Format::number($info['touch_misses']),
367-
['Hit Rate', $touch_hit_rate.'%', $touch_hit_rate, 'higher'],
368-
],
369+
],
370+
[
371+
'title' => 'touch',
372+
'data' => [
373+
'Hits' => Format::number($info['touch_hits']),
374+
'Misses' => Format::number($info['touch_misses']),
375+
['Hit Rate', $touch_hit_rate.'%', $touch_hit_rate, 'higher'],
369376
],
370-
[
371-
'title' => 'cas',
372-
'data' => [
373-
'Hits' => Format::number($info['cas_hits']),
374-
'Misses' => Format::number($info['cas_misses']),
375-
['Hit Rate', $cas_hit_rate.'%', $cas_hit_rate, 'higher'],
376-
'Bad Value' => $info['cas_badval'],
377-
],
377+
],
378+
[
379+
'title' => 'cas',
380+
'data' => [
381+
'Hits' => Format::number($info['cas_hits']),
382+
'Misses' => Format::number($info['cas_misses']),
383+
['Hit Rate', $cas_hit_rate.'%', $cas_hit_rate, 'higher'],
384+
'Bad Value' => $info['cas_badval'],
378385
],
379-
[
380-
'title' => 'set',
381-
'data' => [
382-
'Total' => Format::number($info['cmd_set']),
383-
],
386+
],
387+
[
388+
'title' => 'set',
389+
'data' => [
390+
'Total' => Format::number($info['cmd_set']),
384391
],
385-
[
386-
'title' => 'flush',
387-
'data' => [
388-
'Total' => Format::number($info['cmd_flush']),
389-
],
392+
],
393+
[
394+
'title' => 'flush',
395+
'data' => [
396+
'Total' => Format::number($info['cmd_flush']),
390397
],
391-
];
398+
],
399+
];
400+
}
401+
402+
private function commandsStats(): string {
403+
try {
404+
$info = $this->memcached->getServerStats();
405+
$commands = $this->commandsStatsData($info);
392406
} catch (MemcachedException $e) {
393407
$commands = ['error' => $e->getMessage()];
394408
}

templates/partials/panel.twig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
{% endif %}
1919

2020
<div class="mb-0.5 w-full bg-gray-200 rounded-sm h-1.5 dark:bg-gray-700">
21-
<div class="{{ color }} h-1.5 rounded-sm transition-[width,background-color] duration-500 ease-in-out" style="width: {{ percentage }}%;"{{ (id ? ' id="' ~ id ~ '_progress" data-type="' ~ type ~ '"' : '')|raw }}></div>
21+
<div class="{{ color }} h-1.5 rounded-sm transition-[width,background-color] duration-500 ease-in-out" style="width: {{ percentage }}%;"{{ (id ? ' data-progress="' ~ id ~ '" data-type="' ~ type ~ '"' : '')|raw }}></div>
2222
</div>
2323
{% endmacro %}
2424

0 commit comments

Comments
 (0)