|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Meilisearch\Bundle\Debug; |
| 6 | + |
| 7 | +use Doctrine\Persistence\ObjectManager; |
| 8 | +use Meilisearch\Bundle\Collection; |
| 9 | +use Meilisearch\Bundle\SearchService; |
| 10 | +use Symfony\Component\Stopwatch\Stopwatch; |
| 11 | + |
| 12 | +/** |
| 13 | + * @author Antoine Makdessi <[email protected]> |
| 14 | + */ |
| 15 | +final class TraceableMeilisearchService implements SearchService |
| 16 | +{ |
| 17 | + private SearchService $searchService; |
| 18 | + private Stopwatch $stopwatch; |
| 19 | + private array $data = []; |
| 20 | + |
| 21 | + public function __construct(SearchService $searchService, Stopwatch $stopwatch) |
| 22 | + { |
| 23 | + $this->searchService = $searchService; |
| 24 | + $this->stopwatch = $stopwatch; |
| 25 | + } |
| 26 | + |
| 27 | + public function index(ObjectManager $objectManager, $searchable): array |
| 28 | + { |
| 29 | + $this->stopwatch->start(__METHOD__); |
| 30 | + |
| 31 | + $index = $this->searchService->index(...\func_get_args()); |
| 32 | + |
| 33 | + $event = $this->stopwatch->stop(__METHOD__); |
| 34 | + |
| 35 | + $this->data[__FUNCTION__] = [ |
| 36 | + '_params' => [ |
| 37 | + '$searchable' => $searchable, |
| 38 | + ], |
| 39 | + '_results' => $index, |
| 40 | + '_duration' => $event->getDuration(), |
| 41 | + '_memory' => $event->getMemory(), |
| 42 | + ]; |
| 43 | + |
| 44 | + return $index; |
| 45 | + } |
| 46 | + |
| 47 | + public function remove(ObjectManager $objectManager, $searchable): array |
| 48 | + { |
| 49 | + $this->stopwatch->start(__METHOD__); |
| 50 | + |
| 51 | + $remove = $this->searchService->remove(...\func_get_args()); |
| 52 | + |
| 53 | + $event = $this->stopwatch->stop(__METHOD__); |
| 54 | + |
| 55 | + $this->data[__FUNCTION__] = [ |
| 56 | + '_params' => [ |
| 57 | + '$searchable' => $searchable, |
| 58 | + ], |
| 59 | + '_results' => $remove, |
| 60 | + '_duration' => $event->getDuration(), |
| 61 | + '_memory' => $event->getMemory(), |
| 62 | + ]; |
| 63 | + |
| 64 | + return $remove; |
| 65 | + } |
| 66 | + |
| 67 | + public function clear(string $className): array |
| 68 | + { |
| 69 | + $this->stopwatch->start(__METHOD__); |
| 70 | + |
| 71 | + $clear = $this->searchService->clear(...\func_get_args()); |
| 72 | + |
| 73 | + $event = $this->stopwatch->stop(__METHOD__); |
| 74 | + |
| 75 | + $this->data[__FUNCTION__] = [ |
| 76 | + '_params' => [ |
| 77 | + '$className' => $className, |
| 78 | + ], |
| 79 | + '_results' => $clear, |
| 80 | + '_duration' => $event->getDuration(), |
| 81 | + '_memory' => $event->getMemory(), |
| 82 | + ]; |
| 83 | + |
| 84 | + return $clear; |
| 85 | + } |
| 86 | + |
| 87 | + public function deleteByIndexName(string $indexName): ?array |
| 88 | + { |
| 89 | + $this->stopwatch->start(__METHOD__); |
| 90 | + |
| 91 | + $deleteByIndexName = $this->searchService->deleteByIndexName(...\func_get_args()); |
| 92 | + |
| 93 | + $event = $this->stopwatch->stop(__METHOD__); |
| 94 | + |
| 95 | + $this->data[__FUNCTION__] = [ |
| 96 | + '_params' => [ |
| 97 | + '$indexName' => $indexName, |
| 98 | + ], |
| 99 | + '_results' => $deleteByIndexName, |
| 100 | + '_duration' => $event->getDuration(), |
| 101 | + '_memory' => $event->getMemory(), |
| 102 | + ]; |
| 103 | + |
| 104 | + return $deleteByIndexName; |
| 105 | + } |
| 106 | + |
| 107 | + public function delete(string $className): ?array |
| 108 | + { |
| 109 | + $this->stopwatch->start(__METHOD__); |
| 110 | + |
| 111 | + $delete = $this->searchService->delete(...\func_get_args()); |
| 112 | + |
| 113 | + $event = $this->stopwatch->stop(__METHOD__); |
| 114 | + |
| 115 | + $this->data[__FUNCTION__] = [ |
| 116 | + '_params' => [ |
| 117 | + '$className' => $className, |
| 118 | + ], |
| 119 | + '_results' => $delete, |
| 120 | + '_duration' => $event->getDuration(), |
| 121 | + '_memory' => $event->getMemory(), |
| 122 | + ]; |
| 123 | + |
| 124 | + return $delete; |
| 125 | + } |
| 126 | + |
| 127 | + public function search(ObjectManager $objectManager, string $className, string $query = '', array $searchParams = []): array |
| 128 | + { |
| 129 | + $this->stopwatch->start(__METHOD__); |
| 130 | + |
| 131 | + $search = $this->searchService->search(...\func_get_args()); |
| 132 | + |
| 133 | + $event = $this->stopwatch->stop(__METHOD__); |
| 134 | + |
| 135 | + $this->data[__FUNCTION__] = [ |
| 136 | + '_params' => [ |
| 137 | + '$className' => $className, |
| 138 | + '$query' => $query, |
| 139 | + '$searchParams' => $searchParams, |
| 140 | + ], |
| 141 | + '_results' => $search, |
| 142 | + '_duration' => $event->getDuration(), |
| 143 | + '_memory' => $event->getMemory(), |
| 144 | + ]; |
| 145 | + |
| 146 | + return $search; |
| 147 | + } |
| 148 | + |
| 149 | + public function rawSearch(string $className, string $query = '', array $searchParams = []): array |
| 150 | + { |
| 151 | + $this->stopwatch->start(__METHOD__); |
| 152 | + |
| 153 | + $rawSearch = $this->searchService->rawSearch(...\func_get_args()); |
| 154 | + |
| 155 | + $event = $this->stopwatch->stop(__METHOD__); |
| 156 | + |
| 157 | + $this->data[__FUNCTION__] = [ |
| 158 | + '_params' => [ |
| 159 | + '$className' => $className, |
| 160 | + '$query' => $query, |
| 161 | + '$searchParams' => $searchParams, |
| 162 | + ], |
| 163 | + '_results' => $rawSearch, |
| 164 | + '_duration' => $event->getDuration(), |
| 165 | + '_memory' => $event->getMemory(), |
| 166 | + ]; |
| 167 | + |
| 168 | + return $rawSearch; |
| 169 | + } |
| 170 | + |
| 171 | + public function count(string $className, string $query = '', array $searchParams = []): int |
| 172 | + { |
| 173 | + $this->stopwatch->start(__METHOD__); |
| 174 | + |
| 175 | + $count = $this->searchService->count(...\func_get_args()); |
| 176 | + |
| 177 | + $event = $this->stopwatch->stop(__METHOD__); |
| 178 | + |
| 179 | + $this->data[__FUNCTION__] = [ |
| 180 | + '_params' => [ |
| 181 | + '$className' => $className, |
| 182 | + '$query' => $query, |
| 183 | + '$searchParams' => $searchParams, |
| 184 | + ], |
| 185 | + '_results' => $count, |
| 186 | + '_duration' => $event->getDuration(), |
| 187 | + '_memory' => $event->getMemory(), |
| 188 | + ]; |
| 189 | + |
| 190 | + return $count; |
| 191 | + } |
| 192 | + |
| 193 | + public function isSearchable($className): bool |
| 194 | + { |
| 195 | + return $this->searchService->isSearchable($className); |
| 196 | + } |
| 197 | + |
| 198 | + public function getSearchable(): array |
| 199 | + { |
| 200 | + return $this->searchService->getSearchable(); |
| 201 | + } |
| 202 | + |
| 203 | + public function getConfiguration(): Collection |
| 204 | + { |
| 205 | + return $this->searchService->getConfiguration(); |
| 206 | + } |
| 207 | + |
| 208 | + public function searchableAs(string $className): string |
| 209 | + { |
| 210 | + return $this->searchService->searchableAs($className); |
| 211 | + } |
| 212 | + |
| 213 | + /** @internal */ |
| 214 | + public function getData(): array |
| 215 | + { |
| 216 | + return $this->data; |
| 217 | + } |
| 218 | +} |
0 commit comments