From a20828f6187902b2d5915b1a8690168916271b01 Mon Sep 17 00:00:00 2001 From: Philipp Date: Mon, 21 Apr 2025 20:12:54 +0200 Subject: [PATCH] Add Caching stats --- src/Module/StatsCaching.php | 98 +++++++++++++++++++++++++++++++++++++ static/routes.config.php | 3 +- 2 files changed, 100 insertions(+), 1 deletion(-) create mode 100644 src/Module/StatsCaching.php diff --git a/src/Module/StatsCaching.php b/src/Module/StatsCaching.php new file mode 100644 index 0000000000..72ba61c67a --- /dev/null +++ b/src/Module/StatsCaching.php @@ -0,0 +1,98 @@ +config = $config; + $this->cache = $cache; + $this->lock = $lock; + } + + private function isAllowed(array $request): bool + { + return empty(!$request['key']) && $request['key'] == $this->config->get('system', 'stats_key'); + } + + protected function content(array $request = []): string + { + if (!$this->isAllowed($request)) { + throw new HTTPException\NotFoundException($this->l10n->t('Page not found.')); + } + return ''; + } + + protected function rawContent(array $request = []) + { + if (!$this->isAllowed($request)) { + return; + } + + $data = []; + + // OPcache + if (function_exists('opcache_get_status')) { + $status = opcache_get_status(false); + $data['opcache'] = [ + 'enabled' => $status['opcache_enabled'] ?? false, + 'hit_rate' => $status['opcache_statistics']['opcache_hit_rate'] ?? null, + 'used_memory' => $status['memory_usage']['used_memory'] ?? null, + 'free_memory' => $status['memory_usage']['free_memory'] ?? null, + 'num_cached_scripts' => $status['opcache_statistics']['num_cached_scripts'] ?? null, + ]; + } else { + $data['opcache'] = [ + 'enabled' => false, + ]; + } + + if ($this->cache instanceof ICanCacheInMemory) { + $data['cache'] = [ + 'type' => $this->cache->getName(), + 'stats' => $this->cache->getStats(), + ]; + } else { + $data['cache'] = [ + 'type' => $this->cache->getName(), + ]; + } + + if ($this->lock instanceof CacheLock) { + $data['lock'] = [ + 'type' => $this->lock->getName(), + 'stats' => $this->lock->getCacheStats(), + ]; + } else { + $data['lock'] = [ + 'type' => $this->lock->getName(), + ]; + } + + $this->jsonExit($data); + } +} diff --git a/static/routes.config.php b/static/routes.config.php index 88e642c27a..4c7b6949ec 100644 --- a/static/routes.config.php +++ b/static/routes.config.php @@ -642,7 +642,8 @@ return [ ], ], - '/stats' => [Module\Stats::class, [R::GET]], + '/stats' => [Module\Stats::class, [R::GET]], + '/stats/caching' => [Module\StatsCaching::class, [R::GET]], '/network' => [ '[/{content}]' => [Module\Conversation\Network::class, [R::GET]],