<commit_before><?php

class Application_Model_Nowplaying
{

    private static function CreateHeaderRow($p_showName, $p_showStart, $p_showEnd){
        return array("h", "", $p_showStart, $p_showEnd, $p_showName, "", "", "", "", "", "");
    }

    private static function CreateDatatableRows($p_dbRows){
        $dataTablesRows = array();

        $epochNow = time();

        $lastRow = end($p_dbRows);
        
        //Information about show is true for all rows in parameter so only check the last row's show
        //start and end times.
        if (isset($lastRow)){
            $showStartDateTime = Application_Model_DateHelper::ConvertToLocalDateTime($lastRow['show_starts']);
            $showEndDateTime = Application_Model_DateHelper::ConvertToLocalDateTime($lastRow['show_ends']);
            $showStarts = $showStartDateTime->format("Y-m-d H:i:s");
            $showEnds = $showEndDateTime->format("Y-m-d H:i:s");
        }
        
        foreach ($p_dbRows as $dbRow) {

            $itemStartDateTime = Application_Model_DateHelper::ConvertToLocalDateTime($dbRow['item_starts']);
            $itemEndDateTime = Application_Model_DateHelper::ConvertToLocalDateTime($dbRow['item_ends']);
            
            $itemStarts = $itemStartDateTime->format("Y-m-d H:i:s");
            $itemEnds = $itemEndDateTime->format("Y-m-d H:i:s");

            $status = ($dbRow['show_ends'] < $dbRow['item_ends']) ? "x" : "";

            $type = "a";
            $type .= ($itemStartDateTime->getTimestamp() <= $epochNow
                    && $epochNow < $itemEndDateTime->getTimestamp()
                    && $epochNow < $showEndDateTime->getTimestamp()) ? "c" : "";

            // remove millisecond from the time format
            $itemStart = explode('.', $dbRow['item_starts']);
            $itemEnd = explode('.', $dbRow['item_ends']);

            //format duration
            $duration = explode('.', $dbRow['clip_length']);
            $formatted = self::FormatDuration($duration[0]);
            $dataTablesRows[] = array($type, $itemStarts, $itemStarts, $itemEnds,
                $formatted, $dbRow['track_title'], $dbRow['artist_name'], $dbRow['album_title'],
                $dbRow['playlist_name'], $dbRow['show_name'], $status);
        }

        //Modify the last entry in the data table to adjust its end time to be equal to the
        //shows end time if it exceeds it.
        $lastRow = end($dataTablesRows);
        if (isset($lastRow) && strtotime($showEnds) < strtotime($lastRow[3])){
            $dataTablesRows[sizeof($dataTablesRows)-1][3] = $showEnds;
        }
        return $dataTablesRows;
    }

    private static function CreateGapRow($p_gapTime){
        return array("g", "", "", "", $p_gapTime, "", "", "", "", "", "");
    }

    private static function CreateRecordingRow($p_showInstance){
        return array("r", "", "", "", $p_showInstance->getName(), "", "", "", "", "", "");
    }

    public static function GetDataGridData($viewType,  $dateString){

        if ($viewType == "now"){
            $dateTime = new DateTime("now", new DateTimeZone("UTC"));
            $timeNow = $dateTime->format("Y-m-d H:i:s");
            
            $startCutoff = 60;
            $endCutoff = 86400; //60*60*24 - seconds in a day
        } else {
            $date = new Application_Model_DateHelper;
            $time = $date->getTime();
            $date->setDate($dateString." ".$time);
            $timeNow = $date->getUtcTimestamp();

            $startCutoff = $date->getNowDayStartDiff();
            $endCutoff = $date->getNowDayEndDiff();
        }

        $data = array();

        $showIds = Application_Model_ShowInstance::GetShowsInstancesIdsInRange($timeNow, $startCutoff, $endCutoff);
    
        //get all the pieces to be played between the start cut off and the end cut off.
        $scheduledItems = Application_Model_Schedule::getScheduleItemsInRange($timeNow, $startCutoff, $endCutoff);

        $orderedScheduledItems;
        foreach ($scheduledItems as $scheduledItem){
            $orderedScheduledItems[$scheduledItem['instance_id']][] = $scheduledItem;
        }

        foreach ($showIds as $showId){
            $instanceId = $showId['id'];

            //gets the show information
            $si = new Application_Model_ShowInstance($instanceId);

            $showId = $si->getShowId();
            $show = new Application_Model_Show($showId);
            
            $showStartDateTime = Application_Model_DateHelper::ConvertToLocalDateTime($si->getShowInstanceStart());
            $showEndDateTime = Application_Model_DateHelper::ConvertToLocalDateTime($si->getShowInstanceEnd());

            //append show header row
            $data[] = self::CreateHeaderRow($show->getName(), $showStartDateTime->format("Y-m-d H:i:s"), $showEndDateTime->format("Y-m-d H:i:s"));

            $dataTablesRows = self::CreateDatatableRows($orderedScheduledItems[$instanceId]);

            //append show audio item rows<commit_msg>CC-3359: Now Playing -> Day view has a bug with supporting UTC time<commit_after><?php

class Application_Model_Nowplaying
{

    private static function CreateHeaderRow($p_showName, $p_showStart, $p_showEnd){
        return array("h", "", $p_showStart, $p_showEnd, $p_showName, "", "", "", "", "", "");
    }

    private static function CreateDatatableRows($p_dbRows){
        $dataTablesRows = array();

        $epochNow = time();

        $lastRow = end($p_dbRows);
        
        //Information about show is true for all rows in parameter so only check the last row's show
        //start and end times.
        if (isset($lastRow)){
            $showStartDateTime = Application_Model_DateHelper::ConvertToLocalDateTime($lastRow['show_starts']);
            $showEndDateTime = Application_Model_DateHelper::ConvertToLocalDateTime($lastRow['show_ends']);
            $showStarts = $showStartDateTime->format("Y-m-d H:i:s");
            $showEnds = $showEndDateTime->format("Y-m-d H:i:s");
        }
        
        foreach ($p_dbRows as $dbRow) {

            $itemStartDateTime = Application_Model_DateHelper::ConvertToLocalDateTime($dbRow['item_starts']);
            $itemEndDateTime = Application_Model_DateHelper::ConvertToLocalDateTime($dbRow['item_ends']);
            
            $itemStarts = $itemStartDateTime->format("Y-m-d H:i:s");
            $itemEnds = $itemEndDateTime->format("Y-m-d H:i:s");

            $status = ($dbRow['show_ends'] < $dbRow['item_ends']) ? "x" : "";

            $type = "a";
            $type .= ($itemStartDateTime->getTimestamp() <= $epochNow
                    && $epochNow < $itemEndDateTime->getTimestamp()
                    && $epochNow < $showEndDateTime->getTimestamp()) ? "c" : "";

            // remove millisecond from the time format
            $itemStart = explode('.', $dbRow['item_starts']);
            $itemEnd = explode('.', $dbRow['item_ends']);

            //format duration
            $duration = explode('.', $dbRow['clip_length']);
            $formatted = self::FormatDuration($duration[0]);
            $dataTablesRows[] = array($type, $itemStarts, $itemStarts, $itemEnds,
                $formatted, $dbRow['track_title'], $dbRow['artist_name'], $dbRow['album_title'],
                $dbRow['playlist_name'], $dbRow['show_name'], $status);
        }

        //Modify the last entry in the data table to adjust its end time to be equal to the
        //shows end time if it exceeds it.
        $lastRow = end($dataTablesRows);
        if (isset($lastRow) && strtotime($showEnds) < strtotime($lastRow[3])){
            $dataTablesRows[sizeof($dataTablesRows)-1][3] = $showEnds;
        }
        return $dataTablesRows;
    }

    private static function CreateGapRow($p_gapTime){
        return array("g", "", "", "", $p_gapTime, "", "", "", "", "", "");
    }

    private static function CreateRecordingRow($p_showInstance){
        return array("r", "", "", "", $p_showInstance->getName(), "", "", "", "", "", "");
    }

    public static function GetDataGridData($viewType,  $dateString){

        if ($viewType == "now"){
            $dateTime = new DateTime("now", new DateTimeZone("UTC"));
            $timeNow = $dateTime->format("Y-m-d H:i:s");
            
            $startCutoff = 60;
            $endCutoff = 86400; //60*60*24 - seconds in a day
        } else {
            $time = date("H:i:s");
            $utcDate = Application_Model_DateHelper::ConvertToUtcDateTimeString($dateString." ".$time);
            
            $date = new Application_Model_DateHelper;
            $date->setDate($utcDate);
            
            $timeNow = $date->getUtcTimestamp();

            $startCutoff = $date->getNowDayStartDiff();
            $endCutoff = $date->getNowDayEndDiff();
        }

        $data = array();

        $showIds = Application_Model_ShowInstance::GetShowsInstancesIdsInRange($timeNow, $startCutoff, $endCutoff);
        
    
        //get all the pieces to be played between the start cut off and the end cut off.
        $scheduledItems = Application_Model_Schedule::getScheduleItemsInRange($timeNow, $startCutoff, $endCutoff);

        $orderedScheduledItems;
        foreach ($scheduledItems as $scheduledItem){
            $orderedScheduledItems[$scheduledItem['instance_id']][] = $scheduledItem;
        }

        foreach ($showIds as $showId){
            $instanceId = $showId['id'];

            //gets the show information
            $si = new Application_Model_ShowInstance($instanceId);

            $showId = $si->getShowId();
            $show = new Application_Model_Show($showId);
            
            $showStartDateTime = Application_Model_DateHelper::ConvertToLocalDateTime($si->getShowInstanceStart());
            $showEndDateTime = Application_Model_DateHelper::ConvertToLocalDateTime($si->getShowInstanceEnd());

            //append show header row
            $data[] = self::CreateHeaderRow($show->getName(), $showStartDateTime->format("Y-m-d H:i:s"), $showEndDateTime->format("Y-m-d H:i:s"));

            $dataTablesRows = self::CreateDatatableRows($orderedScheduledItems[$instanceId]);

            //append show audio item rows
{'fraction_non_alphanumeric': 0.14880660053039976, 'fraction_numerical': 0.0037324427855809843, 'mean_word_length': 2.6573275862068964, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 43, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '924586', 'n_tokens_mistral': 2927, 'n_tokens_neox': 2857, 'n_words': 566}