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

Implement sorting functionality and add total_supply for Universe Ass… #485

Merged
merged 1 commit into from
Sep 12, 2023

Conversation

ben2077
Copy link
Contributor

@ben2077 ben2077 commented Sep 4, 2023

Implement sorting functionality and add total_supply for Universe Asset Stats

  • Introduced ascending and descending sorting options for the Universe Asset Stats query.
  • Added 'total_supply' field to Universe Asset Stats, enhancing the available dataset.
  • Enhanced TestUniverseQuerySyncStatsSortingDirection to validate new sorting mechanisms and the inclusion of total_supply.
  • Optimized SQL for efficient sorting based on user-selected parameters.

@guggero guggero linked an issue Sep 5, 2023 that may be closed by this pull request
Copy link
Member

@guggero guggero left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks a lot for the PR!
Looks pretty good, main comments are around code style/optimizations.

WHEN sqlc.narg('sort_by') = 'genesis_height' THEN asset_info.genesis_height
ELSE NULL
END
CASE WHEN sqlc.narg('sort_by') = 'asset_id' AND sqlc.narg('sort_direction') = 0 THEN asset_info.asset_id END ASC,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we unify the formatting here a bit so it's easier to read on narrow terminals? We aim at ~80 characters for those who like to have many rows of vim open.
Also, the ASC is kind of superfluous as it's the default.

My suggestion:

ORDER BY
    CASE WHEN sqlc.narg('sort_by') = 'asset_id' AND sqlc.narg('sort_direction') = 0 THEN
        asset_info.asset_id END,
    CASE WHEN sqlc.narg('sort_by') = 'asset_id' AND sqlc.narg('sort_direction') = 1 THEN
        asset_info.asset_id END DESC ,
    CASE WHEN sqlc.narg('sort_by') = 'asset_name' AND sqlc.narg('sort_direction') = 0 THEN
        asset_info.asset_name END,
    CASE WHEN sqlc.narg('sort_by') = 'asset_name' AND sqlc.narg('sort_direction') = 1 THEN
        asset_info.asset_name END DESC ,
    CASE WHEN sqlc.narg('sort_by') = 'asset_type' AND sqlc.narg('sort_direction') = 0 THEN
        asset_info.asset_type END,
    CASE WHEN sqlc.narg('sort_by') = 'asset_type' AND sqlc.narg('sort_direction') = 1 THEN
        asset_info.asset_type END DESC,
    CASE WHEN sqlc.narg('sort_by') = 'total_syncs' AND sqlc.narg('sort_direction') = 0 THEN
        universe_stats.total_asset_syncs END,
    CASE WHEN sqlc.narg('sort_by') = 'total_syncs' AND sqlc.narg('sort_direction') = 1 THEN
        universe_stats.total_asset_syncs END DESC,
    CASE WHEN sqlc.narg('sort_by') = 'total_proofs' AND sqlc.narg('sort_direction') = 0 THEN
        universe_stats.total_asset_proofs END,
    CASE WHEN sqlc.narg('sort_by') = 'total_proofs' AND sqlc.narg('sort_direction') = 1 THEN
        universe_stats.total_asset_proofs END DESC,
    CASE WHEN sqlc.narg('sort_by') = 'genesis_height' AND sqlc.narg('sort_direction') = 0 THEN
        asset_info.genesis_height END,
    CASE WHEN sqlc.narg('sort_by') = 'genesis_height' AND sqlc.narg('sort_direction') = 1 THEN
        asset_info.genesis_height END DESC,
    CASE WHEN sqlc.narg('sort_by') = 'total_supply' AND sqlc.narg('sort_direction') = 0 THEN
        asset_supply END,
    CASE WHEN sqlc.narg('sort_by') = 'total_supply' AND sqlc.narg('sort_direction') = 1 THEN
        asset_supply END DESC
LIMIT @num_limit OFFSET @num_offset;

@@ -247,81 +247,247 @@ func TestUniverseQuerySyncStatsSorting(t *testing.T) {

// sortCheck is used to generate an IsSorted func bound to the
// response, for each sort type below.
type sortCheck func([]universe.AssetSyncSnapshot) func(i, j int) bool
type sortCheck func([]universe.AssetSyncSnapshot, universe.SortDirection) func(i, j int) bool
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: line length, break before universe.SortDirection.


return func(i, j int) bool {
return s[i].AssetName < s[j].AssetName
if d == universe.SortAscending {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could extract all of these individual isSortedFunc into a single one, now that they have become a bit more complicated.
For example:

	isSorted := func(s []universe.AssetSyncSnapshot,
		t universe.SyncStatsSort,
		d universe.SortDirection) func(i, j int) bool {

		asc := d == universe.SortAscending
		desc := d == universe.SortDescending
			
		return func(i, j int) bool {
			switch {
			case t == universe.SortByAssetName && asc:
				return s[i].AssetName < s[j].AssetName
			case t == universe.SortByAssetName && desc:
				return s[i].AssetName > s[j].AssetName
				
				...
			}
		}
	}

tapdb/universe_stats_test.go Outdated Show resolved Hide resolved
tapdb/universe_stats_test.go Outdated Show resolved Hide resolved
universe/interface.go Show resolved Hide resolved
universe/interface.go Outdated Show resolved Hide resolved
@dstadulis
Copy link
Collaborator

Thanks very much for the PR! @jamaljsr will test shortly

@jamaljsr
Copy link
Member

jamaljsr commented Sep 5, 2023

tACK. I tested the functionality. LGTM 👌

Copy link
Member

@guggero guggero left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A few more nits, then this is good to go.
And can you please squash your commits into a single one? Thanks!

tapdb/universe_stats_test.go Outdated Show resolved Hide resolved
universe/interface.go Outdated Show resolved Hide resolved
@dstadulis
Copy link
Collaborator

Hey @ben2077 we'd love to get this PR in by the 14th, do you think that's possible?

@ben2077
Copy link
Contributor Author

ben2077 commented Sep 7, 2023

no problem.

@ben2077 ben2077 closed this Sep 7, 2023
@ben2077 ben2077 reopened this Sep 7, 2023
…et Stats

- Introduced ascending and descending sorting options for the Universe Asset Stats query.
- Added 'total_supply' field to Universe Asset Stats, enhancing the available dataset.
- Enhanced TestUniverseQuerySyncStatsSortingDirection to validate new sorting mechanisms and the inclusion of total_supply.
- Optimized SQL for efficient sorting based on user-selected parameters.
Copy link
Member

@Roasbeef Roasbeef left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 🪁

Thanks for such a great first contribution PR!

@Roasbeef Roasbeef merged commit 06aa00c into lightninglabs:main Sep 12, 2023
14 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Status: ✅ Done
Development

Successfully merging this pull request may close these issues.

[enhancement]: improve QueryAssets sorting
5 participants