Piwik\Plugins\API\ProcessedReport::translateMetric PHP Method

translateMetric() public method

Translates the given metric in case the report exists and in case the metric acutally belongs to the report.
public translateMetric ( string $metric, integer $idSite, string $apiMethodUniqueId ) : null | string
$metric string For example 'nb_visits'
$idSite integer
$apiMethodUniqueId string For example 'MultiSites_getAll'
return null | string
    public function translateMetric($metric, $idSite, $apiMethodUniqueId)
    {
        $report = $this->getReportMetadataByUniqueId($idSite, $apiMethodUniqueId);
        if (empty($report)) {
            return;
        }
        $properties = array('metrics', 'processedMetrics', 'processedMetricsGoal');
        foreach ($properties as $prop) {
            if (!empty($report[$prop]) && is_array($report[$prop]) && array_key_exists($metric, $report[$prop])) {
                return $report[$prop][$metric];
            }
        }
    }

Usage Example

コード例 #1
0
 protected function enrichTriggeredAlerts($triggeredAlerts)
 {
     $processedReport = new ProcessedReport();
     $cached = array();
     foreach ($triggeredAlerts as &$alert) {
         $idSite = $alert['idsite'];
         $metric = $alert['metric'];
         $report = $alert['report'];
         if (!array_key_exists($idSite, $cached)) {
             $cached[$idSite] = array('report' => array(), 'metric' => array(), 'siteName' => '', 'siteTimezone' => null);
         }
         if (empty($cached[$idSite]['siteName'])) {
             $cached[$idSite]['siteName'] = $this->findSiteName($alert);
         }
         if (empty($cached[$idSite]['siteTimezone']) && !empty($cached[$idSite]['siteName'])) {
             $cached[$idSite]['siteTimezone'] = Site::getTimezoneFor($idSite);
         }
         if (!array_key_exists($report, $cached[$idSite]['report'])) {
             $cached[$idSite]['report'][$report] = $this->findReportMetadata($alert);
             $cached[$idSite]['metric'][$report] = array();
         }
         if (is_array($cached[$idSite]['metric'][$report]) && !array_key_exists($metric, $cached[$idSite]['metric'][$report])) {
             $cached[$idSite]['metric'][$report][$metric] = $processedReport->translateMetric($metric, $idSite, $alert['report']);
         }
     }
     foreach ($triggeredAlerts as &$alert) {
         $idSite = $alert['idsite'];
         $metric = $alert['metric'];
         $report = $alert['report'];
         $cachedSite = $cached[$idSite];
         $alert['value_old'] = (int) $alert['value_old'] == $alert['value_old'] ? (int) $alert['value_old'] : $alert['value_old'];
         $alert['value_new'] = (int) $alert['value_new'] == $alert['value_new'] ? (int) $alert['value_new'] : $alert['value_new'];
         $alert['reportName'] = null;
         $alert['dimension'] = null;
         $alert['reportMetric'] = !empty($cachedSite['metric'][$report][$metric]) ? $cachedSite['metric'][$report][$metric] : null;
         $alert['reportConditionName'] = null;
         $alert['siteName'] = $cached[$idSite]['siteName'];
         $alert['ts_triggered'] = $this->getPrettyDateForSite($alert['ts_triggered'], $alert['period'], $cachedSite['siteTimezone']);
         if (!empty($cachedSite['report'][$report])) {
             $reportMetadata = $cachedSite['report'][$report];
             $alert['reportName'] = $reportMetadata['name'];
             $alert['dimension'] = !empty($reportMetadata['dimension']) ? $reportMetadata['dimension'] : null;
             $conditionTranslation = array_search($alert['report_condition'], Processor::getGroupConditions(), true);
             $alert['reportConditionName'] = $conditionTranslation ? Piwik::translate($conditionTranslation) : null;
         }
     }
     return $triggeredAlerts;
 }