Skip to content

Commit c0be6e9

Browse files
Fix queue export (#1819)
* Fix queue export * Fix queue export * Fix queue export * WIP
1 parent e4ef233 commit c0be6e9

File tree

6 files changed

+53
-45
lines changed

6 files changed

+53
-45
lines changed

src/DataSource/Builder.php

+7-2
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,12 @@ public static function make(
3939

4040
public function filter(): EloquentBuilder|QueryBuilder
4141
{
42-
$filters = collect($this->component->filters());
42+
// To make it work on export, we need to use ->filters instead of filters()
43+
$filters = collect(
44+
app()->runningInConsole() && !app()->runningUnitTests()
45+
? $this->component->filters
46+
: $this->component->filters()
47+
);
4348

4449
if ($filters->isEmpty()) {
4550
return $this->query;
@@ -223,7 +228,7 @@ private function filterNestedRelation(string $table, array $columns, string $sea
223228
});
224229
}
225230
} catch (RelationNotFoundException $e) {
226-
$query->leftJoin($nestedTable, "$table.$nestedTable" . "_id", '=', "$nestedTable.id")
231+
$query->leftJoin($nestedTable, "$table.$nestedTable" . '_id', '=', "$nestedTable.id")
227232
->orWhere(function (EloquentBuilder $query) use ($nestedTable, $nestedColumns, $search) {
228233
foreach ($nestedColumns as $nestedColumn) {
229234
$search = $this->getBeforeSearchMethod($nestedColumn, $search);

src/DataSource/Processors/DataSourceBase.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ private static function processRows(BaseCollection $results, PowerGridComponent
133133
$row = (object) $row;
134134
$data = $fields->map(fn ($field) => $field($row, $index));
135135

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

138138
if ($renderActions) {
139139
try {

src/Jobs/ExportJob.php

+5-11
Original file line numberDiff line numberDiff line change
@@ -15,28 +15,24 @@ class ExportJob implements ShouldQueue
1515
{
1616
use Batchable;
1717
use Dispatchable;
18+
use ExportableJob;
1819
use InteractsWithQueue;
1920
use Queueable;
2021
use SerializesModels;
21-
use ExportableJob;
2222

2323
private array $properties;
2424

25-
/**
26-
* @param string $componentTable
27-
* @param array $columns
28-
* @param array $params
29-
*/
3025
public function __construct(
3126
string $componentTable,
32-
array $columns,
33-
array $params
27+
array $columns,
28+
array $params
3429
) {
3530
$this->columns = $columns;
3631
$this->exportableClass = $params['exportableClass'];
3732
$this->fileName = $params['fileName'];
3833
$this->offset = $params['offset'];
3934
$this->limit = $params['limit'];
35+
$this->filtered = $params['filtered'];
4036
$this->filters = (array) Crypt::decrypt($params['filters']);
4137
$this->properties = (array) Crypt::decrypt($params['parameters']);
4238

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

4743
public function handle(): void
4844
{
49-
$exportable = new $this->exportableClass();
50-
5145
$currentHiddenStates = collect($this->columns)
5246
->mapWithKeys(fn ($column) => [data_get($column, 'field') => data_get($column, 'hidden')]);
5347

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

6054
/** @phpstan-ignore-next-line */
61-
$exportable
55+
(new $this->exportableClass())
6256
->fileName($this->getFilename())
6357
->setData($columnsWithHiddenState, $this->prepareToExport($this->properties))
6458
->download([]);

src/PowerGridComponent.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ public function updatedPage(): void
8080
{
8181
$this->checkboxAll = false;
8282

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

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

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

101101
data_set($this->setUp, 'lazy.items', 0);

src/Traits/ExportableJob.php

+14-9
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ trait ExportableJob
2626

2727
private array $filters;
2828

29+
private array $filtered;
30+
2931
private function getFilename(): Stringable
3032
{
3133
return Str::of($this->fileName)
@@ -35,28 +37,31 @@ private function getFilename(): Stringable
3537

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

41-
$inClause = $processDataSource->component->filtered ?? [];
43+
$processDataSource = tap(
44+
ProcessDataSource::make($this->componentTable, $properties),
45+
fn ($datasource) => $datasource->get()
46+
);
4247

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

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

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

51-
$results = $processDataSource->prepareDataSource() // @phpstan-ignore-line
54+
$results = $this->componentTable->datasource($this->properties ?? []) // @phpstan-ignore-line
5255
->where(
5356
fn ($query) => Builder::make($query, $this->componentTable)
5457
->filterContains()
5558
->filter()
5659
)
57-
->when($inClause, function ($query, $inClause) use ($processDataSource) {
58-
return $query->whereIn($processDataSource->component->primaryKey, $inClause);
60+
->when($filtered, function ($query, $filtered) use ($processDataSource) {
61+
return $query->whereIn($processDataSource->component->primaryKey, $filtered);
5962
})
63+
->offset($this->offset)
64+
->limit($this->limit)
6065
->orderBy($sortField, $processDataSource->component->sortDirection)
6166
->get();
6267

src/Traits/WithExport.php

+24-20
Original file line numberDiff line numberDiff line change
@@ -54,17 +54,19 @@ public function getExportBatchProperty(): ?Batch
5454

5555
public function updateExportProgress(): void
5656
{
57-
if (!is_null($this->exportBatch)) {
58-
$this->batchFinished = $this->exportBatch->finished();
59-
$this->batchProgress = $this->exportBatch->progress();
60-
$this->batchErrors = $this->exportBatch->hasFailures();
57+
if (is_null($this->exportBatch)) {
58+
return;
59+
}
6160

62-
if ($this->batchFinished) {
63-
$this->batchExporting = false;
64-
}
61+
$this->batchFinished = $this->exportBatch->finished();
62+
$this->batchProgress = $this->exportBatch->progress();
63+
$this->batchErrors = $this->exportBatch->hasFailures();
6564

66-
$this->onBatchExecuting($this->exportBatch);
65+
if ($this->batchFinished) {
66+
$this->batchExporting = false;
6767
}
68+
69+
$this->onBatchExecuting($this->exportBatch);
6870
}
6971

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

105107
$this->exportedFiles = [];
106108
$filters = $processDataSource?->component?->filters ?? [];
109+
$filtered = $processDataSource?->component?->filtered ?? [];
107110
$queues = collect([]);
108-
$countQueue = $this->total > $this->getQueuesCount() ? $this->getQueuesCount() : 1;
109-
$perPage = $this->total > $countQueue ? ($this->total / $countQueue) : 1;
111+
$queueCount = $this->total > $this->getQueuesCount() ? $this->getQueuesCount() : 1;
112+
$perPage = $this->total > $queueCount ? ($this->total / $queueCount) : 1;
110113
$offset = 0;
111114
$limit = $perPage;
112115

113-
for ($i = 1; $i < ($countQueue + 1); $i++) {
114-
$fileName = 'powergrid-' . Str::kebab(strval(data_get($this->setUp, 'exportable.fileName'))) .
116+
for ($i = 1; $i < ($queueCount + 1); $i++) {
117+
$fileName = Str::kebab(strval(data_get($this->setUp, 'exportable.fileName'))) .
115118
'-' . round(($offset + 1), 2) .
116119
'-' . round($limit, 2) .
117120
'-' . $this->getId() .
118121
'.' . $fileExtension;
119122

120123
$params = [
124+
'filtered' => $filtered,
121125
'exportableClass' => $exportableClass,
122126
'fileName' => $fileName,
123127
'offset' => $offset,
@@ -164,15 +168,16 @@ public function prepareToExport(bool $selected = false): Eloquent\Collection|Sup
164168
{
165169
$processDataSource = tap(ProcessDataSource::make($this), fn ($datasource) => $datasource->get());
166170

167-
$inClause = $processDataSource->component->filtered;
171+
$filtered = $processDataSource->component->filtered;
168172

169173
if ($selected && filled($processDataSource->component->checkboxValues)) {
170-
$inClause = $processDataSource->component->checkboxValues;
174+
$filtered = $processDataSource->component->checkboxValues;
171175
}
172176

173177
if ($processDataSource->component->datasource() instanceof Collection) {
174-
if ($inClause) {
175-
$results = $processDataSource->get(isExport: true)->whereIn($this->primaryKey, $inClause);
178+
if ($filtered) {
179+
$results = $processDataSource->get(isExport: true)
180+
->whereIn($this->primaryKey, $filtered);
176181

177182
return DataSourceBase::transform($results, $this);
178183
}
@@ -191,8 +196,8 @@ public function prepareToExport(bool $selected = false): Eloquent\Collection|Sup
191196
->filterContains()
192197
->filter()
193198
)
194-
->when($inClause, function ($query, $inClause) use ($processDataSource) {
195-
return $query->whereIn($processDataSource->component->primaryKey, $inClause);
199+
->when($filtered, function ($query, $filtered) use ($processDataSource) {
200+
return $query->whereIn($processDataSource->component->primaryKey, $filtered);
196201
})
197202
->orderBy($sortField, $processDataSource->component->sortDirection)
198203
->get();
@@ -241,10 +246,9 @@ private function export(string $exportType, bool $selected): BinaryFileResponse|
241246
/** @var string $fileName */
242247
$fileName = data_get($this->setUp, 'exportable.fileName');
243248
$exportable
244-
->fileName($fileName) /** @phpstan-ignore-next-line */
249+
->fileName($fileName)
245250
->setData($columnsWithHiddenState, $this->prepareToExport($selected));
246251

247-
/** @phpstan-ignore-next-line */
248252
return $exportable->download(
249253
exportOptions: $this->setUp['exportable']
250254
);

0 commit comments

Comments
 (0)