Skip to content

Commit

Permalink
Fix queue export
Browse files Browse the repository at this point in the history
  • Loading branch information
luanfreitasdev committed Dec 23, 2024
1 parent e4ef233 commit 517b57d
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 22 deletions.
2 changes: 1 addition & 1 deletion src/DataSource/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public static function make(

public function filter(): EloquentBuilder|QueryBuilder
{
$filters = collect($this->component->filters());
$filters = collect($this->component->filters);

if ($filters->isEmpty()) {
return $this->query;
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
11 changes: 3 additions & 8 deletions src/Jobs/ExportJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,17 @@ 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'];
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
15 changes: 10 additions & 5 deletions src/Traits/ExportableJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,22 @@ 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());

$inClause = $processDataSource->component->filtered ?? [];
$this->componentTable->filters = $this->filters ?? [];

/** @phpstan-ignore-next-line */
$this->componentTable->filters = $this->filters ?? [];
$processDataSource = tap(
ProcessDataSource::make($this->componentTable, $properties),
fn ($datasource) => $datasource->get()
);

$inClause = $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()
Expand All @@ -57,6 +60,8 @@ private function prepareToExport(array $properties = []): Eloquent\Collection|Co
->when($inClause, function ($query, $inClause) use ($processDataSource) {
return $query->whereIn($processDataSource->component->primaryKey, $inClause);
})
->offset($this->offset)
->limit($this->limit)
->orderBy($sortField, $processDataSource->component->sortDirection)
->get();

Expand Down
11 changes: 6 additions & 5 deletions src/Traits/WithExport.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,13 +105,13 @@ private function putQueuesToBus(string $exportableClass, string $fileExtension):
$this->exportedFiles = [];
$filters = $processDataSource?->component?->filters ?? [];
$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() .
Expand Down Expand Up @@ -172,7 +172,8 @@ public function prepareToExport(bool $selected = false): Eloquent\Collection|Sup

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

return DataSourceBase::transform($results, $this);
}
Expand Down

0 comments on commit 517b57d

Please sign in to comment.