public function testMongoID() { $object = new \MongoDB\BSON\ObjectID(); $expected = Utils::getMongoID($object); $known = $object->__toString(); $this->assertEquals($known, $expected, 'ObjectID is not converted to string'); $this->assertInstanceOf('MongoDB\\BSON\\ObjectID', Utils::mongoObjectId($expected), 'Failed to convert it to bson object'); }
public static function remove(&$categories) { $success = true; foreach ($categories as $c) { if (!$c->inUse()) { unset($categories[Utils::getMongoID($c->_id)]); $c->delete(); } else { $success = false; } } return $success; }
public static function rssFeeds($org) { $users = \User::all(['org_id' => $org->_id, 'type' => 'advertiser'], ['_id']); $in = []; $result = []; foreach ($users as $u) { $in[] = $u->_id; } $platforms = \Platform::all(['user_id' => ['$in' => $in]], ['_id', 'url', 'user_id', 'meta']); foreach ($platforms as $p) { if (isset($p->meta['rss'])) { $result[Utils::getMongoID($p->_id)] = $p; } } return $result; }
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; }
/** * Finds the commission based on the "ad_id" * @param array &$search Array of Commission (to prevent querying from database again and again) * @param mixed $key Object|String representing Ad ID */ public static function find(&$search, $key) { $key = \Shared\Utils::getMongoID($key); if (!array_key_exists($key, $search)) { $commissions = self::all(['ad_id' => $key], ['rate', 'revenue', 'model', 'coverage']); $search[$key] = $comm = self::filter($commissions); } else { $comm = $search[$key]; } return $comm; }
public static function classify($clicks, $type = 'adid') { $classify = []; foreach ($clicks as $result) { $c = ArrayMethods::toObject($result); $key = Utils::getMongoID($c->{$type} ?? ''); if (strlen($key) == 0) { $key = "Empty"; } $key = str_replace(".", "-", $key); if (!isset($classify[$key]) || !array_key_exists($key, $classify)) { $classify[$key] = []; } $classify[$key][] = $c; } return $classify; }
/** * @before _secure * @after _displayData */ public function platforms($id = null) { $this->seo(["title" => "Platform wise click stats"]); $view = $this->getActionView(); $org = $this->org; $clickCol = Registry::get("MongoDB")->clicks; // find the platforms $platforms = \Platform::all(['user_id' => ['$in' => $org->users('advertisers')]], ['_id', 'url']); if (count($platforms) === 0) { return $view->set(['platforms' => [], 'publishers' => []]); } $key = array_rand($platforms); $url = RM::get('link', $platforms[$key]->url); // find ads having this url $ads = \Ad::all(['org_id' => $org->_id], ['_id', 'url']); $in = Utils::mongoObjectId(array_keys($ads)); $matched = []; foreach ($ads as $a) { $regex = preg_quote($url, '.'); if (preg_match('#^' . $regex . '#', $a->url)) { $matched[] = Utils::mongoObjectId($a->_id); } } if (count($matched) === 0) { $query['adid'] = ['$in' => $in]; } else { $query['adid'] = ['$in' => $matched]; } $query['is_bot'] = false; $query['created'] = Db::dateQuery($this->start, $this->end); $records = $clickCol->aggregate([['$match' => $query], ['$projection' => ['_id' => 1, 'pid' => 1]], ['$group' => ['_id' => '$pid', 'count' => ['$sum' => 1]]], ['$sort' => ['count' => -1]]]); $result = []; $publishers = []; foreach ($records as $r) { $obj = (object) $r; $id = Utils::getMongoID($obj->_id); $user = User::first(['_id' => $id], ['_id', 'name']); $result[$id] = (object) ['_id' => $user->_id, 'name' => $user->name, 'clicks' => $obj->count]; } $view->set(['platforms' => $platforms, 'link' => $url, 'publishers' => $result]); }
protected function _all($where = [], $fields = [], $order = null, $direction = null, $limit = null, $page = null) { $collection = $this->getTable(); $opts = Db::opts($fields, $order, $direction, $limit, $page); $cursor = $collection->find($where, $opts); $results = []; foreach ($cursor as $c) { $converted = $this->_convert($c); if ($converted->_id) { $key = Utils::getMongoID($converted->_id); $results[$key] = $converted; } else { $results[] = $converted; } } return $results; }
/** * @before _secure */ public function manage() { $this->seo(['title' => 'Campaign: List', 'description' => 'Manage campaigns']); $view = $this->getActionView(); $campaigns = []; $page = RM::get("page", 1); $limit = RM::get("limit", 10); $property = RM::get("property"); $value = RM::get("value"); switch (RM::get("sort")) { case 'trending': $start = RM::get("start", date("Y-m-d", strtotime('now'))); $end = RM::get("end", date("Y-m-d", strtotime('now'))); $q = ['start' => $start, 'end' => $end]; $view->set($q); // Only find the ads for this organizations $allAds = \Ad::all(['org_id' => $this->org->_id], ['_id']); $in = Db::convertType(array_keys($allAds)); $clickCol = Db::collection('Click'); $match = ['created' => Db::dateQuery($start, $end), 'is_bot' => false, 'adid' => ['$in' => $in]]; $records = $clickCol->aggregate([['$match' => $match], ['$project' => ['adid' => 1, '_id' => 0]], ['$group' => ['_id' => '$adid', 'count' => ['$sum' => 1]]], ['$sort' => ['count' => -1]], ['$limit' => (int) $limit]]); foreach ($records as $r) { $arr = Utils::toArray($r); $id = Utils::getMongoID($arr['_id']); $campaigns[] = Ad::first(["id = ?" => $id]); } $count = $limit; break; default: $query = ["org_id = ?" => $this->org->id]; if (in_array($property, ["user_id", "live", "id"])) { $query[$property] = $value; } else { if (in_array($property, ["url", "title"])) { $query[$property] = Utils::mongoRegex(preg_quote($value)); } } $campaigns = \Ad::all($query, [], 'created', 'desc', $limit, $page); $count = \Ad::count($query); break; } $categories = \Category::all(['org_id' => $this->org->_id], ['_id', 'name']); $active = \Ad::count(["org_id = ?" => $this->org->id, "live = ?" => 1]); $inactive = \Ad::count(["org_id = ?" => $this->org->id, "live = ?" => 0]); $view->set("campaigns", $campaigns)->set("count", $count)->set("active", $active)->set("inactive", $inactive)->set("limit", $limit)->set("page", $page)->set("property", $property)->set("value", $value)->set("categories", $categories); }
public static function find(&$orgs, $key) { $key = Utils::getMongoID($key); if (!array_key_exists($key, $orgs)) { $org = self::first(['_id' => $key], ['url', 'meta']); $orgs[$key] = $org; } else { $org = $orgs[$key]; } return $org; }
public static function find(&$search, $key, $fields = []) { $key = Utils::getMongoID($key); if (!array_key_exists($key, $search)) { $ad = self::first(['_id' => $key], $fields); $search[$key] = $ad; } else { $ad = $search[$key]; } return $ad; }
public function _widgets() { $this->log("Widgets Started"); $start = $end = date('Y-m-d'); $orgs = Organization::all(["live = ?" => true]); foreach ($orgs as $org) { if (!array_key_exists("widgets", $org->meta)) { continue; } $pubs = User::all(["org_id = ?" => $org->_id, "type = ?" => "publisher"], ["_id", "username"]); $in = array_keys($pubs); $in = Db::convertType($in); $match = ["created" => Db::dateQuery($start, $end), "is_bot" => false, "pid" => ['$in' => $in]]; $records = Db::collection('Click')->aggregate([['$match' => $match], ['$project' => ['adid' => 1, '_id' => 0, 'pid' => 1]], ['$group' => ['_id' => ['adid' => '$adid', 'pid' => '$pid'], 'count' => ['$sum' => 1]]], ['$sort' => ['count' => -1]]]); $adClicks = []; $pubClicks = []; foreach ($records as $r) { $c = Utils::toArray($r); $adid = Utils::getMongoID($c['_id']['adid']); $pid = Utils::getMongoID($c['_id']['pid']); ArrayMethods::counter($adClicks, $adid, $c['count']); ArrayMethods::counter($pubClicks, $pid, $c['count']); } $org->widgets($pubClicks, $adClicks, $pubs); $logMsg = sprintf("Widget Saved for Org: %s", $org->name); $this->log($logMsg); } $this->log("Widgets Done"); }