Skip to content

Commit

Permalink
db-stats global row count fix
Browse files Browse the repository at this point in the history
  • Loading branch information
mi7chal committed Oct 14, 2024
1 parent 372c064 commit 079351f
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions Moosh/Command/Moodle39/Report/DbStats.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,26 +88,35 @@ public function fetchStats($limit = null, $additionalTableStats = []) {

// formatting query result, adding row count and calculating size
$values = array_values($results);
$resultsLength = count($values);
if(is_null($limit) || $limit > $resultsLength) {
$limit = $resultsLength;
}

$databaseSize = 0;
$rowCount = 0;
$tableData = [];
// we have to loop over every table in order to calculate rowCount precisely
foreach($values as $index => $result) {
$databaseSize += $result->size;
$rowCount += $result->row_count;

// we only calculate size and skips other actions when limit is reached
// we include tables from additionalTableStats
if($index >= $limit && !in_array($result->name, $additionalTableStats)) {
continue;
}

// querying row count
$tableName = $result->name;

// calculating precise table row count
$countResult = $DB->get_records_sql("SELECT COUNT(*) AS 'count' from $tableName");

$firstRow = array_values($countResult)[0];

$result->rowCount = strval($firstRow->count);

$tableData[$result->name] = $result;

$rowCount += $firstRow->count;
}

return (object) [
Expand Down

0 comments on commit 079351f

Please sign in to comment.