Example #1
0
 /**
  * Querys the MongoDB and retrieves the raw data through map/reduce
  *
  * @param string $type: The type of data to get. Selects the correct map/reduce functions
  * @param string $domain: The domain used to find the correct collection
  * @param string $fromDate: The fromDate of the range
  * @param string $toDate: The toDate of the range
  * @param string $aggregation: The aggregation type (daily, weekly, monthly)
  * @return array The raw data as it comes from the mongo db
  */
 public static function getDataForRange($type, $domain, $fromDate, $toDate, $aggregation, $url = null, $dealId = null)
 {
     $type = strstr($type, 'activities') ? 'activities' : 'demografics';
     // Merging types
     $col = MongoUtils::getCollection($dealId ? 'deals' : 'charts', $domain);
     $keys = array("date" => 1);
     $cond = array();
     if ($url) {
         $cond['url'] = $url;
     }
     if ($dealId) {
         $dealId = intval($dealId);
         $cond['d_id'] = $dealId;
     } else {
         $cond['date'] = array('$gte' => new MongoDate(strtotime($fromDate)), '$lte' => new MongoDate(strtotime($toDate)));
     }
     $initial = MongoUtils::getInitial($type);
     $reduce = MongoUtils::getReduce($type);
     $g = $col->group($keys, $initial, $reduce, array("condition" => $cond));
     $data['data'] = MongoUtils::getDataWithEmptyDayPadding($g['retval'], $fromDate, $toDate);
     $data['filter'] = MongoUtils::getFilter($domain, $fromDate, $toDate, $aggregation);
     if ($url) {
         $data['filter']['url'] = $url;
     }
     if ($dealId) {
         $data['filter']['deal_id'] = $dealId;
     }
     if ($type == 'activities') {
         if ($url) {
             $data['pis'] = MongoUtils::getPisDataForUrl($url, $fromDate, $toDate, $dealId);
         } else {
             $data['pis'] = MongoUtils::getPisDataForHost($domain, $fromDate, $toDate, $dealId);
         }
         $data['statistics'] = MongoUtils::getAdditionalStatistics($g['retval'], $data['pis'], $fromDate, $toDate);
     } elseif ($type == 'demografics') {
         $data['statistics'] = MongoUtils::getAdditionalDemograficStatistics($data['data'], $fromDate, $toDate);
     }
     return $data;
 }