protected static function _getStats($records, &$stats, $date) { $keys = ['country', 'os', 'device', 'referer']; foreach ($records as $r) { $obj = Utils::toArray($r); $arr =& $stats[$date]['meta']; foreach ($keys as $k) { if (!isset($arr[$k])) { $arr[$k] = []; } $index = $r['_id'][$k] ?? null; if (is_null($index)) { continue; } if (strlen(trim($index)) === 0) { $index = "Empty"; } ArrayMethods::counter($arr[$k], $index, $obj['count']); } } }
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; }
/** * @before _secure * @after _displayData * @todo fetch realtime data for today (if start == end) But if start != today then fetch data from * performance table and add it with realtime */ public function publishers() { $this->seo(["title" => "Publisher Rankings"]); $view = $this->getActionView(); $match = ['pid' => ['$in' => $this->org->users('publisher')], 'is_bot' => false, 'created' => Db::dateQuery($this->start, $this->end)]; $records = Db::collection('Click')->aggregate([['$match' => $match], ['$project' => ['pid' => 1, 'device' => 1, '_id' => 0]], ['$group' => ['_id' => ['pid' => '$pid', 'device' => '$device'], 'count' => ['$sum' => 1]]], ['$sort' => ['count' => -1]], ['$limit' => (int) $this->limit]]); $stats = $deviceStats = []; foreach ($records as $r) { $obj = Utils::toArray($r); $_id = $obj['_id']; $pid = Utils::getMongoID($_id['pid']); ArrayMethods::counter($stats, $pid, $obj['count']); if (!isset($deviceStats[$pid])) { $deviceStats[$pid] = []; } ArrayMethods::counter($deviceStats[$pid], $_id['device'], $obj['count']); /*$adClicks = Click::classify($pubClicks, 'adid'); $br = 0; $total = count($adClicks); foreach ($adClicks as $adid => $records) { $pageViews = Db::query('PageView', ['pid' => $pid, 'adid' => $adid], ['cookie']); // Create a counter based on cookie and select only the values whose // counter is less than 2 $multiPageSessions = 0; $totalClicks = count($records); $pageViews = Click::classify($pageViews, 'cookie'); foreach ($pageViews as $ckid => $rows) { if (count($rows) >= 2) { $multiPageSessions++; } } $bounce = 1 - ($multiPageSessions / $totalClicks); $br += $bounce; } $bounceRate[$pid] = (int) (round($br / $total, 2) * 100);*/ } $view->set('stats', $stats)->set('deviceStats', $deviceStats)->set('bounceRate', $bounceRate ?? []); }
public static function total($dq = [], $user = null, $fields = []) { $name = __CLASS__; if (count($fields) === 0) { $fields = ['clicks', 'conversions', 'impressions', 'revenue']; } $project = ['user_id' => 1, '_id' => 0]; $group = ['_id' => '$user_id']; foreach ($fields as $f) { $project[$f] = 1; $group[$f] = ['$sum' => '$' . $f]; } $match = ['created' => Db::dateQuery($dq['start'], $dq['end'])]; if (is_array($user)) { $keys = ArrayMethods::arrayKeys($user, '_id'); $match['user_id'] = ['$in' => Db::convertType($keys)]; } else { $match['user_id'] = Db::convertType($user->_id); } $records = Db::collection($name)->aggregate([['$match' => $match], ['$project' => $project], ['$group' => $group]]); $result = []; foreach ($records as $r) { $obj = Utils::toArray($r); $add = []; foreach ($fields as $f) { $add[$f] = $obj[$f]; } ArrayMethods::add($add, $result); } return $result; }
/** * Converts the MongoDB result to an object of class * whose parent is \Shared\Model */ protected function _convert($record) { if (!$record) { return null; } $columns = $this->getColumns(); $record = (array) $record; $class = get_class($this); $c = new $class(); foreach ($record as $key => $value) { if (!property_exists($this, "_{$key}")) { continue; } $raw = "_{$key}"; if (is_object($value)) { if (Db::isType($value, 'id')) { $c->{$raw} = $this->getMongoID($value); } else { if (Db::isType($value, 'date')) { $v = $value->toDateTime(); $v->settimezone(new \DateTimeZone('Asia/Kolkata')); $c->{$raw} = $v; } else { if (Db::isType($value, 'document')) { $c->{$raw} = Utils::toArray($value); } else { // fallback case $c->{$raw} = (object) $value; } } } } else { $c->{$raw} = $value; } } return $c; }
/** * @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); }
/** * @before _secure * @after _cleanUp */ public function campaign($id = null) { $view = $this->getActionView(); $org = $this->_org; $active = RequestMethods::get('active', 1); $fields = ['_id', 'title', 'description', 'image', 'url', 'device', 'expiry', 'created']; $commFields = ['model', 'rate', 'revenue', 'coverage']; if ($id) { $campaign = Ad::first(['_id' => $id, 'org_id' => $org->_id], $fields); } else { $campaign = null; } if ($id && !$campaign) { return $this->failure('30'); } $type = RequestMethods::type(); switch ($type) { case 'GET': if (!$id) { // display list of campaigns $ads = Ad::all(['org_id' => $org->_id, 'live' => $active], $fields); $ads = Ad::objectArr($ads, $fields); $results = []; foreach ($ads as $id => $a) { $arr = Utils::toArray($a); $comms = Commission::all(['ad_id' => $a->_id], $commFields); $arr['commissions'] = Ad::objectArr($comms, $commFields); $results[$id] = (object) $arr; } $data = ['campaigns' => $results]; $view->set('data', $data); } else { $ads = Ad::objectArr($campaign, $fields); $campaign = array_shift($ads); $comm = Commission::all(['ad_id' => $campaign->_id], $commFields); $data = ['campaign' => $campaign, 'commissions' => Commission::objectArr($comm, $commFields)]; $view->set('data', $data); } break; case 'POST': if ($id) { // edit a particular campaign } else { // create a new campaign $fields = ['title', 'description', 'url', 'expiry', 'category', 'device', 'user_id']; $img = RequestMethods::post('image'); // contains image url $campaign = new Ad(['org_id' => $org->_id, 'type' => 'article', 'image' => Utils::media($img, 'download')]); foreach ($fields as $f) { $campaign->{$f} = RequestMethods::post($f); } $view->set('success', false); $visibility = RequestMethods::post('visibility', 'public'); if ($visibility === 'private') { $campaign->getMeta()['private'] = true; } $opts = ['devices' => array_keys(Shared\Markup::devices()), 'advertisers' => $org->users('advertiser', false)]; if (true) { // $campaign->save(); $arr = ArrayMethods::reArray($_POST['commissions']); var_dump($arr); var_dump($_POST['commissions']); $view->set('success', true); } else { $data = ['errors' => $campaign->errors]; $view->set('data', $data); } } break; case 'DELETE': $message = $campaign->delete(); $view->set($message); break; } }
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"); }