Esempio n. 1
0
 /**
  * Find the Performance of Affiliates|Advertisers of an organization
  * @param  object $org  \Organization
  * @param  string $type Type of user
  * @return [type]       [description]
  */
 public static function perf($org, $type, $opts = [])
 {
     $start = $opts['start'] ?? RequestMethods::get('start', date('Y-m-d', strtotime("-5 day")));
     $end = $opts['end'] ?? RequestMethods::get('end', date('Y-m-d', strtotime("-1 day")));
     switch ($type) {
         case 'publisher':
             $perfFields = $opts['fields'] ?? ['clicks', 'impressions', 'conversions', 'revenue', 'created'];
             $meta = $opts['meta'] ?? false;
             if ($meta) {
                 $perfFields[] = 'meta';
             }
             $publishers = $opts['publishers'] ?? $org->users($type);
             $pubPerf = Perf::all(['user_id' => ['$in' => $publishers], 'created' => Db::dateQuery($start, $end)], $perfFields, 'created', 'asc');
             $pubPerf = Perf::objectArr($pubPerf, $perfFields);
             return $pubPerf;
         case 'advertiser':
             $advertisers = $opts['advertisers'] ?? $org->users($type);
             $fields = $opts['fields'] ?? ['revenue', 'created'];
             $advertPerf = Perf::all(['user_id' => ['$in' => $advertisers], 'created' => Db::dateQuery($start, $end)], $fields, 'created', 'asc');
             $advertPerf = Perf::objectArr($advertPerf, $fields);
             return $advertPerf;
     }
     return [];
 }
Esempio n. 2
0
 public static function livePerf($user, $s = null, $e = null)
 {
     $start = $end = date('Y-m-d');
     $type = $user->type ?? '';
     if ($s) {
         $start = $s;
     }
     if ($e) {
         $end = $e;
     }
     $perf = new \Performance();
     $match = ['is_bot' => false, 'created' => Db::dateQuery($start, $end)];
     switch ($user->type) {
         case 'publisher':
             $match['pid'] = Db::convertType($user->_id);
             break;
         case 'advertiser':
             $ads = \Ad::all(['user_id' => $user->_id], ['_id']);
             $keys = array_keys($ads);
             $match['adid'] = ['$in' => Db::convertType($keys)];
             break;
         default:
             return $perf;
     }
     $results = $commissions = [];
     $records = self::_livePerfQuery($match);
     foreach ($records as $r) {
         $obj = Utils::toArray($r);
         $adid = Utils::getMongoID($obj['_id']);
         $results[$adid] = $obj;
     }
     $comms = Db::query('Commission', ['ad_id' => ['$in' => array_keys($results)]], ['ad_id', 'rate', 'revenue', 'model', 'coverage']);
     $comms = \Click::classify($comms, 'ad_id');
     foreach ($comms as $adid => $value) {
         $value = array_map(function ($v) {
             $v["ad_id"] = Utils::getMongoID($v["ad_id"]);
             unset($v["_id"]);
             return (object) $v;
         }, Utils::toArray($value));
         $commissions[$adid] = \Commission::filter($value);
     }
     foreach ($results as $adid => $obj) {
         $comms = $commissions[$adid];
         foreach ($obj['countries'] as $value) {
             $country = $value['country'];
             $clicks = $value['count'];
             $commission = \Commission::campaignRate($adid, $comms, $country, ['type' => $type, "{$type}" => $user, 'start' => $start, 'end' => $end, 'commFetched' => true]);
             $updateData = [];
             $earning = \Ad::earning($commission, $clicks);
             AM::copy($earning, $updateData);
             $perf->update($updateData);
         }
     }
     return $perf;
 }
Esempio n. 3
0
 public static function performance($id, $extra = [])
 {
     $stats = [];
     $start = $end = date('Y-m-d');
     $match = ['adid' => Db::convertType($id), 'is_bot' => false];
     $user_id = $extra['pid'] ?? null;
     if ($user_id) {
         $match["pid"] = Db::convertType($user_id);
     }
     if (isset($extra['start']) && isset($extra['end'])) {
         $start = $extra['start'];
         $end = $extra['end'];
     }
     $diff = date_diff(date_create($start), date_create($end));
     $dateWise = $extra['meta'] ?? true;
     // by default datewise query
     if ($dateWise) {
         for ($i = 0; $i <= $diff->format("%a"); $i++) {
             $date = date('Y-m-d', strtotime($start . " +{$i} day"));
             $stats[$date] = ['clicks' => 0, 'meta' => []];
             $match['created'] = Db::dateQuery($date, $date);
             $records = self::_perfQuery($match, $extra);
             self::_getStats($records, $stats, $date);
         }
     } else {
         $match['created'] = Db::dateQuery($start, $end);
         $stats[$start] = ['clicks' => 0, 'meta' => []];
         $records = self::_perfQuery($match, $extra);
         self::_getStats($records, $stats, $start);
     }
     $records = self::earning($stats, $id, $user_id);
     $total = Performance::calTotal($records);
     return ['stats' => $records, 'total' => $total];
 }