Commit 5d4e947c authored by Richard Mansfield's avatar Richard Mansfield
Browse files

Faster calculate quota cron job


Signed-off-by: default avatarRichard Mansfield <richardm@catalyst.net.nz>
parent 2ddfbacc
......@@ -221,6 +221,20 @@ class PluginArtefactFile extends PluginArtefact {
public static function get_attachment_types() {
return array('file', 'image', 'archive');
}
public static function recalculate_quota() {
$data = get_records_sql_assoc("
SELECT a.owner, SUM(f.size) AS usage
FROM {artefact} a JOIN {artefact_file_files} f ON a.id = f.artefact
WHERE a.artefacttype IN ('file', 'image', 'profileicon', 'archive')
AND a.owner IS NOT NULL
GROUP BY a.owner", array()
);
if ($data) {
return array_map(create_function('$a', 'return $a->usage;'), $data);
}
return array();
}
}
abstract class ArtefactTypeFileBase extends ArtefactType {
......@@ -1559,11 +1573,6 @@ class ArtefactTypeProfileIcon extends ArtefactTypeImage {
return true;
}
public static function get_quota_usage($artefact) {
return filesize(get_config('dataroot') . 'artefact/file/profileicons/originals/'
. ($artefact % 256) . '/' . $artefact);
}
public function default_parent_for_copy(&$view, &$template, $artefactstoignore) {
return null;
}
......
......@@ -2112,19 +2112,24 @@ function tags_sideblock() {
* has caused the quota count to get out of sync
*/
function recalculate_quota() {
if (!$artefacts = get_records_select_array('artefact', 'owner IS NOT NULL', null, '', 'id, artefacttype, owner')) {
// Nothing to do
return;
}
$plugins = plugins_installed('artefact', true);
$userquotas = array();
foreach ($artefacts as $artefact) {
safe_require('artefact', get_field('artefact_installed_type', 'plugin', 'name', $artefact->artefacttype));
if (!isset($userquotas[$artefact->owner])) {
$userquotas[$artefact->owner] = 0;
foreach ($plugins as $plugin) {
safe_require('artefact', $plugin->name);
$classname = generate_class_name('artefact', $plugin->name);
if (is_callable($classname . '::recalculate_quota')) {
$pluginuserquotas = call_static_method($classname, 'recalculate_quota');
foreach ($pluginuserquotas as $userid => $usage) {
if (!isset($userquotas[$userid])) {
$userquotas[$userid] = $usage;
}
else {
$userquotas[$userid] += $usage;
}
}
}
$userquotas[$artefact->owner] += call_static_method(generate_artefact_class_name($artefact->artefacttype), 'get_quota_usage', $artefact->id);
}
foreach ($userquotas as $user => $quota) {
......
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