SAAS-223: Airtime Usage Metric: Amount of time spent broadcasting

-fixed inserts into cc_live_log
This commit is contained in:
denise 2012-05-09 16:31:36 -04:00
parent 99738fad30
commit de3e5a6e3c
2 changed files with 37 additions and 21 deletions

View File

@ -843,6 +843,8 @@ class ApiController extends Zend_Controller_Action
$data = array("sourcename"=>$sourcename, "status"=>"off"); $data = array("sourcename"=>$sourcename, "status"=>"off");
Application_Model_RabbitMq::SendMessageToPypo("switch_source", $data); Application_Model_RabbitMq::SendMessageToPypo("switch_source", $data);
Application_Model_Preference::SetSourceSwitchStatus($sourcename, "off"); Application_Model_Preference::SetSourceSwitchStatus($sourcename, "off");
Application_Model_LiveLog::SetEndTime($sourcename == 'scheduled_play'?'S':'L',
new DateTime("now", new DateTimeZone('UTC')));
} }
Application_Model_Preference::SetSourceStatus($sourcename, $status); Application_Model_Preference::SetSourceStatus($sourcename, $status);
} }

View File

@ -7,9 +7,9 @@ class Application_Model_LiveLog
try { try {
$con = Propel::getConnection(); $con = Propel::getConnection();
$rows = self::GetNumLogs(); $count = self::GetNumLogs();
Logging::log($rows);
if ($data['count'] > 1) { if ($count > 1) {
$sql = "SELECT * FROM CC_LIVE_LOG" $sql = "SELECT * FROM CC_LIVE_LOG"
." WHERE state = 'L'" ." WHERE state = 'L'"
." and (start_time >= (now() - INTERVAL '1 day'))" ." and (start_time >= (now() - INTERVAL '1 day'))"
@ -19,18 +19,22 @@ class Application_Model_LiveLog
$duration = self::GetDuration($rows); $duration = self::GetDuration($rows);
return $duration; return $duration;
} }
else if ($data['count'] == 1 && $data['state'] == 'S') { else if ($count == 1) {
$sql = "SELECT state FROM CC_LIVE_LOG";
$state = $con->query($sql)->fetchColumn(0);
if ($state == 'S') {
$duration = new DateTime("00:00:00"); $duration = new DateTime("00:00:00");
return $duration->format("H:i:s"); return $duration->format("H:i:s");
} }
else if ($data['count'] == 1 && $data['state'] == 'L') { else if ($state == 'L') {
$duration = new DateTime("23:59:59"); $duration = new DateTime("23:59:59");
return $duration->format("H:i:s"); return $duration->format("H:i:s");
} }
}
} catch (Exception $e) { } catch (Exception $e) {
header('HTTP/1.0 503 Service Unavailable'); header('HTTP/1.0 503 Service Unavailable');
Logging::log("Could not connect to database."); Logging::log("GetLiveShowDuration - Could not connect to database.");
exit; exit;
} }
} }
@ -57,7 +61,7 @@ class Application_Model_LiveLog
} catch (Exception $e) { } catch (Exception $e) {
header('HTTP/1.0 503 Service Unavailable'); header('HTTP/1.0 503 Service Unavailable');
Logging::log("Could not connect to database."); Logging::log("GetScheduledDuration - Could not connect to database.");
exit; exit;
} }
} }
@ -87,10 +91,15 @@ class Application_Model_LiveLog
$rows = $con->query($sql)->fetchAll(); $rows = $con->query($sql)->fetchAll();
return $rows; return $rows;
*/ */
$sql = "SELECT count(*) FROM CC_LIVE_LOG"
." WHERE (start_time >= (now() - INTERVAL '1 day'))";
$count = $con->query($sql)->fetchColumn(0);
return $count;
} catch (Exception $e) { } catch (Exception $e) {
header('HTTP/1.0 503 Service Unavailable'); header('HTTP/1.0 503 Service Unavailable');
Logging::log("Could not connect to database."); Logging::log("GetNumLogs - Could not connect to database.");
exit; exit;
} }
} }
@ -103,16 +112,14 @@ class Application_Model_LiveLog
self::SetEndTime('S', $dateTime); self::SetEndTime('S', $dateTime);
} }
/* Check if airtime is currently broadcasting live. /* Only insert new state if last log
* Only insert new state if scheduled switch is on * has ended
* or live broadcasting is off
*/ */
$sql_select = "SELECT max(id) from CC_LIVE_LOG" $sql_select = "SELECT max(id) from CC_LIVE_LOG"
." WHERE state='L' and end_time is NULL"; ." WHERE (state='L' and end_time is NULL) or (state='S' and end_time is NULL)";
$id = $con->query($sql_select)->fetchColumn(0); $id = $con->query($sql_select)->fetchColumn(0);
if ($id == null) { if ($id == null) {
$sql_insert = "INSERT INTO CC_LIVE_LOG (state, start_time)" $sql_insert = "INSERT INTO CC_LIVE_LOG (state, start_time)"
." VALUES ('$state', '{$dateTime->format("Y-m-d H:i:s")}')"; ." VALUES ('$state', '{$dateTime->format("Y-m-d H:i:s")}')";
$con->exec($sql_insert); $con->exec($sql_insert);
@ -135,13 +142,20 @@ class Application_Model_LiveLog
} }
if (($dj_live=='off' && $master_live=='off') || $state == 'S') { if (($dj_live=='off' && $master_live=='off') || $state == 'S') {
$sql = "SELECT max(id) FROM CC_LIVE_LOG" /*$sql = "SELECT max(id) FROM CC_LIVE_LOG"
." WHERE state = '$state'" ." WHERE state = '$state'"
." UNION" ." UNION"
." SELECT max(id) FROM CC_LIVE_LOG"; ." SELECT max(id) FROM CC_LIVE_LOG";
*/
$sql = "SELECT id, state from cc_live_log where id in (select max(id) from cc_live_log)";
$row = $con->query($sql)->fetch(); $row = $con->query($sql)->fetch();
Logging::log($state);
if ($row != null && $row['max'] == $row[0]) { Logging::log($row);
/* Only set end time if state recevied ($state)
* is the last row in cc_live_log
*/
//if ($row != null && $row['max'] == $row[0]) {
if ($row['state'] == $state) {
$update_sql = "UPDATE CC_LIVE_LOG" $update_sql = "UPDATE CC_LIVE_LOG"
." SET end_time = '{$dateTime->format("Y-m-d H:i:s")}'" ." SET end_time = '{$dateTime->format("Y-m-d H:i:s")}'"
." WHERE id = '$row[0]'"; ." WHERE id = '$row[0]'";