From cbaeda1d6bb98be45b5fa43bd43e0707718e6fd6 Mon Sep 17 00:00:00 2001 From: Daniel Date: Tue, 13 Mar 2012 10:21:01 -0400 Subject: [PATCH 1/4] CC-3404: Change "Now Playing" dashboard SQL query to take advantage of the "playout_status" field in the schedule table - Updated the logic in GetPlayOrderRange now I get the previous, current and next shows and tracks to be played. - This information is then sent back to the caller. - if the previous track is null the previous show is sent back as an example of the algorithm. --- airtime_mvc/application/logging/Logging.php | 2 +- airtime_mvc/application/models/Schedule.php | 84 ++++++++++++++++++--- airtime_mvc/application/models/Show.php | 65 ++++++++++++++++ 3 files changed, 141 insertions(+), 10 deletions(-) diff --git a/airtime_mvc/application/logging/Logging.php b/airtime_mvc/application/logging/Logging.php index 4b23de04f..d6fb070b3 100644 --- a/airtime_mvc/application/logging/Logging.php +++ b/airtime_mvc/application/logging/Logging.php @@ -23,7 +23,7 @@ class Logging { } public static function debug($p_msg){ - if (APPLICATION_ENV == "development"){ + if (defined('APPLICATION_ENV') && APPLICATION_ENV == "development"){ $logger = self::getLogger(); $logger->debug($p_msg); } diff --git a/airtime_mvc/application/models/Schedule.php b/airtime_mvc/application/models/Schedule.php index 81fe4edc5..78ae664c9 100644 --- a/airtime_mvc/application/models/Schedule.php +++ b/airtime_mvc/application/models/Schedule.php @@ -45,7 +45,6 @@ class Application_Model_Schedule { } } - /** * Returns data related to the scheduled items. * @@ -60,24 +59,91 @@ class Application_Model_Schedule { return array(); } - global $CC_CONFIG; - $date = new Application_Model_DateHelper; $timeNow = $date->getTimestamp(); $utcTimeNow = $date->getUtcTimestamp(); + + $shows = Application_Model_Show::getPrevCurrentNext($utcTimeNow); + $currentShowID = count($shows['currentShow'])>0?$shows['currentShow'][0]['id']:null; + $results = Application_Model_Schedule::GetPrevCurrentNext($currentShowID, $utcTimeNow); + $range = array("env"=>APPLICATION_ENV, "schedulerTime"=>$timeNow, - "previous"=>Application_Model_Dashboard::GetPreviousItem($utcTimeNow), - "current"=>Application_Model_Dashboard::GetCurrentItem($utcTimeNow), - "next"=>Application_Model_Dashboard::GetNextItem($utcTimeNow), - "currentShow"=>Application_Model_Show::GetCurrentShow($utcTimeNow), - "nextShow"=>Application_Model_Show::GetNextShows($utcTimeNow, 1), + "previous"=>isset($results['previous'])?$results['previous']:count($shows['previousShow'])>0?$shows['previousShow'][0]:null, + "current"=>isset($results['current'])?$results['current']:null, + "next"=> isset($results['next'])?$results['next']:count($shows['nextShow'])>0?$shows['nextShow'][0]:null, + "currentShow"=>$shows['currentShow'], + "nextShow"=>$shows['nextShow'], "timezone"=> date("T"), "timezoneOffset"=> date("Z")); return $range; } - + + /** + * Queries the database for the set of schedules one hour before and after the given time. + * If a show starts and ends within that time that is considered the current show. Then the + * scheduled item before it is the previous show, and the scheduled item after it is the next + * show. This way the dashboard getCurrentPlaylist is very fast. But if any one of the three + * show types are not found through this mechanism a call is made to the old way of querying + * the database to find the track info. + **/ + public static function GetPrevCurrentNext($currentShowID, $p_timeNow) + { + global $CC_CONFIG, $CC_DBC; + + if (!isset($currentShowID)) { + return array(); + } + $sql = "Select ft.artist_name, ft.track_title, st.starts as starts, st.ends as ends, st.media_item_played as media_item_played + FROM cc_schedule st LEFT JOIN cc_files ft ON st.file_id = ft.id + WHERE st.instance_id = '$currentShowID' AND st.playout_status > 0 + ORDER BY st.starts"; + + //Logging::log($sql); + $rows = $CC_DBC->GetAll($sql); + $numberOfRows = count($rows); + $results; + $results['previous'] = null; + $results['current'] = null; + $results['next'] = null; + + $timeNowAsMillis = strtotime($p_timeNow); + for( $i = 0; $i < $numberOfRows; ++$i ){ + if ((strtotime($rows[$i]['starts']) <= $timeNowAsMillis) && (strtotime($rows[$i]['ends']) >= $timeNowAsMillis)){ + if ( $i - 1 >= 0){ + $results['previous'] = array("name"=>$rows[$i-1]["artist_name"]." - ".$rows[$i-1]["track_title"], + "starts"=>$rows[$i-1]["starts"], + "ends"=>$rows[$i-1]["ends"]); + } + $results['current'] = array("name"=>$rows[$i]["artist_name"]." - ".$rows[$i]["track_title"], + "starts"=>$rows[$i]["starts"], + "ends"=>$rows[$i]["ends"], + "media_item_played"=>$rows[$i]["media_item_played"], + "record"=>0); + if ( isset($rows[$i+1])){ + $results['next'] = array("name"=>$rows[$i+1]["artist_name"]." - ".$rows[$i+1]["track_title"], + "starts"=>$rows[$i+1]["starts"], + "ends"=>$rows[$i+1]["ends"]); + } + break; + } + if (strtotime($rows[$i]['ends']) < $timeNowAsMillis ) { + $results['previous'] = array("name"=>$rows[$i]["artist_name"]." - ".$rows[$i]["track_title"], + "starts"=>$rows[$i]["starts"], + "ends"=>$rows[$i]["ends"]);; + } + if (strtotime($rows[$i]['starts']) > $timeNowAsMillis) { + $results['next'] = array("name"=>$rows[$i]["artist_name"]." - ".$rows[$i]["track_title"], + "starts"=>$rows[$i]["starts"], + "ends"=>$rows[$i]["ends"]); + break; + } + } + + return $results; + } + public static function GetLastScheduleItem($p_timeNow){ global $CC_CONFIG, $CC_DBC; diff --git a/airtime_mvc/application/models/Show.php b/airtime_mvc/application/models/Show.php index 06e72261b..bcbde769b 100644 --- a/airtime_mvc/application/models/Show.php +++ b/airtime_mvc/application/models/Show.php @@ -1680,6 +1680,71 @@ class Application_Model_Show { return $rows; } + /** + * Gets the current show, previous and next with an 2day window from the given timeNow, so timeNow-2days and timeNow+2days. + */ + public static function getPrevCurrentNext($timeNow) + { + global $CC_CONFIG, $CC_DBC; + //TODO, returning starts + ends twice (once with an alias). Unify this after the 2.0 release. --Martin + $sql = "SELECT si.starts as start_timestamp, si.ends as end_timestamp, s.name, s.id, si.id as instance_id, si.record, s.url, starts, ends" + ." FROM $CC_CONFIG[showInstances] si, $CC_CONFIG[showTable] s" + ." WHERE si.show_id = s.id" + ." AND si.starts > TIMESTAMP '$timeNow' - INTERVAL '2 days'" + ." AND si.ends < TIMESTAMP '$timeNow' + INTERVAL '2 days'" + ." AND modified_instance != TRUE"; + + //Logging::log($sql); + // Convert back to local timezone + $rows = $CC_DBC->GetAll($sql); + $numberOfRows = count($rows); + $results; + $results['previousShow'] = array(); + $results['currentShow'] = array(); + $results['nextShow'] = array(); + + $timeNowAsMillis = strtotime($timeNow); + for( $i = 0; $i < $numberOfRows; ++$i ){ + if ((strtotime($rows[$i]['starts']) <= $timeNowAsMillis) && (strtotime($rows[$i]['ends']) >= $timeNowAsMillis)){ + if ( $i - 1 >= 0){ + $results['previousShow'][0] = array("name"=>$rows[$i-1]['name'], + "start_timestamp"=>$rows[$i-1]['start_timestamp'], + "end_timestamp"=>$rows[$i-1]['end_timestamp'], + "starts"=>$rows[$i-1]['starts'], + "ends"=>$rows[$i-1]['ends']); + } + + $results['currentShow'][0] = $rows[$i]; + + if ( isset($rows[$i+1])){ + $results['nextShow'][0] = array("name"=>$rows[$i+1]['name'], + "start_timestamp"=>$rows[$i+1]['start_timestamp'], + "end_timestamp"=>$rows[$i+1]['end_timestamp'], + "starts"=>$rows[$i+1]['starts'], + "ends"=>$rows[$i+1]['ends']); + + } + break; + } + if (strtotime($rows[$i]['ends']) < $timeNowAsMillis ) { + $results['previousShow'][0] = array("name"=>$rows[$i]['name'], + "start_timestamp"=>$rows[$i]['start_timestamp'], + "end_timestamp"=>$rows[$i]['end_timestamp'], + "starts"=>$rows[$i]['starts'], + "ends"=>$rows[$i]['ends']); + } + if (strtotime($rows[$i]['starts']) > $timeNowAsMillis) { + $results['nextShow'][0] = array("name"=>$rows[$i]['name'], + "start_timestamp"=>$rows[$i]['start_timestamp'], + "end_timestamp"=>$rows[$i]['end_timestamp'], + "starts"=>$rows[$i]['starts'], + "ends"=>$rows[$i]['ends']); + break; + } + } + + return $results; + } /** * Given a start time $timeStart and end time $timeEnd, returns the next $limit * number of shows within the time interval; From 9e850838caee11b6e88eec0b34c30f690e712bd0 Mon Sep 17 00:00:00 2001 From: Daniel Date: Tue, 13 Mar 2012 10:36:11 -0400 Subject: [PATCH 2/4] CC-3404: Change "Now Playing" dashboard SQL query to take advantage of the "playout_status" field in the schedule table - Updated ApiController to use Schedule's GetPlayOrderRange which has better performance. --- airtime_mvc/application/controllers/ApiController.php | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/airtime_mvc/application/controllers/ApiController.php b/airtime_mvc/application/controllers/ApiController.php index 7d54065d1..128782ec0 100644 --- a/airtime_mvc/application/controllers/ApiController.php +++ b/airtime_mvc/application/controllers/ApiController.php @@ -270,13 +270,8 @@ class ApiController extends Zend_Controller_Action $limit = "5"; } - $result = array("env"=>APPLICATION_ENV, - "schedulerTime"=>gmdate("Y-m-d H:i:s"), - "currentShow"=>Application_Model_Show::GetCurrentShow($utcTimeNow), - "nextShow"=>Application_Model_Show::GetNextShows($utcTimeNow, $limit, $utcTimeEnd), - "timezone"=> date("T"), - "timezoneOffset"=> date("Z"), - "AIRTIME_API_VERSION"=>AIRTIME_API_VERSION); //used by caller to determine if the airtime they are running or widgets in use is out of date. + $result = Application_Model_Schedule::GetPlayOrderRange(); + $result['AIRTIME_API_VERSION'] = AIRTIME_API_VERSION; //used by caller to determine if the airtime they are running or widgets in use is out of date. //Convert from UTC to localtime for user. Application_Model_Show::ConvertToLocalTimeZone($result["currentShow"], array("starts", "ends", "start_timestamp", "end_timestamp")); From 13e3197ebfe5ff1ba0f3b8c8d82811568fdb3a8d Mon Sep 17 00:00:00 2001 From: Daniel Date: Tue, 13 Mar 2012 11:31:24 -0400 Subject: [PATCH 3/4] CC-3423: nowPlaying getCurrentPlaylist doesn't work for next song - added brackets to an statement --- airtime_mvc/application/models/Schedule.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/airtime_mvc/application/models/Schedule.php b/airtime_mvc/application/models/Schedule.php index 6c3730bcf..5abee1f36 100644 --- a/airtime_mvc/application/models/Schedule.php +++ b/airtime_mvc/application/models/Schedule.php @@ -71,11 +71,12 @@ class Application_Model_Schedule { "schedulerTime"=>$timeNow, "previous"=>isset($results['previous'])?$results['previous']:count($shows['previousShow'])>0?$shows['previousShow'][0]:null, "current"=>isset($results['current'])?$results['current']:null, - "next"=> isset($results['next'])?$results['next']:count($shows['nextShow'])>0?$shows['nextShow'][0]:null, + "next"=> isset($results['next'])?$results['next']:(count($shows['nextShow'])>0?$shows['nextShow'][0]:null), "currentShow"=>$shows['currentShow'], "nextShow"=>$shows['nextShow'], "timezone"=> date("T"), "timezoneOffset"=> date("Z")); + return $range; } @@ -120,6 +121,7 @@ class Application_Model_Schedule { "ends"=>$rows[$i]["ends"], "media_item_played"=>$rows[$i]["media_item_played"], "record"=>0); + if ( isset($rows[$i+1])){ $results['next'] = array("name"=>$rows[$i+1]["artist_name"]." - ".$rows[$i+1]["track_title"], "starts"=>$rows[$i+1]["starts"], @@ -139,7 +141,6 @@ class Application_Model_Schedule { break; } } - return $results; } From 48c8607d179b192a5882ba485d9587210fdf293d Mon Sep 17 00:00:00 2001 From: Daniel Date: Tue, 13 Mar 2012 11:34:43 -0400 Subject: [PATCH 4/4] CC-3423: nowPlaying getCurrentPlaylist doesn't work for next song -added brackets around other if condition --- airtime_mvc/application/models/Schedule.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/airtime_mvc/application/models/Schedule.php b/airtime_mvc/application/models/Schedule.php index 5abee1f36..86d2986e5 100644 --- a/airtime_mvc/application/models/Schedule.php +++ b/airtime_mvc/application/models/Schedule.php @@ -69,7 +69,7 @@ class Application_Model_Schedule { $range = array("env"=>APPLICATION_ENV, "schedulerTime"=>$timeNow, - "previous"=>isset($results['previous'])?$results['previous']:count($shows['previousShow'])>0?$shows['previousShow'][0]:null, + "previous"=>isset($results['previous'])?$results['previous']:(count($shows['previousShow'])>0?$shows['previousShow'][0]:null), "current"=>isset($results['current'])?$results['current']:null, "next"=> isset($results['next'])?$results['next']:(count($shows['nextShow'])>0?$shows['nextShow'][0]:null), "currentShow"=>$shows['currentShow'],