Skip to content

Commit

Permalink
listing_price_symcode required for listing_price_amount aggregation
Browse files Browse the repository at this point in the history
  • Loading branch information
Pelotfr committed Nov 9, 2023
1 parent 003918b commit 8faeb29
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 10 deletions.
10 changes: 5 additions & 5 deletions src/queries.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ JOIN Assets AS a ON a.asset_id = s.asset_ids AND a.collection_name = s.collectio

test("getAggregate", () => {
expect(getAggregate(new URLSearchParams({aggregate_function: 'count', collection_name})))
.toBe(`SELECT count() FROM Sales AS s JOIN blocks ON blocks.block_id = s.block_id WHERE (collection_name == '${collection_name}')`);
.toBe(`SELECT count() FROM Sales AS s WHERE (collection_name == '${collection_name}')`);

expect(getAggregate(new URLSearchParams({aggregate_function: 'count', aggregate_column: 'sale_id'})))
.toBe(`SELECT count(sale_id) FROM Sales AS s JOIN blocks ON blocks.block_id = s.block_id`);
.toBe(`SELECT count(sale_id) FROM Sales AS s`);

expect(getAggregate(new URLSearchParams({aggregate_function: 'max', aggregate_column: 'listing_price_amount'})))
.toBe(`SELECT max(listing_price_amount) FROM Sales AS s JOIN blocks ON blocks.block_id = s.block_id`);
expect(getAggregate(new URLSearchParams({aggregate_function: 'max', aggregate_column: 'listing_price_amount', listing_price_symcode: 'EOS'})))
.toBe(`SELECT max(listing_price_amount) FROM Sales AS s WHERE (listing_price_symcode == 'EOS')`);

expect(getAggregate(new URLSearchParams({aggregate_function: 'max', aggregate_column: 'total_asset_ids'})))
.toBe(`SELECT max(total_asset_ids) FROM (SELECT length(asset_ids) AS total_asset_ids, block_id FROM Sales) AS s JOIN blocks ON blocks.block_id = s.block_id`);
.toBe(`SELECT max(total_asset_ids) FROM (SELECT length(asset_ids) AS total_asset_ids FROM Sales) AS s`);
});
12 changes: 7 additions & 5 deletions src/queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,14 +85,18 @@ export function getAggregate(searchParams: URLSearchParams) {
// Aggregate Column
const aggregate_column = parseAggregateColumn(searchParams.get("aggregate_column"));

// Listing Price Symcode
const listing_price_symcode = parseListingPriceSymcode(searchParams.get('listing_price_symcode'));

if (aggregate_function == "count" && aggregate_column != "total_asset_ids") {
if (aggregate_column) query += ` count(${aggregate_column})`;
else query += ` count()`;
}

// for total asset ids we need a subquery
else if (aggregate_column == "total_asset_ids") query += ` ${aggregate_function}(${aggregate_column}) FROM (SELECT length(asset_ids) AS total_asset_ids, block_id`;
else if (aggregate_column != "sale_id") query += ` ${aggregate_function}(${aggregate_column})`
else if (aggregate_column == "total_asset_ids") query += ` ${aggregate_function}(${aggregate_column}) FROM (SELECT length(asset_ids) AS total_asset_ids`;
else if (aggregate_column == "listing_price_amount" && (listing_price_symcode)) query += ` ${aggregate_function}(${aggregate_column})`
else if (aggregate_column == "listing_price_amount" && !(listing_price_symcode)) throw new Error("Please specify a Symbol Code");
else throw new Error("Invalid aggregate column with given aggregate function");

query += ` FROM Sales`;
Expand All @@ -103,9 +107,6 @@ export function getAggregate(searchParams: URLSearchParams) {
// alias needed for subqueries so we include it all the time
query += ` AS s`;

// JOIN block table using alias
query += ` JOIN blocks ON blocks.block_id = s.block_id`;

const where = [];
// Clickhouse Operators
// https://clickhouse.com/docs/en/sql-reference/operators
Expand All @@ -129,6 +130,7 @@ export function getAggregate(searchParams: URLSearchParams) {
if (collection_name) where.push(`collection_name == '${collection_name}'`);
if (block_number) where.push(`block_number == '${block_number}'`);
if (timestamp) where.push(`toUnixTimestamp(timestamp) == ${timestamp}`);
if (listing_price_symcode) where.push(`listing_price_symcode == '${listing_price_symcode}'`);

// Join WHERE statements with AND
if ( where.length ) query += ` WHERE (${where.join(' AND ')})`;
Expand Down

0 comments on commit 8faeb29

Please sign in to comment.