Commit 33e184ff authored by Hugh Davenport's avatar Hugh Davenport
Browse files

Store daily institutional stats



Change-Id: Ifdc7dbaf2ec7c3ebaa40cb557e0a093423c5c71d
Signed-off-by: default avatarHugh Davenport <hugh@catalyst.net.nz>
parent d2a1852c
......@@ -903,6 +903,18 @@
<KEY NAME="primary" TYPE="primary" FIELDS="ctime,type" />
</KEYS>
</TABLE>
<TABLE NAME="institution_data">
<FIELDS>
<FIELD NAME="ctime" TYPE="datetime" NOTNULL="true" />
<FIELD NAME="institution" TYPE="char" LENGTH="255" NOTNULL="true" />
<FIELD NAME="type" TYPE="char" LENGTH="255" NOTNULL="true" />
<FIELD NAME="value" TYPE="text" LENGTH="small" NOTNULL="false"/>
</FIELDS>
<KEYS>
<KEY NAME="primary" TYPE="primary" FIELDS="ctime,institution,type" />
<KEY NAME="institutionfk" TYPE="foreign" FIELDS="institution" REFTABLE="institution" REFFIELDS="name" />
</KEYS>
</TABLE>
<TABLE NAME="collection">
<FIELDS>
<FIELD NAME="id" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="true" />
......
......@@ -2994,5 +2994,36 @@ function xmldb_core_upgrade($oldversion=0) {
insert_record('cron', $cron);
}
if ($oldversion < 2012062902) {
// Add institution stats table
$table = new XMLDBTable('institution_data');
$table->addFieldInfo('ctime', XMLDB_TYPE_DATETIME, null, XMLDB_NOTNULL);
$table->addFieldInfo('institution', XMLDB_TYPE_CHAR, 255, null, null);
$table->addFieldInfo('type', XMLDB_TYPE_CHAR, 255, null, XMLDB_NOTNULL);
$table->addFieldInfo('value', XMLDB_TYPE_TEXT, 'small', null);
$table->addKeyInfo('primary', XMLDB_KEY_PRIMARY, array('ctime','institution','type'));
$table->addKeyInfo('institutionfk', XMLDB_KEY_FOREIGN, array('institution'), 'institution', array('name'));
create_table($table);
// Insert cron jobs to save institution data
$cron = new StdClass;
$cron->callfunction = 'cron_institution_data_weekly';
$cron->minute = 55;
$cron->hour = 23;
$cron->day = '*';
$cron->month = '*';
$cron->dayofweek = 6;
insert_record('cron', $cron);
$cron = new StdClass;
$cron->callfunction = 'cron_institution_data_daily';
$cron->minute = 51;
$cron->hour = 23;
$cron->day = '*';
$cron->month = '*';
$cron->dayofweek = '*';
insert_record('cron', $cron);
}
return $status;
}
......@@ -2876,6 +2876,68 @@ function cron_site_data_daily() {
graph_site_data_daily();
}
function cron_institution_data_weekly() {
require_once(get_config('libroot') . 'registration.php');
foreach (get_column('institution', 'name') as $institution) {
$current = institution_data_current($institution);
$time = db_format_timestamp(time());
insert_record('institution_data', (object) array(
'ctime' => $time,
'institution' => $institution,
'type' => 'user-count',
'value' => $current['users'],
));
insert_record('institution_data', (object) array(
'ctime' => $time,
'institution' => $institution,
'type' => 'view-count',
'value' => $current['views'],
));
$current['name'] = $institution;
graph_institution_data_weekly($current);
}
}
function cron_institution_data_daily() {
require_once(get_config('libroot') . 'registration.php');
foreach (get_column('institution', 'name') as $institution) {
$current = institution_data_current($institution);
$time = db_format_timestamp(time());
// Total users
insert_record('institution_data', (object) array(
'ctime' => $time,
'institution' => $institution,
'type' => 'user-count-daily',
'value' => $current['users'],
));
// Logged-in users
$interval = is_postgres() ? "'1 day'" : '1 day';
if ($current['users'] != 0) {
$where = "lastaccess >= DATE(?) AND lastaccess < DATE(?)+ INTERVAL $interval";
$where .= " AND id IN (" . join(',', array_fill(0, $current['users'], '?')) . ")";
$loggedin = count_records_select('usr', $where, array_merge(array($time, $time), $current['members']));
}
else {
$loggedin = 0;
}
insert_record('institution_data', (object) array(
'ctime' => $time,
'institution' => $institution,
'type' => 'loggedin-users-daily',
'value' => $loggedin,
));
$current['name'] = $institution;
graph_institution_data_daily($current);
}
}
/**
* A cronjob to generate a sitemap
*/
......
......@@ -392,6 +392,39 @@ function site_statistics($full=false) {
return($data);
}
function institution_data_current($institution) {
$data = array();
if ($institution == 'mahara') {
$data['members'] = get_column_sql('SELECT id
FROM {usr}
WHERE deleted = 0 AND id > 0 AND id NOT IN
(SELECT usr FROM {usr_institution})
', array());
}
else {
$data['members'] = get_column_sql('SELECT usr
FROM {usr_institution} ui
JOIN {usr} u ON (u.id = ui.usr)
WHERE u.deleted = 0 AND ui.institution = ?
', array($institution));
}
$data['users'] = count($data['members']);
if (!$data['users']) {
$data['views'] = 0;
}
else {
$data['viewids'] = get_column_sql('
SELECT id FROM {view}
WHERE owner IS NOT NULL AND owner IN (' . join(',', array_fill(0, $data['users'], '?')) . ')
UNION
SELECT id FROM {view}
WHERE institution IS NOT NULL AND institution = ?'
, array_merge($data['members'], array($institution)));
$data['views'] = count($data['viewids']);
}
return $data;
}
function user_statistics($limit, $offset, &$sitedata) {
$data = array();
$data['tableheadings'] = array(
......@@ -997,6 +1030,75 @@ function view_type_graph() {
}
}
function institution_view_type_graph(&$institutiondata) {
if ($institutiondata['views'] == 0) {
return;
}
// Draw a pie graph of views broken down by view type.
$viewtypes = get_records_sql_array('
SELECT type, COUNT(id) AS views
FROM {view} WHERE type != ?
AND id IN (' . join(',', array_fill(0, $institutiondata['views'], '?')) . ')
GROUP BY type',
array_merge(array('dashboard'), $institutiondata['viewids'])
);
if (count($viewtypes) > 1) {
$dataarray = array();
foreach ($viewtypes as &$t) {
$dataarray[get_string($t->type, 'view')] = $t->views;
}
arsort($dataarray);
require_once(get_config('libroot') . "pear/Image/Graph.php");
$Graph =& Image_Graph::factory('graph', array(300, 200));
$Font =& $Graph->addNew('font', 'Vera');
$Font->setSize(9);
$Graph->setFont($Font);
$Graph->add(
Image_Graph::vertical(
Image_Graph::vertical(
Image_Graph::factory('title', array(get_string('viewsbytype', 'admin'), 9)),
$Plotarea = Image_Graph::factory('plotarea'),
5
),
$Date = Image_Graph::factory('title', array(format_date(time(), 'strftimew3cdate'), 7)),
96
)
);
$Date->setAlignment(IMAGE_GRAPH_ALIGN_RIGHT);
$DateFont =& $Graph->addNew('font', 'Vera');
$DateFont->setColor('gray@0.8');
$Date->setFont($DateFont);
$Plotarea->hideAxis();
$Dataset =& Image_Graph::factory('dataset', array($dataarray));
$Plot =& $Plotarea->addNew('pie', array(&$Dataset));
$Plot->setLineColor('black');
$FillArray =& Image_Graph::factory('Image_Graph_Fill_Array');
$Plot->setFillStyle($FillArray);
$FillArray->addColor('blue@0.6');
$FillArray->addColor('green@0.6');
$FillArray->addColor('red@0.6');
$FillArray->addColor('yellow@0.6');
$FillArray->addColor('orange@0.6');
$Marker =& $Plot->addNew('Image_Graph_Marker_Value', IMAGE_GRAPH_VALUE_X);
$Marker->setBorderColor('white');
$Marker->setFontSize(8);
$PointingMarker =& $Plot->addNew('Image_Graph_Marker_Pointing_Angular', array(20, &$Marker));
$Plot->setMarker($PointingMarker);
$Graph->done(array('filename' => stats_graph_path($institutiondata['name'] . '_viewtypes')));
}
}
function graph_site_data_weekly() {
......@@ -1091,6 +1193,97 @@ function graph_site_data_daily() {
view_type_graph();
}
function graph_institution_data_weekly(&$institutiondata) {
$lastyear = db_format_timestamp(time() - 60*60*12*365);
$values = array($lastyear, 'view-count', 'user-count', $institutiondata['name']);
$weekly = get_records_sql_array('
SELECT ctime, type, "value", ' . db_format_tsfield('ctime', 'ts') . '
FROM {institution_data}
WHERE ctime >= ? AND type IN (?,?) AND institution = ?
ORDER BY ctime, type', $values);
if (!count($weekly) > 1) {
return;
}
$dataarray = array();
foreach ($weekly as &$r) {
$dataarray[$r->type][strftime("%d %b", $r->ts)] = $r->value;
}
foreach ($dataarray as &$t) {
// The graph will look nasty until we have 2 points to plot.
if (count($t) < 2) {
return;
}
}
require_once(get_config('libroot') . "pear/Image/Graph.php");
$Graph =& Image_Graph::factory('graph', array(350, 200));
$Font =& $Graph->addNew('font', 'Vera');
$Font->setSize(9);
$Graph->setFont($Font);
$Graph->add(
Image_Graph::vertical(
Image_Graph::vertical(
$Plotarea = Image_Graph::factory('plotarea'),
$Legend = Image_Graph::factory('legend'),
88
),
$Date = Image_Graph::factory('title', array(format_date(time(), 'strftimew3cdate'), 7)),
96
)
);
$Date->setAlignment(IMAGE_GRAPH_ALIGN_RIGHT);
$DateFont =& $Graph->addNew('font', 'Vera');
$DateFont->setColor('gray@0.8');
$Date->setFont($DateFont);
$Legend->setPlotarea($Plotarea);
$datasetinfo = array(
'user-count' => array('color' => 'blue@0.6', 'name' => get_string('users')),
'view-count' => array('color' => 'green@0.6', 'name' => get_string('Views', 'view')),
'group-count' => array('color' => 'red@0.6', 'name' => get_string('groups')),
);
$yaxis = array('min' => array(), 'max' => array());
$points = 1;
foreach (array_keys($datasetinfo) as $k) {
$dataset =& Image_Graph::factory('dataset', array($dataarray[$k]));
$dataset->setName($datasetinfo[$k]['name']);
$plot =& $Plotarea->addNew('line', array(&$dataset));
$linestyle =& Image_Graph::factory('Image_Graph_Line_Solid', array($datasetinfo[$k]['color']));
$linestyle->setThickness(3);
$plot->setLineStyle($linestyle);
$yaxis['max'][$k] = max($dataarray[$k]);
$yaxis['min'][$k] = min($dataarray[$k]);
$points = max($points, count($dataarray[$k]));
}
$AxisX =& $Plotarea->getAxis(IMAGE_GRAPH_AXIS_X);
$AxisX->setFontAngle('vertical');
$AxisX->setFontSize(8);
$AxisX->setLabelInterval(ceil($points/30)); // Avoid label crowding
$AxisY =& $Plotarea->getAxis(IMAGE_GRAPH_AXIS_Y);
$maxy = max($yaxis['max']);
$AxisY->forceMaximum($maxy * 1.025);
// $miny = min($yaxis['min']);
// $padding = ($maxy - $miny) * 0.025;
// $AxisY->forceMaximum($maxy + $padding);
// $AxisY->forceMinimum($miny - $padding);
$Graph->done(array('filename' => stats_graph_path($institutiondata['name'] . '_weekly')));
}
function graph_institution_data_daily(&$institutiondata) {
institution_view_type_graph($institutiondata);
}
function stats_graph_path($name) {
return get_config('dataroot') . 'images/' . $name . '.png';
}
......
......@@ -874,6 +874,8 @@ function core_install_firstcoredata_defaults() {
'file_cleanup_old_cached_files' => array(0, 1, '*', '*', '*'),
'user_login_tries_to_zero' => array('*/5', '*', '*', '*', '*'),
'cron_institution_registration_data' => array(rand(0, 59), rand(0, 23), '*', '*', rand(0, 6)),
'cron_institution_data_weekly' => array('55', '23', '*', '*', '6'),
'cron_institution_data_daily' => array('51', '23', '*', '*', '*'),
);
foreach ($cronjobs as $callfunction => $times) {
$cron = new StdClass;
......
......@@ -30,7 +30,7 @@ defined('INTERNAL') || die();
$config = new StdClass;
// See https://wiki.mahara.org/index.php/Developer_Area/Version_Numbering_Policy
// For upgrades on stable branches, increment the version by one. On master, use the date.
$config->version = 2012062901;
$config->version = 2012062902;
$config->release = '1.6.0dev';
$config->minupgradefrom = 2008040200;
$config->minupgraderelease = '1.0.0 (release tag 1.0.0_RELEASE)';
......
Markdown is supported
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