Skip to content
This repository has been archived by the owner on Sep 10, 2021. It is now read-only.

Commit

Permalink
ENH: refs #869. Auto-update URL in item view when paramters change
Browse files Browse the repository at this point in the history
Also improve the info on the bottom of the map.
  • Loading branch information
zachmullen committed Nov 6, 2012
1 parent 35a9c64 commit 14567a2
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 7 deletions.
38 changes: 33 additions & 5 deletions modules/statistics/controllers/ItemController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -76,15 +81,37 @@ 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);
}
$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;
}

/**
Expand Down Expand Up @@ -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
{
Expand All @@ -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)
Expand All @@ -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
19 changes: 19 additions & 0 deletions modules/statistics/models/pdo/DownloadModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
15 changes: 14 additions & 1 deletion modules/statistics/public/js/item/item.index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -109,5 +121,6 @@ $(document).ready(function() {

$('#startdate').val(json.initialStartDate);
$('#enddate').val(json.initialEndDate);
$('#downloadResultLimit').val(json.limit);
$('#filterForm').submit();
});
2 changes: 1 addition & 1 deletion modules/statistics/views/item/index.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ $this->headScript()->appendFile($this->coreWebroot.'/public/js/jquery/jquery.for
</form>
</div>
<div id="map_canvas" style="width:800px; height:470px;"></div>
<span id="filteredCount"></span> downloads displayed
<span id="filteredCount"></span>
</div>
<div id="tabs-chart">
<div id="chartDownloads" style="height:450px; width:800px;"></div>
Expand Down

0 comments on commit 14567a2

Please sign in to comment.