diff --git a/airtime_mvc/application/controllers/ListenerstatController.php b/airtime_mvc/application/controllers/ListenerstatController.php index 2247a6b03..97516e815 100644 --- a/airtime_mvc/application/controllers/ListenerstatController.php +++ b/airtime_mvc/application/controllers/ListenerstatController.php @@ -59,9 +59,18 @@ class ListenerstatController extends Zend_Controller_Action public function getDataAction(){ list($startsDT, $endsDT) = Application_Common_HTTPHelper::getStartEndFromRequest($this->getRequest()); - + $data = Application_Model_ListenerStat::getDataPointsWithinRange($startsDT->format(DEFAULT_TIMESTAMP_FORMAT), $endsDT->format(DEFAULT_TIMESTAMP_FORMAT)); $this->_helper->json->sendJson($data); } + public function getShowDataAction(){ + list($startsDT, $endsDT) = Application_Common_HTTPHelper::getStartEndFromRequest($this->getRequest()); + $show_id = $this->getRequest()->getParam("show_id", null); + Logging::info($this->getRequest()->getParam("show_id", null)); + Logging::info($show_id); + $data = Application_Model_ListenerStat::getShowDataPointsWithinRange($startsDT->format(DEFAULT_TIMESTAMP_FORMAT), + $endsDT->format(DEFAULT_TIMESTAMP_FORMAT),$show_id); + $this->_helper->json->sendJson($data); + } } diff --git a/airtime_mvc/application/models/ListenerStat.php b/airtime_mvc/application/models/ListenerStat.php index c43cf0fb7..b4e037697 100644 --- a/airtime_mvc/application/models/ListenerStat.php +++ b/airtime_mvc/application/models/ListenerStat.php @@ -16,8 +16,8 @@ group by mount_name SQL; $data = Application_Common_Database::prepareAndExecute($sql, array('p1'=>$p_start, 'p2'=>$p_end)); - $out = array(); + foreach ($data as $d) { $jump = intval($d['count']/1000); $jump = max(1, $jump); @@ -36,7 +36,7 @@ WHERE (temp.rownum%:p4) = :p5; SQL; $result = Application_Common_Database::prepareAndExecute($sql, array('p1'=>$p_start, 'p2'=>$p_end, 'p3'=>$d['mount_name'], 'p4'=>$jump, 'p5'=>$remainder)); - + $utcTimezone = new DateTimeZone("UTC"); $displayTimezone = new DateTimeZone(Application_Model_Preference::GetUserTimezone()); @@ -51,6 +51,7 @@ SQL; } } + return $out; $enabledStreamIds = Application_Model_StreamSetting::getEnabledStreamIds(); $enabledOut = array(); @@ -77,6 +78,41 @@ SQL; return $enabledOut; } +// this will currently log the average number of listeners to a specific show during a certain range + public static function getShowDataPointsWithinRange($p_start, $p_end, $show_id) { + $showData = []; + $ccShow = CcShowQuery::create()->findPk($show_id); + $showName = $ccShow->getDbName(); + + + // this query selects all show instances that aired in this date range that match the show_id + $sql = <<=:p2 AND ends <=:p3 +SQL; + $data = Application_Common_Database::prepareAndExecute($sql, + array('p1'=>$show_id,'p2'=>$p_start, 'p3'=>$p_end)); + foreach ($data as $d) { + $sql = <<=:p1 AND ts.timestamp <=:p2) +GROUP BY timestamp +SQL; + $data = Application_Common_Database::prepareAndExecute($sql, + array('p1'=>$d['starts'], 'p2'=>$d['ends'])); + if (sizeof($data) > 0) { + $average_listeners = array_sum(array_column($data, 'listeners')) / sizeof($data); + $max_num_listeners = max(array_column($data, 'listeners')); + $entry = array("show" => $showName, "time" => $data[0]['timestamp'], "average_number_of_listeners" => $average_listeners, + "maximum_number_of_listeners" => $max_num_listeners); + array_push($showData, $entry); + } + } + return($showData); + } public static function insertDataPoints($p_dataPoints) {