CC-3331: Optimize "Now Playing" to load in less than 1/2 second

- Reorganized the now playing to find all the scheduled items within two time frames instead of in a loop per show id.
- I've cut the time in half
This commit is contained in:
Daniel 2012-02-17 16:47:12 -05:00
parent 6d71ec8c9c
commit 5cb99e9532
3 changed files with 92 additions and 157 deletions

View file

@ -3,16 +3,16 @@
class Application_Model_Nowplaying class Application_Model_Nowplaying
{ {
private static function CreateHeaderRow($p_showName, $p_showStart, $p_showEnd){ private static function CreateHeaderRow($p_showName, $p_showStart, $p_showEnd){
return array("h", "", $p_showStart, $p_showEnd, $p_showName, "", "", "", "", "", ""); return array("h", "", $p_showStart, $p_showEnd, $p_showName, "", "", "", "", "", "");
} }
private static function CreateDatatableRows($p_dbRows){ private static function CreateDatatableRows($p_dbRows){
$dataTablesRows = array(); $dataTablesRows = array();
$epochNow = time(); $epochNow = time();
foreach ($p_dbRows as $dbRow){ foreach ($p_dbRows as $dbRow) {
$showStartDateTime = Application_Model_DateHelper::ConvertToLocalDateTime($dbRow['show_starts']); $showStartDateTime = Application_Model_DateHelper::ConvertToLocalDateTime($dbRow['show_starts']);
$showEndDateTime = Application_Model_DateHelper::ConvertToLocalDateTime($dbRow['show_ends']); $showEndDateTime = Application_Model_DateHelper::ConvertToLocalDateTime($dbRow['show_ends']);
@ -43,24 +43,24 @@ class Application_Model_Nowplaying
$dbRow['playlist_name'], $dbRow['show_name'], $status); $dbRow['playlist_name'], $dbRow['show_name'], $status);
} }
return $dataTablesRows; return $dataTablesRows;
} }
private static function CreateGapRow($p_gapTime){ private static function CreateGapRow($p_gapTime){
return array("g", "", "", "", $p_gapTime, "", "", "", "", "", ""); return array("g", "", "", "", $p_gapTime, "", "", "", "", "", "");
} }
private static function CreateRecordingRow($p_showInstance){ private static function CreateRecordingRow($p_showInstance){
return array("r", "", "", "", $p_showInstance->getName(), "", "", "", "", "", ""); return array("r", "", "", "", $p_showInstance->getName(), "", "", "", "", "", "");
} }
public static function GetDataGridData($viewType, $dateString){ public static function GetDataGridData($viewType, $dateString){
if ($viewType == "now"){ if ($viewType == "now"){
$dateTime = new DateTime("now", new DateTimeZone("UTC")); $dateTime = new DateTime("now", new DateTimeZone("UTC"));
$timeNow = $dateTime->format("Y-m-d H:i:s"); $timeNow = $dateTime->format("Y-m-d H:i:s");
$startCutoff = 60; $startCutoff = 60;
$endCutoff = 86400; //60*60*24 - seconds in a day $endCutoff = 86400; //60*60*24 - seconds in a day
} else { } else {
$date = new Application_Model_DateHelper; $date = new Application_Model_DateHelper;
@ -76,20 +76,21 @@ class Application_Model_Nowplaying
$showIds = Application_Model_ShowInstance::GetShowsInstancesIdsInRange($timeNow, $startCutoff, $endCutoff); $showIds = Application_Model_ShowInstance::GetShowsInstancesIdsInRange($timeNow, $startCutoff, $endCutoff);
//get all the pieces to be played between the start cut off and the end cut off. //get all the pieces to be played between the start cut off and the end cut off.
$scheduledItems = Application_Model_ShowInstance::getScheduleItemsForAllShowsInRange($timeNow, $startCutoff, $endCutoff); $scheduledItems = Application_Model_Schedule::getScheduleItemsInRange($timeNow, $startCutoff, $endCutoff);
$orderedScheduledItems;
foreach ($scheduledItems as $scheduledItem){ $orderedScheduledItems;
$orderedScheduledItems[$scheduledItem['instance_id']][] = $scheduledItem; foreach ($scheduledItems as $scheduledItem){
} $orderedScheduledItems[$scheduledItem['instance_id']][] = $scheduledItem;
}
foreach ($showIds as $showId){ foreach ($showIds as $showId){
$instanceId = $showId['id']; $instanceId = $showId['id'];
//gets the show information //gets the show information
$si = new Application_Model_ShowInstance($instanceId); $si = new Application_Model_ShowInstance($instanceId);
$showId = $si->getShowId(); $showId = $si->getShowId();
$show = new Application_Model_Show($showId); $show = new Application_Model_Show($showId);
$showStartDateTime = Application_Model_DateHelper::ConvertToLocalDateTime($si->getShowInstanceStart()); $showStartDateTime = Application_Model_DateHelper::ConvertToLocalDateTime($si->getShowInstanceStart());
@ -106,73 +107,9 @@ class Application_Model_Nowplaying
//append show gap time row //append show gap time row
$gapTime = self::FormatDuration($si->getShowEndGapTime(), true); $gapTime = self::FormatDuration($si->getShowEndGapTime(), true);
if ($si->isRecorded()) if ($si->isRecorded())
$data[] = self::CreateRecordingRow($si); $data[] = self::CreateRecordingRow($si);
else if ($gapTime > 0) else if ($gapTime > 0)
$data[] = self::CreateGapRow($gapTime); $data[] = self::CreateGapRow($gapTime);
}
$rows = Application_Model_Show::GetCurrentShow($timeNow);
Application_Model_Show::ConvertToLocalTimeZone($rows, array("starts", "ends", "start_timestamp", "end_timestamp"));
return array("currentShow"=>$rows, "rows"=>$data);
}
public static function GetDataGridDataOLD($viewType, $dateString){
if ($viewType == "now"){
$dateTime = new DateTime("now", new DateTimeZone("UTC"));
$timeNow = $dateTime->format("Y-m-d H:i:s");
$startCutoff = 60;
//$endCutoff = 2592000; // 30 days
//$endCutoff = 604800; // 7 days
$endCutoff = 259200; // 3 days
//$endCutoff = 86400; //60*60*24 - seconds in a day
} else {
$date = new Application_Model_DateHelper;
$time = $date->getTime();
$date->setDate($dateString." ".$time);
$timeNow = $date->getUtcTimestamp();
$startCutoff = $date->getNowDayStartDiff();
$endCutoff = $date->getNowDayEndDiff();
}
$data = array();
$showIds = Application_Model_ShowInstance::GetShowsInstancesIdsInRange($timeNow, $startCutoff, $endCutoff);
//Logging::log(print_r($showIds, true));
foreach ($showIds as $showId){
$instanceId = $showId['id'];
//gets the show information
$si = new Application_Model_ShowInstance($instanceId);
//Logging::log(print_r($si, true));
$showId = $si->getShowId();
$show = new Application_Model_Show($showId);
//Logging::log(print_r($show, true));
$showStartDateTime = Application_Model_DateHelper::ConvertToLocalDateTime($si->getShowInstanceStart());
$showEndDateTime = Application_Model_DateHelper::ConvertToLocalDateTime($si->getShowInstanceEnd());
//append show header row
$data[] = self::CreateHeaderRow($show->getName(), $showStartDateTime->format("Y-m-d H:i:s"), $showEndDateTime->format("Y-m-d H:i:s"));
$scheduledItems = $si->getScheduleItemsInRange($timeNow, $startCutoff, $endCutoff);
//Logging::log(print_r($scheduledItems, true));
$dataTablesRows = self::CreateDatatableRows($scheduledItems);
//Logging::log(print_r($dataTablesRows, true));
//append show audio item rows
$data = array_merge($data, $dataTablesRows);
//append show gap time row
$gapTime = self::FormatDuration($si->getShowEndGapTime(), true);
if ($si->isRecorded())
$data[] = self::CreateRecordingRow($si);
else if ($gapTime > 0)
$data[] = self::CreateGapRow($gapTime);
} }
$rows = Application_Model_Show::GetCurrentShow($timeNow); $rows = Application_Model_Show::GetCurrentShow($timeNow);

View file

@ -276,24 +276,59 @@ class Application_Model_Schedule {
." WHERE st.starts < si.ends"; ." WHERE st.starts < si.ends";
if ($timePeriod < 0){ if ($timePeriod < 0){
$sql .= " AND st.ends < TIMESTAMP '$timeStamp'" $sql .= " AND st.ends < TIMESTAMP '$timeStamp'"
." AND st.ends > (TIMESTAMP '$timeStamp' - INTERVAL '$interval')" ." AND st.ends > (TIMESTAMP '$timeStamp' - INTERVAL '$interval')"
." ORDER BY st.starts DESC" ." ORDER BY st.starts DESC"
." LIMIT $count"; ." LIMIT $count";
} else if ($timePeriod == 0){ } else if ($timePeriod == 0){
$sql .= " AND st.starts <= TIMESTAMP '$timeStamp'" $sql .= " AND st.starts <= TIMESTAMP '$timeStamp'"
." AND st.ends >= TIMESTAMP '$timeStamp'"; ." AND st.ends >= TIMESTAMP '$timeStamp'";
} else if ($timePeriod > 0){ } else if ($timePeriod > 0){
$sql .= " AND st.starts > TIMESTAMP '$timeStamp'" $sql .= " AND st.starts > TIMESTAMP '$timeStamp'"
." AND st.starts < (TIMESTAMP '$timeStamp' + INTERVAL '$interval')" ." AND st.starts < (TIMESTAMP '$timeStamp' + INTERVAL '$interval')"
." ORDER BY st.starts" ." ORDER BY st.starts"
." LIMIT $count"; ." LIMIT $count";
} }
$rows = $CC_DBC->GetAll($sql); $rows = $CC_DBC->GetAll($sql);
return $rows; return $rows;
} }
public static function getScheduleItemsInRange($timeNow, $start, $end)
{
global $CC_DBC, $CC_CONFIG;
$sql = "SELECT"
." si.starts as show_starts,"
." si.ends as show_ends,"
." si.rebroadcast as rebroadcast,"
." st.starts as item_starts,"
." st.ends as item_ends,"
." st.clip_length as clip_length,"
." ft.track_title as track_title,"
." ft.artist_name as artist_name,"
." ft.album_title as album_title,"
." s.name as show_name,"
." si.id as instance_id,"
." pt.name as playlist_name"
." FROM ".$CC_CONFIG["showInstances"]." si"
." LEFT JOIN ".$CC_CONFIG[scheduleTable]." st"
." ON st.instance_id = si.id"
." LEFT JOIN ".$CC_CONFIG[playListTable]." pt"
." ON st.playlist_id = pt.id"
." LEFT JOIN ".$CC_CONFIG[filesTable]." ft"
." ON st.file_id = ft.id"
." LEFT JOIN ".$CC_CONFIG[showTable]." s"
." ON si.show_id = s.id"
." WHERE ((si.starts < TIMESTAMP '$timeNow' - INTERVAL '$start seconds' AND si.ends > TIMESTAMP '$timeNow' - INTERVAL '$start seconds')"
." OR (si.starts > TIMESTAMP '$timeNow' - INTERVAL '$start seconds' AND si.ends < TIMESTAMP '$timeNow' + INTERVAL '$end seconds')"
." OR (si.starts < TIMESTAMP '$timeNow' + INTERVAL '$end seconds' AND si.ends > TIMESTAMP '$timeNow' + INTERVAL '$end seconds'))"
." AND (st.starts < si.ends)"
." ORDER BY si.id, si.starts, st.starts";
return $CC_DBC->GetAll($sql);
}
public static function GetShowInstanceItems($instance_id) public static function GetShowInstanceItems($instance_id)
{ {
global $CC_CONFIG, $CC_DBC; global $CC_CONFIG, $CC_DBC;
@ -322,7 +357,7 @@ class Application_Model_Schedule {
} }
public static function getSchduledPlaylistCount(){ public static function getSchduledPlaylistCount(){
global $CC_CONFIG, $CC_DBC; global $CC_CONFIG, $CC_DBC;
$sql = "SELECT count(*) as cnt FROM ".$CC_CONFIG['scheduleTable']; $sql = "SELECT count(*) as cnt FROM ".$CC_CONFIG['scheduleTable'];
return $CC_DBC->GetOne($sql); return $CC_DBC->GetOne($sql);
} }
@ -535,16 +570,16 @@ class Application_Model_Schedule {
$isSaas = Application_Model_Preference::GetPlanLevel() == 'disabled'?false:true; $isSaas = Application_Model_Preference::GetPlanLevel() == 'disabled'?false:true;
$formWhat = new Application_Form_AddShowWhat(); $formWhat = new Application_Form_AddShowWhat();
$formWho = new Application_Form_AddShowWho(); $formWho = new Application_Form_AddShowWho();
$formWhen = new Application_Form_AddShowWhen(); $formWhen = new Application_Form_AddShowWhen();
$formRepeats = new Application_Form_AddShowRepeats(); $formRepeats = new Application_Form_AddShowRepeats();
$formStyle = new Application_Form_AddShowStyle(); $formStyle = new Application_Form_AddShowStyle();
$formWhat->removeDecorator('DtDdWrapper'); $formWhat->removeDecorator('DtDdWrapper');
$formWho->removeDecorator('DtDdWrapper'); $formWho->removeDecorator('DtDdWrapper');
$formWhen->removeDecorator('DtDdWrapper'); $formWhen->removeDecorator('DtDdWrapper');
$formRepeats->removeDecorator('DtDdWrapper'); $formRepeats->removeDecorator('DtDdWrapper');
$formStyle->removeDecorator('DtDdWrapper'); $formStyle->removeDecorator('DtDdWrapper');
$p_view->what = $formWhat; $p_view->what = $formWhat;
$p_view->when = $formWhen; $p_view->when = $formWhen;
@ -555,11 +590,11 @@ class Application_Model_Schedule {
$formWhat->populate(array('add_show_id' => '-1')); $formWhat->populate(array('add_show_id' => '-1'));
$formWhen->populate(array('add_show_start_date' => date("Y-m-d"), $formWhen->populate(array('add_show_start_date' => date("Y-m-d"),
'add_show_start_time' => '00:00', 'add_show_start_time' => '00:00',
'add_show_end_date_no_repeate' => date("Y-m-d"), 'add_show_end_date_no_repeate' => date("Y-m-d"),
'add_show_end_time' => '01:00', 'add_show_end_time' => '01:00',
'add_show_duration' => '1h')); 'add_show_duration' => '1h'));
$formRepeats->populate(array('add_show_end_date' => date("Y-m-d"))); $formRepeats->populate(array('add_show_end_date' => date("Y-m-d")));
if(!$isSaas){ if(!$isSaas){
$formRecord = new Application_Form_AddShowRR(); $formRecord = new Application_Form_AddShowRR();
@ -577,4 +612,3 @@ class Application_Model_Schedule {
$p_view->addNewShow = true; $p_view->addNewShow = true;
} }
} }

View file

@ -757,42 +757,6 @@ class Application_Model_ShowInstance {
return $CC_DBC->GetAll($sql); return $CC_DBC->GetAll($sql);
} }
public static function getScheduleItemsForAllShowsInRange($timeNow, $start, $end)
{
global $CC_DBC, $CC_CONFIG;
$sql = "SELECT"
." si.starts as show_starts,"
." si.ends as show_ends,"
." si.rebroadcast as rebroadcast,"
." st.starts as item_starts,"
." st.ends as item_ends,"
." st.clip_length as clip_length,"
." ft.track_title as track_title,"
." ft.artist_name as artist_name,"
." ft.album_title as album_title,"
." s.name as show_name,"
." si.id as instance_id,"
." pt.name as playlist_name"
." FROM $CC_CONFIG[showInstances] si"
." LEFT JOIN $CC_CONFIG[scheduleTable] st"
." ON st.instance_id = si.id"
." LEFT JOIN $CC_CONFIG[playListTable] pt"
." ON st.playlist_id = pt.id"
." LEFT JOIN $CC_CONFIG[filesTable] ft"
." ON st.file_id = ft.id"
." LEFT JOIN $CC_CONFIG[showTable] s"
." ON si.show_id = s.id"
." WHERE ((si.starts < TIMESTAMP '$timeNow' - INTERVAL '$start seconds' AND si.ends > TIMESTAMP '$timeNow' - INTERVAL '$start seconds')"
." OR (si.starts > TIMESTAMP '$timeNow' - INTERVAL '$start seconds' AND si.ends < TIMESTAMP '$timeNow' + INTERVAL '$end seconds')"
." OR (si.starts < TIMESTAMP '$timeNow' + INTERVAL '$end seconds' AND si.ends > TIMESTAMP '$timeNow' + INTERVAL '$end seconds'))"
." AND (st.starts < si.ends)"
." ORDER BY si.id, si.starts, st.starts";
return $CC_DBC->GetAll($sql);
}
public function getLastAudioItemEnd(){ public function getLastAudioItemEnd(){
global $CC_DBC; global $CC_DBC;