Skip to content

Commit

Permalink
Merge 4812223 into 2913f0c
Browse files Browse the repository at this point in the history
  • Loading branch information
rjmackay committed Nov 27, 2018
2 parents 2913f0c + 4812223 commit 67fa76e
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 55 deletions.
3 changes: 2 additions & 1 deletion app/Jobs/CombineExportedPostBatchesJob.php
Expand Up @@ -6,6 +6,7 @@
use Log;
use Ushahidi\Core\Entity\ExportJob;
use Ushahidi\Core\Entity\ExportJobRepository;
use Ushahidi\Core\Entity\ExportBatch;
use Ushahidi\Core\Entity\ExportBatchRepository;
use Ushahidi\App\Multisite\MultisiteAwareJob;
use Illuminate\Http\File;
Expand Down Expand Up @@ -65,7 +66,7 @@ public function handle(
}

// Load batches
$batches = $exportBatchRepo->getByJobId($this->jobId);
$batches = $exportBatchRepo->getByJobId($this->jobId, ExportBatch::STATUS_COMPLETED);
// Get just filenames
$fileNames = $batches
->sortBy('batch_number')
Expand Down
8 changes: 6 additions & 2 deletions src/App/Repository/ExportBatchRepository.php
Expand Up @@ -39,12 +39,16 @@ protected function getTable()
/**
* Get all batches for job id
* @param int $jobId
* @param string $status
* @return \Illuminate\Support\Collection
*/
public function getByJobId($jobId)
public function getByJobId($jobId, $status = ExportBatch::STATUS_COMPLETED)
{
$results = $this
->selectQuery(['export_job_id' => $jobId])
->selectQuery([
'export_job_id' => $jobId,
'status' => $status
])
->get();

return $this->getCollection($results);
Expand Down
3 changes: 2 additions & 1 deletion src/Core/Entity/ExportBatchRepository.php
Expand Up @@ -21,7 +21,8 @@ interface ExportBatchRepository extends
/**
* Get all batches for job id
* @param int $jobId
* @param string $status
* @return \Illuminate\Support\Collection
*/
public function getByJobId($jobId);
public function getByJobId($jobId, $status = ExportBatch::STATUS_COMPLETED);
}
113 changes: 63 additions & 50 deletions src/Core/Usecase/Post/Export.php
Expand Up @@ -110,57 +110,70 @@ public function interact()
]);
$batchId = $this->repo->create($batchEntity);

// verify the user can export posts
$this->verifyAuth($job, 'export');
// merge filters from the controller/cli call with the job's saved filters
$data = $this->constructSearchData($job);
$this->postExportRepository->setSearchParams($data);

// get the form attributes for the export
$attributes = $this->formAttributeRepository->getExportAttributes($data->include_attributes);
$keyAttributes = $this->getAttributesWithKeys($attributes);

/**
* get the search results based on filters
* and retrieve the metadata for each of the posts
**/
$posts = $this->postExportRepository->getSearchResults();
foreach ($posts as $idx => $post) {
// Retrieved Attribute Labels for Entity's values
$post = $this->postExportRepository->retrieveMetaData($post->asArray(), $keyAttributes);
$posts[$idx] = $post;
try {
// verify the user can export posts
$this->verifyAuth($job, 'export');
// merge filters from the controller/cli call with the job's saved filters
$data = $this->constructSearchData($job);
$this->postExportRepository->setSearchParams($data);

// get the form attributes for the export
$attributes = $this->formAttributeRepository->getExportAttributes($data->include_attributes);
$keyAttributes = $this->getAttributesWithKeys($attributes);

/**
* get the search results based on filters
* and retrieve the metadata for each of the posts
**/
$posts = $this->postExportRepository->getSearchResults();
foreach ($posts as $idx => $post) {
// Retrieved Attribute Labels for Entity's values
$post = $this->postExportRepository->retrieveMetaData($post->asArray(), $keyAttributes);
$posts[$idx] = $post;
}
Log::debug('EXPORTER: on interact Count posts: ' . count($posts));

/**
* update the header attributes
* in the job table so we know which headers to
* use in other chunks of the export
*/
$this->saveHeaderRow($job, $attributes);

/**
* set 'add header' in the formatter
* so it knows how to return the results
* for the csv (with or without a header row)
*/
$this->formatter->setAddHeader($this->filters['add_header']);
// handle hxl
$hxl_rows = $this->formatter->generateHXLRows(
$this->formatter->createHeading($attributes),
$this->getHxlRows($job)
);
$this->saveHXLHeaderRow($job, $hxl_rows);
$this->formatter->setHxlHeading($hxl_rows);
$formatter = $this->formatter;
Log::debug('EXPORTER: Count posts: ' . count($posts));

/**
* KeyAttributes is sent instead of the header row because it contains
* the attributes with the corresponding features (type, priority) that
* we need for manipulating the data
*/
$file = $formatter($posts, $job, $keyAttributes);
} catch (\Exception $e) {
// Mark batch as failed
$batchEntity = $this->repo->get($batchId);
$batchEntity->setState([
'status' => ExportBatch::STATUS_FAILED,
'filename' => $file->file,
'rows' => count($posts),
]);
$this->repo->update($batchEntity);
// And rethrow the error
throw $e;
}
Log::debug('EXPORTER: on interact Count posts: ' . count($posts));

/**
* update the header attributes
* in the job table so we know which headers to
* use in other chunks of the export
*/
$this->saveHeaderRow($job, $attributes);

/**
* set 'add header' in the formatter
* so it knows how to return the results
* for the csv (with or without a header row)
*/
$this->formatter->setAddHeader($this->filters['add_header']);
// handle hxl
$hxl_rows = $this->formatter->generateHXLRows(
$this->formatter->createHeading($attributes),
$this->getHxlRows($job)
);
$this->saveHXLHeaderRow($job, $hxl_rows);
$this->formatter->setHxlHeading($hxl_rows);
$formatter = $this->formatter;
Log::debug('EXPORTER: Count posts: ' . count($posts));

/**
* KeyAttributes is sent instead of the header row because it contains
* the attributes with the corresponding features (type, priority) that
* we need for manipulating the data
*/
$file = $formatter($posts, $job, $keyAttributes);

// Update export batch status=done
// Include filename, post count, header row etc
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/App/Jobs/CombineExportedPostBatchesJobTest.php
Expand Up @@ -57,7 +57,7 @@ public function testCombineBatchesJob()
->andReturn(true);

$exportBatchRepo->shouldReceive('getByJobId')
->with($jobId)
->with($jobId, 'completed')
->once()
->andReturn(collect([
new ExportBatch([
Expand Down

0 comments on commit 67fa76e

Please sign in to comment.