diff --git a/modules/statistics/controllers/ItemController.php b/modules/statistics/controllers/ItemController.php index cf3e5f448..707f0cb86 100644 --- a/modules/statistics/controllers/ItemController.php +++ b/modules/statistics/controllers/ItemController.php @@ -25,7 +25,12 @@ class Statistics_ItemController extends Statistics_AppController public $_models = array('Item'); public $_components = array('Utility'); - /** index action*/ + /** + * Render the statistics view for a set of items + * @param [startDate] The start of the date range (inclusive, default = 1 month ago) + * @param [endDate] The end of the date range (inclusive, default = today) + * @param [limit] Result limit for the map (default = 1000) + */ function indexAction() { $itemIds = $this->_getParam('id'); @@ -76,6 +81,27 @@ function indexAction() } } + $limit = $this->_getParam('limit'); + if(isset($limit) && is_numeric($limit) && $limit > 0) + { + $limit = (int)$limit; + } + else + { + $limit = 1000; + } + + $startDate = $this->_getParam('startDate'); + if(!isset($startDate)) + { + $startDate = date('m/d/Y', strtotime('-1 month')); + } + $endDate = $this->_getParam('endDate'); + if(!isset($endDate)) + { + $endDate = date('m/d/Y'); + } + foreach($arrayDownload as $key => $value) { $jqplotArray[] = array($key.' 8:00AM', $value); @@ -83,8 +109,9 @@ function indexAction() $this->view->json['stats']['downloads'] = $jqplotArray; $this->view->itemIds = $itemIds; $this->view->json['itemId'] = $itemIds; - $this->view->json['initialStartDate'] = date('m/d/Y', strtotime('-1 month')); - $this->view->json['initialEndDate'] = date('m/d/Y'); + $this->view->json['initialStartDate'] = $startDate; + $this->view->json['initialEndDate'] = $endDate; + $this->view->json['limit'] = $limit; } /** @@ -132,7 +159,7 @@ public function filterAction() } if($this->_getParam('enddate') == '') { - $endDate = date('Y-m-d'); + $endDate = date('Y-m-d 23:59:59'); } else { @@ -146,6 +173,7 @@ public function filterAction() } $downloads = $this->Statistics_Download->getLocatedDownloads($idArray, $startDate, $endDate, $limit); + $totalCount = $this->Statistics_Download->getCountInRange($idArray, $startDate, $endDate, $limit); $markers = array(); foreach($downloads as $download) @@ -159,7 +187,7 @@ public function filterAction() 'longitude' => $longitude); } } - echo JsonComponent::encode(array('downloads' => $markers)); + echo JsonComponent::encode(array('downloads' => $markers, 'count' => $totalCount)); } }//end class diff --git a/modules/statistics/models/pdo/DownloadModel.php b/modules/statistics/models/pdo/DownloadModel.php index 74ed8e8b9..7d4369fae 100644 --- a/modules/statistics/models/pdo/DownloadModel.php +++ b/modules/statistics/models/pdo/DownloadModel.php @@ -72,6 +72,25 @@ function getLocatedDownloads($ids, $startDate, $endDate, $limit = 99999) return $result; } + /** + * Return the total number of downloads for the given items in the given date range + * @param ids Array of item ids to aggregate statistics for + */ + function getCountInRange($ids, $startDate, $endDate, $limit = 99999) + { + $result = array(); + $sql = $this->database->select() + ->setIntegrityCheck(false) + ->from(array('d' => 'statistics_download'), array('count' => 'count(*)')) + ->joinLeft(array('ipl' => 'statistics_ip_location'), 'd.ip_location_id = ipl.ip_location_id') + ->where('date >= ?', $startDate) + ->where('date <= ?', $endDate) + ->where('item_id IN (?)', $ids) + ->limit($limit); + $row = $this->database->fetchRow($sql); + return $row['count']; + } + /** * Return a list of downloads that have not yet had geolocation run on them */ diff --git a/modules/statistics/public/js/item/item.index.js b/modules/statistics/public/js/item/item.index.js index 0c34bbd7c..a9a072404 100644 --- a/modules/statistics/public/js/item/item.index.js +++ b/modules/statistics/public/js/item/item.index.js @@ -29,7 +29,19 @@ midas.statistics.populateMap = function(responseText, statusText, xhr, form) { } midas.statistics.clusterer.clearMarkers(); midas.statistics.clusterer.addMarkers(midas.statistics.mapMarkers); - $('#filteredCount').html(response.downloads.length); + + if(typeof window.history.replaceState == 'function') { + var params = '?id='+json.itemId; + params += '&startDate='+$('#startdate').val(); + params += '&endDate='+$('#enddate').val(); + params += '&limit='+$('#downloadResultLimit').val(); + + window.history.replaceState({}, '', params); + } + + var html = response.count + ' downloads in the selected period ('; + html += (response.count - response.downloads.length) + ' from unknown locations)'; + $('#filteredCount').html(html); } catch (e) { alert("An error occured. Please check the logs."); return false; @@ -109,5 +121,6 @@ $(document).ready(function() { $('#startdate').val(json.initialStartDate); $('#enddate').val(json.initialEndDate); + $('#downloadResultLimit').val(json.limit); $('#filterForm').submit(); }); diff --git a/modules/statistics/views/item/index.phtml b/modules/statistics/views/item/index.phtml index 6bc8faa3b..3100f5ff1 100644 --- a/modules/statistics/views/item/index.phtml +++ b/modules/statistics/views/item/index.phtml @@ -48,7 +48,7 @@ $this->headScript()->appendFile($this->coreWebroot.'/public/js/jquery/jquery.for
- downloads displayed +