|
| 1 | +<?php |
| 2 | +declare(strict_types=1); |
| 3 | + |
| 4 | +namespace Flowpack\DecoupledContentStore\Core\Infrastructure; |
| 5 | + |
| 6 | +use Flowpack\DecoupledContentStore\Core\Domain\ValueObject\ContentReleaseIdentifier; |
| 7 | +use Flowpack\DecoupledContentStore\Core\RedisKeyService; |
| 8 | +use Flowpack\DecoupledContentStore\Exception; |
| 9 | +use Neos\Flow\Annotations as Flow; |
| 10 | + |
| 11 | +#[Flow\Scope("singleton")] |
| 12 | +class RedisStatisticsEventService |
| 13 | +{ |
| 14 | + #[Flow\Inject] |
| 15 | + protected RedisClientManager $redisClientManager; |
| 16 | + |
| 17 | + #[Flow\Inject] |
| 18 | + protected RedisKeyService $redisKeyService; |
| 19 | + |
| 20 | + public function addEvent(ContentReleaseIdentifier $contentReleaseIdentifier, string $prefix, string $event, array $additionalPayload): void |
| 21 | + { |
| 22 | + $this->redisClientManager->getPrimaryRedis()->rPush($this->redisKeyService->getRedisKeyForPostfix($contentReleaseIdentifier, 'statisticsEvents'), json_encode([ |
| 23 | + 'event' => $event, |
| 24 | + 'prefix' => $prefix, |
| 25 | + 'additionalPayload' => $additionalPayload, |
| 26 | + ])); |
| 27 | + } |
| 28 | + |
| 29 | + /** |
| 30 | + * @param ContentReleaseIdentifier $contentReleaseIdentifier |
| 31 | + * @param array<string,string> $where |
| 32 | + * @param string[] $groupBy |
| 33 | + * @return array<> |
| 34 | + * @throws Exception |
| 35 | + */ |
| 36 | + public function countEvents( |
| 37 | + ContentReleaseIdentifier $contentReleaseIdentifier, |
| 38 | + array $where, |
| 39 | + array $groupBy, |
| 40 | + ): array |
| 41 | + { |
| 42 | + $redis = $this->redisClientManager->getPrimaryRedis(); |
| 43 | + $key = $this->redisKeyService->getRedisKeyForPostfix($contentReleaseIdentifier, 'statisticsEvents'); |
| 44 | + $chunkSize = 1000; |
| 45 | + |
| 46 | + $countedEvents = []; |
| 47 | + |
| 48 | + $listLength = $redis->lLen($key); |
| 49 | + for ($start = 0; $start < $listLength; $start += $chunkSize) { |
| 50 | + $events = $redis->lRange($key, $start, $start + $chunkSize - 1); |
| 51 | + |
| 52 | + foreach ($events as $eventJson) { |
| 53 | + $event = $this->flatten(json_decode($eventJson, true)); |
| 54 | + if($this->shouldCount($event, $where)) { |
| 55 | + $group = $this->groupValues($event, $groupBy); |
| 56 | + $eventKey = json_encode($group); |
| 57 | + if (array_key_exists($eventKey, $countedEvents)) { |
| 58 | + $countedEvents[$eventKey]['count'] += 1; |
| 59 | + } else { |
| 60 | + $countedEvents[$eventKey] = array_merge(['count' => 1], $group); |
| 61 | + } |
| 62 | + } |
| 63 | + } |
| 64 | + } |
| 65 | + // throw away the keys and sort in _reverse_ order by count |
| 66 | + usort($countedEvents, fn($a, $b) => $b['count'] - $a['count']); |
| 67 | + return $countedEvents; |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * @phpstan-type JSONArray array<string, string|JSONArray> |
| 72 | + *´ |
| 73 | + * @param JSONArray $array |
| 74 | + * @return array<string,string> |
| 75 | + */ |
| 76 | + private function flatten(array $array): array |
| 77 | + { |
| 78 | + $results = []; |
| 79 | + |
| 80 | + foreach ($array as $key => $value) { |
| 81 | + if (is_array($value) && ! empty($value)) { |
| 82 | + foreach ($this->flatten($value) as $subKey => $subValue) { |
| 83 | + $results[$key . '.' . $subKey] = $subValue; |
| 84 | + } |
| 85 | + } else { |
| 86 | + $results[$key] = $value; |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + return $results; |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * @param array<string,string> $event |
| 95 | + * @param array<string,string> $where |
| 96 | + * @return bool |
| 97 | + */ |
| 98 | + private function shouldCount(array $event, array $where): bool |
| 99 | + { |
| 100 | + foreach ($where as $key=>$value) { |
| 101 | + if (!array_key_exists($key, $event) || $event[$key] !== $value) { |
| 102 | + return false; |
| 103 | + } |
| 104 | + } |
| 105 | + return true; |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * @param array<string,string> $event |
| 110 | + * @param string[] $groupedBy |
| 111 | + * @return array<string,string> |
| 112 | + */ |
| 113 | + private function groupValues(array $event, array $groupedBy): array |
| 114 | + { |
| 115 | + $group = []; |
| 116 | + foreach ($groupedBy as $path) { |
| 117 | + $group[$path] = $event[$path] ?? null; |
| 118 | + } |
| 119 | + return $group; |
| 120 | + } |
| 121 | +} |
0 commit comments