@@ -34,88 +34,88 @@ |
||
| 34 | 34 | use OC\SystemConfig; |
| 35 | 35 | |
| 36 | 36 | class Profiler implements IProfiler { |
| 37 | - /** @var array<string, IDataCollector> */ |
|
| 38 | - private array $dataCollectors = []; |
|
| 39 | - |
|
| 40 | - private ?FileProfilerStorage $storage = null; |
|
| 41 | - |
|
| 42 | - private bool $enabled = false; |
|
| 43 | - |
|
| 44 | - public function __construct(SystemConfig $config) { |
|
| 45 | - $this->enabled = $config->getValue('profiler', false); |
|
| 46 | - if ($this->enabled) { |
|
| 47 | - $this->storage = new FileProfilerStorage($config->getValue('datadirectory', \OC::$SERVERROOT . '/data') . '/profiler'); |
|
| 48 | - } |
|
| 49 | - } |
|
| 50 | - |
|
| 51 | - public function add(IDataCollector $dataCollector): void { |
|
| 52 | - $this->dataCollectors[$dataCollector->getName()] = $dataCollector; |
|
| 53 | - } |
|
| 54 | - |
|
| 55 | - public function loadProfileFromResponse(Response $response): ?IProfile { |
|
| 56 | - if (!$token = $response->getHeaders()['X-Debug-Token']) { |
|
| 57 | - return null; |
|
| 58 | - } |
|
| 59 | - |
|
| 60 | - return $this->loadProfile($token); |
|
| 61 | - } |
|
| 62 | - |
|
| 63 | - public function loadProfile(string $token): ?IProfile { |
|
| 64 | - if ($this->storage) { |
|
| 65 | - return $this->storage->read($token); |
|
| 66 | - } else { |
|
| 67 | - return null; |
|
| 68 | - } |
|
| 69 | - } |
|
| 70 | - |
|
| 71 | - public function saveProfile(IProfile $profile): bool { |
|
| 72 | - if ($this->storage) { |
|
| 73 | - return $this->storage->write($profile); |
|
| 74 | - } else { |
|
| 75 | - return false; |
|
| 76 | - } |
|
| 77 | - } |
|
| 78 | - |
|
| 79 | - public function collect(Request $request, Response $response): IProfile { |
|
| 80 | - $profile = new Profile($request->getId()); |
|
| 81 | - $profile->setTime(time()); |
|
| 82 | - $profile->setUrl($request->getRequestUri()); |
|
| 83 | - $profile->setMethod($request->getMethod()); |
|
| 84 | - $profile->setStatusCode($response->getStatus()); |
|
| 85 | - foreach ($this->dataCollectors as $dataCollector) { |
|
| 86 | - $dataCollector->collect($request, $response, null); |
|
| 87 | - |
|
| 88 | - // We clone for subrequests |
|
| 89 | - $profile->addCollector(clone $dataCollector); |
|
| 90 | - } |
|
| 91 | - return $profile; |
|
| 92 | - } |
|
| 93 | - |
|
| 94 | - /** |
|
| 95 | - * @return array[] |
|
| 96 | - */ |
|
| 97 | - public function find(?string $url, ?int $limit, ?string $method, ?int $start, ?int $end, |
|
| 98 | - string $statusCode = null): array { |
|
| 99 | - if ($this->storage) { |
|
| 100 | - return $this->storage->find($url, $limit, $method, $start, $end, $statusCode); |
|
| 101 | - } else { |
|
| 102 | - return []; |
|
| 103 | - } |
|
| 104 | - } |
|
| 105 | - |
|
| 106 | - public function dataProviders(): array { |
|
| 107 | - return array_keys($this->dataCollectors); |
|
| 108 | - } |
|
| 109 | - |
|
| 110 | - public function isEnabled(): bool { |
|
| 111 | - return $this->enabled; |
|
| 112 | - } |
|
| 113 | - |
|
| 114 | - public function setEnabled(bool $enabled): void { |
|
| 115 | - $this->enabled = $enabled; |
|
| 116 | - } |
|
| 117 | - |
|
| 118 | - public function clear(): void { |
|
| 119 | - $this->storage->purge(); |
|
| 120 | - } |
|
| 37 | + /** @var array<string, IDataCollector> */ |
|
| 38 | + private array $dataCollectors = []; |
|
| 39 | + |
|
| 40 | + private ?FileProfilerStorage $storage = null; |
|
| 41 | + |
|
| 42 | + private bool $enabled = false; |
|
| 43 | + |
|
| 44 | + public function __construct(SystemConfig $config) { |
|
| 45 | + $this->enabled = $config->getValue('profiler', false); |
|
| 46 | + if ($this->enabled) { |
|
| 47 | + $this->storage = new FileProfilerStorage($config->getValue('datadirectory', \OC::$SERVERROOT . '/data') . '/profiler'); |
|
| 48 | + } |
|
| 49 | + } |
|
| 50 | + |
|
| 51 | + public function add(IDataCollector $dataCollector): void { |
|
| 52 | + $this->dataCollectors[$dataCollector->getName()] = $dataCollector; |
|
| 53 | + } |
|
| 54 | + |
|
| 55 | + public function loadProfileFromResponse(Response $response): ?IProfile { |
|
| 56 | + if (!$token = $response->getHeaders()['X-Debug-Token']) { |
|
| 57 | + return null; |
|
| 58 | + } |
|
| 59 | + |
|
| 60 | + return $this->loadProfile($token); |
|
| 61 | + } |
|
| 62 | + |
|
| 63 | + public function loadProfile(string $token): ?IProfile { |
|
| 64 | + if ($this->storage) { |
|
| 65 | + return $this->storage->read($token); |
|
| 66 | + } else { |
|
| 67 | + return null; |
|
| 68 | + } |
|
| 69 | + } |
|
| 70 | + |
|
| 71 | + public function saveProfile(IProfile $profile): bool { |
|
| 72 | + if ($this->storage) { |
|
| 73 | + return $this->storage->write($profile); |
|
| 74 | + } else { |
|
| 75 | + return false; |
|
| 76 | + } |
|
| 77 | + } |
|
| 78 | + |
|
| 79 | + public function collect(Request $request, Response $response): IProfile { |
|
| 80 | + $profile = new Profile($request->getId()); |
|
| 81 | + $profile->setTime(time()); |
|
| 82 | + $profile->setUrl($request->getRequestUri()); |
|
| 83 | + $profile->setMethod($request->getMethod()); |
|
| 84 | + $profile->setStatusCode($response->getStatus()); |
|
| 85 | + foreach ($this->dataCollectors as $dataCollector) { |
|
| 86 | + $dataCollector->collect($request, $response, null); |
|
| 87 | + |
|
| 88 | + // We clone for subrequests |
|
| 89 | + $profile->addCollector(clone $dataCollector); |
|
| 90 | + } |
|
| 91 | + return $profile; |
|
| 92 | + } |
|
| 93 | + |
|
| 94 | + /** |
|
| 95 | + * @return array[] |
|
| 96 | + */ |
|
| 97 | + public function find(?string $url, ?int $limit, ?string $method, ?int $start, ?int $end, |
|
| 98 | + string $statusCode = null): array { |
|
| 99 | + if ($this->storage) { |
|
| 100 | + return $this->storage->find($url, $limit, $method, $start, $end, $statusCode); |
|
| 101 | + } else { |
|
| 102 | + return []; |
|
| 103 | + } |
|
| 104 | + } |
|
| 105 | + |
|
| 106 | + public function dataProviders(): array { |
|
| 107 | + return array_keys($this->dataCollectors); |
|
| 108 | + } |
|
| 109 | + |
|
| 110 | + public function isEnabled(): bool { |
|
| 111 | + return $this->enabled; |
|
| 112 | + } |
|
| 113 | + |
|
| 114 | + public function setEnabled(bool $enabled): void { |
|
| 115 | + $this->enabled = $enabled; |
|
| 116 | + } |
|
| 117 | + |
|
| 118 | + public function clear(): void { |
|
| 119 | + $this->storage->purge(); |
|
| 120 | + } |
|
| 121 | 121 | } |