Skip to content

Commit

Permalink
Fix queue export (#1819)
Browse files Browse the repository at this point in the history
* Fix queue export

* Fix queue export

* Fix queue export

* WIP
  • Loading branch information
luanfreitasdev authored Dec 23, 2024
1 parent e4ef233 commit c0be6e9
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 45 deletions.
9 changes: 7 additions & 2 deletions src/DataSource/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,12 @@ public static function make(

public function filter(): EloquentBuilder|QueryBuilder
{
$filters = collect($this->component->filters());
// To make it work on export, we need to use ->filters instead of filters()
$filters = collect(
app()->runningInConsole() && !app()->runningUnitTests()
? $this->component->filters
: $this->component->filters()
);

if ($filters->isEmpty()) {
return $this->query;
Expand Down Expand Up @@ -223,7 +228,7 @@ private function filterNestedRelation(string $table, array $columns, string $sea
});
}
} catch (RelationNotFoundException $e) {
$query->leftJoin($nestedTable, "$table.$nestedTable" . "_id", '=', "$nestedTable.id")
$query->leftJoin($nestedTable, "$table.$nestedTable" . '_id', '=', "$nestedTable.id")
->orWhere(function (EloquentBuilder $query) use ($nestedTable, $nestedColumns, $search) {
foreach ($nestedColumns as $nestedColumn) {
$search = $this->getBeforeSearchMethod($nestedColumn, $search);
Expand Down
2 changes: 1 addition & 1 deletion src/DataSource/Processors/DataSourceBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ private static function processRows(BaseCollection $results, PowerGridComponent
$row = (object) $row;
$data = $fields->map(fn ($field) => $field($row, $index));

$rowId = data_get($row, $component->realPrimaryKey);
$rowId = data_get($row, $component->primaryKeyAlias ?? $component->primaryKey);

if ($renderActions) {
try {
Expand Down
16 changes: 5 additions & 11 deletions src/Jobs/ExportJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,28 +15,24 @@ class ExportJob implements ShouldQueue
{
use Batchable;
use Dispatchable;
use ExportableJob;
use InteractsWithQueue;
use Queueable;
use SerializesModels;
use ExportableJob;

private array $properties;

/**
* @param string $componentTable
* @param array $columns
* @param array $params
*/
public function __construct(
string $componentTable,
array $columns,
array $params
array $columns,
array $params
) {
$this->columns = $columns;
$this->exportableClass = $params['exportableClass'];
$this->fileName = $params['fileName'];
$this->offset = $params['offset'];
$this->limit = $params['limit'];
$this->filtered = $params['filtered'];
$this->filters = (array) Crypt::decrypt($params['filters']);
$this->properties = (array) Crypt::decrypt($params['parameters']);

Expand All @@ -46,8 +42,6 @@ public function __construct(

public function handle(): void
{
$exportable = new $this->exportableClass();

$currentHiddenStates = collect($this->columns)
->mapWithKeys(fn ($column) => [data_get($column, 'field') => data_get($column, 'hidden')]);

Expand All @@ -58,7 +52,7 @@ public function handle(): void
}, $this->componentTable->columns());

/** @phpstan-ignore-next-line */
$exportable
(new $this->exportableClass())
->fileName($this->getFilename())
->setData($columnsWithHiddenState, $this->prepareToExport($this->properties))
->download([]);
Expand Down
4 changes: 2 additions & 2 deletions src/PowerGridComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public function updatedPage(): void
{
$this->checkboxAll = false;

if ($this->hasLazyEnabled) {
if (!app()->runningInConsole() && $this->hasLazyEnabled) {
$this->additionalCacheKey = uniqid();

data_set($this->setUp, 'lazy.items', 0);
Expand All @@ -95,7 +95,7 @@ public function updatedSearch(): void
{
$this->gotoPage(1, data_get($this->setUp, 'footer.pageName'));

if ($this->hasLazyEnabled) {
if (!app()->runningInConsole() && $this->hasLazyEnabled) {
$this->additionalCacheKey = uniqid();

data_set($this->setUp, 'lazy.items', 0);
Expand Down
23 changes: 14 additions & 9 deletions src/Traits/ExportableJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ trait ExportableJob

private array $filters;

private array $filtered;

private function getFilename(): Stringable
{
return Str::of($this->fileName)
Expand All @@ -35,28 +37,31 @@ private function getFilename(): Stringable

private function prepareToExport(array $properties = []): Eloquent\Collection|Collection
{
/** @phpstan-ignore-next-line */
$processDataSource = tap(ProcessDataSource::make($this->componentTable, $properties), fn ($datasource) => $datasource->get());
$this->componentTable->filters = $this->filters ?? [];
$this->componentTable->filtered = $this->filtered ?? [];

$inClause = $processDataSource->component->filtered ?? [];
$processDataSource = tap(
ProcessDataSource::make($this->componentTable, $properties),
fn ($datasource) => $datasource->get()
);

/** @phpstan-ignore-next-line */
$this->componentTable->filters = $this->filters ?? [];
$filtered = $processDataSource->component->filtered ?? [];

/** @phpstan-ignore-next-line */
$currentTable = $processDataSource->component->currentTable;

$sortField = Str::of($processDataSource->component->sortField)->contains('.') ? $processDataSource->component->sortField : $currentTable . '.' . $processDataSource->component->sortField;

$results = $processDataSource->prepareDataSource() // @phpstan-ignore-line
$results = $this->componentTable->datasource($this->properties ?? []) // @phpstan-ignore-line
->where(
fn ($query) => Builder::make($query, $this->componentTable)
->filterContains()
->filter()
)
->when($inClause, function ($query, $inClause) use ($processDataSource) {
return $query->whereIn($processDataSource->component->primaryKey, $inClause);
->when($filtered, function ($query, $filtered) use ($processDataSource) {
return $query->whereIn($processDataSource->component->primaryKey, $filtered);
})
->offset($this->offset)
->limit($this->limit)
->orderBy($sortField, $processDataSource->component->sortDirection)
->get();

Expand Down
44 changes: 24 additions & 20 deletions src/Traits/WithExport.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,17 +54,19 @@ public function getExportBatchProperty(): ?Batch

public function updateExportProgress(): void
{
if (!is_null($this->exportBatch)) {
$this->batchFinished = $this->exportBatch->finished();
$this->batchProgress = $this->exportBatch->progress();
$this->batchErrors = $this->exportBatch->hasFailures();
if (is_null($this->exportBatch)) {
return;
}

if ($this->batchFinished) {
$this->batchExporting = false;
}
$this->batchFinished = $this->exportBatch->finished();
$this->batchProgress = $this->exportBatch->progress();
$this->batchErrors = $this->exportBatch->hasFailures();

$this->onBatchExecuting($this->exportBatch);
if ($this->batchFinished) {
$this->batchExporting = false;
}

$this->onBatchExecuting($this->exportBatch);
}

public function downloadExport(string $file): BinaryFileResponse
Expand Down Expand Up @@ -104,20 +106,22 @@ private function putQueuesToBus(string $exportableClass, string $fileExtension):

$this->exportedFiles = [];
$filters = $processDataSource?->component?->filters ?? [];
$filtered = $processDataSource?->component?->filtered ?? [];
$queues = collect([]);
$countQueue = $this->total > $this->getQueuesCount() ? $this->getQueuesCount() : 1;
$perPage = $this->total > $countQueue ? ($this->total / $countQueue) : 1;
$queueCount = $this->total > $this->getQueuesCount() ? $this->getQueuesCount() : 1;
$perPage = $this->total > $queueCount ? ($this->total / $queueCount) : 1;
$offset = 0;
$limit = $perPage;

for ($i = 1; $i < ($countQueue + 1); $i++) {
$fileName = 'powergrid-' . Str::kebab(strval(data_get($this->setUp, 'exportable.fileName'))) .
for ($i = 1; $i < ($queueCount + 1); $i++) {
$fileName = Str::kebab(strval(data_get($this->setUp, 'exportable.fileName'))) .
'-' . round(($offset + 1), 2) .
'-' . round($limit, 2) .
'-' . $this->getId() .
'.' . $fileExtension;

$params = [
'filtered' => $filtered,
'exportableClass' => $exportableClass,
'fileName' => $fileName,
'offset' => $offset,
Expand Down Expand Up @@ -164,15 +168,16 @@ public function prepareToExport(bool $selected = false): Eloquent\Collection|Sup
{
$processDataSource = tap(ProcessDataSource::make($this), fn ($datasource) => $datasource->get());

$inClause = $processDataSource->component->filtered;
$filtered = $processDataSource->component->filtered;

if ($selected && filled($processDataSource->component->checkboxValues)) {
$inClause = $processDataSource->component->checkboxValues;
$filtered = $processDataSource->component->checkboxValues;
}

if ($processDataSource->component->datasource() instanceof Collection) {
if ($inClause) {
$results = $processDataSource->get(isExport: true)->whereIn($this->primaryKey, $inClause);
if ($filtered) {
$results = $processDataSource->get(isExport: true)
->whereIn($this->primaryKey, $filtered);

return DataSourceBase::transform($results, $this);
}
Expand All @@ -191,8 +196,8 @@ public function prepareToExport(bool $selected = false): Eloquent\Collection|Sup
->filterContains()
->filter()
)
->when($inClause, function ($query, $inClause) use ($processDataSource) {
return $query->whereIn($processDataSource->component->primaryKey, $inClause);
->when($filtered, function ($query, $filtered) use ($processDataSource) {
return $query->whereIn($processDataSource->component->primaryKey, $filtered);
})
->orderBy($sortField, $processDataSource->component->sortDirection)
->get();
Expand Down Expand Up @@ -241,10 +246,9 @@ private function export(string $exportType, bool $selected): BinaryFileResponse|
/** @var string $fileName */
$fileName = data_get($this->setUp, 'exportable.fileName');
$exportable
->fileName($fileName) /** @phpstan-ignore-next-line */
->fileName($fileName)
->setData($columnsWithHiddenState, $this->prepareToExport($selected));

/** @phpstan-ignore-next-line */
return $exportable->download(
exportOptions: $this->setUp['exportable']
);
Expand Down

0 comments on commit c0be6e9

Please sign in to comment.