GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — DataExplorer/Query/SavedReportReader.php at 6.x-dev · openmost/DataExplorer Expand file tree/ Copy pathSavedReportReader.phpMore file actions 292 lines (248 loc) · 12.1 KB / Copy pathSavedReportReader.phpFile metadata and controls292 lines (248 loc) · 12.1 KB 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 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 <?php /** * Matomo - free/libre analytics platform * * @link https://matomo.org * @license https://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later */ declare(strict_types=1); namespace Piwik\Plugins\DataExplorer\Query; use Piwik\Archive; use Piwik\Archive\DataTableFactory; use Piwik\DataTable; use Piwik\DataTable\DataTableInterface; use Piwik\Period; use Piwik\Piwik; use Piwik\Period\Range; use Piwik\Plugins\DataExplorer\Access\ReportPermissions; use Piwik\Plugins\DataExplorer\Archiving\BrowserArchiver; use Piwik\Plugins\DataExplorer\Configuration; use Piwik\Plugins\DataExplorer\Model\ReportDefinition; use Piwik\Plugins\DataExplorer\RecordBuilders\SavedReport; use Piwik\Plugins\DataExplorer\Sql\RawSqlRunner; use Piwik\Site; /** * Data of a saved report at display time: visual reports from their archives (current revision only), with the * raw archived values turned into display labels; raw SQL reports run live through the cache. */ class SavedReportReader { /** table metadata naming the raw SQL column shown as the label */ public const LABEL_COLUMN_METADATA = 'dataexplorer_label_column'; /** table metadata giving the row dimension level of a table: 0 the report, 1 a subtable, ... */ public const DEPTH_METADATA = 'dataexplorer_depth'; /** @var RawSqlRunner */ private $sqlRunner; /** @var LabelFormatter */ private $labels; /** @var callable(): float */ private $clock; /** @var BrowserArchiver */ private $browserArchiver; /** * @param callable(): float|null $clock seconds as a float, microtime(true) by default */ public function __construct(?RawSqlRunner $sqlRunner = null, ?callable $clock = null, ?BrowserArchiver $browserArchiver = null) { $this->sqlRunner = $sqlRunner ?: new RawSqlRunner(); $this->labels = new LabelFormatter(); $this->clock = $clock ?: fn () => microtime(true); $this->browserArchiver = $browserArchiver ?: new BrowserArchiver(); } /** * @param array $report a hydrated row of ReportsDao * @throws \Piwik\NoAccessException when the user may not view the report on this site */ public function read(array $report, int $idSite, string $period, string $date, string $segment = '', bool $expanded = false, int $idSubtable = 0): DataTableInterface { (new ReportPermissions())->checkCanView($report, $idSite); if ($report['type'] === ReportDefinition::TYPE_SQL) { return $this->readSql($report, $idSite, $period, $date); } $definition = ReportDefinition::fromReport($report); if ($definition->isFunnel()) { return $this->readFunnel($report, $definition, $idSite, $period, $date, $segment); } if ($definition->getReportType() === ReportDefinition::REPORT_TYPE_TABLE) { return $this->readTable($report, $definition, $idSite, $period, $date, $segment, $expanded, $idSubtable); } $resolved = (new VisualQueryBuilder())->resolve($definition, $idSite); $names = []; foreach ($resolved['metrics'] as $metric) { $names[SavedReport::numericRecordName((int) $report['idreport'], (int) $report['revision'], $metric->id)] = $metric->id; } $this->browserArchiver->archiveMissingRecords($report, array_keys($names), $idSite, $period, $date, $segment); $table = Archive::build($idSite, $period, $date, $segment)->getDataTableFromNumeric(array_keys($names)); $table->filter('ReplaceColumnNames', [$names]); $processed = ProcessedMetrics::forResolved($resolved, $idSite); $processed->apply($table); $processed->decorate($table); return $table; } /** * The archived steps of a saved funnel with their breakdown values, rates and average times computed. */ private function readFunnel(array $report, ReportDefinition $definition, int $idSite, string $period, string $date, string $segment): DataTableInterface { $recordName = SavedReport::recordName((int) $report['idreport'], (int) $report['revision']); $this->browserArchiver->archiveMissingRecords($report, [$recordName], $idSite, $period, $date, $segment); $table = Archive::createDataTableFromArchive($recordName, $idSite, $period, $date, $segment, true, false); $breakdown = $definition->getBreakdown() === null ? null : (new VisualQueryBuilder())->resolveDimension($definition->getBreakdown(), $idSite); $table->filter(function (DataTable $table) use ($definition, $breakdown, $idSite) { FunnelResult::present($table, $definition, $breakdown, $idSite, $this->labels); }); return $table; } private function readTable(array $report, ReportDefinition $definition, int $idSite, string $period, string $date, string $segment, bool $expanded, int $idSubtable): DataTableInterface { $resolved = (new VisualQueryBuilder())->resolve($definition, $idSite); $recordName = SavedReport::recordName((int) $report['idreport'], (int) $report['revision']); if (!$idSubtable) { $this->browserArchiver->archiveMissingRecords($report, [$recordName], $idSite, $period, $date, $segment); } $table = Archive::createDataTableFromArchive($recordName, $idSite, $period, $date, $segment, $expanded, false, $idSubtable ?: null); $depth = $idSubtable ? $this->subtableDepth($recordName, count($resolved['rows']), $idSite, $period, $date, $segment, $idSubtable) : 0; $rows = array_slice($resolved['rows'], $depth); $table->filter(function (DataTable $table) use ($rows, $resolved, $idSite, $depth) { $this->labels->formatArchivedTable($table, $rows, $resolved['column'], $idSite); $table->setMetadata(self::DEPTH_METADATA, $depth); }); $processed = ProcessedMetrics::forResolved($resolved, $idSite); $processed->apply($table); $processed->decorate($table); return $table; } /** * Row dimension level of a subtable: subtable ids say nothing about their depth, so with 3 or more row * dimensions the expanded table is searched for the row that owns it. */ private function subtableDepth(string $recordName, int $dimensionCount, int $idSite, string $period, string $date, string $segment, int $idSubtable): int { if ($dimensionCount <= 2) { return 1; } $expanded = Archive::build($idSite, $period, $date, $segment)->getDataTableExpanded($recordName); if ($expanded instanceof DataTable\Map) { $tables = $expanded->getDataTables(); $expanded = $tables ? reset($tables) : null; } $depth = $expanded instanceof DataTable ? $this->findDepth($expanded, $idSubtable, 1) : null; return $depth ?? 1; } private function findDepth(DataTable $table, int $idSubtable, int $depth): ?int { foreach ($table->getRows() as $row) { // an expanded table renumbers its subtables in memory, the archive id stays in the metadata $inDb = $row->getMetadata('idsubdatatable_in_db'); if ((int) ($inDb !== false ? $inDb : $row->getIdSubDataTable()) === $idSubtable) { return $depth; } $subtable = $row->getSubtable(); if ($subtable instanceof DataTable) { $found = $this->findDepth($subtable, $idSubtable, $depth + 1); if ($found !== null) { return $found; } } } return null; } /** * A table runs once for the requested period. Evolution and KPI card keep the first row and its numeric * columns. Several periods (lastN, a date range) run once per sub-period, keeping the most recent ones only, * all of them within one max_execution_time_seconds budget: each run gets the time left, a run that times * out or no time left stops the whole report with the timeout error. */ private function readSql(array $report, int $idSite, string $period, string $date): DataTableInterface { return $this->readSqlWith( fn (string $runPeriod, string $runDate, ?int $maxSeconds) => $this->sqlRunner->runSaved($report, $idSite, $runPeriod, $runDate, $maxSeconds), (string) $report['report_type'], $idSite, $period, $date ); } /** * The query of the Raw SQL tab, not saved yet, read like a saved raw SQL report (one run per sub-period for * an evolution, the first numeric row for a KPI card) through the same short cache (super user only). */ public function readSqlPreview(string $sql, string $reportType, int $idSite, string $period, string $date): DataTableInterface { (new ReportPermissions())->checkCanRunRawSql(); return $this->readSqlWith( fn (string $runPeriod, string $runDate, ?int $maxSeconds) => $this->sqlRunner->run($sql, $idSite, $runPeriod, $runDate, true, $maxSeconds), $reportType, $idSite, $period, $date ); } /** * @param callable(string, string, ?int): DataTable $run runs the query for a period and a date */ private function readSqlWith(callable $run, string $reportType, int $idSite, string $period, string $date): DataTableInterface { $numeric = $reportType !== ReportDefinition::REPORT_TYPE_TABLE; if (!Period::isMultiplePeriod($date, $period)) { $table = $run($period, $date, null); return $numeric ? $this->firstNumericRow($table) : $this->withLabel($table); } $site = new Site($idSite); $subPeriods = (new Range($period, $date, $site->getTimezone()))->getSubperiods(); $subPeriods = array_slice($subPeriods, -Configuration::getRawSqlMaxPoints()); $budget = Configuration::getMaxExecutionTimeSeconds(); $deadline = ($this->clock)() + $budget; $map = new DataTable\Map(); $map->setKeyName('date'); foreach ($subPeriods as $subPeriod) { $left = (int) ceil($deadline - ($this->clock)()); if ($left <= 0) { throw new \InvalidArgumentException(Piwik::translate('DataExplorer_ErrorRawSqlTimeout', [$budget])); } $table = $run($subPeriod->getLabel(), $subPeriod->getDateStart()->toString(), $left); $table = $numeric ? $this->firstNumericRow($table) : $this->withLabel($table); $table->setMetadata(DataTableFactory::TABLE_METADATA_PERIOD_INDEX, $subPeriod); $table->setMetadata(DataTableFactory::TABLE_METADATA_SITE_INDEX, $site); $label = $subPeriod->getLabel(); $map->addTable($table, $label === 'week' ? $subPeriod->getRangeString() : $subPeriod->getPrettyString()); } return $map; } /** * Matomo tables are keyed by their label column: without a "label" column of its own, the first column of * the result becomes the label, its name kept in the table metadata for the column header. */ private function withLabel(DataTable $table): DataTable { $first = $table->getFirstRow(); if ($first === false || $first->hasColumn('label')) { return $table; } $columns = $first->getColumns(); $labelColumn = (string) array_key_first($columns); foreach ($table->getRows() as $row) { $columns = $row->getColumns(); $label = $columns[$labelColumn] ?? ''; unset($columns[$labelColumn]); $row->setColumns(['label' => $label] + $columns); } $table->setMetadata(self::LABEL_COLUMN_METADATA, $labelColumn); return $table; } private function firstNumericRow(DataTable $table): DataTable { $columns = []; $first = $table->getFirstRow(); if ($first !== false) { foreach ($first->getColumns() as $name => $value) { if (is_numeric($value)) { $columns[$name] = $value + 0; } } } $result = new DataTable\Simple(); $result->addRowsFromArray($columns); return $result; } } |
| You are visitor 01402775 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |