private function runAlerts($period, $idSite)
 {
     $processor = new Processor();
     $processor->processAlerts($period, (int) $idSite);
     $notifier = new Notifier();
     $notifier->sendNewAlerts($period, (int) $idSite);
 }
 /**
  * Calculates the alert value for each site for the given days/weeks/months in past. If the period of the alert is
  * weeks and subPeriodN is "7" it will return the value for the week 7 weeks ago. Set subPeriodN to "0" to test the
  * current day/week/month.
  *
  * @param int $idAlert
  * @param int $subPeriodN
  *
  * @return array
  */
 public function getValuesForAlertInPast($idAlert, $subPeriodN)
 {
     $alert = $this->getAlert($idAlert);
     $processor = new Processor();
     $values = array();
     foreach ($alert['id_sites'] as $idSite) {
         $values[] = array('idSite' => (int) $idSite, 'value' => $processor->getValueForAlertInPast($alert, $idSite, (int) $subPeriodN));
     }
     return $values;
 }
 public function shouldBeTriggered($alert, $metricOne, $metricTwo)
 {
     return parent::shouldBeTriggered($alert, $metricOne, $metricTwo);
 }
 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;
 }
 public function isValidMetricCondition($condition)
 {
     $conditions = Processor::getMetricConditions();
     $conditions = array_values($conditions);
     return in_array($condition, $conditions);
 }