2014-10-02 19:35:07 -04:00
|
|
|
<?php
|
2024-05-23 03:26:56 -04:00
|
|
|
|
2014-10-02 19:35:07 -04:00
|
|
|
/**
|
2024-05-23 03:26:56 -04:00
|
|
|
* SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
|
|
|
|
|
* SPDX-FileCopyrightText: 2016 ownCloud, Inc.
|
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
2014-10-02 19:35:07 -04:00
|
|
|
*/
|
2014-10-03 14:39:09 -04:00
|
|
|
namespace OC\Diagnostics;
|
2014-10-02 19:35:07 -04:00
|
|
|
|
2022-06-29 09:34:06 -04:00
|
|
|
use OCP\Cache\CappedMemoryCache;
|
2014-10-03 14:39:09 -04:00
|
|
|
use OCP\Diagnostics\IQueryLogger;
|
2014-10-02 19:35:07 -04:00
|
|
|
|
|
|
|
|
class QueryLogger implements IQueryLogger {
|
2022-05-03 05:15:24 -04:00
|
|
|
protected int $index = 0;
|
|
|
|
|
protected ?Query $activeQuery = null;
|
|
|
|
|
/** @var CappedMemoryCache<Query> */
|
|
|
|
|
protected CappedMemoryCache $queries;
|
2016-11-03 09:35:44 -04:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* QueryLogger constructor.
|
|
|
|
|
*/
|
|
|
|
|
public function __construct() {
|
|
|
|
|
$this->queries = new CappedMemoryCache(1024);
|
|
|
|
|
}
|
|
|
|
|
|
2014-10-02 19:35:07 -04:00
|
|
|
|
|
|
|
|
/**
|
2017-04-20 05:31:00 -04:00
|
|
|
* @var bool - Module needs to be activated by some app
|
|
|
|
|
*/
|
|
|
|
|
private $activated = false;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @inheritdoc
|
2014-10-02 19:35:07 -04:00
|
|
|
*/
|
2024-03-28 11:13:19 -04:00
|
|
|
public function startQuery($sql, ?array $params = null, ?array $types = null) {
|
2017-04-20 05:31:00 -04:00
|
|
|
if ($this->activated) {
|
|
|
|
|
$this->activeQuery = new Query($sql, $params, microtime(true), $this->getStack());
|
|
|
|
|
}
|
2016-08-24 08:37:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private function getStack() {
|
|
|
|
|
$stack = debug_backtrace();
|
|
|
|
|
array_shift($stack);
|
|
|
|
|
array_shift($stack);
|
|
|
|
|
array_shift($stack);
|
|
|
|
|
return $stack;
|
2014-10-02 19:35:07 -04:00
|
|
|
}
|
|
|
|
|
|
2017-04-20 05:31:00 -04:00
|
|
|
/**
|
|
|
|
|
* @inheritdoc
|
|
|
|
|
*/
|
2014-10-02 19:35:07 -04:00
|
|
|
public function stopQuery() {
|
2017-04-20 05:31:00 -04:00
|
|
|
if ($this->activated && $this->activeQuery) {
|
2014-10-02 19:35:07 -04:00
|
|
|
$this->activeQuery->end(microtime(true));
|
2022-05-03 05:15:24 -04:00
|
|
|
$this->queries[(string)$this->index] = $this->activeQuery;
|
|
|
|
|
$this->index++;
|
2014-10-02 19:35:07 -04:00
|
|
|
$this->activeQuery = null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2017-04-20 05:31:00 -04:00
|
|
|
* @inheritdoc
|
2014-10-02 19:35:07 -04:00
|
|
|
*/
|
|
|
|
|
public function getQueries() {
|
2016-11-03 09:35:44 -04:00
|
|
|
return $this->queries->getData();
|
2014-10-02 19:35:07 -04:00
|
|
|
}
|
2017-04-20 05:31:00 -04:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @inheritdoc
|
|
|
|
|
*/
|
|
|
|
|
public function activate() {
|
|
|
|
|
$this->activated = true;
|
|
|
|
|
}
|
2014-10-02 19:35:07 -04:00
|
|
|
}
|