File size: 9,542 Bytes
260bf65 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 | <?php
/**
* Matomo - free/libre analytics platform
*
* @link https://matomo.org
* @license https://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*/
namespace Piwik\Plugins\DBStats;
use Piwik\Common;
use Piwik\DataTable;
use Piwik\Piwik;
/**
* DBStats API is used to request the overall status of the Mysql tables in use by Matomo.
* @hideExceptForSuperUser
* @method static \Piwik\Plugins\DBStats\API getInstance()
*/
class API extends \Piwik\Plugin\API
{
/**
* @var MySQLMetadataProvider
*/
private $metadataProvider;
public function __construct(MySQLMetadataProvider $metadataProvider)
{
$this->metadataProvider = $metadataProvider;
}
/**
* Gets some general information about this Matomo installation, including the count of
* websites tracked, the count of users and the total space used by the database.
*
* @return array{0:int|float|string, 1:int|float|string, 2:int|float|string} Website count, user count, and total database size.
*/
public function getGeneralInformation(): array
{
Piwik::checkUserHasSuperUserAccess();
// calculate total size
$totalSpaceUsed = 0;
foreach ($this->metadataProvider->getAllTablesStatus() as $status) {
$totalSpaceUsed += $status['Data_length'] + $status['Index_length'];
}
$siteTableStatus = $this->metadataProvider->getTableStatus('site');
$userTableStatus = $this->metadataProvider->getTableStatus('user');
$siteCount = $siteTableStatus['Rows'];
$userCount = $userTableStatus['Rows'];
return array($siteCount, $userCount, $totalSpaceUsed);
}
/**
* Gets general database info that is not specific to any table.
*
* See https://dev.mysql.com/doc/refman/5.1/en/show-status.html
*
* @return array<int, array<string, mixed>> Database status rows returned by the metadata provider.
*/
public function getDBStatus(): array
{
Piwik::checkUserHasSuperUserAccess();
return $this->metadataProvider->getDBStatus();
}
/**
* Returns a datatable summarizing how data is distributed among Matomo tables.
*
* This function will group tracker tables, numeric archive tables, blob archive tables
* and other tables together so only four rows are shown.
*
* @return DataTable A datatable with three columns: 'data_size', 'index_size', 'row_count'.
*/
public function getDatabaseUsageSummary(): DataTable
{
Piwik::checkUserHasSuperUserAccess();
$emptyRow = array('data_size' => 0, 'index_size' => 0, 'row_count' => 0);
$rows = array(
'tracker_data' => $emptyRow,
'metric_data' => $emptyRow,
'report_data' => $emptyRow,
'other_data' => $emptyRow,
);
foreach ($this->metadataProvider->getAllTablesStatus() as $status) {
if ($this->isNumericArchiveTable($status['Name'])) {
$rowToAddTo = & $rows['metric_data'];
} elseif ($this->isBlobArchiveTable($status['Name'])) {
$rowToAddTo = & $rows['report_data'];
} elseif ($this->isTrackerTable($status['Name'])) {
$rowToAddTo = & $rows['tracker_data'];
} else {
$rowToAddTo = & $rows['other_data'];
}
$rowToAddTo['data_size'] += $status['Data_length'];
$rowToAddTo['index_size'] += $status['Index_length'];
$rowToAddTo['row_count'] += $status['Rows'];
}
return DataTable::makeFromIndexedArray($rows);
}
/**
* Returns a datatable describing how much space is taken up by each log table.
*
* @return DataTable A datatable with three columns: 'data_size', 'index_size', 'row_count'.
*/
public function getTrackerDataSummary(): DataTable
{
Piwik::checkUserHasSuperUserAccess();
return $this->getTablesSummary($this->metadataProvider->getAllLogTableStatus());
}
/**
* Returns a datatable describing how much space is taken up by each numeric
* archive table.
*
* @return DataTable A datatable with three columns: 'data_size', 'index_size', 'row_count'.
*/
public function getMetricDataSummary(): DataTable
{
Piwik::checkUserHasSuperUserAccess();
return $this->getTablesSummary($this->metadataProvider->getAllNumericArchiveStatus());
}
/**
* Returns a datatable describing how much space is taken up by each numeric
* archive table, grouped by year.
*
* @return DataTable A datatable with three columns: 'data_size', 'index_size', 'row_count'.
*/
public function getMetricDataSummaryByYear(): DataTable
{
Piwik::checkUserHasSuperUserAccess();
$dataTable = $this->getMetricDataSummary();
$dataTable->filter('GroupBy', array(
'label',
function ($tableName) {
return $this->getArchiveTableYear($tableName);
},
));
return $dataTable;
}
/**
* Returns a datatable describing how much space is taken up by each blob
* archive table.
*
* @return DataTable A datatable with three columns: 'data_size', 'index_size', 'row_count'.
*/
public function getReportDataSummary(): DataTable
{
Piwik::checkUserHasSuperUserAccess();
return $this->getTablesSummary($this->metadataProvider->getAllBlobArchiveStatus());
}
/**
* Returns a datatable describing how much space is taken up by each blob
* archive table, grouped by year.
*
* @return DataTable A datatable with three columns: 'data_size', 'index_size', 'row_count'.
*/
public function getReportDataSummaryByYear(): DataTable
{
Piwik::checkUserHasSuperUserAccess();
$dataTable = $this->getReportDataSummary();
$dataTable->filter('GroupBy', array(
'label',
function ($tableName) {
return $this->getArchiveTableYear($tableName);
},
));
return $dataTable;
}
/**
* Returns a datatable describing how much space is taken up by 'admin' tables.
*
* An 'admin' table is a table that is not central to analytics functionality.
* So any table that isn't an archive table or a log table is an 'admin' table.
*
* @return DataTable A datatable with three columns: 'data_size', 'index_size', 'row_count'.
*/
public function getAdminDataSummary(): DataTable
{
Piwik::checkUserHasSuperUserAccess();
return $this->getTablesSummary($this->metadataProvider->getAllAdminTableStatus());
}
/**
* Returns a datatable describing how much total space is taken up by each
* individual report type.
*
* Goal reports and reports of the format .*_[0-9]+ are grouped together.
*
* @param bool $forceCache Whether to bypass the cache and recalculate the summary.
* @return DataTable A datatable with three columns: 'data_size', 'index_size', 'row_count'.
*/
public function getIndividualReportsSummary(bool $forceCache = false): DataTable
{
Piwik::checkUserHasSuperUserAccess();
return $this->metadataProvider->getRowCountsAndSizeByBlobName($forceCache);
}
/**
* Returns a datatable describing how much total space is taken up by each
* individual metric type.
*
* Goal metrics, metrics of the format .*_[0-9]+ and 'done...' metrics are grouped together.
*
* @param bool $forceCache Whether to bypass the cache and recalculate the summary.
* @return DataTable A datatable with three columns: 'data_size', 'index_size', 'row_count'.
*/
public function getIndividualMetricsSummary(bool $forceCache = false): DataTable
{
Piwik::checkUserHasSuperUserAccess();
return $this->metadataProvider->getRowCountsAndSizeByMetricName($forceCache);
}
/**
* @param array<int, array{Name:string, Data_length:int|float|string, Index_length:int|float|string, Rows:int|float|string}> $statuses
*/
private function getTablesSummary($statuses): DataTable
{
$dataTable = new DataTable();
foreach ($statuses as $status) {
$dataTable->addRowFromSimpleArray(array(
'label' => $status['Name'],
'data_size' => $status['Data_length'],
'index_size' => $status['Index_length'],
'row_count' => $status['Rows'],
));
}
return $dataTable;
}
private function isNumericArchiveTable(string $name): bool
{
return strpos($name, Common::prefixTable('archive_numeric_')) === 0;
}
private function isBlobArchiveTable(string $name): bool
{
return strpos($name, Common::prefixTable('archive_blob_')) === 0;
}
private function isTrackerTable(string $name): bool
{
return strpos($name, Common::prefixTable('log_')) === 0;
}
/**
* Gets the year of an archive table from its name.
*/
private function getArchiveTableYear(string $tableName): string
{
preg_match("/archive_(?:numeric|blob)_([0-9]+)_/", $tableName, $matches);
return $matches[1] ?? '';
}
}
|