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

refactor: sorting by volume change in a specific period #607

Merged
merged 22 commits into from
Jan 19, 2024
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
8 changes: 7 additions & 1 deletion app/Http/Controllers/CollectionController.php
Original file line number Diff line number Diff line change
Expand Up @@ -149,9 +149,15 @@ private function getPopularCollections(Request $request): Paginator

$currency = $user ? $user->currency() : CurrencyCode::USD;

$volumeColumn = match ($request->query('period')) {
'7d' => 'avg_volume_7d',
'30d' => 'avg_volume_30d',
default => 'avg_volume_1d',
};

/** @var Paginator<PopularCollectionData> $collections */
$collections = Collection::query()
->when($request->query('sort') !== 'floor-price', fn ($q) => $q->orderBy('volume', 'desc')) // TODO: order by top...
->when($request->query('sort') !== 'floor-price', fn ($q) => $q->orderByWithNulls($volumeColumn.'::numeric', 'desc'))
->filterByChainId($chainId)
->orderByFloorPrice('desc', $currency)
->with([
Expand Down
47 changes: 25 additions & 22 deletions app/Models/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -235,8 +235,6 @@ public function scopeErc721(Builder $query): Builder
*/
public function scopeOrderByValue(Builder $query, ?Wallet $wallet, ?string $direction, ?CurrencyCode $currency = CurrencyCode::USD): Builder
{
$nullsPosition = strtolower($direction) === 'asc' ? 'NULLS FIRST' : 'NULLS LAST';

$walletFilter = $wallet ? "WHERE nfts.wallet_id = $wallet->id" : '';

return $query->selectRaw(
Expand All @@ -251,7 +249,7 @@ public function scopeOrderByValue(Builder $query, ?Wallet $wallet, ?string $dire
GROUP BY collection_id
) nc"), 'collections.id', '=', 'nc.collection_id')
->groupBy('collections.id')
->orderByRaw("total_value {$direction} {$nullsPosition}")
->orderByWithNulls('total_value', $direction)
->orderBy('collections.id', $direction);
}

Expand All @@ -262,11 +260,9 @@ public function scopeOrderByValue(Builder $query, ?Wallet $wallet, ?string $dire
*/
public function scopeOrderByFloorPrice(Builder $query, string $direction, CurrencyCode $currency = CurrencyCode::USD): Builder
{
$nullsPosition = $direction === 'asc' ? 'NULLS FIRST' : 'NULLS LAST';

return $query->selectRaw(
sprintf('collections.*, CAST(collections.fiat_value->>\'%s\' AS float) as total_floor_price', $currency->value)
)->orderByRaw("total_floor_price {$direction} {$nullsPosition}");
)->orderByWithNulls('total_floor_price', $direction);
}

/**
Expand All @@ -276,9 +272,7 @@ public function scopeOrderByFloorPrice(Builder $query, string $direction, Curren
*/
public function scopeOrderByName(Builder $query, string $direction): Builder
{
$nullsPosition = $direction === 'asc' ? 'NULLS FIRST' : 'NULLS LAST';

return $query->orderByRaw("lower(collections.name) {$direction} {$nullsPosition}");
return $query->orderByWithNulls('lower(collections.name)', $direction);
}

/**
Expand All @@ -288,11 +282,7 @@ public function scopeOrderByName(Builder $query, string $direction): Builder
*/
public function scopeOrderByMintDate(Builder $query, string $direction): Builder
{
if ($direction === 'asc') {
return $query->orderByRaw('minted_at ASC NULLS FIRST');
}

return $query->orderByRaw('minted_at DESC NULLS LAST');
return $query->orderByWithNulls('minted_at', $direction);
}

/**
Expand All @@ -314,11 +304,7 @@ public function scopeOrderByReceivedDate(Builder $query, Wallet $wallet, string
->addSelect(DB::raw('MAX(nft_activity.timestamp) as received_at'))
->groupBy('collections.id');

if ($direction === 'asc') {
return $query->orderByRaw('received_at ASC NULLS FIRST');
}

return $query->orderByRaw('received_at DESC NULLS LAST');
return $query->orderByWithNulls('received_at', $direction);
}

/**
Expand Down Expand Up @@ -581,7 +567,7 @@ public function isBlacklisted(): bool
*/
public function scopeOrderByOldestNftLastFetchedAt(Builder $query): Builder
{
return $query->orderByRaw('extra_attributes->>\'nft_last_fetched_at\' ASC NULLS FIRST');
return $query->orderByWithNulls('extra_attributes->>\'nft_last_fetched_at\'', 'asc');
}

/**
Expand All @@ -590,7 +576,7 @@ public function scopeOrderByOldestNftLastFetchedAt(Builder $query): Builder
*/
public function scopeOrderByFloorPriceLastFetchedAt(Builder $query): Builder
{
return $query->orderByRaw('extra_attributes->>\'floor_price_last_fetched_at\' ASC NULLS FIRST');
return $query->orderByWithNulls('extra_attributes->>\'floor_price_last_fetched_at\'', 'asc');
}

/**
Expand All @@ -599,7 +585,7 @@ public function scopeOrderByFloorPriceLastFetchedAt(Builder $query): Builder
*/
public function scopeOrderByOpenseaSlugLastFetchedAt(Builder $query): Builder
{
return $query->orderByRaw('extra_attributes->>\'opensea_slug_last_fetched_at\' ASC NULLS FIRST');
return $query->orderByWithNulls('extra_attributes->>\'opensea_slug_last_fetched_at\'', 'asc');
}

public function isSpam(): bool
Expand Down Expand Up @@ -731,4 +717,21 @@ public function averageVolumeSince(Carbon $date): string
->where('created_at', '>', $date)
->value('aggregate');
}

/**
* Modify the query to apply ORDER BY, but with respect to NULL values.
* If direction is ascending, the query will order NULL values first,
* otherwise it will order NULL values last.
*
* @param Builder<self> $query
* @return Builder<self>
*/
public function scopeOrderByWithNulls(Builder $query, string $column, string $direction): Builder
{
$nullsPosition = strtolower($direction) === 'asc'
? 'NULLS FIRST'
: 'NULLS LAST';

return $query->orderByRaw("{$column} {$direction} {$nullsPosition}");
}
}
Loading