Sets several metadata values by name.
public setMetadataValues ( array $values ) | ||
$values | array | Array mapping metadata names with metadata values. |
/** * Sets the total visits, actions & revenue for a DataTable returned by * $this->buildDataTable. * * @param DataTable $dataTable * @param array $apiMetrics Metrics info. * @return array Array of three values: total visits, total actions, total revenue */ private function setMetricsTotalsMetadata($dataTable, $apiMetrics) { if ($dataTable instanceof DataTable\Map) { foreach ($dataTable->getDataTables() as $table) { $this->setMetricsTotalsMetadata($table, $apiMetrics); } } else { $totals = array(); foreach ($apiMetrics as $label => $recordName) { $totals[$label] = 0; } foreach ($dataTable->getRows() as $row) { foreach ($apiMetrics as $totalMetadataName => $recordName) { $totals[$totalMetadataName] += $row->getColumn($recordName); } } $dataTable->setMetadataValues($totals); } }
/** * Merges the rows of every child {@link DataTable} into a new one and * returns it. This function will also set the label of the merged rows * to the label of the {@link DataTable} they were originally from. * * The result of this function is determined by the type of DataTable * this instance holds. If this DataTable\Map instance holds an array * of DataTables, this function will transform it from: * * Label 0: * DataTable(row1) * Label 1: * DataTable(row2) * * to: * * DataTable(row1[label = 'Label 0'], row2[label = 'Label 1']) * * If this instance holds an array of DataTable\Maps, this function will * transform it from: * * Outer Label 0: // the outer DataTable\Map * Inner Label 0: // one of the inner DataTable\Maps * DataTable(row1) * Inner Label 1: * DataTable(row2) * Outer Label 1: * Inner Label 0: * DataTable(row3) * Inner Label 1: * DataTable(row4) * * to: * * Inner Label 0: * DataTable(row1[label = 'Outer Label 0'], row3[label = 'Outer Label 1']) * Inner Label 1: * DataTable(row2[label = 'Outer Label 0'], row4[label = 'Outer Label 1']) * * If this instance holds an array of DataTable\Maps, the * metadata of the first child is used as the metadata of the result. * * This function can be used, for example, to smoosh IndexedBySite archive * query results into one DataTable w/ different rows differentiated by site ID. * * Note: This DataTable/Map will be destroyed and will be no longer usable after the tables have been merged into * the new dataTable to reduce memory usage. Destroying all DataTables witihn the Map also seems to fix a * Segmentation Fault that occurred in the AllWebsitesDashboard when having > 16k sites. * * @return DataTable|Map */ public function mergeChildren() { $firstChild = reset($this->array); if ($firstChild instanceof Map) { $result = $firstChild->getEmptyClone(); /** @var $subDataTableMap Map */ foreach ($this->getDataTables() as $label => $subDataTableMap) { foreach ($subDataTableMap->getDataTables() as $innerLabel => $subTable) { if (!isset($result->array[$innerLabel])) { $dataTable = new DataTable(); $dataTable->setMetadataValues($subTable->getAllTableMetadata()); $result->addTable($dataTable, $innerLabel); } $this->copyRowsAndSetLabel($result->array[$innerLabel], $subTable, $label); } } } else { $result = new DataTable(); foreach ($this->getDataTables() as $label => $subTable) { $this->copyRowsAndSetLabel($result, $subTable, $label); Common::destroy($subTable); } $this->array = array(); } return $result; }
/** * Creates a DataTable for one record from an archive data row. * * @see makeFromBlobRow * * @param array $blobRow * @return DataTable */ private function makeDataTableFromSingleBlob($blobRow) { $recordName = reset($this->dataNames); if ($this->idSubtable !== null) { $recordName .= '_' . $this->idSubtable; } if (!empty($blobRow[$recordName])) { $table = DataTable::fromSerializedArray($blobRow[$recordName]); } else { $table = new DataTable(); } // set table metadata $table->setMetadataValues(DataCollection::getDataRowMetadata($blobRow)); if ($this->expandDataTable) { $table->enableRecursiveFilters(); $this->setSubtables($table, $blobRow); } return $table; }
/** * @param array $reportMetadata * @param string $period * @param string $date * @param string $lastDate * @param string $metric * @param DataTable $currentReport * @param DataTable $lastReport * @param int $totalValue * @param int $minMoversPercent Exclude rows who moved and the difference is not at least min percent * visits of totalVisits. -1 excludes movers. * @param int $minNewPercent Exclude rows who are new and the difference is not at least min percent * visits of totalVisits. -1 excludes all new. * @param int $minDisappearedPercent Exclude rows who are disappeared and the difference is not at least min * percent visits of totalVisits. -1 excludes all disappeared. * @param int $minGrowthPercentPositive The actual growth of a row must be at least percent compared to the * previous value (not total value) * @param int $minGrowthPercentNegative The actual growth of a row must be lower percent compared to the * previous value (not total value) * @param string $orderBy Order by absolute, relative, importance * @param int $limitIncreaser * @param int $limitDecreaser * * @return DataTable */ public function generateInsight($reportMetadata, $period, $date, $lastDate, $metric, $currentReport, $lastReport, $totalValue, $minMoversPercent, $minNewPercent, $minDisappearedPercent, $minGrowthPercentPositive, $minGrowthPercentNegative, $orderBy, $limitIncreaser, $limitDecreaser) { $minChangeMovers = $this->getMinVisits($totalValue, $minMoversPercent); $minIncreaseNew = $this->getMinVisits($totalValue, $minNewPercent); $minDecreaseDisappeared = $this->getMinVisits($totalValue, $minDisappearedPercent); $dataTable = new DataTable(); $dataTable->filter('Piwik\\Plugins\\Insights\\DataTable\\Filter\\Insight', array($currentReport, $lastReport, $metric, $considerMovers = -1 !== $minMoversPercent, $considerNew = -1 !== $minNewPercent, $considerDisappeared = -1 !== $minDisappearedPercent)); $dataTable->filter('Piwik\\Plugins\\Insights\\DataTable\\Filter\\MinGrowth', array('growth_percent_numeric', $minGrowthPercentPositive, $minGrowthPercentNegative)); if ($minIncreaseNew) { $dataTable->filter('Piwik\\Plugins\\Insights\\DataTable\\Filter\\ExcludeLowValue', array('difference', $minIncreaseNew, 'isNew')); } if ($minChangeMovers) { $dataTable->filter('Piwik\\Plugins\\Insights\\DataTable\\Filter\\ExcludeLowValue', array('difference', $minChangeMovers, 'isMover')); } if ($minDecreaseDisappeared) { $dataTable->filter('Piwik\\Plugins\\Insights\\DataTable\\Filter\\ExcludeLowValue', array('difference', $minDecreaseDisappeared, 'isDisappeared')); } $dataTable->filter('Piwik\\Plugins\\Insights\\DataTable\\Filter\\OrderBy', array($this->getOrderByColumn($orderBy), $orderBy === self::ORDER_BY_RELATIVE ? $this->getOrderByColumn(self::ORDER_BY_ABSOLUTE) : $this->getOrderByColumn(self::ORDER_BY_RELATIVE), $metric)); $dataTable->filter('Piwik\\Plugins\\Insights\\DataTable\\Filter\\Limit', array('growth_percent_numeric', $limitIncreaser, $limitDecreaser)); $metricName = $metric; if (!empty($reportMetadata['metrics'][$metric])) { $metricName = $reportMetadata['metrics'][$metric]; } $dataTable->setMetadataValues(array('reportName' => $reportMetadata['name'], 'metricName' => $metricName, 'date' => $date, 'lastDate' => $lastDate, 'period' => $period, 'report' => $reportMetadata, 'totalValue' => $totalValue, 'orderBy' => $orderBy, 'metric' => $metric, 'minChangeMovers' => $minChangeMovers, 'minIncreaseNew' => $minIncreaseNew, 'minDecreaseDisappeared' => $minDecreaseDisappeared, 'minGrowthPercentPositive' => $minGrowthPercentPositive, 'minGrowthPercentNegative' => $minGrowthPercentNegative, 'minMoversPercent' => $minMoversPercent, 'minNewPercent' => $minNewPercent, 'minDisappearedPercent' => $minDisappearedPercent)); return $dataTable; }