diff options
author | diosmosis <benaka@piwik.pro> | 2015-03-11 15:16:06 +0300 |
---|---|---|
committer | diosmosis <benaka@piwik.pro> | 2015-03-11 15:16:06 +0300 |
commit | 446554ef52e0e6c9a90c0610d7e72429eb2c7cca (patch) | |
tree | b9d5e4590a9ab0c2f089e1b9c8bcbc9baf217265 /plugins | |
parent | 1caf60c21989651d6b8e0cc5de14a41f5448b6a0 (diff) | |
parent | 761cd3e14c94e87e44ca950ab72b93e9b36f10a7 (diff) |
Merge branch 'master' into geo-attribution-task
Diffstat (limited to 'plugins')
-rw-r--r-- | plugins/Live/API.php | 47 | ||||
-rw-r--r-- | plugins/Live/Controller.php | 4 | ||||
-rw-r--r-- | plugins/Live/Model.php | 113 | ||||
-rw-r--r-- | plugins/Live/Reports/GetSimpleLastVisitCount.php | 3 | ||||
-rw-r--r-- | plugins/Live/tests/System/APITest.php | 32 |
5 files changed, 159 insertions, 40 deletions
diff --git a/plugins/Live/API.php b/plugins/Live/API.php index ed11c6c277..9fc5b82b16 100644 --- a/plugins/Live/API.php +++ b/plugins/Live/API.php @@ -60,13 +60,56 @@ class API extends \Piwik\Plugin\API * @param int $idSite Id Site * @param int $lastMinutes Number of minutes to look back at * @param bool|string $segment + * @param array $showColumns The columns to show / not to request. Eg 'visits', 'actions', ... + * @param array $hideColumns The columns to hide / not to request. Eg 'visits', 'actions', ... * @return array( visits => N, actions => M, visitsConverted => P ) */ - public function getCounters($idSite, $lastMinutes, $segment = false) + public function getCounters($idSite, $lastMinutes, $segment = false, $showColumns = array(), $hideColumns = array()) { Piwik::checkUserHasViewAccess($idSite); $model = new Model(); - return $model->queryCounters($idSite, $lastMinutes, $segment); + + $counters = array(); + + $hasVisits = true; + if ($this->shouldColumnBePresentInResponse('visits', $showColumns, $hideColumns)) { + $counters['visits'] = $model->getNumVisits($idSite, $lastMinutes, $segment); + $hasVisits = !empty($counters['visits']); + } + + if ($this->shouldColumnBePresentInResponse('actions', $showColumns, $hideColumns)) { + if ($hasVisits) { + $counters['actions'] = $model->getNumActions($idSite, $lastMinutes, $segment); + } else { + $counters['actions'] = 0; + } + } + + if ($this->shouldColumnBePresentInResponse('visitors', $showColumns, $hideColumns)) { + if ($hasVisits) { + $counters['visitors'] = $model->getNumVisitors($idSite, $lastMinutes, $segment); + } else { + $counters['visitors'] = 0; + } + } + + if ($this->shouldColumnBePresentInResponse('visitsConverted', $showColumns, $hideColumns)) { + if ($hasVisits) { + $counters['visitsConverted'] = $model->getNumVisitsConverted($idSite, $lastMinutes, $segment); + } else { + $counters['visitsConverted'] = 0; + } + } + + return array($counters); + } + + private function shouldColumnBePresentInResponse($column, $showColumns, $hideColumns) + { + $show = (empty($showColumns) || in_array($column, $showColumns)); + $hide = in_array($column, $hideColumns); + + return $show && !$hide; } /** diff --git a/plugins/Live/Controller.php b/plugins/Live/Controller.php index 421a0afb62..a2d3e44026 100644 --- a/plugins/Live/Controller.php +++ b/plugins/Live/Controller.php @@ -88,9 +88,9 @@ class Controller extends \Piwik\Plugin\Controller private function setCounters($view) { $segment = Request::getRawSegmentFromRequest(); - $last30min = API::getInstance()->getCounters($this->idSite, $lastMinutes = 30, $segment); + $last30min = API::getInstance()->getCounters($this->idSite, $lastMinutes = 30, $segment, array('visits', 'actions')); $last30min = $last30min[0]; - $today = API::getInstance()->getCounters($this->idSite, $lastMinutes = 24 * 60, $segment); + $today = API::getInstance()->getCounters($this->idSite, $lastMinutes = 24 * 60, $segment, array('visits', 'actions')); $today = $today[0]; $view->visitorsCountHalfHour = $last30min['visits']; $view->visitorsCountToday = $today['visits']; diff --git a/plugins/Live/Model.php b/plugins/Live/Model.php index 19a44ba1d6..0d1b0318fe 100644 --- a/plugins/Live/Model.php +++ b/plugins/Live/Model.php @@ -201,58 +201,101 @@ class Model * @param $idSite * @param $lastMinutes * @param $segment - * @return array + * @return int * @throws Exception */ - public function queryCounters($idSite, $lastMinutes, $segment) + public function getNumActions($idSite, $lastMinutes, $segment) { - $lastMinutes = (int)$lastMinutes; + return $this->getLastMinutesCounterForQuery( + $idSite, + $lastMinutes, + $segment, + 'COUNT(*)', + 'log_link_visit_action', + 'log_link_visit_action.server_time >= ?' + ); + } - $counters = array( - 'visits' => 0, - 'actions' => 0, - 'visitors' => 0, - 'visitsConverted' => 0, + /** + * @param $idSite + * @param $lastMinutes + * @param $segment + * @return int + * @throws Exception + */ + public function getNumVisitsConverted($idSite, $lastMinutes, $segment) + { + return $this->getLastMinutesCounterForQuery( + $idSite, + $lastMinutes, + $segment, + 'COUNT(*)', + 'log_conversion', + 'log_conversion.server_time >= ?' ); + } + + /** + * @param $idSite + * @param $lastMinutes + * @param $segment + * @return int + * @throws Exception + */ + public function getNumVisits($idSite, $lastMinutes, $segment) + { + return $this->getLastMinutesCounterForQuery( + $idSite, + $lastMinutes, + $segment, + 'COUNT(log_visit.visit_last_action_time)', + 'log_visit', + 'log_visit.visit_last_action_time >= ?' + ); + } + + /** + * @param $idSite + * @param $lastMinutes + * @param $segment + * @return int + * @throws Exception + */ + public function getNumVisitors($idSite, $lastMinutes, $segment) + { + return $this->getLastMinutesCounterForQuery( + $idSite, + $lastMinutes, + $segment, + 'COUNT(DISTINCT log_visit.idvisitor)', + 'log_visit', + 'log_visit.visit_last_action_time >= ?' + ); + } + + private function getLastMinutesCounterForQuery($idSite, $lastMinutes, $segment, $select, $from, $where) + { + $lastMinutes = (int)$lastMinutes; if (empty($lastMinutes)) { - return array($counters); + return 0; } - list($whereIdSites, $idSites) = $this->getIdSitesWhereClause($idSite); + list($whereIdSites, $idSites) = $this->getIdSitesWhereClause($idSite, $from); - $select = "count(*) as visits, COUNT(DISTINCT log_visit.idvisitor) as visitors"; - $where = $whereIdSites . "AND log_visit.visit_last_action_time >= ?"; - $bind = $idSites; + $bind = $idSites; $bind[] = Date::factory(time() - $lastMinutes * 60)->toString('Y-m-d H:i:s'); - $segment = new Segment($segment, $idSite); - $query = $segment->getSelectQuery($select, 'log_visit', $where, $bind); - - $data = Db::fetchAll($query['sql'], $query['bind']); - - $counters['visits'] = $data[0]['visits']; - $counters['visitors'] = $data[0]['visitors']; + $where = $whereIdSites . "AND " . $where; - $select = "count(*)"; - $from = 'log_link_visit_action'; - list($whereIdSites) = $this->getIdSitesWhereClause($idSite, $from); - $where = $whereIdSites . "AND log_link_visit_action.server_time >= ?"; - $query = $segment->getSelectQuery($select, $from, $where, $bind); - $counters['actions'] = Db::fetchOne($query['sql'], $query['bind']); + $segment = new Segment($segment, $idSite); + $query = $segment->getSelectQuery($select, $from, $where, $bind); - $select = "count(*)"; - $from = 'log_conversion'; - list($whereIdSites) = $this->getIdSitesWhereClause($idSite, $from); - $where = $whereIdSites . "AND log_conversion.server_time >= ?"; - $query = $segment->getSelectQuery($select, $from, $where, $bind); - $counters['visitsConverted'] = Db::fetchOne($query['sql'], $query['bind']); + $numVisitors = Db::fetchOne($query['sql'], $query['bind']); - return array($counters); + return $numVisitors; } - - /** * @param $idSite * @param string $table diff --git a/plugins/Live/Reports/GetSimpleLastVisitCount.php b/plugins/Live/Reports/GetSimpleLastVisitCount.php index 34ba773fa9..e2a17135c5 100644 --- a/plugins/Live/Reports/GetSimpleLastVisitCount.php +++ b/plugins/Live/Reports/GetSimpleLastVisitCount.php @@ -30,7 +30,8 @@ class GetSimpleLastVisitCount extends Base { $lastMinutes = Config::getInstance()->General[Controller::SIMPLE_VISIT_COUNT_WIDGET_LAST_MINUTES_CONFIG_KEY]; - $lastNData = Request::processRequest('Live.getCounters', array('lastMinutes' => $lastMinutes)); + $params = array('lastMinutes' => $lastMinutes, 'showColumns' => array('visits', 'visitors', 'actions')); + $lastNData = Request::processRequest('Live.getCounters', $params); $formatter = new Formatter(); diff --git a/plugins/Live/tests/System/APITest.php b/plugins/Live/tests/System/APITest.php index 2c85088a19..4b65a5ae4e 100644 --- a/plugins/Live/tests/System/APITest.php +++ b/plugins/Live/tests/System/APITest.php @@ -70,6 +70,38 @@ class APITest extends SystemTestCase $this->assertEquals($this->buildCounter(0, 0, 0, 0), $counters); } + public function test_GetCounters_ShouldHideAllColumnsIfRequested() + { + $exampleCounter = $this->buildCounter(0, 0, 0, 0); + $counters = $this->api->getCounters($this->idSite, 5, false, array(), array_keys($exampleCounter[0])); + $this->assertEquals(array(array()), $counters); + } + + public function test_GetCounters_ShouldHideSomeColumnsIfRequested() + { + $counters = $this->api->getCounters($this->idSite, 20, false, array(), array('visitsConverted', 'visitors')); + $this->assertEquals(array(array('visits' => 24, 'actions' => 60)), $counters); + } + + public function test_GetCounters_ShouldShowAllColumnsIfRequested() + { + $counter = $this->buildCounter(24, 60, 20, 40); + $counters = $this->api->getCounters($this->idSite, 20, false, array_keys($counter[0])); + $this->assertEquals($counter, $counters); + } + + public function test_GetCounters_ShouldShowSomeColumnsIfRequested() + { + $counters = $this->api->getCounters($this->idSite, 20, false, array('visits', 'actions')); + $this->assertEquals(array(array('visits' => 24, 'actions' => 60)), $counters); + } + + public function test_GetCounters_ShouldHideColumnIfGivenInShowAndHide() + { + $counters = $this->api->getCounters($this->idSite, 20, false, array('visits', 'actions'), array('actions')); + $this->assertEquals(array(array('visits' => 24)), $counters); + } + private function trackSomeVisits() { $nowTimestamp = time(); |