public function getLocation(Request $request) { if (!$request->has('q')) { $list = []; } else { $limit = 10; if ($request->has('limit')) { $limit = intval($request->input('limit')); } $offset = 0; if (!$request->has('offset')) { $offset = intval($request->input('offset')); } $list = \App\DmaMap::getDmaList(['str' => $request->input('q'), 'limit' => $limit, 'offset' => $offset]); } return response()->json($list)->header('Access-Control-Allow-Origin', '*')->header('Access-Control-Allow-Methods', 'GET, POST')->setCallback($request->input('callback')); }
private static function prepareJobRecord($data) { $currentRecord = []; // VALIDATE DATA if (empty($data->external_job_id)) { ExportRecordFailure::create(['job_info' => json_encode($data), 'reason' => '[ external_job_id is missing ]', 'action' => '[ exiting process ]', 'calling_function' => __FUNCTION__]); return null; } if (empty($data->job_title)) { ExportRecordFailure::create(['external_job_id' => $data->external_job_id, 'job_info' => json_encode($data), 'reason' => '[ title is missing ]', 'action' => '[ exiting process ]', 'calling_function' => __FUNCTION__]); return null; } $end_date = null; if (preg_match("/^([0-9]{4})-([0-9]{2})-([0-9]{2})T.*\$/", $data->job_end_date, $end_date_matches)) { $end_date_mm = $end_date_matches[2]; $end_date_dd = $end_date_matches[3]; $end_date_yyyy = $end_date_matches[1]; $end_date = $end_date_yyyy . '-' . $end_date_mm . "-" . $end_date_dd; } if (empty($end_date)) { ExportRecordFailure::create(['external_job_id' => $data->external_job_id, 'job_info' => json_encode($data), 'reason' => '[ end date is missing ]', 'action' => '[ exiting process ]', 'calling_function' => __FUNCTION__]); return null; } else { $jobObj = Job::firstOrNew(['external_job_id' => $data->external_job_id, 'source_id' => 1]); if (strtotime($end_date) < strtotime(date('Y-m-d'))) { if (!empty($jobObj->id)) { $record_id = $jobObj->id; $jobObj->delete(); ExportRecordFailure::create(['external_job_id' => $data->external_job_id, 'job_info' => json_encode($data), 'reason' => '[ expired job ]', 'action' => '[ record deleted ][ record id: ' . $record_id . ' ] [ exiting process ]', 'calling_function' => __FUNCTION__]); return $record_id; } else { ExportRecordFailure::create(['external_job_id' => $data->external_job_id, 'job_info' => json_encode($data), 'reason' => '[ expired job ] ', 'action' => '[ will not create a record ] [ exiting process ]', 'calling_function' => __FUNCTION__]); $jobObj = null; return 1; } } } if (!empty($data->job_state) && $data->job_state == 'expired') { if (!empty($jobObj->id)) { $record_id = $jobObj->id; $jobObj->delete(); ExportRecordFailure::create(['external_job_id' => $data->external_job_id, 'job_info' => json_encode($data), 'reason' => '[ expired job ]', 'action' => '[ record deleted ][ record id: ' . $record_id . ' ] [ exiting process ]', 'calling_function' => __FUNCTION__]); return $record_id; } else { ExportRecordFailure::create(['external_job_id' => $data->external_job_id, 'job_info' => json_encode($data), 'reason' => '[ expired job ] ', 'action' => '[ will not create a record ] [ exiting process ]', 'calling_function' => __FUNCTION__]); $jobObj = null; return 1; } } // END OF DATA VALIDATION if (!empty($jobObj->id)) { $currentRecord = Job::getInfo($jobObj->id, ['JobState', 'JobCity', 'JobCompany', 'JobCategory', 'JobIndustry']); } $jobObj->source_id = 1; $jobObj->external_job_id = $data->external_job_id; if (!empty($data->job_name)) { $jobObj->name = self::cleanUpStr($data->job_name); } if (!empty($data->job_title)) { $jobObj->title = self::cleanUpStr($data->job_title); } if (!empty($data->org_name)) { $company_name = trim(str_replace("'", "'", $data->org_name)); } else { $company_name = 'Confidential'; } if (empty($currentRecord) || $currentRecord['JobCompany'] != $company_name) { $company_obj = Company::saveInstance(['name' => $company_name]); $jobObj->company_id = $company_obj->id; } $jobObj->apply_url = 'http://jobview.monster.com/getjob.aspx?jobid=' . $data->external_job_id; if ($company_name == 'Comcast Cable Communications Management, LLC') { if (!empty($data->apply_url)) { $jobObj->apply_url = $data->apply_url; } } if (preg_match("/^([0-9]{4})-([0-9]{2})-([0-9]{2})T.*\$/", $data->job_begin_date, $begin_date_matches)) { $begin_date_mm = $begin_date_matches[2]; $begin_date_dd = $begin_date_matches[3]; $begin_date_yyyy = $begin_date_matches[1]; $jobObj->begin_date = $begin_date_yyyy . '-' . $begin_date_mm . '-' . $begin_date_dd; } $jobObj->end_date = $end_date; if (preg_match("/^([0-9]{4})-([0-9]{2})-([0-9]{2})T.*\$/", $data->job_post_date, $post_date_matches)) { $post_date_mm = $post_date_matches[2]; $post_date_dd = $post_date_matches[3]; $post_date_yyyy = $post_date_matches[1]; $jobObj->post_date = $post_date_yyyy . '-' . $post_date_mm . '-' . $post_date_dd; } if (!empty($data->job_category)) { if (empty($currentRecord) || $currentRecord['JobCategory'] != $data->job_category) { $cat_obj = Category::firstOrCreate(['name' => $data->job_category]); $jobObj->category_id = $cat_obj->id; } } if (!empty($data->job_industries[0])) { if (empty($currentRecord) || $currentRecord['JobIndustry'] != $data->job_industries[0]) { $ind_obj = Industry::firstOrCreate(['name' => $data->job_industries[0]]); $jobObj->industry_id = $ind_obj->id; } } if (isset($data->location) && !empty($data->location)) { $location = $data->location; if (!empty(trim($location->city))) { $jobObj->city = trim($location->city); } if (!empty(trim($location->state))) { $jobObj->state = trim($location->state); } if (!empty(trim($location->country))) { $jobObj->country = trim($location->country); } if (isset($location->zip) && !empty(trim($location->zip))) { if (trim($location->zip) == '00000') { $jobObj->zip = null; } else { $jobObj->zip = trim($location->zip); } } if (!empty($jobObj->city) && !empty($jobObj->state)) { if (empty($currentRecord) || $currentRecord['JobCity'] != $jobObj->city || $currentRecord['JobState'] != $jobObj->state) { $dma_obj = DmaMap::where(['city' => $jobObj->city, 'state_abbr' => $jobObj->state])->first(); if (!empty($dma_obj)) { $jobObj->dma_code = $dma_obj->code; } } } } return $jobObj; }
public static function getAll(array $args = []) { $max = '+inf'; $min = '-inf'; $limit = 20; if (!empty($args['limit'])) { $limit = intval($args['limit']); } $offset = 0; if (!empty($args['offset'])) { $offset = intval($args['offset']); } $flag = ''; if (!empty($args['flag'])) { $flag = $args['flag']; } $dma_code = null; $dma_cache_key = ''; // request DMA jobs cache if user requested jobs by location if (!empty($args['code']) && ($args['code'] >= 500 && $args['code'] <= 881)) { $dma_code = $args['code']; $dma_cache_key = self::getJobsByDmaCahceKey($dma_code); self::setJobsByDmaCache($dma_code); } else { if (!empty($args['location'])) { // lookup dma code $dma_code = DmaMap::getDmaCode($args['location']); if (!empty($dma_code)) { $args['code'] = $dma_code; // will be return as a part of a payload self::setJobsByDmaCache($dma_code); $dma_cache_key = self::getJobsByDmaCahceKey($dma_code); } } } $kw_list = []; $kw_cache_list = []; $cache_key_union_out = ''; $result_type = ''; $cache_key_intersect_out = 'intersect'; $cache_key_union_out = 'union'; $union_out = ''; $key_out = ''; $intersect_out = ''; $intersect_in = []; // request KEYWORD jobs caches if user requested jobs by keyword if (!empty($args['keyword'])) { $req_k = explode(' ', strtolower($args['keyword'])); foreach ($req_k as $k) { $kw = trim($k); if (!empty($kw) && !in_array($kw, $kw_list)) { $cache_key_union_out .= ':' . $kw; $cache_key_intersect_out .= ':' . $kw; $kw_list[] = $kw; } } if (!empty($kw_list)) { foreach ($kw_list as $ck) { // request jobs by keyword $kw_cache_key = Keyword::getKeywordJobsCacheKey($ck); Keyword::setKeywordJobsCache($ck); $kw_cache_list[] = $kw_cache_key; // request jobs by company keyword $kw_company_cache_key = Keyword::getKeywordCompaniesCacheKey($ck); Keyword::setKeywordCompaniesCache($ck); $all_companies_str_ids = Redis::smembers($kw_company_cache_key); if (!empty($all_companies_str_ids)) { foreach ($all_companies_str_ids as $c_str_id) { $company_jobs_cache = Company::getCompanyJobsCacheKey($c_str_id); Company::setCompanyJobsCache($c_str_id); $kw_cache_list[] = $company_jobs_cache; } } } } } if (!empty($dma_code)) { // DMA - Yes if (!empty($kw_cache_list)) { // Keyword - Yes if (count($kw_cache_list) == 1) { // only one keyword - intersect vars $intersect_out = $cache_key_intersect_out . ':' . $dma_code; $intersect_in = [$kw_cache_list[0], $dma_cache_key]; $key_out = $intersect_out; } else { // multiple keywords foreach ($kw_cache_list as $kin) { $keys_in[] = $kin; } // union first $union_out = $cache_key_union_out; Redis::zunionstore($union_out, $keys_in, ['aggregate' => 'sum']); Redis::expire($union_out, 60 * 5); // intersect vars $intersect_out = $cache_key_intersect_out . ':' . $dma_code; $intersect_in = [$union_out, $dma_cache_key]; $key_out = $intersect_out; } Redis::zinterstore($intersect_out, $intersect_in, ['aggregate' => 'sum']); Redis::expire($intersect_out, 60 * 5); $interstore_total = Redis::zcard($key_out); if ($interstore_total > 0) { // we got result $key_out = $intersect_out; $result_type = 'kw&dma'; } else { $dma_total = Redis::zcard($dma_cache_key); if ($dma_total > 0) { $key_out = $dma_cache_key; $result_type = 'dma'; } else { $key_out = self::getRecentJobsCacheKey(); self::setRecentJobsCache(); $result_type = 'recent'; } } } else { // Keyword - No $dma_total = Redis::zcard($dma_cache_key); if ($dma_total > 0) { $key_out = $dma_cache_key; $result_type = 'dma'; } else { $key_out = self::getRecentJobsCacheKey(); self::setRecentJobsCache(); $result_type = 'recent'; } } } else { // DMA No if (!empty($kw_cache_list)) { // Keyword - Yes if (count($kw_cache_list) == 1) { // only one keyword return keyword cache $key_out = $kw_cache_list[0]; } else { // multiple keywords foreach ($kw_cache_list as $kin) { $keys_in[] = $kin; } // union first $union_out = $cache_key_union_out; Redis::zunionstore($union_out, $keys_in, ['aggregate' => 'sum']); Redis::expire($union_out, 60 * 5); $key_out = $union_out; } $union_total = Redis::zcard($key_out); if ($union_total > 0) { // we got result $result_type = 'kw'; } else { $key_out = self::getRecentJobsCacheKey(); self::setRecentJobsCache(); $result_type = 'recent'; } } else { // Keyword - No $key_out = self::getRecentJobsCacheKey(); self::setRecentJobsCache(); $result_type = 'recent'; } } $final_list = []; $total = Redis::zcard($key_out); if ($flag == 'random') { if ($total > $limit) { $offset = rand(0, $total - $limit); } } $keys = Redis::zrevrangebyscore($key_out, $max, $min, ['limit' => [$offset, $limit]]); if (!empty($keys)) { foreach ($keys as $job_id) { $job_info = self::getInfo($job_id, self::$defaultFields); if (!empty($job_info['JobID'])) { $final_list[] = $job_info; } } } return ['total' => $total, 'jobs' => $final_list, 'result_type' => $result_type, 'args' => $args]; }