SAAS-223: Airtime Usage Metric: Amount of time spent broadcasting
-calculated scheduled broadcasting
This commit is contained in:
parent
195d0d8311
commit
746e55de32
1 changed files with 178 additions and 101 deletions
|
@ -22,7 +22,43 @@ class Application_Model_LiveLog
|
||||||
array_push($rows, $last_row);
|
array_push($rows, $last_row);
|
||||||
}
|
}
|
||||||
|
|
||||||
$duration = self::GetDuration($rows);
|
//$duration = self::GetDuration($rows);
|
||||||
|
$hours = 0;
|
||||||
|
$minutes = 0;
|
||||||
|
$seconds = 0;
|
||||||
|
|
||||||
|
foreach ($rows as $row) {
|
||||||
|
$end = new DateTime($row['end_time']);
|
||||||
|
$start = new DateTime($row['start_time']);
|
||||||
|
$duration = $start->diff($end);
|
||||||
|
$duration = $duration->format("%H:%i:%s");
|
||||||
|
$intervals = explode(":", $duration);
|
||||||
|
// Trim milliseconds (DateInteral does not support)
|
||||||
|
$sec = explode(".", $intervals[2]);
|
||||||
|
$intervals[2] = $sec[0];
|
||||||
|
|
||||||
|
$seconds += $intervals[2];
|
||||||
|
if ($seconds / 60 >= 1) {
|
||||||
|
$minutes += 1;
|
||||||
|
$seconds -= 60;
|
||||||
|
}
|
||||||
|
|
||||||
|
$minutes += $intervals[1];
|
||||||
|
if ($minutes / 60 >= 1) {
|
||||||
|
$hours += 1;
|
||||||
|
$minutes -= 60;
|
||||||
|
}
|
||||||
|
|
||||||
|
$hours += $intervals[0];
|
||||||
|
|
||||||
|
// Delete data we just used to start a new log history
|
||||||
|
$sql_delete = "DELETE FROM CC_LIVE_LOG"
|
||||||
|
." WHERE id = '{$row['id']}'";
|
||||||
|
$con->exec($sql_delete);
|
||||||
|
}
|
||||||
|
$seconds = explode(".", $seconds);
|
||||||
|
$duration = new DateInterval("PT". $hours . "H" . $minutes . "M" . $seconds[0] ."S");
|
||||||
|
return $duration->format("%H:%i:%s");
|
||||||
return $duration;
|
return $duration;
|
||||||
} catch (Exception $e) {
|
} catch (Exception $e) {
|
||||||
header('HTTP/1.0 503 Service Unavailable');
|
header('HTTP/1.0 503 Service Unavailable');
|
||||||
|
@ -50,57 +86,98 @@ class Application_Model_LiveLog
|
||||||
array_push($rows, $last_row);
|
array_push($rows, $last_row);
|
||||||
}
|
}
|
||||||
|
|
||||||
$duration = new DateTime("00:00:00");
|
$hours = 0;
|
||||||
|
$minutes = 0;
|
||||||
|
$seconds = 0;
|
||||||
|
|
||||||
/* Get all tracks from cc_schedule that played
|
/* Get all shows and tracks from cc_schedule that played
|
||||||
* during a scheduled state
|
* during a scheduled state
|
||||||
*/
|
*/
|
||||||
foreach ($rows as $row) {
|
foreach ($rows as $row) {
|
||||||
//Get all show instances
|
|
||||||
$sql_get_shows = "SELECT * FROM cc_show_instances"
|
|
||||||
." WHERE starts >= '{$row['start_time']}'"
|
|
||||||
." AND ends < '{$row['end_time']}'";
|
|
||||||
$shows = $con->query($sql_get_shows)->fetchAll();
|
|
||||||
|
|
||||||
//Get all tracks from each show and calculate total duration
|
|
||||||
foreach ($shows as $show) {
|
|
||||||
$sql_get_tracks = "SELECT * FROM cc_schedule"
|
$sql_get_tracks = "SELECT * FROM cc_schedule"
|
||||||
." WHERE starts >= '{$show['starts']}'"
|
." WHERE starts >= '{$row['start_time']}'"
|
||||||
." AND starts < '{$show['ends']}'"
|
." AND starts < '{$row['end_time']}'"
|
||||||
." AND file_id IS NOT NULL"
|
." AND file_id IS NOT NULL"
|
||||||
." AND media_item_played IS TRUE"
|
." AND media_item_played IS TRUE";
|
||||||
." AND instance_id = '{$show['show_id']}'";
|
|
||||||
$tracks = $con->query($sql_get_tracks)->fetchAll();
|
$tracks = $con->query($sql_get_tracks)->fetchAll();
|
||||||
$last_track = array_pop($tracks);
|
|
||||||
foreach ($tracks as $track) {
|
foreach ($tracks as $track) {
|
||||||
$track_start = new DateTime($track['starts']);
|
if ($track['ends'] > $row['end_time']) {
|
||||||
$track_end = new DateTime($track['ends']);
|
$scheduled_ends = new DateTime($row['end_time']);
|
||||||
$duration->add($track_start->diff($track_end));
|
$track_ends = new DateTime($track['ends']);
|
||||||
}
|
$extra_time = $scheduled_ends->diff($track_ends);
|
||||||
if ($last_track['ends'] > $show['ends']) {
|
|
||||||
/*
|
|
||||||
$show_end = new DateTime($show['ends']);
|
|
||||||
$last_track_start = new DateTime($last_track['starts']);
|
|
||||||
$last_track_end = new DateTime($last_track['ends']);
|
|
||||||
|
|
||||||
$last_track_length = new DateTime($last_track_start->diff($last_track_end));
|
/* Get difference between clip_length
|
||||||
|
* and the extra time. We need to subtract
|
||||||
$trackdiff = new DateTime($show_end->diff($last_track_end));
|
* this difference from the track's
|
||||||
|
* clip length.
|
||||||
$new_track_length = new DateTime($trackdiff->diff($last_track_length));
|
|
||||||
|
|
||||||
$duration->add($new_track_length);
|
|
||||||
*/
|
*/
|
||||||
|
$clip_length = $track['clip_length'];
|
||||||
|
//Convert clip_length into seconds
|
||||||
|
$clip_length_intervals = explode(":", $clip_length);
|
||||||
|
$clip_length_seconds = $clip_length_intervals[0]*3600 + $clip_length_intervals[1]*60 + $clip_length_intervals[2];
|
||||||
|
|
||||||
|
$extra_time = $extra_time->format("%H:%i:%s");
|
||||||
|
//Convert extra_time into seconds;
|
||||||
|
$extra_time_intervals = explode(":", $extra_time);
|
||||||
|
$extra_time_seconds = $extra_time_intervals[0]*3600 + $extra_time_intervals[1]*60 + $extra_time_intervals[2];
|
||||||
|
|
||||||
|
$clip_length_seconds -= $extra_time_seconds;
|
||||||
|
|
||||||
|
//Convert new clip_length into "H-i-s" format
|
||||||
|
$clip_length_arr = array();
|
||||||
|
if ($clip_length_seconds / 3600 >= 1) {
|
||||||
|
array_push($clip_length_arr, str_pad(floor($clip_length_seconds / 3600), 2, "0", STR_PAD_LEFT));
|
||||||
|
$clip_length_seconds -= floor($clip_length_seconds / 3600);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
$last_track_start = new DateTime($last_track['starts']);
|
array_push($clip_length_arr, "00");
|
||||||
$last_track_end = new DateTime($last_track['ends']);
|
|
||||||
$duration->add($last_track_start->diff($last_track_end));
|
|
||||||
}
|
}
|
||||||
|
if ($clip_length_seconds / 60 >= 1) {
|
||||||
|
array_push($clip_length_arr, str_pad(floor($clip_length_seconds / 60), 2, "0", STR_PAD_LEFT));
|
||||||
|
$clip_length_seconds -= floor($clip_length_seconds / 60);
|
||||||
}
|
}
|
||||||
$duration = $duration->format("H:i:s");
|
else {
|
||||||
return $duration;
|
array_push($clip_length_arr, "00");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
array_push($clip_length_arr, str_pad($clip_length_seconds, 2, "0", STR_PAD_LEFT));
|
||||||
|
$clip_length = implode(":", $clip_length_arr);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
$clip_length = $track['clip_length'];
|
||||||
|
}
|
||||||
|
|
||||||
|
$intervals = explode(":", $clip_length);
|
||||||
|
// Trim milliseconds (DateInteral does not support)
|
||||||
|
$sec = explode(".", $intervals[2]);
|
||||||
|
$intervals[2] = $sec[0];
|
||||||
|
|
||||||
|
$seconds += $intervals[2];
|
||||||
|
if ($seconds / 60 >= 1) {
|
||||||
|
$minutes += 1;
|
||||||
|
$seconds -= 60;
|
||||||
|
}
|
||||||
|
|
||||||
|
$minutes += $intervals[1];
|
||||||
|
if ($minutes / 60 >= 1) {
|
||||||
|
$hours += 1;
|
||||||
|
$minutes -= 60;
|
||||||
|
}
|
||||||
|
|
||||||
|
$hours += $intervals[0];
|
||||||
|
}
|
||||||
|
|
||||||
|
//Delete row because we do not need data anymore
|
||||||
|
$sql_delete = "DELETE FROM CC_LIVE_LOG"
|
||||||
|
." WHERE id = '{$row['id']}'";
|
||||||
|
$con->exec($sql_delete);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
$seconds = explode(".", $seconds);
|
||||||
|
$duration = new DateInterval("PT". $hours . "H" . $minutes . "M" . $seconds[0] ."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("GetScheduledDuration - Could not connect to database.");
|
Logging::log("GetScheduledDuration - Could not connect to database.");
|
||||||
|
@ -120,15 +197,17 @@ class Application_Model_LiveLog
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function UpdateLastLogEndTime($log) {
|
public static function UpdateLastLogEndTime($log) {
|
||||||
|
if ($log['end_time'] == null) {
|
||||||
$current_time = new DateTime("now", new DateTimeZone('UTC'));
|
$current_time = new DateTime("now", new DateTimeZone('UTC'));
|
||||||
$log['end_time'] = $current_time;
|
$log['end_time'] = $current_time;
|
||||||
$log['end_time'] = $log['end_time']->format("Y-m-d H:i:s");
|
$log['end_time'] = $log['end_time']->format("Y-m-d H:i:s");
|
||||||
self::SetEndTime($log['state'], $current_time);
|
self::SetEndTime($log['state'], $current_time, true);
|
||||||
self::SetNewLogTime($log['state'], $current_time);
|
self::SetNewLogTime($log['state'], $current_time);
|
||||||
|
}
|
||||||
return $log;
|
return $log;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function SetNewLogTime($state, $dateTime, $end_scheduled=true){
|
public static function SetNewLogTime($state, $dateTime){
|
||||||
try {
|
try {
|
||||||
$con = Propel::getConnection();
|
$con = Propel::getConnection();
|
||||||
|
|
||||||
|
@ -156,16 +235,14 @@ class Application_Model_LiveLog
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function SetEndTime($state, $dateTime){
|
public static function SetEndTime($state, $dateTime, $override=false){
|
||||||
try {
|
try {
|
||||||
$con = Propel::getConnection();
|
$con = Propel::getConnection();
|
||||||
|
|
||||||
//if ($state == 'L') {
|
|
||||||
$dj_live = Application_Model_Preference::GetSourceSwitchStatus('live_dj');
|
$dj_live = Application_Model_Preference::GetSourceSwitchStatus('live_dj');
|
||||||
$master_live = Application_Model_Preference::GetSourceSwitchStatus('master_dj');
|
$master_live = Application_Model_Preference::GetSourceSwitchStatus('master_dj');
|
||||||
//}
|
|
||||||
|
|
||||||
if (($dj_live=='off' && $master_live=='off') || $state == 'S') {
|
if (($dj_live=='off' && $master_live=='off') || $state == 'S' || $override) {
|
||||||
$sql = "SELECT id, state from cc_live_log"
|
$sql = "SELECT id, state from cc_live_log"
|
||||||
." where id in (select max(id) from cc_live_log)";
|
." where id in (select max(id) from cc_live_log)";
|
||||||
$row = $con->query($sql)->fetch();
|
$row = $con->query($sql)->fetch();
|
||||||
|
@ -176,13 +253,13 @@ class Application_Model_LiveLog
|
||||||
if ($row['state'] == $state) {
|
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['id']}'";
|
||||||
$con->exec($update_sql);
|
$con->exec($update_sql);
|
||||||
}
|
}
|
||||||
|
|
||||||
//If live broadcasting is off, turn scheduled play on
|
//If live broadcasting is off, turn scheduled play on
|
||||||
$scheduled = Application_Model_Preference::GetSourceSwitchStatus('scheduled_play');
|
$scheduled = Application_Model_Preference::GetSourceSwitchStatus('scheduled_play');
|
||||||
if ($state == 'L' && $scheduled=='on') {
|
if ($state == 'L' && $scheduled=='on' && !$override) {
|
||||||
self::SetNewLogTime('S', $dateTime);
|
self::SetNewLogTime('S', $dateTime);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue