FileProfilerStorage.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. <?php
  2. declare(strict_types = 1);
  3. /**
  4. * SPDX-FileCopyrightText: 2022 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OC\Profiler;
  8. use OCP\Profiler\IProfile;
  9. /**
  10. * Storage for profiler using files.
  11. */
  12. class FileProfilerStorage {
  13. // Folder where profiler data are stored.
  14. private string $folder;
  15. /**
  16. * Constructs the file storage using a "dsn-like" path.
  17. *
  18. * Example : "file:/path/to/the/storage/folder"
  19. *
  20. * @throws \RuntimeException
  21. */
  22. public function __construct(string $folder) {
  23. $this->folder = $folder;
  24. if (!is_dir($this->folder) && @mkdir($this->folder, 0777, true) === false && !is_dir($this->folder)) {
  25. throw new \RuntimeException(sprintf('Unable to create the storage directory (%s).', $this->folder));
  26. }
  27. }
  28. public function find(?string $url, ?int $limit, ?string $method, ?int $start = null, ?int $end = null, ?string $statusCode = null): array {
  29. $file = $this->getIndexFilename();
  30. if (!file_exists($file)) {
  31. return [];
  32. }
  33. $file = fopen($file, 'r');
  34. fseek($file, 0, \SEEK_END);
  35. $result = [];
  36. while (\count($result) < $limit && $line = $this->readLineFromFile($file)) {
  37. $values = str_getcsv($line);
  38. [$csvToken, $csvMethod, $csvUrl, $csvTime, $csvParent, $csvStatusCode] = $values;
  39. $csvTime = (int)$csvTime;
  40. if ($url && !str_contains($csvUrl, $url) || $method && !str_contains($csvMethod, $method) || $statusCode && !str_contains($csvStatusCode, $statusCode)) {
  41. continue;
  42. }
  43. if (!empty($start) && $csvTime < $start) {
  44. continue;
  45. }
  46. if (!empty($end) && $csvTime > $end) {
  47. continue;
  48. }
  49. $result[$csvToken] = [
  50. 'token' => $csvToken,
  51. 'method' => $csvMethod,
  52. 'url' => $csvUrl,
  53. 'time' => $csvTime,
  54. 'parent' => $csvParent,
  55. 'status_code' => $csvStatusCode,
  56. ];
  57. }
  58. fclose($file);
  59. return array_values($result);
  60. }
  61. public function purge(): void {
  62. $flags = \FilesystemIterator::SKIP_DOTS;
  63. $iterator = new \RecursiveDirectoryIterator($this->folder, $flags);
  64. $iterator = new \RecursiveIteratorIterator($iterator, \RecursiveIteratorIterator::CHILD_FIRST);
  65. foreach ($iterator as $file) {
  66. $path = $file->getPathname();
  67. if (is_file($path)) {
  68. unlink($path);
  69. } else {
  70. rmdir($path);
  71. }
  72. }
  73. }
  74. public function read(string $token): ?IProfile {
  75. if (!$token || !file_exists($file = $this->getFilename($token))) {
  76. return null;
  77. }
  78. if (\function_exists('gzcompress')) {
  79. $file = 'compress.zlib://' . $file;
  80. }
  81. return $this->createProfileFromData($token, unserialize(file_get_contents($file)));
  82. }
  83. /**
  84. * @throws \RuntimeException
  85. */
  86. public function write(IProfile $profile): bool {
  87. $file = $this->getFilename($profile->getToken());
  88. $profileIndexed = is_file($file);
  89. if (!$profileIndexed) {
  90. // Create directory
  91. $dir = \dirname($file);
  92. if (!is_dir($dir) && @mkdir($dir, 0777, true) === false && !is_dir($dir)) {
  93. throw new \RuntimeException(sprintf('Unable to create the storage directory (%s).', $dir));
  94. }
  95. }
  96. $profileToken = $profile->getToken();
  97. // when there are errors in sub-requests, the parent and/or children tokens
  98. // may equal the profile token, resulting in infinite loops
  99. $parentToken = $profile->getParentToken() !== $profileToken ? $profile->getParentToken() : null;
  100. $childrenToken = array_filter(array_map(function (IProfile $p) use ($profileToken) {
  101. return $profileToken !== $p->getToken() ? $p->getToken() : null;
  102. }, $profile->getChildren()));
  103. // Store profile
  104. $data = [
  105. 'token' => $profileToken,
  106. 'parent' => $parentToken,
  107. 'children' => $childrenToken,
  108. 'data' => $profile->getCollectors(),
  109. 'method' => $profile->getMethod(),
  110. 'url' => $profile->getUrl(),
  111. 'time' => $profile->getTime(),
  112. 'status_code' => $profile->getStatusCode(),
  113. ];
  114. $context = stream_context_create();
  115. if (\function_exists('gzcompress')) {
  116. $file = 'compress.zlib://' . $file;
  117. stream_context_set_option($context, 'zlib', 'level', 3);
  118. }
  119. if (file_put_contents($file, serialize($data), 0, $context) === false) {
  120. return false;
  121. }
  122. if (!$profileIndexed) {
  123. // Add to index
  124. if (false === $file = fopen($this->getIndexFilename(), 'a')) {
  125. return false;
  126. }
  127. fputcsv($file, [
  128. $profile->getToken(),
  129. $profile->getMethod(),
  130. $profile->getUrl(),
  131. $profile->getTime(),
  132. $profile->getParentToken(),
  133. $profile->getStatusCode(),
  134. ]);
  135. fclose($file);
  136. }
  137. return true;
  138. }
  139. /**
  140. * Gets filename to store data, associated to the token.
  141. *
  142. * @return string The profile filename
  143. */
  144. protected function getFilename(string $token): string {
  145. // Uses 4 last characters, because first are mostly the same.
  146. $folderA = substr($token, -2, 2);
  147. $folderB = substr($token, -4, 2);
  148. return $this->folder . '/' . $folderA . '/' . $folderB . '/' . $token;
  149. }
  150. /**
  151. * Gets the index filename.
  152. *
  153. * @return string The index filename
  154. */
  155. protected function getIndexFilename(): string {
  156. return $this->folder . '/index.csv';
  157. }
  158. /**
  159. * Reads a line in the file, backward.
  160. *
  161. * This function automatically skips the empty lines and do not include the line return in result value.
  162. *
  163. * @param resource $file The file resource, with the pointer placed at the end of the line to read
  164. *
  165. * @return ?string A string representing the line or null if beginning of file is reached
  166. */
  167. protected function readLineFromFile($file): ?string {
  168. $line = '';
  169. $position = ftell($file);
  170. if ($position === 0) {
  171. return null;
  172. }
  173. while (true) {
  174. $chunkSize = min($position, 1024);
  175. $position -= $chunkSize;
  176. fseek($file, $position);
  177. if ($chunkSize === 0) {
  178. // bof reached
  179. break;
  180. }
  181. $buffer = fread($file, $chunkSize);
  182. if (false === ($upTo = strrpos($buffer, "\n"))) {
  183. $line = $buffer . $line;
  184. continue;
  185. }
  186. $position += $upTo;
  187. $line = substr($buffer, $upTo + 1) . $line;
  188. fseek($file, max(0, $position), \SEEK_SET);
  189. if ($line !== '') {
  190. break;
  191. }
  192. }
  193. return $line === '' ? null : $line;
  194. }
  195. protected function createProfileFromData(string $token, array $data, ?IProfile $parent = null): IProfile {
  196. $profile = new Profile($token);
  197. $profile->setMethod($data['method']);
  198. $profile->setUrl($data['url']);
  199. $profile->setTime($data['time']);
  200. $profile->setStatusCode($data['status_code']);
  201. $profile->setCollectors($data['data']);
  202. if (!$parent && $data['parent']) {
  203. $parent = $this->read($data['parent']);
  204. }
  205. if ($parent) {
  206. $profile->setParent($parent);
  207. }
  208. foreach ($data['children'] as $token) {
  209. if (!$token || !file_exists($file = $this->getFilename($token))) {
  210. continue;
  211. }
  212. if (\function_exists('gzcompress')) {
  213. $file = 'compress.zlib://' . $file;
  214. }
  215. $profile->addChild($this->createProfileFromData($token, unserialize(file_get_contents($file)), $profile));
  216. }
  217. return $profile;
  218. }
  219. }