-
Notifications
You must be signed in to change notification settings - Fork 1
/
upei_roblib_reports.module
154 lines (141 loc) · 5.86 KB
/
upei_roblib_reports.module
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
<?php
/**
* @file
* Implementation of robertson library reports module.
*/
/**
* Implements hook_menu().
*/
function upei_roblib_reports_menu() {
$items['admin/islandora/tools/roblib/reports'] = array(
'title' => 'Roblib Reports Config',
'description' => 'Configuration for UPEI Roblib reports',
'access arguments' => array('administer site configuration'),
'page callback' => 'drupal_get_form',
'page arguments' => array('upei_roblib_reports_config_form'),
'type' => MENU_NORMAL_ITEM,
);
$items['admin/reports/upei-roblib-reports'] = array(
'title' => 'Islandora Disk Usage Report',
'description' => 'Reports on local Drupal disk usage, database size and islandora disk usage',
'access arguments' => array('access site reports'),
'page callback' => 'upei_roblib_reports_size',
'type' => MENU_NORMAL_ITEM,
);
return $items;
}
/**
* A form for managing the modules configuration.
*
* @param array $form
* A Drupal form array
* @param array $form_state
* A Drupal formstate array
*
* @return array
* A Drupal form
*/
function upei_roblib_reports_config_form($form, &$form_state) {
$form['upei_roblib_reports_interval'] = array(
'#type' => 'textfield',
'#title' => t('A time interval to run the reports.'),
'#default_value' => variable_get('upei_roblib_reports_interval', 7),
'#description' => t('A number of days, if you enter 7 the reports will be generated every 7 days. Must be a positive number'),
'#required' => TRUE,
'#element_validate' => array('element_validate_integer_positive'),
);
$form['upei_roblib_reports_max_storage'] = array(
'#type' => 'textfield',
'#title' => t('The maximum amount of storage this site is allowed to use, in GB'),
'#default_value' => variable_get('upei_roblib_reports_max_storage', 1000),
'#description' => t('The maximum amount of storage including the Drupal file system and the Islandora objects.'),
'#required' => TRUE,
'#element_validate' => array('element_validate_integer_positive'),
);
return system_settings_form($form);
}
/**
* Print the report to the screen
*/
function upei_roblib_reports_size() {
module_load_include('inc', 'upei_roblib_reports', 'includes/utilities');
$drupal_sizes = variable_get('upei_roblib_reports_drupal_file_size', array(
'public' => 0,
'private' => 0
));
$islandora_storage = variable_get('upei_roblib_reports_islandora_size', 0);
$db_storage = variable_get('upei_roblib_reports_db_size', 0);
$total_storage = upei_roblib_reports_get_total_storage();
$rows = array(
array(t('Files in Islandora (Fedora)'), format_size($islandora_storage)),
array(t('Files Uploaded To Drupal (Public)'), format_size($drupal_sizes['public'])),
array(t('Files Uploaded To Drupal (Private)'), format_size($drupal_sizes['private'])),
array(t('Drupal Content (Database)'), format_size($db_storage)),
//t('Total Storage Used %tsu', array('%tsu' => format_size($total_storage))),
);
$report_timestamp = variable_get('upei_roblib_reports_last_execution', NULL);
$report_timestamp = $report_timestamp ? 'Reports last ran: ' . format_date($report_timestamp) : t('Reports have not yet run.');
$output = array(
'upei_roblib_report_output' => array(
'#header' => array(t('Storage Type'), t('Storage Size')),
'#rows' => $rows,
'#theme' => 'table',
'#prefix' => '<div class=upei-roblib-storage-report>',
'#suffix' => '</div>',
),
'upei_roblib_total_storage_used' => array(
'#prefix' => '<div class=upei-roblib-storage-report-total-used>',
'#suffix' => '</div>',
'#type' => 'markup',
'#markup' => t('Total Storage Used %tsu', array('%tsu' => format_size($total_storage))),
),
'upei_roblib_allowed_storage' => array(
'#prefix' => '<div class=upei-roblib-storage-report-max-allowed>',
'#suffix' => '</div>',
'#type' => 'markup',
'#markup' => t('Total Allowed Storage is %ts', array('%ts' => variable_get('upei_roblib_reports_max_storage', '1000') . ' GB')),
),
'report_timestamp' => array(
'#prefix' => '<div class=upei-roblib-storage-report-timestamp>',
'#suffix' => '</div>',
'#type' => 'markup',
'#markup' => $report_timestamp,
),
);
drupal_alter('upei_roblib_reports_size', $output);
return $output;
}
/**
* Implements hook_mail.
*/
function upei_roblib_reports_mail($key, &$message, $params) {
switch ($key) {
case 'over_storage':
$message['subject'] = $params['subject'];
$message['body'][] = $params['body'];
break;
}
}
/**
* Implements hook_cron().
*/
function upei_roblib_reports_cron() {
// Load user 1 who hopefully has access to all objects so we get accurate results on islandora objects with XACML.
$account = user_load(1);
module_load_include('inc', 'upei_roblib_reports', 'includes/utilities');
$interval = variable_get('upei_roblib_reports_interval', 7) * 60 * 60 * 24;
// We usually don't want to act every time cron runs (which could be every
// minute) so keep a time for the next run in a variable.
// Also keep the time this was run, to display with the report.
if (time() >= variable_get('upei_roblib_reports_next_execution', 0)) {
module_load_include('inc', 'upei_roblib_reports', 'includes/batch');
module_load_include('inc', 'upei_roblib_reports', 'includes/db');
upei_roblib_reports_run_unprogressive_batch('upei_roblib_reports_get_batch', 100, -1, $account);
variable_set('upei_roblib_reports_drupal_file_size', upei_roblib_reports_get_local_disk_usage());
variable_set('upei_roblib_reports_db_size', upei_roblib_reports_get_db_size());
watchdog('upei_roblib_reports', 'Ran Islandora Disk Usage Report', NULL, WATCHDOG_NOTICE);
variable_set('upei_roblib_reports_next_execution', time() + $interval);
variable_set('upei_roblib_reports_last_execution', time());
}
upei_roblib_reports_check_storage_and_notify();
}