コード例 #1
0
ファイル: apiController.php プロジェクト: harixxy/CrowdTruth
 public function getWorker()
 {
     $result = array();
     $aggregateOperators = $this->processAggregateInput(Input::all());
     $crowdAgentID = Input::get('agent');
     $result['infoStat'] = \MongoDB\CrowdAgent::where('_id', $crowdAgentID)->get()->toArray()[0];
     $result['infoStat']['avgAgreementAcrossJobs'] = \MongoDB\CrowdAgent::avg('avg_agreement');
     $result['infoStat']['avgCosineAcrossJobs'] = \MongoDB\CrowdAgent::avg('avg_cosine');
     $selection = \MongoDB\Entity::raw(function ($collection) use($aggregateOperators, $crowdAgentID) {
         $aggregateOperators['$match']['crowdAgent_id'] = $crowdAgentID;
         $aggregateOperators['$match']['documentType'] = 'workerunit';
         $aggregateOperators['$project']['job_id'] = array('$ifNull' => array('$' . 'job_id', 0));
         $aggregateOperators['$project']['unit_id'] = array('$ifNull' => array('$' . 'unit_id', 0));
         $aggregateOperators['$project']['type'] = array('$ifNull' => array('$' . 'type', 0));
         $aggregateOperators['$project']['workerunit'] = array('$ifNull' => array('$' . 'annotationVector', 0));
         $aggregateOperators['$group']['_id'] = '$unit_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'];
     $unitIDs = array();
     $jobIDs = array();
     foreach ($response as $unit => $value) {
         $result['workerunitContent'][$value['_id']] = $value;
         array_push($unitIDs, $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'] = $workerunitType;
         $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;
         }
     }
     $unitIDs = array_unique($unitIDs);
     $units = \MongoDB\Entity::whereIn('_id', $unitIDs)->get(array('content.sentence.formatted', 'content.sentence.formatted', 'documentType', 'domain', 'content.relation.noPrefix', 'content.terms.first.formatted', 'content.terms.second.formatted'))->toArray();
     foreach ($units as $index => $value) {
         $result['workerunitContent'][$value['_id']]['unitContent'] = $value;
     }
     $jobIDs = array_unique($jobIDs);
     $jobs = \MongoDB\Entity::whereIn('_id', $jobIDs)->get(array('metrics.workers.withFilter.' . $crowdAgentID, 'metrics.aggWorkers', 'type', 'jobConf_id', 'template', 'platformJobId', 'metrics.units', 'results'))->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;
 }