Skip to content

Commit

Permalink
db-stats global row count
Browse files Browse the repository at this point in the history
  • Loading branch information
mi7chal committed Oct 14, 2024
1 parent fc55cd6 commit 372c064
Showing 1 changed file with 7 additions and 10 deletions.
17 changes: 7 additions & 10 deletions Moosh/Command/Moodle39/Report/DbStats.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public function execute() {
// returns minimal stats
$results = $this->fetchStats(3, array_values(TABLE_NAMES));

$results->rowCount = $results->rowCount . "\u{00A0}";
// non-breaking space prevents from formatting as size
foreach($results->tables as $key => $table) {
$results->tables[$key]->rowCount = $table->rowCount . "\u{00A0}";
Expand Down Expand Up @@ -87,22 +88,14 @@ 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;

// 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;

Expand All @@ -113,10 +106,13 @@ public function fetchStats($limit = null, $additionalTableStats = []) {
$result->rowCount = strval($firstRow->count);

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

$rowCount += $firstRow->count;
}

return (object) [
'size' => $databaseSize,
'rowCount' => $rowCount,
'tables' => $tableData
];
}
Expand All @@ -131,6 +127,7 @@ public function mapStatsToSimpleFormat($results) {
$tablesIndexed = array_values($tables);

return ['Database size' => $results->size,
'Database row count' => $results->rowCount,
'Biggest table name' => $tablesIndexed[0]->name,
'Biggest table size' => $tablesIndexed[0]->size,
'Biggest table number of records' => $tablesIndexed[0]->rowCount,
Expand Down

0 comments on commit 372c064

Please sign in to comment.