Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dont try to combine failed batches #3415

Merged
merged 2 commits into from
Nov 27, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion app/Jobs/CombineExportedPostBatchesJob.php
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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