Exemplo n.º 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}.");
     });
 }
Exemplo n.º 2
0
 public static function createImageCache()
 {
     \Session::flash('rawArray', 1);
     $db = \DB::getMongoDB();
     $db = $db->temp;
     // \Queue::push('Queues\UpdateUnits', \MongoDB\Entity::whereIn('documentType', ['painting', 'drawing', 'picture'])->lists('_id'));
     $result = \MongoDB\Entity::whereIn('documentType', ['painting', 'drawing', 'picture'])->get()->toArray();
     if (count($result) > 0) {
         foreach ($result as &$parent) {
             $children = \MongoDB\Entity::whereIn('parents', [$parent['_id']])->get(['recognizedFeatures', 'content.features'])->toArray();
             $parent['content']['features'] = [];
             $parent['totalRelevantFeatures'] = 0;
             foreach ($children as $child) {
                 if (isset($child['recognizedFeatures'])) {
                     $parent['totalRelevantFeatures'] = $parent['totalRelevantFeatures'] + count($child['recognizedFeatures']);
                 }
                 if (isset($child['content']['features'])) {
                     if (is_array($child['content']['features'])) {
                         foreach ($child['content']['features'] as $k => $v) {
                             if (array_key_exists($k, $parent['content']['features'])) {
                                 if (!is_array($parent['content']['features'][$k])) {
                                     $parent['content']['features'][$k] = [$parent['content']['features'][$k]];
                                 }
                                 if (is_array($v)) {
                                     foreach ($v as $vit) {
                                         array_push($parent['content']['features'][$k], $vit);
                                     }
                                 }
                             } else {
                                 $parent['content']['features'][$k] = $v;
                             }
                         }
                     }
                 }
                 // $featureKey = key($child['content']['features']);
                 // if(!isset($parent['content']['features'][$featureKey]))
                 // {
                 //     $parent['content']['features'][$featureKey] = [];
                 //     if(is_array($child['content']['features'][$featureKey]))
                 //     {
                 //         foreach($child['content']['features'][$featureKey] as $k => $v)
                 //         {
                 //             $parent['content']['features'][$featureKey][$k] = $v;
                 //         }
                 //     }
                 // }
                 // else {
                 //     if(is_array($child['content']['features'][$featureKey]))
                 //     {
                 //         foreach($child['content']['features'][$featureKey] as $k => $v)
                 //         {
                 //             $parent['content']['features'][$featureKey][$k] = $v;
                 //         }
                 //     }
                 //     else
                 //     {
                 //         $parent['content']['features'][$featureKey] = $child['content']['features'][$featureKey];
                 //     }
                 // }
             }
         }
         try {
             \MongoDB\Temp::whereIn('documentType', ['painting', 'drawing', 'picture'])->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');
 }