public function incTotalInventory($fid, $event) { $sumID = (string) $fid; $check = $this->where(array('sum_id' => $sumID))->first(); if (empty($check)) { $this->sum_id = $sumID; $this->flight_id = $fid; $this->inventory = (int) TrackingSummaryBaseModel::where(array('flight_id' => $fid, 'ovr' => 1))->sum($event); $this->save() + 1; } else { $check->increment('inventory'); } }
public function reportSchedule($numRecord = 30000) { set_time_limit(0); $tblTSS = 'tracking_summary_schedule'; $cacheKey = "Report:Schedule"; if (RedisHelper::get($cacheKey)) { return false; } RedisHelper::set($cacheKey, 1, 2); // begin transaction DB::beginTransaction(); $beginAt = new MongoDate(strtotime('2015-03-31 14:46:18')); $lastReport = DB::table($tblTSS)->orderBy('id', 'desc')->first(); if ($lastReport) { // time - 1 để chắc rằng ko bị sót record $beginAt = new MongoDate($lastReport->last_record_sec - 2); } $endAt = new MongoDate(strtotime("-1 minute")); $data = $this->where('created_at', '>=', $beginAt)->where('created_at', '<', $endAt)->take($numRecord)->get(); pr($data->count()); // pr($endAtTimestamp); // pr($data->toArray()); try { if ($data->count() > 0) { // insert new report schedule $trackingSummaryScheduleID = DB::table($tblTSS)->insertGetId(array('begin_at' => date('Y-m-d H:i:s'))); $reportArr = array(); $uniqueVisitor = array(); $lastRecord = ''; foreach ($data as $k => $record) { //check duplicate record on report if ($lastReport && $record->_id == $lastReport->last_record_id) { $reportArr = array(); continue; } $record->event = strtolower($record->event); $isOverReport = !empty($record->ovr) ? 'OverReport' : "NormalReport"; $key = "{$isOverReport}:{$record->date}:{$record->hour}:{$record->publisher_ad_zone_id}:{$record->website_id}:{$record->flight_website_id}"; if (empty($reportArr[$key])) { $reportArr[$key] = array('publisher_ad_zone_id' => $record->publisher_ad_zone_id, 'ad_id' => $record->ad_id, 'ad_format_id' => $record->ad_format_id, 'campaign_id' => $record->campaign_id, 'flight_id' => $record->flight_id, 'flight_website_id' => $record->flight_website_id, 'website_id' => $record->website_id, 'hour' => $record->hour, 'date' => $record->date); } //ads reponses summary $reportArr[$key][$record->response_type] = empty($reportArr[$key][$record->response_type]) ? 1 : $reportArr[$key][$record->response_type] + 1; //ads request summary if ($record->request_type == Delivery::REQUEST_TYPE_AD) { $reportArr[$key]['ads_request'] = empty($reportArr[$key]['ads_request']) ? 1 : $reportArr[$key]['ads_request'] + 1; } if ($record->status) { if ($record->response_type == Delivery::RESPONSE_TYPE_TRACKING_BEACON_SUCCESS) { if (!empty($reportArr[$key][$record->event])) { $reportArr[$key][$record->event]++; } else { $reportArr[$key][$record->event] = 1; } if ($record->event == 'impression' && !empty($record->unique_impression)) { $reportArr[$key]['unique_impression'] = empty($reportArr[$key]['unique_impression']) ? 1 : $reportArr[$key]['unique_impression'] + 1; } if ($record->event == 'click' && !empty($record->unique_click)) { $reportArr[$key]['unique_click'] = empty($reportArr[$key]['unique_click']) ? 1 : $reportArr[$key]['unique_click'] + 1; } } } else { $reportArr[$key]['failed_request'] = empty($reportArr[$key]['failed_request']) ? 1 : $reportArr[$key]['failed_request'] + 1; } } //đánh dấu record cuối cùng đã xử lý $lastRecord = $record; // pr($lastRecord); pr($record->created_at); // no record -> return if (!$lastRecord) { throw new Exception(); } $incrFields = array('impression', 'unique_impression', 'ads_request', 'click', 'unique_click', 'start', 'firstquartile', 'midpoint', 'thirdquartile', 'complete', 'fullscreen', 'mute', 'invalid_request', 'empty_referrer', 'referrer_not_match', 'log_preprocess', 'tracking_beacon_success', 'ads_success', 'geo_not_suitable', 'gender_not_suitable', 'age_not_suitable', 'audience_not_suitable', 'ad_zone_inventory_limited', 'ad_zone_daily_limited', 'inventory_limited', 'daily_inventory_limited', 'channel_not_suitable', 'no_ads_available', 'too_many_request', 'empty_visitor_id', 'ready_to_deliver', 'frequency_capped', 'checksum_error', 'failed_request'); $affectedRows = 0; // pr($reportArr); foreach ($reportArr as $key => $record) { $isOVR = strpos($key, 'OverReport') !== FALSE ? 1 : 0; $checkRecord = TrackingSummaryBaseModel::where(['flight_website_id' => $record['flight_website_id'], 'publisher_ad_zone_id' => $record['publisher_ad_zone_id'], 'website_id' => $record['website_id'], 'date' => $record['date'], 'hour' => $record['hour'], 'ovr' => $isOVR])->first(); if (!$checkRecord) { $trackingSummary = new TrackingSummaryBaseModel(); $update = false; } else { $trackingSummary = $checkRecord; $update = true; } foreach ($record as $k => $v) { $trackingSummary->ovr = $isOVR; if ($update) { if (in_array($k, $incrFields)) { $trackingSummary->{$k} = $trackingSummary->{$k} + $v; } } else { $trackingSummary->{$k} = $v; } } $trackingSummary->save(); $affectedRows++; } DB::table($tblTSS)->where('id', $trackingSummaryScheduleID)->update(array('finish_at' => date('Y-m-d H:i:s'), 'last_record_id' => $lastRecord->_id, 'last_record_sec' => $lastRecord->created_at->sec, 'last_record_usec' => $lastRecord->created_at->usec)); if (!empty($lastRecord)) { DB::commit(); echo "Mongo Records: " . $data->count(); echo "\r\n<br/> Last Mongo Date : " . date('H:i:s d-m-Y', $lastRecord->created_at->sec); echo "\r\nTracking Summary rows affected : " . $affectedRows; echo "\r\n"; pr($affectedRows); return $affectedRows; } } } catch (\Exception $e) { DB::rollback(); // throw $e; return false; } }