Commit 8ebe1657 authored by Mark Jordan's avatar Mark Jordan
Browse files

Second commit.

parent eee55603
# Islandora Usage Stats Test Data Generator
## Warning
This module is for development purposes only. Do not use it on a production Islandora instance.
## Overview
Drush script to randomize the timestamp and IP address of entries in the `islandora_usage_stats_object_access_log` table. The principle use of this script is to simulate the traffic to a public-facing Islandora for development and testing purposes.
## Dependencies
* [Islandora](https://github.com/Islandora/islandora)
* [Islandora Usage Stats](https://github.com/Islandora/islandora_usage_stats)
## Installation and configuration
Installs as any other Drupal module. There are no configuration options.
### Adding test data
To add test data, run:
`drush islandora_usage_stats_generate_test_access_data_add --pids=islandora:100 --updated_db_ids_file=/tmp/foo.txt --number_hits=5`
will output the following:
```
Hitting http://localhost:8000/islandora/object/islandora:100 5 times
Row 1229 in the islandora_usage_stats_object_access_log table now uses 1485132246 and 70.242.247.190
Row 1230 in the islandora_usage_stats_object_access_log table now uses 1500676890 and 154.193.147.136
Row 1231 in the islandora_usage_stats_object_access_log table now uses 1504726142 and 227.114.8.122
Row 1232 in the islandora_usage_stats_object_access_log table now uses 1497229949 and 207.109.114.78
Row 1233 in the islandora_usage_stats_object_access_log table now uses 1525721306 and 137.60.72.70
```
with the database table looking like this:
```
mysql> select * from islandora_usage_stats_object_access_log;
+----+--------+------------+-------------------+-----+
| id | pid_id | time | ip | uid |
+----+--------+------------+-------------------+-----+
| 1229 | 44 | 1485132246 | 70.242.247.190 | 0 |
| 1230 | 44 | 1500676890 | 154.193.147.136 | 0 |
| 1231 | 44 | 1504726142 | 227.114.8.122 | 0 |
| 1232 | 44 | 1497229949 | 207.109.114.78 | 0 |
| 1233 | 44 | 1525721306 | 137.60.72.70 | 0 |
+------+--------+------------+-----------------+-----+
```
It does this by hitting an object's URL with an HTTP request and then updating the resulting row in the `islandora_usage_stats_object_access_log` table with a random timestamp and random IP address.
### Removing your test data
`drush islandora_usage_stats_generate_test_access_data_delete --updated_db_ids_file=/tmp/foo.txt`
## Maintainer
* [Mark Jordan](https://github.com/mjordan)
## Development and feedback
Pull requests are welcome, as are use cases and suggestions.
## License
* [GPLv3](http://www.gnu.org/licenses/gpl-3.0.txt)
* The sample metadata in includes/sample_metadata.tsv is distriubuted under the [Creative Commons Attribution-NonCommercial 3.0 Unported](http://creativecommons.org/licenses/by-nc/3.0/legalcode) license.
......@@ -10,7 +10,7 @@
*/
function islandora_usage_stats_generate_test_access_data_drush_command() {
$items = array();
$items['islandora_usage_stats_generate_test_access_data'] = array(
$items['islandora_usage_stats_generate_test_access_data_add'] = array(
'aliases' => array('iusgtad'),
'description' => 'Generate test usage stats data for the Islandora Usate Stats module.',
'drupal dependencies' => array('islandora_usage_stats'),
......@@ -28,15 +28,18 @@ function islandora_usage_stats_generate_test_access_data_drush_command() {
'updated_db_ids_file' => array(
'description' => 'Path to a file where IDs of the updated rows in the db are saved. Defaults to "updated_db_ids.txt".',
),
'time_range' => array(
'description' => 'Earliest and latest Unix timestamps defining the randomly selected time data. Separate by a "-". Defaults to "1452384000-1544572800".',
),
),
'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_LOGIN,
);
$items['islandora_usage_stats_delete_test_access_data'] = array(
$items['islandora_usage_stats_generate_test_access_data_delete'] = array(
'aliases' => array('iusdtad'),
'description' => "Delete test usage data based on an input file containing the database IDs of the data.",
'options' => array(
'input_file' => array(
'updated_db_ids_file' => array(
'description' => 'The full or relative path to the input file.',
'required' => TRUE,
),
......@@ -47,10 +50,11 @@ function islandora_usage_stats_generate_test_access_data_drush_command() {
return $items;
}
/**
* Drush command function.
*/
function drush_islandora_usage_stats_generate_test_access_data() {
function drush_islandora_usage_stats_generate_test_access_data_add() {
$number_hits = drush_get_option('number_hits', 1);
$pid_list = drush_get_option('pids');
$base_url = drush_get_option('base_url', 'http://localhost:8000');
......@@ -72,6 +76,10 @@ function drush_islandora_usage_stats_generate_test_access_data() {
'!ip' => $update['ip']))
);
}
elseif ($result->code == '-1') {
drush_set_error('HTTP ERROR',
dt('HTTP request timed out.'));
}
else {
drush_set_error('HTTP ERROR',
dt('HTTP request returned !code', array('!code' => $result->code)));
......@@ -90,11 +98,33 @@ function drush_islandora_usage_stats_generate_test_access_data() {
}
}
/**
* Drush command function.
*/
function drush_islandora_usage_stats_generate_test_access_data_delete() {
$path = drush_get_option('updated_db_ids_file', 'updated_db_ids.txt');
$ids = islandora_usage_stats_generate_test_access_data_read_id_file($path);
foreach ($ids as &$id) {
$id = trim($id);
$num_deleted = db_delete('islandora_usage_stats_object_access_log')
->condition('id', $id)
->execute();
if ($num_deleted == 1) {
drush_print(dt("Row !id of islandora_usage_stats_object_access_log deleted", array('!id' => $id)));
}
if ($num_deleted == 0) {
drush_print(dt("Row !id doesn't exist in islandora_usage_stats_object_access_log", array('!id' => $id)));
}
}
}
/**
* Utility function to randomly generate a random timestamp/IP address pair.
*/
function islandora_usage_stats_generate_test_access_data_get_random() {
$timestamp = mt_rand(1452384000, 1544572800);
$time_range = drush_get_option('time_range', '1452384000-1544572800');
list($start, $end) = explode('-', $time_range);
$timestamp = mt_rand($start, $end);
$ip = long2ip(rand(0, '4294967295'));
return array('time' => $timestamp, 'ip' => $ip);
}
......@@ -140,10 +170,11 @@ function islandora_usage_stats_generate_test_access_data_read_id_file($path) {
$ids = array();
$lines = file($path);
foreach ($lines as $id) {
foreach ($lines as &$id) {
$id = trim($id);
$ids[] = $id;
}
return $pids;
return $ids;
}
/**
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment