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
|
<?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\Columns;
use Piwik\Cache;
use Piwik\CacheId;
use Piwik\Piwik;
use Piwik\Plugin\ArchivedMetric;
use Piwik\Plugin\Metric;
use Piwik\Plugin\ProcessedMetric;
/**
* Manages the global list of metrics that can be used in reports.
*
* Metrics are added automatically by dimensions as well as through the {@hook Metric.addMetrics} and
* {@hook Metric.addComputedMetrics} and filtered through the {@hook Metric.filterMetrics} event.
* Observers for this event should call the {@link addMetric()} method to add metrics or use any of the other
* methods to remove metrics.
*
* @api since Piwik 3.2.0
*/
class MetricsList
{
/**
* List of metrics
*
* @var Metric[]
*/
private $metrics = array();
private $metricsByNameCache = array();
public function addMetric(Metric $metric)
{
$this->metrics[] = $metric;
$this->metricsByNameCache = array();
}
/**
* Get all available metrics.
*
* @return Metric[]
*/
public function getMetrics()
{
return $this->metrics;
}
/**
* Removes one or more metrics from the metrics list.
*
* @param string $metricCategory The metric category id. Can be a translation token eg 'General_Visits'
* see {@link Metric::getCategory()}.
* @param string|false $metricName The name of the metric to remove eg 'nb_visits'.
* If not supplied, all metrics within that category will be removed.
*/
public function remove($metricCategory, $metricName = false)
{
foreach ($this->metrics as $index => $metric) {
if ($metric->getCategoryId() === $metricCategory) {
if (!$metricName || $metric->getName() === $metricName) {
unset($this->metrics[$index]);
$this->metricsByNameCache = array();
}
}
}
}
/**
* @param string $metricName
* @return Metric|ArchivedMetric|null
*/
public function getMetric($metricName)
{
if (empty($this->metricsByNameCache)) {
// this method might be called quite often... eg when having heaps of goals... need to cache it
foreach ($this->metrics as $index => $metric) {
$this->metricsByNameCache[$metric->getName()] = $metric;
}
}
if (!empty($this->metricsByNameCache[$metricName])) {
return $this->metricsByNameCache[$metricName];
}
return null;
}
/**
* Get all metrics defined in the Piwik platform.
* @ignore
* @return static
*/
public static function get()
{
$cache = Cache::getTransientCache();
$cacheKey = CacheId::siteAware('MetricsList');
if ($cache->contains($cacheKey)) {
return $cache->fetch($cacheKey);
}
$list = new static();
/**
* Triggered to add new metrics that cannot be picked up automatically by the platform.
* This is useful if the plugin allows a user to create metrics dynamically. For example
* CustomDimensions or CustomVariables.
*
* **Example**
*
* public function addMetric(&$list)
* {
* $list->addMetric(new MyCustomMetric());
* }
*
* @param MetricsList $list An instance of the MetricsList. You can add metrics to the list this way.
*/
Piwik::postEvent('Metric.addMetrics', array($list));
$dimensions = Dimension::getAllDimensions();
foreach ($dimensions as $dimension) {
$factory = new DimensionMetricFactory($dimension);
$dimension->configureMetrics($list, $factory);
}
$computedFactory = new ComputedMetricFactory($list);
/**
* Triggered to add new metrics that cannot be picked up automatically by the platform.
* This is useful if the plugin allows a user to create metrics dynamically. For example
* CustomDimensions or CustomVariables.
*
* **Example**
*
* public function addMetric(&$list)
* {
* $list->addMetric(new MyCustomMetric());
* }
*
* @param MetricsList $list An instance of the MetricsList. You can add metrics to the list this way.
*/
Piwik::postEvent('Metric.addComputedMetrics', array($list, $computedFactory));
/**
* Triggered to filter metrics.
*
* **Example**
*
* public function removeMetrics(Piwik\Columns\MetricsList $list)
* {
* $list->remove($category='General_Visits'); // remove all metrics having this category
* }
*
* @param MetricsList $list An instance of the MetricsList. You can change the list of metrics this way.
*/
Piwik::postEvent('Metric.filterMetrics', array($list));
$availableMetrics = array();
foreach ($list->getMetrics() as $metric) {
$availableMetrics[] = $metric->getName();
}
foreach ($list->metrics as $index => $metric) {
if ($metric instanceof ProcessedMetric) {
$depMetrics = $metric->getDependentMetrics();
if (is_array($depMetrics)) {
foreach ($depMetrics as $depMetric) {
if (!in_array($depMetric, $availableMetrics, $strict = true)) {
unset($list->metrics[$index]); // not resolvable metric
}
}
}
}
}
$cache->save($cacheKey, $list);
return $list;
}
}
|