Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Naos-14.14.0-PLAT-9670 - improve blackout conflict performance #8109

Merged
merged 2 commits into from
Feb 13, 2019
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -149,11 +149,37 @@ public function getListResponse(KalturaFilterPager $pager, KalturaDetachedRespon
$filter->attachToCriteria($c);
$pager->attachToCriteria($c);

$list = ScheduleEventPeer::doSelect($c);
$eventsList = ScheduleEventPeer::doSelect($c);
if(count($eventsList) && $type != ScheduleEventType::BLACKOUT)
{
$this->loadSessionBlackoutEvents($eventsList);
}

$response = new KalturaScheduleEventListResponse();
$response->objects = KalturaScheduleEventArray::fromDbArray($list, $responseProfile);
$response->objects = KalturaScheduleEventArray::fromDbArray($eventsList, $responseProfile);
$response->totalCount = $c->getRecordsCount();
return $response;
}

protected function loadSessionBlackoutEvents($eventsList)
{
$startDate = PHP_INT_MAX;
$endDate = 0;
foreach ($eventsList as $event)
{
$eventStartDate = $event->getStartDate('U');
$eventEndDate = $event->getEndDate('U');
if ($eventStartDate < $startDate)
{
$startDate = $eventStartDate;
}

if ($eventEndDate > $endDate)
{
$endDate = $eventEndDate;
}
}

ScheduleEventPeer::retrieveBlackoutEventsByDateWindow($startDate, $endDate);
}
}
43 changes: 41 additions & 2 deletions plugins/schedule/base/lib/model/ScheduleEventPeer.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,12 @@ class ScheduleEventPeer extends BaseScheduleEventPeer implements IRelatedObjectP
const LIVE_STREAM_OM_CLASS = 'LiveStreamScheduleEvent';
const RECORD_OM_CLASS = 'RecordScheduleEvent';
const BLACKOUT_OM_CLASS = 'BlackoutScheduleEvent';

const BLACKOUT_SESSION_CACHE_START_DATE = 'start_date';
const BLACKOUT_SESSION_CACHE_END_DATE = 'end_date';
const BLACKOUT_SESSION_CACHE_RESULT = 'result';

protected static $blackoutSessionCache = array();

// cache classes by their type
protected static $class_types_cache = array(
ScheduleEventType::LIVE_STREAM => self::LIVE_STREAM_OM_CLASS,
Expand Down Expand Up @@ -248,9 +253,43 @@ public static function retrieveEventsByResourceIdsAndDateWindow($resourceIds, $s
*/
public static function retrieveBlackoutEventsByDateWindow($startDate, $endDate, $scheduleEventIdToIgnore = null)
{
$cacheResult = self::tryGetBlackoutResultFromSessionCache($startDate, $endDate);
if(isset($cacheResult))
{
return $cacheResult;
}

$c = self::getRetrieveEventsByDateWindowCriteria($startDate, $endDate, $scheduleEventIdToIgnore);
$c->addAnd(ScheduleEventPeer::TYPE, scheduleEventType::BLACKOUT, Criteria::EQUAL);
return self::doSelect($c);
$result = self::doSelect($c);
self::addBlackoutResultToCache($startDate, $endDate, $result);
return $result;
}

protected static function tryGetBlackoutResultFromSessionCache($startDate, $endDate)
{
if(self::$blackoutSessionCache && self::$blackoutSessionCache[self::BLACKOUT_SESSION_CACHE_START_DATE]
<= $startDate && self::$blackoutSessionCache[self::BLACKOUT_SESSION_CACHE_END_DATE] >= $endDate)
{
$result = array();
foreach (self::$blackoutSessionCache[self::BLACKOUT_SESSION_CACHE_RESULT] as $event)
{
if($event->getStartDate('U') <= $endDate && $eventEndDate = $event->getEndDate('U') >= $startDate)
{
$result[] = $event;
}
}

return $result;
}

return null;
}

protected static function addBlackoutResultToCache($startDate, $endDate, $result)
{
self::$blackoutSessionCache = array(self::BLACKOUT_SESSION_CACHE_START_DATE => $startDate,
self::BLACKOUT_SESSION_CACHE_END_DATE => $endDate, self::BLACKOUT_SESSION_CACHE_RESULT => $result);
}

/**
Expand Down