Commit f8901976 authored by Aaron Wells's avatar Aaron Wells Committed by Gerrit Code Review
Browse files

Merge "Make institution member queries more scalable (Bug #1497053)"

parents 579a170c 75d9375a
......@@ -3788,8 +3788,8 @@ function cron_institution_data_daily() {
$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']));
$where .= " AND id IN (" . $current['memberssql'] . ")";
$loggedin = count_records_select('usr', $where, array_merge(array($time, $time), $current['memberssqlparams']));
}
else {
$loggedin = 0;
......
......@@ -452,8 +452,9 @@ function institution_data_current($institution) {
WHERE u.deleted = 0 AND ui.institution = ?';
$membersqueryparams = array($institution);
}
$data['members'] = get_column_sql($membersquery, $membersqueryparams);
$data['users'] = count($data['members']);
$data['memberssql'] = $membersquery;
$data['memberssqlparams'] = $membersqueryparams;
$data['users'] = get_field_sql('SELECT COUNT(*) FROM (' . $membersquery . ') AS members', $membersqueryparams);
if (!$data['users']) {
$data['views'] = 0;
}
......@@ -495,8 +496,8 @@ function institution_statistics($institution, $full=false) {
}
else {
$sql = "SELECT SUM($todaysql) AS today, SUM($thisweeksql) AS thisweek, $weekago AS weekago, SUM($eversql) AS ever FROM {usr}
WHERE id IN (" . join(',', array_fill(0, $data['users'], '?')) . ")";
$active = get_record_sql($sql, $data['members']);
WHERE id IN (" . $data['memberssql'] . ")";
$active = get_record_sql($sql, $data['memberssqlparams']);
}
$data['usersloggedin'] = get_string('loggedinsince', 'admin', $active->today, $active->thisweek, format_date(strtotime($active->weekago), 'strftimedateshort'), $active->ever);
......@@ -507,8 +508,8 @@ function institution_statistics($institution, $full=false) {
$memberships = count_records_sql("
SELECT COUNT(*)
FROM {group_member} m JOIN {group} g ON g.id = m.group
WHERE g.deleted = 0 AND m.member IN (" . join(',', array_fill(0, $data['users'], '?')) . ")
", $data['members']);
WHERE g.deleted = 0 AND m.member IN (" . $data['memberssql'] . ")
", $data['memberssqlparams']);
$data['groupmemberaverage'] = round($memberships/$data['users'], 1);
}
$data['strgroupmemberaverage'] = get_string('groupmemberaverage', 'admin', $data['groupmemberaverage']);
......@@ -540,8 +541,8 @@ function institution_statistics($institution, $full=false) {
$data['diskusage'] = get_field_sql("
SELECT SUM(quotaused)
FROM {usr}
WHERE deleted = 0 AND id IN (" . join(',', array_fill(0, $data['users'], '?')) . ")
", $data['members']);
WHERE deleted = 0 AND id IN (" . $data['memberssql'] . ")
", $data['memberssqlparams']);
}
return($data);
......@@ -785,17 +786,18 @@ function institution_user_statistics($limit, $offset, &$institutiondata) {
FROM {usr_friend}
GROUP BY usr2
) f ON u.id = f.id
WHERE u.id IN (" . join(',', array_fill(0, $institutiondata['users'], '?')) . ")
WHERE u.id IN (" . $institutiondata['memberssql'] . ")
GROUP BY u.id, u.firstname, u.lastname, u.preferredname, u.urlid
ORDER BY friends DESC
LIMIT 1", $institutiondata['members']);
LIMIT 1", $institutiondata['memberssqlparams']);
$maxfriends = $maxfriends[0];
$meanfriends = count_records_sql('SELECT COUNT(*) FROM
(SELECT * FROM {usr_friend}
WHERE usr1 IN (' . join(',', array_fill(0, $institutiondata['users'], '?')) . ')
WHERE usr1 IN (' . $institutiondata['memberssql'] . ')
UNION ALL SELECT * FROM {usr_friend}
WHERE usr2 IN (' . join(',', array_fill(0, $institutiondata['users'], '?')) . ')
) tmp', array_merge($institutiondata['members'], $institutiondata['members'])) / $institutiondata['users'];
WHERE usr2 IN (' . $institutiondata['memberssql'] . ')
) tmp', array_merge($institutiondata['memberssqlparams'], $institutiondata['memberssqlparams'])) /
$institutiondata['users'];
if ($maxfriends) {
$data['strmaxfriends'] = get_string(
'statsmaxfriends1',
......@@ -812,10 +814,10 @@ function institution_user_statistics($limit, $offset, &$institutiondata) {
$maxviews = get_records_sql_array("
SELECT u.id, u.firstname, u.lastname, u.preferredname, u.urlid, COUNT(v.id) AS views
FROM {usr} u JOIN {view} v ON u.id = v.owner
WHERE \"owner\" IN (" . join(',', array_fill(0, $institutiondata['users'], '?')) . ")
WHERE \"owner\" IN (" . $institutiondata['memberssql'] . ")
GROUP BY u.id, u.firstname, u.lastname, u.preferredname, u.urlid
ORDER BY views DESC
LIMIT 1", $institutiondata['members']);
LIMIT 1", $institutiondata['memberssqlparams']);
$maxviews = $maxviews[0];
if ($maxviews) {
$data['strmaxviews'] = get_string(
......@@ -833,10 +835,10 @@ function institution_user_statistics($limit, $offset, &$institutiondata) {
$maxgroups = get_records_sql_array("
SELECT u.id, u.firstname, u.lastname, u.preferredname, u.urlid, COUNT(m.group) AS groups
FROM {usr} u JOIN {group_member} m ON u.id = m.member JOIN {group} g ON m.group = g.id
WHERE g.deleted = 0 AND u.id IN (" . join(',', array_fill(0, $institutiondata['users'], '?')) . ")
WHERE g.deleted = 0 AND u.id IN (" . $institutiondata['memberssql'] . ")
GROUP BY u.id, u.firstname, u.lastname, u.preferredname, u.urlid
ORDER BY groups DESC
LIMIT 1", $institutiondata['members']);
LIMIT 1", $institutiondata['memberssqlparams']);
$maxgroups = $maxgroups[0];
if ($maxgroups) {
$data['strmaxgroups'] = get_string(
......@@ -854,15 +856,15 @@ function institution_user_statistics($limit, $offset, &$institutiondata) {
$maxquotaused = get_records_sql_array("
SELECT id, firstname, lastname, preferredname, urlid, quotaused
FROM {usr}
WHERE id IN (" . join(',', array_fill(0, $institutiondata['users'], '?')) . ")
WHERE id IN (" . $institutiondata['memberssql'] . ")
ORDER BY quotaused DESC
LIMIT 1", $institutiondata['members']);
LIMIT 1", $institutiondata['memberssqlparams']);
$maxquotaused = $maxquotaused[0];
$avgquota = get_field_sql("
SELECT AVG(quotaused)
FROM {usr}
WHERE id IN (" . join(',', array_fill(0, $institutiondata['users'], '?')) . ")
", $institutiondata['members']);
WHERE id IN (" . $institutiondata['memberssql'] . ")
", $institutiondata['memberssqlparams']);
$data['strmaxquotaused'] = get_string(
'statsmaxquotaused1',
'admin',
......
@javascript @core @core_administration @sdfsdf
Feature: Institution statistics are displayed correctly
In order to view information about an institution
As an admin
So I can benefit from seeing the current state and history of an institution
Scenario: Viewing user statistics
Given the following "institutions" exist:
| name | displayname | registerallowed | registerconfirm |
| instone | Institution One | ON | ON |
And the following "users" exist:
| username | password | email | firstname | lastname | institution | authname | role |
| userA | Password1 | test01@example.com | Pete | Mc | mahara | internal | member |
| userB | Password1 | test02@example.com | Miles | Morales | instone | internal | member |
| userC | Password1 | test03@example.com | Jessica | Jones | mahara | internal | member |
Given I log in as "admin" with password "Password1"
And I follow "Administration"
# Users without an institution
When I choose "Statistics" in "Institutions"
Then I should see "Institution statistics for 'Institution One'"
And I should see "Users 1"
# Users with an institution
When I select "No Institution" from "usertypeselect_institution"
Then I should see "Institution statistics for 'No Institution'"
And I should see "Users 3"
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