Skip to content

Commit

Permalink
Add function to report storage usage by file area and component
Browse files Browse the repository at this point in the history
A function to get storage usage by both file area and component has been added to the DataStats report. An adjustment was also made in a database query within the 'getFileAreaStorageUsage' function to sort the results by 'filearea' instead of 'component'.
  • Loading branch information
obanach committed Oct 19, 2023
1 parent 85a5f2d commit f4519a3
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion Moosh/Command/Moodle39/Report/DataStats.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ public function execute() {

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

$i = 0;
foreach ($sortarray as $courseid => $values) {
Expand Down Expand Up @@ -118,7 +119,7 @@ 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;");
$fileAreas = $DB->get_records_sql("SELECT filearea as name FROM mdl_files WHERE filesize > 0 GROUP BY filearea 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]);
Expand All @@ -130,4 +131,17 @@ protected function getFileAreaStorageUsage(): array {

return $data;
}

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

$usageRecords = $DB->get_records_sql("SELECT filearea, component, SUM(filesize) AS size FROM (SELECT DISTINCT contenthash, component, filearea, filesize FROM mdl_files WHERE filesize > 0) AS files GROUP BY filearea,component ORDER BY size DESC LIMIT 15");

foreach($usageRecords as $record) {
$data['- ' . $record->filearea . ', ' . $record->component] = $record->size;
}

return $data;
}
}

0 comments on commit f4519a3

Please sign in to comment.