Example #1
0
 public static function boot()
 {
     parent::boot();
     static::saving(function ($job) {
         \Log::debug('Clearing jobCache and mainSearchFilters.');
         \MongoDB\Temp::whereIn('_id', ['mainSearchFilters', 'jobCache', $job->_id])->forceDelete();
     });
     static::creating(function ($job) {
         try {
             if (!SoftwareAgent::find('jobcreator')) {
                 $softwareAgent = new SoftwareAgent();
                 $softwareAgent->_id = 'jobcreator';
                 $softwareAgent->label = "Job creation";
             }
             if (!isset($job->projectedCost) and !isset($job->iamemptyjob)) {
                 $reward = $job->jobConfiguration->content['reward'];
                 $workerunitsPerUnit = intval($job->jobConfiguration->content['workerunitsPerUnit']);
                 $unitsPerTask = intval($job->jobConfiguration->content['unitsPerTask']);
                 $unitsCount = count($job->batch->wasDerivedFrom);
                 if (!$unitsPerTask) {
                     $projectedCost = 0;
                 } else {
                     $projectedCost = round($reward / $unitsPerTask * ($unitsCount * $workerunitsPerUnit), 2);
                 }
                 $job->expectedWorkerunitsCount = $unitsCount * $job->jobConfiguration->content['workerunitsPerUnit'];
                 $job->projectedCost = $projectedCost;
             }
             $job->unitsCount = count($job->batch->wasDerivedFrom);
             $job->latestMetrics = 0;
             $job->workerunitsCount = 0;
             $job->completion = 0.0;
             // 0.00-1.00
             if (!isset($job->activity_id)) {
                 $activity = new Activity();
                 $activity->label = "Job is uploaded to crowdsourcing platform.";
                 $activity->softwareAgent_id = 'jobcreator';
                 // TODO: JOB softwareAgent_id = $platform. Does this need to be the same?
                 $activity->save();
                 $job->activity_id = $activity->_id;
             }
         } catch (Exception $e) {
             // Something went wrong with creating the Entity
             $job->forceDelete();
             throw $e;
         }
         Log::debug("Saved entity {$job->_id} with activity {$job->activity_id}.");
     });
 }
Example #2
0
 protected static function boot()
 {
     parent::boot();
     static::creating(function ($entity) {
         if (!Schema::hasCollection('entities')) {
             static::createSchema();
         }
         if (!empty($entity->hash)) {
             if (Entity::withTrashed()->where('hash', $entity->hash)->first()) {
                 throw new Exception("Hash already exists for: " . $entity->title);
             }
         }
         if (empty($entity->_id)) {
             $entity->_id = static::generateIncrementedBaseURI($entity);
         }
         if (Auth::check()) {
             $entity->user_id = Auth::user()->_id;
         } else {
             $entity->user_id = "crowdwatson";
         }
     });
     static::saving(function ($entity) {
         if (Auth::check()) {
             $entity->user_id = Auth::user()->_id;
         } else {
             $entity->user_id = "crowdwatson";
         }
         static::validateEntity($entity);
     });
     static::saved(function ($entity) {
         \MongoDB\Temp::truncate();
         Cache::flush();
     });
     static::deleted(function ($entity) {
         Cache::flush();
     });
 }
Example #3
0
 public function getDeletepl($id)
 {
     $platform = App::make('cf2');
     //dd($id);
     $platform->deleteJobPL($id);
     \MongoDB\Temp::truncate();
     return Redirect::to("jobs");
 }
Example #4
0
 public function getUnit()
 {
     $result = array();
     $aggregateOperators = $this->processAggregateInput(Input::all());
     $unitID = Input::get('unit');
     $resultT = \MongoDB\Temp::where('_id', $unitID)->get()->toArray();
     if (sizeof($resultT) != 0) {
         $result['infoStat'] = \MongoDB\Temp::where('_id', $unitID)->get()->toArray()[0];
     } else {
         $result['infoStat'] = \MongoDB\Entity::where('_id', $unitID)->get()->toArray()[0];
     }
     $selection = \MongoDB\Entity::raw(function ($collection) use($aggregateOperators, $unitID) {
         $aggregateOperators['$match']['unit_id'] = $unitID;
         $aggregateOperators['$match']['documentType'] = 'workerunit';
         $aggregateOperators['$project']['job_id'] = array('$ifNull' => array('$' . 'job_id', 0));
         $aggregateOperators['$project']['crowdAgent_id'] = array('$ifNull' => array('$' . 'crowdAgent_id', 0));
         $aggregateOperators['$project']['type'] = array('$ifNull' => array('$' . 'type', 0));
         $aggregateOperators['$project']['workerunit'] = array('$ifNull' => array('$' . 'annotationVector', 0));
         $aggregateOperators['$group']['_id'] = '$crowdAgent_id';
         $aggregateOperators['$group']['count'] = array('$sum' => 1);
         $aggregateOperators['$group']['job_id'] = array('$push' => '$job_id');
         $aggregateOperators['$group']['type'] = array('$push' => '$type');
         $aggregateOperators['$group']['workerunit'] = array('$push' => '$workerunit');
         return $collection->aggregate(array(array('$match' => $aggregateOperators['$match']), array('$project' => $aggregateOperators['$project']), array('$group' => $aggregateOperators['$group'])));
     });
     $response = $selection['result'];
     $crowdAgentIDs = array();
     $jobIDs = array();
     $result['workerunitContent'] = array();
     $result['jobContent'] = array();
     $result['agentContent'] = array();
     foreach ($response as $agent => $value) {
         $result['workerunitContent'][$value['_id']] = $value;
         array_push($crowdAgentIDs, $value['_id']);
         $workerunitType = array();
         foreach ($value['job_id'] as $index => $type) {
             array_push($jobIDs, $value['job_id'][$index]);
             if (!array_key_exists($type, $workerunitType)) {
                 $workerunitType[$type] = $value['workerunit'][$index];
             } else {
                 $annInfo = $value['workerunit'][$index];
                 foreach ($annInfo as $k => $v) {
                     if (is_numeric($v)) {
                         $workerunitType[$type][$k] += $v;
                     } else {
                         foreach ($v as $embeddedK => $embeddedV) {
                             $workerunitType[$type][$k][$embeddedK] += $embeddedV;
                         }
                     }
                 }
             }
         }
         $result['workerunitContent'][$value['_id']]['workerunitType'] = array();
         foreach ($workerunitType as $job => $workerunit) {
             $workerunitInfo = array('job_id' => $job, 'workerunit' => $workerunit);
             $result['workerunitContent'][$value['_id']]['workerunitType'][$job] = $workerunitInfo;
         }
     }
     $crowdAgentIDs = array_unique($crowdAgentIDs);
     $agents = \MongoDB\CrowdAgent::whereIn('_id', $crowdAgentIDs)->get(array('cache', 'cfWorkerTrust', 'softwareAgent_id'))->toArray();
     foreach ($agents as $index => $value) {
         $result['workerunitContent'][$value['_id']]["valuesWorker"] = $value;
         //        $result['agentContent'][$value['_id']] = $value;
     }
     $jobIDs = array_unique($jobIDs);
     $jobs = \MongoDB\Entity::whereIn('_id', $jobIDs)->get(array('results.withoutSpam.' . $unitID, 'results.withSpam.' . $unitID, 'metrics.units.withoutSpam.' . $unitID, 'metrics.aggUnits', 'metrics.filteredunits', 'metrics.workers.withFilter', 'sofwareAgent_id', 'platformJobId'))->toArray();
     foreach ($jobs as $index => $value) {
         $result['jobContent'][$value['_id']] = $value;
         $jobConfID = \MongoDB\Entity::where('_id', '=', $value['_id'])->lists('jobConf_id');
         $jobTitle = \MongoDB\Entity::whereIn('_id', $jobConfID)->get(array('content.title'))->toArray();
         $result['jobContent'][$value['_id']]['jobConf'] = $jobTitle[0];
     }
     foreach ($result['workerunitContent'] as $id => $annInfo) {
         foreach ($result['workerunitContent'][$id]['workerunitType'] as $index => $value) {
             $job_id = $value['job_id'];
             $result['workerunitContent'][$id]['workerunitType'][$index]['job_info'] = $result['jobContent'][$job_id];
         }
     }
     return $result;
 }
 /**
  * Store to the database the given entities as entities decendent from the given
  * document.
  * 
  * @param $document Parent document -- Must be a document entity on the database.
  * @param $entities List of entities to be created as decendents from the given document. 
  * 
  * @return multitype:string A status array containing the result status information.
  */
 public function store($document, $entities)
 {
     $nEnts = count($entities);
     if ($nEnts <= 0 && $nEnts >= 10000) {
         // We will have problems processing empty files or more than 10,000 entities
         return ['error' => 'Unable to process files with more than 10,000 lines: ' . $nEnts . 'found'];
     }
     $activity = new Activity();
     $activity->softwareAgent_id = $this->softwareComponent->_id;
     $activity->save();
     $format = $document['format'];
     $domain = $document['domain'];
     $docType = $document['documentType'] . '-sentence';
     $title = $document['title'];
     $parentId = $document['_id'];
     $project = $document['project'];
     $activityId = $activity->_id;
     if (Auth::check()) {
         $userId = Auth::user()->_id;
     } else {
         $userId = "crowdwatson";
     }
     $idBase = 'entity/' . $format . '/' . $domain . '/' . $docType . '/';
     $inc = $this->getLastDocumentInc($format, $domain, $docType);
     $fullEntities = [];
     foreach ($entities as $entitiy) {
         $fullEntity = ["_id" => $idBase . $inc, "documentType" => 'unit', "activity_id" => $activityId, "softwareAgent_id" => $this->softwareComponent->_id, "project" => $project, "user_id" => $userId, "type" => $docType, "unitParents" => [$parentId], "jobParents" => [], "children" => [], "judgements" => [], "metrics" => [], "source" => '', "format" => $format, "title" => strtolower($title), "domain" => $domain, "tags" => ['unit'], "content" => $entitiy, "hash" => md5(serialize($entitiy)), "updated_at" => new MongoDate(time()), "created_at" => new MongoDate(time())];
         $inc++;
         array_push($fullEntities, $fullEntity);
     }
     \DB::collection('entities')->insert($fullEntities);
     \MongoDB\Temp::truncate();
     return ['success' => 'Sentences created successfully'];
 }
Example #6
0
 public function getIndex()
 {
     $mainSearchFilters = \MongoDB\Temp::getMainSearchFiltersCache()['filters'];
     return View::make('media.search.pages.jobs', compact('mainSearchFilters'));
 }
Example #7
0
 public function store(&$parentEntity, $relexStructuredSentences, $inc)
 {
     // dd('test');
     $allEntities = array();
     foreach ($relexStructuredSentences as $tKey => &$relexStructuredSentence) {
         $title = $parentEntity['title'] . "_index_" . $inc;
         $hash = md5(serialize(array_except($relexStructuredSentence, ['properties'])));
         if ($dup = Entity::where('hash', $hash)->first()) {
             array_push($this->status['store']['error']['skipped_duplicates'], $tKey . " ---> " . $dup->_id);
             continue;
         }
         if (Auth::check()) {
             $user_id = Auth::user()->_id;
         } else {
             $user_id = "crowdwatson";
         }
         $entity = ["_id" => 'entity/text/medical/relex-structured-sentence/' . $inc, "title" => strtolower($title), "domain" => $parentEntity['domain'], "format" => $parentEntity['format'], "tags" => ['unit'], "documentType" => "relex-structured-sentence", "parents" => [$parentEntity['_id']], "content" => $relexStructuredSentence, "hash" => $hash, "activity_id" => $this->activity->_id, "user_id" => $user_id, "updated_at" => new MongoDate(time()), "created_at" => new MongoDate(time())];
         array_push($allEntities, $entity);
         $inc++;
         array_push($this->status['store']['success'], $tKey . " ---> URI: {$entity['_id']}");
     }
     if (count($allEntities) > 1) {
         \DB::collection('entities')->insert($allEntities);
         \MongoDB\Temp::truncate();
     }
     return $inc;
 }
Example #8
0
 public function getSearch()
 {
     $mainSearchFilters = \MongoDB\Temp::getMainSearchFiltersCache()['filters'];
     // get projects of a user
     $user = Auth::user();
     foreach ($mainSearchFilters['media']['categories'] as $key => $value) {
         // $key is the name of a project
         if (!PermissionHandler::checkProject($user, $key, Permissions::PROJECT_READ)) {
             unset($mainSearchFilters['media']['categories'][$key]);
         }
     }
     return View::make('media.search.pages.media')->with('mainSearchFilters', $mainSearchFilters);
 }
Example #9
0
 public static function createJobCache()
 {
     \Session::flash('rawArray', 1);
     $db = \DB::getMongoDB();
     $db = $db->temp;
     $result = \MongoDB\Entity::where('documentType', 'job')->with('hasConfiguration')->get()->toArray();
     if (count($result) > 0) {
         try {
             \MongoDB\Temp::where('documentType', 'job')->forceDelete();
             $db->batchInsert($result, array('continueOnError' => true));
         } catch (Exception $e) {
             // ContinueOnError will still throw an exception on duplication, even though it continues, so we just move on.
         }
     }
     \Session::forget('rawArray');
 }