CC-3338: Optimize week display

The response time of the calendar varies depending on whether the week/day views filter or the month filter is used because of the additional percentage scheduled calculation per show instance. This added calculation results in more and heavier hits to the database, I've included the time_filled column in the original show query and have used that to calculate percentage and saved the db call.
This commit is contained in:
Daniel 2012-02-22 14:32:59 -05:00
parent 80e33ee533
commit 8bbccb6f44
1 changed files with 42 additions and 28 deletions

View File

@ -215,22 +215,22 @@ class Application_Model_Show {
} }
foreach($showDays as $showDay) { foreach($showDays as $showDay) {
Logging::log("Local show day is: {$showDay->getDbDay()}"); Logging::log("Local show day is: {$showDay->getDbDay()}");
Logging::log("First show day is: {$showDay->getDbFirstShow()}"); Logging::log("First show day is: {$showDay->getDbFirstShow()}");
Logging::log("Id show days is: {$showDay->getDbId()}"); Logging::log("Id show days is: {$showDay->getDbId()}");
if (in_array($showDay->getDbDay(), $p_uncheckedDays)) { if (in_array($showDay->getDbDay(), $p_uncheckedDays)) {
$showDay->reload(); $showDay->reload();
//Logging::log("Local show day is: {$showDay->getDbDay()}"); //Logging::log("Local show day is: {$showDay->getDbDay()}");
//Logging::log("First show day is: {$showDay->getDbFirstShow()}"); //Logging::log("First show day is: {$showDay->getDbFirstShow()}");
//Logging::log("Id show days is: {$showDay->getDbId()}"); //Logging::log("Id show days is: {$showDay->getDbId()}");
$startDay = new DateTime("{$showDay->getDbFirstShow()} {$showDay->getDbStartTime()}", new DateTimeZone($showDay->getDbTimezone())); $startDay = new DateTime("{$showDay->getDbFirstShow()} {$showDay->getDbStartTime()}", new DateTimeZone($showDay->getDbTimezone()));
Logging::log("Show start day: {$startDay->format('Y-m-d H:i:s')}"); Logging::log("Show start day: {$startDay->format('Y-m-d H:i:s')}");
$startDay->setTimezone(new DateTimeZone("UTC")); $startDay->setTimezone(new DateTimeZone("UTC"));
Logging::log("Show start day UTC: {$startDay->format('Y-m-d H:i:s')}"); Logging::log("Show start day UTC: {$startDay->format('Y-m-d H:i:s')}");
$daysRemovedUTC[] = $startDay->format('w'); $daysRemovedUTC[] = $startDay->format('w');
Logging::log("UTC show day is: {$startDay->format('w')}"); Logging::log("UTC show day is: {$startDay->format('w')}");
} }
} }
$uncheckedDaysImploded = implode(",", $daysRemovedUTC); $uncheckedDaysImploded = implode(",", $daysRemovedUTC);
@ -843,18 +843,18 @@ class Application_Model_Show {
} }
} }
if ($p_data['add_show_start_date'] != $this->getStartDate() if ($p_data['add_show_start_date'] != $this->getStartDate()
|| $p_data['add_show_start_time'] != $this->getStartTime()){ || $p_data['add_show_start_time'] != $this->getStartTime()){
//start date/time has changed //start date/time has changed
$newDate = strtotime($p_data['add_show_start_date']); $newDate = strtotime($p_data['add_show_start_date']);
$oldDate = strtotime($this->getStartDate()); $oldDate = strtotime($this->getStartDate());
if ($newDate > $oldDate){ if ($newDate > $oldDate){
$this->removeAllInstancesBeforeDate($p_data['add_show_start_date']); $this->removeAllInstancesBeforeDate($p_data['add_show_start_date']);
} }
$this->updateStartDateTime($p_data, $p_endDate); $this->updateStartDateTime($p_data, $p_endDate);
} }
} }
//Check if end date for the repeat option has changed. If so, need to take care //Check if end date for the repeat option has changed. If so, need to take care
@ -1397,7 +1397,8 @@ class Application_Model_Show {
} }
$sql = "SELECT starts, ends, record, rebroadcast, instance_id, show_id, name, $sql = "SELECT starts, ends, record, rebroadcast, instance_id, show_id, name,
color, background_color, file_id, cc_show_instances.id AS instance_id color, background_color, file_id, cc_show_instances.id AS instance_id,
time_filled
FROM cc_show_instances FROM cc_show_instances
LEFT JOIN cc_show ON cc_show.id = cc_show_instances.show_id LEFT JOIN cc_show ON cc_show.id = cc_show_instances.show_id
WHERE cc_show_instances.modified_instance = FALSE"; WHERE cc_show_instances.modified_instance = FALSE";
@ -1486,14 +1487,15 @@ class Application_Model_Show {
* -in UTC time * -in UTC time
* @param boolean $editable * @param boolean $editable
*/ */
public static function getFullCalendarEvents($start, $end, $editable=false) public static function getFullCalendarEvents($p_start, $p_end, $p_editable=false)
{ {
$events = array(); $events = array();
$interval = $start->diff($end); $interval = $p_start->diff($p_end);
$days = $interval->format('%a'); $days = $interval->format('%a');
$shows = Application_Model_Show::getShows($start, $end); $shows = Application_Model_Show::getShows($p_start, $p_end);
$today_timestamp = gmdate("Y-m-d H:i:s"); $today_timestamp = gmdate("Y-m-d H:i:s");
@ -1501,12 +1503,12 @@ class Application_Model_Show {
$options = array(); $options = array();
//only bother calculating percent for week or day view. //only bother calculating percent for week or day view.
if(intval($days) <= 7) { if(intval($days) <= 7) {
$show_instance = new Application_Model_ShowInstance($show["instance_id"]); $options["percent"] = Application_Model_Show::getPercentScheduled($show["starts"], $show["ends"], $show["time_filled"]);
$options["percent"] = $show_instance->getPercentScheduled();
} }
if ($editable && (strtotime($today_timestamp) < strtotime($show["starts"]))) { if ($p_editable && (strtotime($today_timestamp) < strtotime($show["starts"]))) {
$options["editable"] = true; $options["editable"] = true;
$events[] = Application_Model_Show::makeFullCalendarEvent($show, $options); $events[] = Application_Model_Show::makeFullCalendarEvent($show, $options);
} else { } else {
@ -1517,6 +1519,18 @@ class Application_Model_Show {
return $events; return $events;
} }
/**
* Calculates the percentage of a show scheduled given the start and end times in date/time format
* and the time_filled as the total time the schow is scheduled for in time format.
**/
private static function getPercentScheduled($p_starts, $p_ends, $p_time_filled){
$durationSeconds = (strtotime($p_ends) - strtotime($p_starts));
$time_filled = Application_Model_Schedule::WallTimeToMillisecs($p_time_filled) / 1000;
$percent = ceil(( $time_filled / $durationSeconds) * 100);
return $percent;
}
private static function makeFullCalendarEvent($show, $options=array()) private static function makeFullCalendarEvent($show, $options=array())
{ {
$event = array(); $event = array();