Skip to content

Commit

Permalink
Added config option fornumber of months data to include.
Browse files Browse the repository at this point in the history
  • Loading branch information
mjordan committed Mar 9, 2018
1 parent 30b4967 commit f49325b
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 11 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Displays object (and collection) usage stats collected by [Islandora Usage Stats

![Example chart](usage_stats_example.png)

Only the last 6 months of data is shown in the chart.
By default, only the last 6 months of data is shown in the chart, although the number of months can be configured.

## Dependencies

Expand All @@ -22,7 +22,7 @@ Install as usual, see [this](https://drupal.org/documentation/install/modules-th

## Configuration

1. Set the colors for your charts' "views" and "downloads" bars at `admin/islandora/tools/islandora_usage_stats_charts`.
1. Set the number of months' data, colors for your charts' "views" and "downloads" bars, etc. at `admin/islandora/tools/islandora_usage_stats_charts`.
1. Enable and configure the "Islandora Usage Stats Charts: object-level report" and "Islandora Usage Stats Charts: collection-level report" blocks.
1. In the object-level block, you have the option of having the block collapsed or open by default. This setting can be found within the block's "configure" options.

Expand Down
20 changes: 12 additions & 8 deletions includes/blocks.inc
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,10 @@ function islandora_usage_stats_charts_generate_stats($pid) {
$js_url = variable_get('islandora_usage_stats_charts_javascript_url', 'https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.7.2/Chart.bundle.js');
drupal_add_js($js_url, 'external');

// Get usage stats going back six months, grouped by month.
$six_months_ago = $six_monts_ago = strtotime('-6 months', time());
$views_result = db_query("SELECT year(FROM_UNIXTIME(time)) AS year, month(FROM_UNIXTIME(time)) AS month, count(*) AS count FROM {islandora_usage_stats_object_access_log} WHERE time BETWEEN :ago AND NOW() AND pid_id = :pid_id GROUP BY month(FROM_UNIXTIME(time))", array(':ago' => $six_months_ago, ':pid_id' => $access_log_id));
// Get usage stats going back X months, grouped by month.
$num_months = variable_get('islandora_usage_stats_charts_months_to_show', '6');
$ago = strtotime('-' . $num_months . ' months', time());
$views_result = db_query("SELECT year(FROM_UNIXTIME(time)) AS year, month(FROM_UNIXTIME(time)) AS month, count(*) AS count FROM {islandora_usage_stats_object_access_log} WHERE time BETWEEN :ago AND NOW() AND pid_id = :pid_id GROUP BY month(FROM_UNIXTIME(time))", array(':ago' => $ago, ':pid_id' => $access_log_id));
$views_data = array();
foreach ($views_result as $row) {
$month = str_pad($row->month, 2, '0', STR_PAD_LEFT);
Expand All @@ -87,11 +88,11 @@ function islandora_usage_stats_charts_generate_stats($pid) {
// drupal_alter('islandora_usage_stats_charts_usage', $views_data, $pid, 'views');
ksort($views_data, SORT_NATURAL);

// Get the downloads stats going back six months, grouped by month, same
// Get the downloads stats going back X months, grouped by month, same
// as the usage data.
$downloads_chart_values = array();
if (variable_get('islandora_usage_stats_charts_show_datastreams_downloads', 1)) {
$downloads_result = db_query("SELECT year(FROM_UNIXTIME({islandora_usage_stats_object_ds_access_log}.time)) AS year, month(FROM_UNIXTIME({islandora_usage_stats_object_ds_access_log}.time)) AS month, COUNT(*) AS count FROM {islandora_usage_stats_datastreams}, {islandora_usage_stats_object_ds_access_log} WHERE {islandora_usage_stats_object_ds_access_log}.time BETWEEN :ago AND NOW() AND {islandora_usage_stats_datastreams}.pid_id = :pid_id GROUP BY month(FROM_UNIXTIME({islandora_usage_stats_object_ds_access_log}.time))", array(':ago' => $six_months_ago, ':pid_id' => $access_log_id));
$downloads_result = db_query("SELECT year(FROM_UNIXTIME({islandora_usage_stats_object_ds_access_log}.time)) AS year, month(FROM_UNIXTIME({islandora_usage_stats_object_ds_access_log}.time)) AS month, COUNT(*) AS count FROM {islandora_usage_stats_datastreams}, {islandora_usage_stats_object_ds_access_log} WHERE {islandora_usage_stats_object_ds_access_log}.time BETWEEN :ago AND NOW() AND {islandora_usage_stats_datastreams}.pid_id = :pid_id GROUP BY month(FROM_UNIXTIME({islandora_usage_stats_object_ds_access_log}.time))", array(':ago' => $ago, ':pid_id' => $access_log_id));
$downloads_data = array();
foreach ($downloads_result as $row) {
$month = str_pad($row->month, 2, '0', STR_PAD_LEFT);
Expand Down Expand Up @@ -150,6 +151,7 @@ function islandora_usage_stats_charts_generate_stats($pid) {
'viewsColor' => variable_get('islandora_usage_stats_charts_views_color', '#002db3'),
'downloadsColor' => variable_get('islandora_usage_stats_charts_downloads_color', '#99b3ff'),
'showDownloads' => variable_get('islandora_usage_stats_charts_show_datastreams_downloads', 0),
'numMonthsData' => $num_months,
);
drupal_add_js(array('islandora_usage_stats_charts' => $config_settings), 'setting');
drupal_add_js(
Expand All @@ -176,9 +178,10 @@ function islandora_usage_stats_charts_generate_collection_stats($pid) {
$js_url = variable_get('islandora_usage_stats_charts_javascript_url', 'https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.7.2/Chart.bundle.js');
drupal_add_js($js_url, 'external');

// Get usage stats going back six months, grouped by month.
$six_months_ago = $six_monts_ago = strtotime('-6 months', time());
$result = db_query("SELECT year(FROM_UNIXTIME({islandora_usage_stats_object_access_log}.time)) AS year, month(FROM_UNIXTIME({islandora_usage_stats_object_access_log}.time)) AS month, COUNT(*) AS count FROM {islandora_usage_stats_object_access_log}, {islandora_usage_stats_collection_access_log} WHERE {islandora_usage_stats_object_access_log}.time between :ago AND NOW() AND {islandora_usage_stats_collection_access_log}.collection = :collection_id AND {islandora_usage_stats_collection_access_log}.object_access_id = {islandora_usage_stats_object_access_log}.id GROUP BY month(from_unixtime({islandora_usage_stats_object_access_log}.time))", array(':ago' => $six_months_ago, ':collection_id' => $collection_access_log_id));
// Get usage stats going back X months, grouped by month.
$num_months = variable_get('islandora_usage_stats_charts_months_to_show', '6');
$ago = strtotime('-' . $num_months . ' months', time());
$result = db_query("SELECT year(FROM_UNIXTIME({islandora_usage_stats_object_access_log}.time)) AS year, month(FROM_UNIXTIME({islandora_usage_stats_object_access_log}.time)) AS month, COUNT(*) AS count FROM {islandora_usage_stats_object_access_log}, {islandora_usage_stats_collection_access_log} WHERE {islandora_usage_stats_object_access_log}.time between :ago AND NOW() AND {islandora_usage_stats_collection_access_log}.collection = :collection_id AND {islandora_usage_stats_collection_access_log}.object_access_id = {islandora_usage_stats_object_access_log}.id GROUP BY month(from_unixtime({islandora_usage_stats_object_access_log}.time))", array(':ago' => $ago, ':collection_id' => $collection_access_log_id));
$data = array();
foreach ($result as $row) {
$month = str_pad($row->month, 2, '0', STR_PAD_LEFT);
Expand All @@ -200,6 +203,7 @@ function islandora_usage_stats_charts_generate_collection_stats($pid) {
'viewsColor' => variable_get('islandora_usage_stats_charts_views_color', '#002db3'),
'downloadsColor' => variable_get('islandora_usage_stats_charts_downloads_color', '#99b3ff'),
'showDownloads' => variable_get('islandora_usage_stats_charts_show_datastreams_downloads', 0),
'numMonthsData' => $num_months,
);
drupal_add_js(array('islandora_usage_stats_charts' => $config_settings), 'setting');
drupal_add_js(
Expand Down
6 changes: 6 additions & 0 deletions islandora_usage_stats_charts.module
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@ function islandora_usage_stats_charts_admin_settings() {
'#default_value' => variable_get('islandora_usage_stats_charts_downloads_color', '#99b3ff'),
'#description' => t('Use hexidecimal color values.'),
);
$form['islandora_usage_stats_charts_months_to_show'] = array(
'#type' => 'textfield',
'#size' => '10',
'#title' => t("Number of months' data to show in chart"),
'#default_value' => variable_get('islandora_usage_stats_charts_months_to_show', '6'),
);
$form['islandora_usage_stats_charts_show_datastreams_downloads'] = array(
'#type' => 'checkbox',
'#title' => t('Show datastream download stats'),
Expand Down
2 changes: 1 addition & 1 deletion js/usageStatsChart.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ var usageChart = new Chart(islandoraUsageChart, {
options: {
title: {
display: true,
text: 'Usage stats for the last six months'
text: 'Usage stats for the last ' + Drupal.settings.islandora_usage_stats_charts.numMonthsData + ' months'
},
scales: {
yAxes: [{
Expand Down

0 comments on commit f49325b

Please sign in to comment.