Skip to content

Commit

Permalink
data-stats command extension (#452)
Browse files Browse the repository at this point in the history
* Enhanced data reporting in DataStats.php

This commit includes an update to DataStats.php where two new methods getComponentStorageUsage() and getFileAreaStorageUsage() were added. These methods provide detailed insight into storage usage by different course components and file areas to give a better handle on data usage within Moodle. Also, modified the data array to include results from these new methods, hence improving the reporting functionality.

* Refactor DataStats.php for accurate storage usage reporting

The existing SQL queries to calculate storage usage were simplified and made more accurate, avoiding potential data duplication. It now retrieves a list of components or file areas, then calculates storage usage based on unique content hashes. This provides a more accurate insight into actual storage usage within Moodle. As a result, the methods getComponentStorageUsage() and getFileAreaStorageUsage() display a precise computation of storage usage by each component and file area.

* Update DataStats array key

Removed message added for debugging.

---------

Co-authored-by: Oliwer Banach <[email protected]>
  • Loading branch information
obanach and obanach authored Oct 18, 2023
1 parent 145f9d7 commit 85a5f2d
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions Moosh/Command/Moodle39/Report/DataStats.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ public function execute() {
'files total' => $all_files->total,
'distinct files total' => $distinctfilestotal);

$data += $this->getComponentStorageUsage();
$data += $this->getFileAreaStorageUsage();

$i = 0;
foreach ($sortarray as $courseid => $values) {
$i++;
Expand All @@ -93,4 +96,38 @@ public function execute() {

$this->display($data, $options['json'], !$options['no-human-readable']);
}

protected function getComponentStorageUsage(): array {
global $DB;
$data = ['Storage usage by component' => null];

$components = $DB->get_records_sql("SELECT component as name FROM mdl_files WHERE filesize > 0 GROUP BY component ORDER BY sum(filesize) DESC LIMIT 15;");
foreach($components as $component) {
$sum = 0;
$files = $DB->get_records_sql("SELECT contenthash, MAX(filesize) AS max_filesize FROM mdl_files WHERE filesize > 0 AND component = :component GROUP BY contenthash", ['component' => $component->name]);
foreach($files as $file){
$sum += $file->max_filesize;
}
$data['- ' . $component->name] = $sum;
}

return $data;
}

protected function getFileAreaStorageUsage(): array {
global $DB;
$data = ['Storage usage by file area' => null];

$fileAreas = $DB->get_records_sql("SELECT filearea as name FROM mdl_files WHERE filesize > 0 GROUP BY component ORDER BY sum(filesize) DESC LIMIT 15;");
foreach($fileAreas as $fileArea) {
$sum = 0;
$files = $DB->get_records_sql("SELECT contenthash, MAX(filesize) AS max_filesize FROM mdl_files WHERE filesize > 0 AND filearea = :filearea GROUP BY contenthash", ['filearea' => $fileArea->name]);
foreach($files as $file){
$sum += $file->max_filesize;
}
$data['- ' . $fileArea->name] = $sum;
}

return $data;
}
}

0 comments on commit 85a5f2d

Please sign in to comment.