public function updateStatsAction()
 {
     /** @var NewsEntry[] $entries */
     $entries = NewsEntry::find([['stats.lastUpdated' => ['$lt' => time() - 2 * 60 * 60], 'publishedAt' => ['$gt' => time() - 24 * 60 * 60]]]);
     $this->log('Will update relevance of ' . count($entries) . ' entries.');
     foreach ($entries as $entry) {
         $this->log(' -> ' . $entry->title);
         $entry->updateStats();
     }
     $this->log('Done');
 }
 public function updateRelevance()
 {
     // Reset
     $this->stats = ['fb' => ['likes' => [], 'shares' => [], 'comments' => [], 'click' => []], 'twitter' => ['count' => []]];
     /** @var NewsEntry[] $entries */
     $entries = NewsEntry::find(['conditions' => ['sourceId' => $this->_id]]);
     // Update Mean
     $count = count($entries);
     foreach ($entries as $entry) {
         $this->stats['fb']['likes'][] = $entry->stats['fb']['likes'];
         $this->stats['fb']['shares'][] = $entry->stats['fb']['shares'];
         $this->stats['fb']['comments'][] = $entry->stats['fb']['comments'];
         $this->stats['fb']['click'][] = $entry->stats['fb']['click'];
         $this->stats['twitter']['count'][] = $entry->stats['twitter']['count'];
     }
     $this->stats['fb']['likes'] = Math::specialMean($this->stats['fb']['likes']);
     $this->stats['fb']['shares'] = Math::specialMean($this->stats['fb']['shares']);
     $this->stats['fb']['comments'] = Math::specialMean($this->stats['fb']['comments']);
     $this->stats['fb']['click'] = Math::specialMean($this->stats['fb']['click']);
     $this->stats['twitter']['count'] = Math::specialMean($this->stats['twitter']['count']);
     // Update Entry Relative Stats
     foreach ($entries as $entry) {
         $updateRelative = function ($social, $type) use(&$entry) {
             if ($this->stats[$social][$type] == 0) {
                 $entry->relativeStats[$social][$type] = 1;
             } else {
                 $entry->relativeStats[$social][$type] = $entry->stats[$social][$type] / $this->stats[$social][$type];
             }
         };
         $updateRelative('fb', 'likes');
         $updateRelative('fb', 'shares');
         $updateRelative('fb', 'comments');
         $updateRelative('fb', 'click');
         $updateRelative('twitter', 'count');
         $entry->updateRelevance();
         $entry->save();
     }
     $this->save();
 }