/** * Execute the console command. * * @return mixed */ public function fire() { $experiments = Experiment::active()->get(); $goals = array_unique(Goal::active()->orderBy('name')->lists('name')->toArray()); $columns = array_merge(['Experiment', 'Visitors', 'Engagement'], array_map('ucfirst', $goals)); $writer = new Writer(new SplTempFileObject()); $writer->insertOne($columns); foreach ($experiments as $experiment) { $engagement = $experiment->visitors ? $experiment->engagement / $experiment->visitors * 100 : 0; $row = [$experiment->name, $experiment->visitors, number_format($engagement, 2) . " % (" . $experiment->engagement . ")"]; $results = $experiment->goals()->lists('count', 'name'); foreach ($goals as $column) { $count = array_get($results, $column, 0); $percentage = $experiment->visitors ? $count / $experiment->visitors * 100 : 0; $row[] = number_format($percentage, 2) . " % ({$count})"; } $writer->insertOne($row); } $output = (string) $writer; if ($file = $this->argument('file')) { $this->info("Creating {$file}"); File::put($file, $output); } else { $this->line($output); } }
/** * Write the given item. * * @param mixed $item * * @throws LogicException if no valid handle exists. */ public function writeItem($item) { $this->verifyHandle(); if ($this->autoDetectHeader && !$this->header && is_array($item)) { $this->header = array_keys($item); $this->writeItem($this->header); } $this->csv->insertOne($item); }
/** * Generate a spreadsheet by transforming 3D data into 2D data. * * @param array $filters (optional) * @return League\Csv\Writer */ public function generate(array $filters = []) { // Query the repository for filtered set of objects to export $objects = $this->query($filters); // Insert headers $this->data->insertOne($this->getHeaders()); // Add objects as rows if ($objects) { // Transform a 3D object into a 2D row $objects->transform([$this, 'transform']); // Insert all the objects as rows $this->data->insertAll($objects->toArray()); } return $this->data; }
public function getIndex() { $c = Input::get('collection', 'Entity'); $collection = $this->repository->returnCollectionObjectFor($c); // Filter data for projects for which the authenticated user has permissions. if (Input::has('authkey')) { $user = \MongoDB\UserAgent::where('api_key', Input::get('authkey'))->first(); if (is_null($user)) { return ['error' => 'Invalid auth key: ' . Input::get('authkey')]; } } elseif (Auth::check()) { $user = Auth::user(); } else { return ['error' => 'Authentication required. Please supply authkey.']; } $projects = ProjectHandler::getUserProjects($user, Permissions::PROJECT_READ); $projectNames = array_column($projects, 'name'); $collection = $collection->whereIn('project', $projectNames); if (Input::has('match')) { $collection = $this->processFields($collection); } $start = (int) Input::get('start', 0); $limit = (int) Input::get('limit', 100); $only = Input::get('only', array()); if ($orderBy = Input::get('orderBy')) { foreach ($orderBy as $sortingColumnName => $sortingDirection) { $collection = $collection->orderBy($sortingColumnName, $sortingDirection); } } $collection = $collection->paginate($limit, $only); $pagination = $collection->links()->render(); $count = $collection->toArray(); unset($count['data']); $documents = $collection->toArray()['data']; if (array_key_exists('tocsv', Input::all())) { set_time_limit(1200); $writer = new Writer(new \SplTempFileObject()); $writer->setNullHandlingMode(Writer::NULL_AS_EMPTY); $headerDotted = array(); foreach ($documents as $line_index => $row) { unset($row['metrics'], $row['platformJobId'], $row['results'], $row['cache']); if (isset($row['parents'])) { $row['wasDerivedFrom'] = implode(",", $row['parents']); unset($row['parents']); } foreach (array_dot($row) as $k => $v) { array_push($headerDotted, $k); } } $headerDotted = array_unique($headerDotted); natcasesort($headerDotted); $csvHeader = array_change_key_case(str_replace('.', '_', array_values($headerDotted)), CASE_LOWER); $writer->insertOne($csvHeader); foreach ($documents as $line_index => $row) { if (isset($row['parents'])) { $row['wasDerivedFrom'] = implode(",", $row['parents']); unset($row['parents']); } $row = array_dot($row); foreach ($headerDotted as $column) { if (isset($row[$column])) { $csvRow[str_replace('.', '_', $column)] = $row[$column]; } else { $csvRow[str_replace('.', '_', $column)] = ""; } } $writer->insertOne($csvRow); } $writer->output(time() . '.csv'); die; } return Response::json(["count" => $count, "pagination" => $pagination, "searchQuery" => Input::except('page'), "documents" => $documents]); }
/** * Display a listing of the resource. * * @return Response */ public function getIndex() { //return Input::all(); $c = Input::get('collection', 'Entity'); $collection = $this->repository->returnCollectionObjectFor($c); if (Input::has('createMetrics')) { exec('/usr/bin/python2.7 ' . base_path() . '/app/lib/generateMetrics.py \'' . Input::get('createMetrics') . '\' \'entity/text/medical/questiontemplate/1\'', $output, $error); return json_decode($output[0], true); } if (Input::has('field')) { $collection = $this->processFields($collection); } if (!array_key_exists('noCache', Input::all())) { $collection = $collection->remember(1, md5(serialize(array_values(Input::except('pretty'))))); } $start = (int) Input::get('start', 0); $limit = (int) Input::get('limit', 100); $only = Input::get('only', array()); if (Input::has('datatables')) { $start = (int) Input::get('iDisplayStart', 0); $limit = (int) Input::get('iDisplayLength', 100); $sortingColumnIndex = (int) Input::get('iSortCol_0', 0); $sortingColumnName = Input::get('mDataProp_' . $sortingColumnIndex, '_id'); $sortingDirection = Input::get('sSortDir_0', 'asc'); $sortingColumnName = $sortingColumnName == "_id" ? "natural" : $sortingColumnName; $iTotalDisplayRecords = new Entity(); $iTotalDisplayRecords = $this->processFields($iTotalDisplayRecords); $iTotalDisplayRecords = $iTotalDisplayRecords->count(); $collection = $collection->skip($start)->orderBy($sortingColumnName, $sortingDirection)->take($limit)->get($only); if ($input = Input::get('field')) { $iTotalRecords = new Entity(); if (isset($input['format'])) { $iTotalRecords = $iTotalRecords->whereIn('format', array_flatten([$input['format']])); } if (isset($input['domain'])) { $iTotalRecords = $iTotalRecords->whereIn('domain', array_flatten([$input['domain']])); } if (isset($input['documentType'])) { $iTotalRecords = $iTotalRecords->whereIn('documentType', array_flatten([$input['documentType']])); } $iTotalRecords = $iTotalRecords->count(); } return Response::json(["sEcho" => Input::get('sEcho', 10), "iTotalRecords" => $iTotalRecords, "iTotalDisplayRecords" => $iTotalDisplayRecords, "aaData" => $collection->toArray()]); } $collection = $collection->skip($start)->take($limit)->get($only); if (array_key_exists('getQueryLog', Input::all())) { return Response::json(\DB::getQueryLog()); } if (array_key_exists('pretty', Input::all())) { echo "<pre>"; return json_encode($collection->toArray(), JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES); } if (array_key_exists('tocsv', Input::all())) { $documents = $collection->toArray(); $writer = new Writer(new \SplTempFileObject()); $writer->setNullHandlingMode(Writer::NULL_AS_EMPTY); foreach ($documents as $documentKey => $documentValue) { if (!isset($documentValue['content']['sentence']['formatted'])) { $documentValue['content']['sentence']['formatted'] = " "; } $this->recur_ksort($documentValue['content']); $row['_id'] = $documentValue['_id']; if (isset($documentValue['parents'])) { $row['wasDerivedFrom'] = implode(",", $documentValue['parents']); } $row['content'] = $documentValue['content']; $row = $documentValue; if ($documentKey == 0) { $writer->insertOne(array_change_key_case(str_replace('.', '_', array_keys(array_dot($row))), CASE_LOWER)); } $row = array_dot($row); $csvRow = array(); foreach ($row as $columnKey => $columnValue) { $csvRow[str_replace('.', '_', $columnKey)] = $columnValue; } $writer->insertOne($csvRow); } $writer->output('test.csv'); die; // return array_dot($csv); } // if(array_key_exists('tocsv', Input::all())) // { // $documents = $collection->toArray(); // $writer = new Writer(new \SplTempFileObject); // foreach($documents as $documentKey => $documentValue) // { // if(!isset($documentValue['content']['sentence']['formatted'])) // { // $documentValue['content']['sentence']['formatted'] = " "; // } // $this->recur_ksort($documentValue['content']); // $row['_id'] = $documentValue['_id']; // if(isset($documentValue['parents'])) // { // $row['wasDerivedFrom'] = implode(",", $documentValue['parents']); // } // $row['content'] = $documentValue['content']; // if($documentKey == 0) // { // $writer->insertOne(array_change_key_case(str_replace('.', '_', array_keys(array_dot($row))), CASE_LOWER)); // } // $writer->insertOne(array_flatten($row)); // } // $writer->output('test.csv'); // die; // // return array_dot($csv); // } return Response::json($collection); }
/** * {@inheritdoc} */ public function writeItem(array $item) { return $this->writer->insertOne($item); }
/** * renders content of the csv file */ protected function renderContent() { foreach ($this->dates as $dates) { $this->csvWriter->insertOne([$dates['month']->format($this->currentMonthFormat), $dates['salaryDate']->format($this->salaryDateFormat), $dates['bonusDate']->format($this->bonusDateFormat)]); } }