Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ REDIS_DB=0
REDIS_PASSWORD=

CACHE_DRIVER=file
CACHE_API_RESPONSE_SUMMIT_DATES_WITH_EVENTS_LIFETIME=300

SESSION_DRIVER=redis
SESSION_COOKIE_DOMAIN=
Expand Down
1 change: 0 additions & 1 deletion app/ModelSerializers/Summit/SummitSerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,6 @@ class SummitSerializer extends SilverStripeSerializer
'badge_types',
'badge_features_types',
'badge_access_level_types',
'dates_with_events',
'supported_currencies',
];

Expand Down
82 changes: 56 additions & 26 deletions app/Models/Foundation/Summit/Summit.php
Original file line number Diff line number Diff line change
Expand Up @@ -6024,39 +6024,69 @@ public function getSummitDays(): array
public function getSummitDaysWithEvents(): array
{
$days = $this->getSummitDays();
$list = [];
foreach ($days as $day) {
$begin = clone($day);
$begin = $begin->setTime(0, 0, 0)->setTimezone(new \DateTimeZone('UTC'));
$end = clone($day);
$end = $end->setTime(23, 59, 59)->setTimezone(new \DateTimeZone('UTC'));
$count = 0;
try {
$sql = <<<SQL
SELECT COUNT(SummitEvent.ID) AS QTY
if (count($days) == 0) return [];

$lifetime = intval(Config::get('cache_api_response.summit_dates_with_events_lifetime', 0));
$compute = function () use ($days): array {
return $this->computeSummitDaysWithEvents($days);
};

if ($lifetime <= 0) {
$with_events = $compute();
} else {
// the key carries the summit dates and timezone, so a stale list is never served after they change
$key = sprintf(
"summit_%s_dates_with_events_%s_%s_%s",
$this->id,
preg_replace('/[^A-Za-z0-9]/', '_', strval($this->getTimeZoneId())),
$this->begin_date->getTimestamp(),
$this->end_date->getTimestamp()
);
Comment thread
coderabbitai[bot] marked this conversation as resolved.
$with_events = Cache::remember($key, $lifetime, $compute);
}

return array_values(array_filter($days, function (DateTime $day) use ($with_events) {
return in_array($day->format('Y-m-d'), $with_events);
}));
}

/**
* Single query over the published events, the per day overlap test runs in PHP
* @param DateTime[] $days summit days (summit timezone)
* @return string[] Y-m-d of the days that have at least one published event
*/
private function computeSummitDaysWithEvents(array $days): array
{
$sql = <<<SQL
SELECT SummitEvent.StartDate, SummitEvent.EndDate
FROM SummitEvent
WHERE
SummitEvent.SummitID = :summit_id
AND SummitEvent.Published = 1
AND (SummitEvent.StartDate <= :end and SummitEvent.EndDate >= :begin)
SQL;
$stmt = $this->prepareRawSQL($sql, [
'summit_id' => $this->id,
'begin' => $begin->format("Y-m-d H:i:s"),
'end' => $end->format('Y-m-d H:i:s'),
]);
$res = $stmt->executeQuery();
$res = $res->fetchFirstColumn();
$count = count($res) > 0 ? $res[0] : 0;
} catch (\Exception $ex) {
Log::debug($ex);
$count = 0;
}
if ($count > 0) {
$list[] = $day;
$stmt = $this->prepareRawSQL($sql, ['summit_id' => $this->id]);
$events = $stmt->executeQuery()->fetchAllAssociative();

$list = [];
foreach ($days as $day) {
$begin = clone($day);
$begin = $begin->setTime(0, 0, 0)->setTimezone(new \DateTimeZone('UTC'))->format("Y-m-d H:i:s");
$end = clone($day);
$end = $end->setTime(23, 59, 59)->setTimezone(new \DateTimeZone('UTC'))->format("Y-m-d H:i:s");
foreach ($events as $event) {
// UTC "Y-m-d H:i:s" strings, same overlap test the SQL used to run
// a published event may lack dates, SQL never matched those rows (NULL comparison)
if (
$event['StartDate'] !== null &&
$event['EndDate'] !== null &&
$event['StartDate'] <= $end &&
$event['EndDate'] >= $begin
) {
$list[] = $day->format('Y-m-d');
break;
}
}
}

return $list;
}

Expand Down
1 change: 1 addition & 0 deletions config/cache_api_response.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@
'get_summits_response_lifetime' => env('CACHE_API_RESPONSE_GET_SUMMITS_LIFETIME', 600),
'file_url_lifetime' => env('CACHE_API_RESPONSE_FILE_URL_LIFETIME', 3600),
'event_has_access_lifetime' => env('CACHE_API_RESPONSE_EVENT_HAS_ACCESS_LIFETIME', 600),
'summit_dates_with_events_lifetime' => env('CACHE_API_RESPONSE_SUMMIT_DATES_WITH_EVENTS_LIFETIME', 300),
];
142 changes: 142 additions & 0 deletions tests/SummitDaysWithEventsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
<?php namespace Tests;
/**
* Copyright 2026 OpenStack Foundation
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/

use App\Models\Foundation\Main\IGroup;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\DB;
use models\summit\Summit;

/**
* Summit::getSummitDaysWithEvents runs one query per summit and applies the per day
* overlap test in PHP, the result must match the former one COUNT per day implementation.
*
* Class SummitDaysWithEventsTest
*/
final class SummitDaysWithEventsTest extends TestCase
{
use InsertSummitTestData;

use InsertMemberTestData;

protected function setUp(): void
{
parent::setUp();
Config::set('cache_api_response.summit_dates_with_events_lifetime', 0);
self::insertMemberTestData(IGroup::TrackChairs);
self::$defaultMember = self::$member;
self::insertSummitTestData();
}

protected function tearDown(): void
{
self::clearSummitTestData();
self::clearMemberTestData();
parent::tearDown();
}

/**
* the former implementation, one COUNT per summit day
* @return string[]
*/
private function legacyDaysWithEvents(Summit $summit): array
{
$res = [];
foreach ($summit->getSummitDays() as $day) {
$begin = (clone $day)->setTime(0, 0, 0)->setTimezone(new \DateTimeZone('UTC'));
$end = (clone $day)->setTime(23, 59, 59)->setTimezone(new \DateTimeZone('UTC'));
$count = DB::selectOne(
"SELECT COUNT(ID) AS QTY FROM SummitEvent WHERE SummitID = ? AND Published = 1 AND StartDate <= ? AND EndDate >= ?",
[$summit->getId(), $end->format('Y-m-d H:i:s'), $begin->format('Y-m-d H:i:s')]
)->QTY;
if ($count > 0) $res[] = $day->format('Y-m-d');
}
return $res;
}

private function daysWithEvents(Summit $summit): array
{
return array_map(function ($day) {
return $day->format('Y-m-d');
}, $summit->getSummitDaysWithEvents());
}

public function testMatchesPerDayImplementation(): void
{
$expected = $this->legacyDaysWithEvents(self::$summit);
$this->assertNotEmpty($expected);
$this->assertSame($expected, $this->daysWithEvents(self::$summit));
}

public function testEventCrossingDaysCountsOnBothDaysAndDaysWithoutEventsAreSkipped(): void
{
$summit_id = self::$summit->getId();
DB::update("UPDATE SummitEvent SET Published = 0 WHERE SummitID = ?", [$summit_id]);
$days = self::$summit->getSummitDays();

// one event from the evening of day 2 to the morning of day 3, summit timezone
$start = (clone $days[1])->setTime(22, 0, 0)->setTimezone(new \DateTimeZone('UTC'));
$end = (clone $days[2])->setTime(2, 0, 0)->setTimezone(new \DateTimeZone('UTC'));
$id = DB::selectOne("SELECT ID FROM SummitEvent WHERE SummitID = ? LIMIT 1", [$summit_id])->ID;
DB::update(
"UPDATE SummitEvent SET Published = 1, StartDate = ?, EndDate = ? WHERE ID = ?",
[$start->format('Y-m-d H:i:s'), $end->format('Y-m-d H:i:s'), $id]
);

$expected = [$days[1]->format('Y-m-d'), $days[2]->format('Y-m-d')];
$this->assertSame($expected, $this->daysWithEvents(self::$summit));
$this->assertSame($expected, $this->legacyDaysWithEvents(self::$summit));
}

public function testPublishedEventsWithoutDatesAreIgnored(): void
{
$summit_id = self::$summit->getId();
DB::update("UPDATE SummitEvent SET Published = 0 WHERE SummitID = ?", [$summit_id]);
$days = self::$summit->getSummitDays();
$end = (clone $days[2])->setTime(2, 0, 0)->setTimezone(new \DateTimeZone('UTC'));
$id = DB::selectOne("SELECT ID FROM SummitEvent WHERE SummitID = ? LIMIT 1", [$summit_id])->ID;
DB::update(
"UPDATE SummitEvent SET Published = 1, StartDate = NULL, EndDate = ? WHERE ID = ?",
[$end->format('Y-m-d H:i:s'), $id]
);

$this->assertSame([], $this->legacyDaysWithEvents(self::$summit));
$this->assertSame([], $this->daysWithEvents(self::$summit));
}

public function testRunsASingleSelectRegardlessOfTheNumberOfDays(): void
{
$this->assertGreaterThan(5, count(self::$summit->getSummitDays()));
$conn = self::$em->getConnection();
$selects = function () use ($conn): int {
return intval($conn->fetchAssociative("SHOW SESSION STATUS LIKE 'Com_select'")['Value']);
};
$before = $selects();
$this->daysWithEvents(self::$summit);
$this->assertSame(1, $selects() - $before);
}

public function testCachedListIsServedWithinTheLifetime(): void
{
Config::set('cache_api_response.summit_dates_with_events_lifetime', 300);
Cache::flush();
$first = $this->daysWithEvents(self::$summit);
$this->assertNotEmpty($first);
DB::update("UPDATE SummitEvent SET Published = 0 WHERE SummitID = ?", [self::$summit->getId()]);
$this->assertSame($first, $this->daysWithEvents(self::$summit));
Config::set('cache_api_response.summit_dates_with_events_lifetime', 0);
$this->assertSame([], $this->daysWithEvents(self::$summit));
Cache::flush();
}
}
Loading