From 8bbccb6f44abce9fde0b3c00f637fa76e7a475c0 Mon Sep 17 00:00:00 2001 From: Daniel Date: Wed, 22 Feb 2012 14:32:59 -0500 Subject: [PATCH] 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. --- airtime_mvc/application/models/Show.php | 70 +++++++++++++++---------- 1 file changed, 42 insertions(+), 28 deletions(-) diff --git a/airtime_mvc/application/models/Show.php b/airtime_mvc/application/models/Show.php index 3e6f2a72c..efce86b2b 100644 --- a/airtime_mvc/application/models/Show.php +++ b/airtime_mvc/application/models/Show.php @@ -215,22 +215,22 @@ class Application_Model_Show { } 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("Id show days is: {$showDay->getDbId()}"); - if (in_array($showDay->getDbDay(), $p_uncheckedDays)) { - $showDay->reload(); - //Logging::log("Local show day is: {$showDay->getDbDay()}"); - //Logging::log("First show day is: {$showDay->getDbFirstShow()}"); - //Logging::log("Id show days is: {$showDay->getDbId()}"); - $startDay = new DateTime("{$showDay->getDbFirstShow()} {$showDay->getDbStartTime()}", new DateTimeZone($showDay->getDbTimezone())); - Logging::log("Show start day: {$startDay->format('Y-m-d H:i:s')}"); + if (in_array($showDay->getDbDay(), $p_uncheckedDays)) { + $showDay->reload(); + //Logging::log("Local show day is: {$showDay->getDbDay()}"); + //Logging::log("First show day is: {$showDay->getDbFirstShow()}"); + //Logging::log("Id show days is: {$showDay->getDbId()}"); + $startDay = new DateTime("{$showDay->getDbFirstShow()} {$showDay->getDbStartTime()}", new DateTimeZone($showDay->getDbTimezone())); + Logging::log("Show start day: {$startDay->format('Y-m-d H:i:s')}"); $startDay->setTimezone(new DateTimeZone("UTC")); Logging::log("Show start day UTC: {$startDay->format('Y-m-d H:i:s')}"); $daysRemovedUTC[] = $startDay->format('w'); Logging::log("UTC show day is: {$startDay->format('w')}"); - } + } } $uncheckedDaysImploded = implode(",", $daysRemovedUTC); @@ -843,18 +843,18 @@ class Application_Model_Show { } } - if ($p_data['add_show_start_date'] != $this->getStartDate() - || $p_data['add_show_start_time'] != $this->getStartTime()){ - //start date/time has changed + if ($p_data['add_show_start_date'] != $this->getStartDate() + || $p_data['add_show_start_time'] != $this->getStartTime()){ + //start date/time has changed - $newDate = strtotime($p_data['add_show_start_date']); - $oldDate = strtotime($this->getStartDate()); - if ($newDate > $oldDate){ - $this->removeAllInstancesBeforeDate($p_data['add_show_start_date']); - } + $newDate = strtotime($p_data['add_show_start_date']); + $oldDate = strtotime($this->getStartDate()); + if ($newDate > $oldDate){ + $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 @@ -1397,7 +1397,8 @@ class Application_Model_Show { } $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 LEFT JOIN cc_show ON cc_show.id = cc_show_instances.show_id WHERE cc_show_instances.modified_instance = FALSE"; @@ -1486,14 +1487,15 @@ class Application_Model_Show { * -in UTC time * @param boolean $editable */ - public static function getFullCalendarEvents($start, $end, $editable=false) + public static function getFullCalendarEvents($p_start, $p_end, $p_editable=false) { + $events = array(); - $interval = $start->diff($end); + $interval = $p_start->diff($p_end); $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"); @@ -1501,21 +1503,33 @@ class Application_Model_Show { $options = array(); //only bother calculating percent for week or day view. - if(intval($days) <= 7) { - $show_instance = new Application_Model_ShowInstance($show["instance_id"]); - $options["percent"] = $show_instance->getPercentScheduled(); + + if(intval($days) <= 7) { + $options["percent"] = Application_Model_Show::getPercentScheduled($show["starts"], $show["ends"], $show["time_filled"]); } - if ($editable && (strtotime($today_timestamp) < strtotime($show["starts"]))) { + if ($p_editable && (strtotime($today_timestamp) < strtotime($show["starts"]))) { $options["editable"] = true; $events[] = Application_Model_Show::makeFullCalendarEvent($show, $options); } else { $events[] = Application_Model_Show::makeFullCalendarEvent($show, $options); } } - + 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()) {