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 1 commit
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 @@ -150,10 +150,36 @@ public function getListResponse(KalturaFilterPager $pager, KalturaDetachedRespon
$pager->attachToCriteria($c);

$list = ScheduleEventPeer::doSelect($c);
ZurPHP marked this conversation as resolved.
Show resolved Hide resolved
if(Count($list) && $type != ScheduleEventType::BLACKOUT)
ZurPHP marked this conversation as resolved.
Show resolved Hide resolved
{
$this->handleBlackoutEventConflictsForList($list);
ZurPHP marked this conversation as resolved.
Show resolved Hide resolved
}

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

protected function handleBlackoutEventConflictsForList($list)
ZurPHP marked this conversation as resolved.
Show resolved Hide resolved
{
$startDate = PHP_INT_MAX;
$endDate = 0;
foreach ($list as $event)
{
$eventStartDate = $event->getStartDate('U');
$eventEndDate = $event->getEndDate('U');
if ($eventStartDate < $startDate)
{
$startDate = $eventStartDate;
}

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

ScheduleEventPeer::retrieveBlackoutEventsByDateWindow($startDate, $endDate);
}
}
56 changes: 54 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_CACHE_START_DATE = 'start_date';
const BLACKOUT_CACHE_END_DATE = 'end_date';
const BLACKOUT_CACHE_RESULT = 'result';

protected static $blackoutCache = 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,56 @@ public static function retrieveEventsByResourceIdsAndDateWindow($resourceIds, $s
*/
public static function retrieveBlackoutEventsByDateWindow($startDate, $endDate, $scheduleEventIdToIgnore = null)
{
$cacheResult = self::tryGetBlackoutResultFromCache($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 tryGetBlackoutResultFromCache($startDate, $endDate)
{
$partnerId = kCurrentContext::getCurrentPartnerId();
ZurPHP marked this conversation as resolved.
Show resolved Hide resolved
if($partnerId)
{
if(isset(self::$blackoutCache[$partnerId]))
{
$cacheResult = self::$blackoutCache[$partnerId];
if($cacheResult[self::BLACKOUT_CACHE_START_DATE] <= $startDate && $cacheResult[self::BLACKOUT_CACHE_END_DATE] >= $endDate)
{
$result = array();
foreach ($cacheResult[self::BLACKOUT_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)
{
$partnerId = kCurrentContext::getCurrentPartnerId();
if($partnerId)
{
$cacheResult = array(self::BLACKOUT_CACHE_START_DATE => $startDate,
self::BLACKOUT_CACHE_END_DATE => $endDate,
self::BLACKOUT_CACHE_RESULT => $result);
self::$blackoutCache[$partnerId] = $cacheResult;
}
}

/**
Expand Down