<commit_before><?php

class Application_Model_LiveLog
{

    public static function GetLiveShowDuration($p_keepData=false) {
        try {
            $con = Propel::getConnection();

            $sql = "SELECT * FROM CC_LIVE_LOG"
            ." WHERE state = 'L'"
            ." and (start_time >= (now() - INTERVAL '1 day'))"
            ." ORDER BY id";

            $rows = $con->query($sql)->fetchAll();

            /* Check if last log has end time.
             * If not, set end time to current time
             */
            if ($rows != null) {
                $last_row = self::UpdateLastLogEndTime(array_pop($rows));
                array_push($rows, $last_row);
            }

            $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 (DateInterval 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];

                if (!$p_keepData) {
                    // 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);
                }
            }
            //Trim milliseconds
            $seconds = explode(".", $seconds);
            $minutes = (double)(($hours*60)+$minutes . "." . $seconds[0]);
            //$duration = new DateInterval("PT" . $minutes . "M" . $seconds[0] ."S");
            //return $duration->format("%i.%s");
            return $minutes;
        } catch (Exception $e) {
            header('HTTP/1.0 503 Service Unavailable');
            Logging::log("GetLiveShowDuration - Could not connect to database.");
            exit;
        }
    }

    public static function GetScheduledDuration($p_keepData = false) 
    {
        try {
            $con = Propel::getConnection();

            $sql_get_logs = "SELECT * FROM CC_LIVE_LOG"
            ." WHERE state = 'S'"
            ." and (start_time >= (now() - INTERVAL '1 day'))"
            ." ORDER BY id";

            $rows = $con->query($sql_get_logs)->fetchAll();

            /* Check if last log has end time.
             * If not, set end time to current time
             */
            if ($rows != null) {
                $last_row = self::UpdateLastLogEndTime(array_pop($rows));
                array_push($rows, $last_row);
            }

            $hours = 0;
            $minutes = 0;
            $seconds = 0;

            /* Get all shows and tracks from cc_schedule that played
             * during a scheduled state
             */
            foreach ($rows as $row) {
                $sql_get_tracks = "SELECT * FROM cc_schedule"
                ." WHERE starts >= '{$row['start_time']}'"
                ." AND starts < '{$row['end_time']}'"
                ." AND file_id IS NOT NULL"
                ." AND media_item_played IS TRUE";
                $tracks = $con->query($sql_get_tracks)->fetchAll();
                foreach ($tracks as $track) {
                    if ($track['ends'] > $row['end_time']) {
                        $scheduled_ends = new DateTime($row['end_time']);
                        $track_ends = new DateTime($track['ends']);
                        $extra_time = $scheduled_ends->diff($track_ends);

                        /* Get difference between clip_length
                         * and the extra time. We need to subtract
                         * this difference from the track's
                         * clip 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 {
                            array_push($clip_length_arr, "00");
                        }
                        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);
                        }
                        else {
                            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];
                }

                if (!$p_keepData) {
                    //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);
            $minutes = (double)(($hours*60)+$minutes . "." . $seconds[0]);
            //$duration = new DateInterval("PT". $minutes . "M" . $seconds[0] ."S");
            //return $duration->format("%i.%s");
            return $minutes;
        } catch (Exception $e) {
            header('HTTP/1.0 503 Service Unavailable');
            Logging::log("GetScheduledDuration - Could not connect to database.");
            exit;
        }
    }

    public static function UpdateLastLogEndTime($log) {
        if ($log['end_time'] == null) {
            $current_time = new DateTime("now", new DateTimeZone('UTC'));
            $log['end_time'] = $current_time;
            $log['end_time'] = $log['end_time']->format("Y-m-d H:i:s");
            self::SetEndTime($log['state'], $current_time, true);
            self::SetNewLogTime($log['state'], $current_time);
        }
        return $log;
    }

    public static function SetNewLogTime($state, $dateTime){
        try {
            $con = Propel::getConnection();<commit_msg>- check if variable exists before using if exlpode() was called<commit_after><?php

class Application_Model_LiveLog
{

    public static function GetLiveShowDuration($p_keepData=false) {
        try {
            $con = Propel::getConnection();

            $sql = "SELECT * FROM CC_LIVE_LOG"
            ." WHERE state = 'L'"
            ." and (start_time >= (now() - INTERVAL '1 day'))"
            ." ORDER BY id";

            $rows = $con->query($sql)->fetchAll();

            /* Check if last log has end time.
             * If not, set end time to current time
             */
            if ($rows != null) {
                $last_row = self::UpdateLastLogEndTime(array_pop($rows));
                array_push($rows, $last_row);
            }

            $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);
                for ($i = 0; $i < sizeof($intervals); $i++) {
                    if (!isset($intervals[$i])) {
                        $intervals[$i] = 0;
                    }
                }
                
                // Trim milliseconds (DateInterval does not support)
                $sec = explode(".", $intervals[2]);
                if (isset($sec[0])) {
                    $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];

                if (!$p_keepData) {
                    // 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);
                }
            }
            //Trim milliseconds
            $seconds = explode(".", $seconds);
            if (isset($seconds[0])) {
                $minutes = (double)(($hours*60)+$minutes . "." . $seconds[0]);
            }
            else {
                $minutes = (double)(($hours*60)+$minutes);    
            }
            return $minutes;
        } catch (Exception $e) {
            header('HTTP/1.0 503 Service Unavailable');
            Logging::log("GetLiveShowDuration - Could not connect to database.");
            exit;
        }
    }

    public static function GetScheduledDuration($p_keepData=false) 
    {
        try {
            $con = Propel::getConnection();

            $sql_get_logs = "SELECT * FROM CC_LIVE_LOG"
            ." WHERE state = 'S'"
            ." and (start_time >= (now() - INTERVAL '1 day'))"
            ." ORDER BY id";

            $rows = $con->query($sql_get_logs)->fetchAll();

            /* Check if last log has end time.
             * If not, set end time to current time
             */
            if ($rows != null) {
                $last_row = self::UpdateLastLogEndTime(array_pop($rows));
                array_push($rows, $last_row);
            }

            $hours = 0;
            $minutes = 0;
            $seconds = 0;

            /* Get all shows and tracks from cc_schedule that played
             * during a scheduled state
             */
            foreach ($rows as $row) {
                $sql_get_tracks = "SELECT * FROM cc_schedule"
                ." WHERE starts >= '{$row['start_time']}'"
                ." AND starts < '{$row['end_time']}'"
                ." AND file_id IS NOT NULL"
                ." AND media_item_played IS TRUE";
                $tracks = $con->query($sql_get_tracks)->fetchAll();
                foreach ($tracks as $track) {
                    if ($track['ends'] > $row['end_time']) {
                        $scheduled_ends = new DateTime($row['end_time']);
                        $track_ends = new DateTime($track['ends']);
                        $extra_time = $scheduled_ends->diff($track_ends);

                        /* Get difference between clip_length
                         * and the extra time. We need to subtract
                         * this difference from the track's
                         * clip length.
                         */
                        $clip_length = $track['clip_length'];
                        //Convert clip_length into seconds
                        $clip_length_intervals = explode(":", $clip_length);
                        for ($i = 0; $i < sizeof($clip_length_intervals); $i++) {
                            if (!isset($clip_length_intervals[$i])) {
                                $clip_length_intervals[$i] = 0;
                            }
                        }
                        $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);
                        for ($i = 0; $i < sizeof($extra_time_intervals); $i++) {
                            if (!isset($extra_time_intervals[$i])) {
                                $extra_time_intervals[$i] = 0;
                            }
                        }
                        $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 {
                            array_push($clip_length_arr, "00");
                        }
                        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);
                        }
                        else {
                            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);
                    for ($i = 0; $i < sizeof($intervals); $i++) {
                        if (!isset($intervals[$i])) {
                            $intervals[$i] = 0;
                        }
                    }
                    // Trim milliseconds (DateInteral does not support)
                    $sec = explode(".", $intervals[2]);
                    if (isset($sec[0])) {
                        $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];
                }

                if (!$p_keepData) {
                    //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);
            if (isset($seconds[0])) {
                $minutes = (double)(($hours*60)+$minutes . "." . $seconds[0]);
            }
            else {
                $minutes = (double)(($hours*60)+$minutes);
            }
            return $minutes;
        } catch (Exception $e) {
            header('HTTP/1.0 503 Service Unavailable');
            Logging::log("GetScheduledDuration - Could not connect to database.");
            exit;
        }
    }

    public static function UpdateLastLogEndTime($log) {
        if ($log['end_time'] == null) {
            $current_time = new DateTime("now", new DateTimeZone('UTC'));
            $log['end_time'] = $current_time;
            $log['end_time'] = $log['end_time']->format("Y-m-d H:i:s");
            self::SetEndTime($log['state'], $current_time, true);
            self::SetNewLogTime($log['state'], $current_time);
        }
        return $log;
    }

    public static function SetNewLogTime($state, $dateTime){
        try {
            $con = Propel::getConnection();
{'fraction_non_alphanumeric': 0.12941306878013634, 'fraction_numerical': 0.012969018455910879, 'mean_word_length': 1.1994149195514383, 'pattern_counts': {'":': 10, '<': 11, '<?xml version=': 0, '>': 51, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '401036', 'n_tokens_mistral': 4973, 'n_tokens_neox': 4479, 'n_words': 1010}