Skip to content

Commit

Permalink
Merge pull request #1730 from cultuurnet/performance-improvement-cano…
Browse files Browse the repository at this point in the history
…nicals

[performance] Improve performance of canonical sorting methods
  • Loading branch information
grubolsch committed Aug 6, 2024
2 parents cc6442c + 44c7a26 commit d477892
Showing 1 changed file with 22 additions and 10 deletions.
32 changes: 22 additions & 10 deletions src/Place/Canonical/CanonicalService.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,30 +68,42 @@ private function getPlacesWithMuseumPasInCluster(array $placeIds): array

private function getPlacesWithMostEvents(array $placeIds): array
{
$eventCountsWithPlaceId = [];
$maxEventCount = -1;
$placesWithMaxEvents = [];

foreach ($placeIds as $placeId) {
$eventCount = count($this->eventRelationsRepository->getEventsLocatedAtPlace($placeId));
if (empty($eventCountsWithPlaceId[$eventCount])) {
$eventCountsWithPlaceId[$eventCount] = [];

if ($eventCount > $maxEventCount) {
$maxEventCount = $eventCount;
$placesWithMaxEvents = [$placeId];
continue;
}

$eventCountsWithPlaceId[$eventCount][] = $placeId;
if ($eventCount === $maxEventCount) {
$placesWithMaxEvents[] = $placeId;
}
}

krsort($eventCountsWithPlaceId);
return $eventCountsWithPlaceId[array_key_first($eventCountsWithPlaceId)];
return $placesWithMaxEvents;
}

private function getOldestPlace(array $placeIds): string
{
$placesByCreationDate = [];
$oldestPlaceId = '';
$oldestDate = null;

foreach ($placeIds as $placeId) {
$jsonDocument = $this->placeRepository->fetch($placeId);
$body = $jsonDocument->getBody();
$placesByCreationDate[$placeId] = $body->created;
$creationDate = strtotime($body->created);

if ($creationDate < $oldestDate || $oldestDate === null) {
$oldestDate = $creationDate;
$oldestPlaceId = $placeId;
}
}

asort($placesByCreationDate);
return array_key_first($placesByCreationDate);
return $oldestPlaceId;
}
}

0 comments on commit d477892

Please sign in to comment.