Skip to content

Commit

Permalink
fix: make stake pool order by name case insensitive and move nulls to…
Browse files Browse the repository at this point in the history
… last
  • Loading branch information
lgobbi-atix committed Jun 9, 2022
1 parent f937654 commit ed0b249
Show file tree
Hide file tree
Showing 4 changed files with 2,503 additions and 2,482 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -666,7 +666,7 @@ export const withPagination = (query: string, pagination?: StakePoolQueryOptions

const orderBy = (query: string, sort: OrderByOptions[]) =>
sort && sort.length > 0
? `${query} ORDER BY ${sort.map(({ field, order }) => `${field} ${order}`).join(', ')}`
? `${query} ORDER BY ${sort.map(({ field, order }) => `${field} ${order} NULLS LAST`).join(', ')}`
: query;

export const addSentenceToQuery = (query: string, sentence: string) => query + sentence;
Expand Down Expand Up @@ -753,14 +753,18 @@ FROM last_pool_update AS pool_update
LEFT JOIN last_pool_retire AS pool_retire
ON pool_update.hash_id = pool_retire.hash_id`;

const sortFieldMapping: Record<string, string> = {
name: "lower((pod.json -> 'name')::TEXT)"
};

export const withSort = (query: string, sort?: StakePoolQueryOptions['sort'], defaultSort?: OrderByOptions[]) => {
if (!sort?.field && defaultSort) {
const defaultMappedSort = defaultSort.map((s) => ({ field: s.field, order: s.order }));
const defaultMappedSort = defaultSort.map((s) => ({ field: sortFieldMapping[s.field] || s.field, order: s.order }));
return orderBy(query, defaultMappedSort);
}
if (!sort?.field) return query;
const sortType = getStakePoolSortType(sort.field);
const mappedSort = { field: sort.field, order: sort.order };
const mappedSort = { field: sortFieldMapping[sort.field] || sort.field, order: sort.order };
switch (sortType) {
case 'data':
return orderBy(query, [mappedSort, { field: 'pool_id', order: 'asc' }]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -813,6 +813,28 @@ exports[`StakePoolBuilder getTotalAmountOfAda getTotalAmountOfAda 1`] = `"691544

exports[`StakePoolBuilder queryPoolData pagination with limit 1`] = `
Array [
Object {
"cost": 340000000n,
"hexId": "5d99282bbb4840380bb98c075498ed1983aee18a4a0925b9b44d93f1",
"id": "pool1tkvjs2amfpqrszae3sr4fx8drxp6acv2fgyjtwd5fkflzguqp96",
"margin": Object {
"denominator": 1000,
"numerator": 27,
},
"metadata": Object {
"description": "Pool a of the banderini devtest staking pools",
"homepage": "http://www.banderini.net",
"name": "banderini-devtest-a",
"ticker": "BANDA",
},
"metadataJson": Object {
"hash": "4d89054c2962215694a7122dfe41bc728d3ec248f80ea9a2e0d493057d7d2338",
"url": "https://git.io/JJ7wm",
},
"pledge": 100000000n,
"rewardAccount": "stake_test1upx9faamuf54pm7alg4lna5l7ll08pz833rj45tgr9m2jyceasqjt",
"vrfKeyHash": "c062fabfeb7a68c61c34532e6f441b999c6a5a30b409d24c93174f047d4d935a",
},
Object {
"cost": 340000000n,
"hexId": "961d329fba1807eef89db767ba405aec0c5426501c6b1df20f5c0995",
Expand Down Expand Up @@ -857,54 +879,32 @@ Array [
"rewardAccount": "stake_test1uz9y7juwzcyanva4x4fzpx7tft6ckntn6ulsjjd2k7a0pxgldmzp5",
"vrfKeyHash": "83a817519ec34d3c637db8f9d46fcf6f7f9e826093d1b9a8158c89da4b47a801",
},
Object {
"cost": 340000000n,
"hexId": "5d99282bbb4840380bb98c075498ed1983aee18a4a0925b9b44d93f1",
"id": "pool1tkvjs2amfpqrszae3sr4fx8drxp6acv2fgyjtwd5fkflzguqp96",
"margin": Object {
"denominator": 1000,
"numerator": 27,
},
"metadata": Object {
"description": "Pool a of the banderini devtest staking pools",
"homepage": "http://www.banderini.net",
"name": "banderini-devtest-a",
"ticker": "BANDA",
},
"metadataJson": Object {
"hash": "4d89054c2962215694a7122dfe41bc728d3ec248f80ea9a2e0d493057d7d2338",
"url": "https://git.io/JJ7wm",
},
"pledge": 100000000n,
"rewardAccount": "stake_test1upx9faamuf54pm7alg4lna5l7ll08pz833rj45tgr9m2jyceasqjt",
"vrfKeyHash": "c062fabfeb7a68c61c34532e6f441b999c6a5a30b409d24c93174f047d4d935a",
},
]
`;

exports[`StakePoolBuilder queryPoolData pagination with startAt 1`] = `
Array [
Object {
"cost": 340000000n,
"hexId": "5d99282bbb4840380bb98c075498ed1983aee18a4a0925b9b44d93f1",
"id": "pool1tkvjs2amfpqrszae3sr4fx8drxp6acv2fgyjtwd5fkflzguqp96",
"cost": 400000000n,
"hexId": "5ee7591bf30eaa4f5dce70b4a676eb02d5be8012d188f04fe3beffb0",
"id": "pool1tmn4jxlnp64y7hwwwz62vahtqt2maqqj6xy0qnlrhmlmq3u8q0e",
"margin": Object {
"denominator": 1000,
"numerator": 27,
"denominator": 10000,
"numerator": 1,
},
"metadata": Object {
"description": "Pool a of the banderini devtest staking pools",
"homepage": "http://www.banderini.net",
"name": "banderini-devtest-a",
"ticker": "BANDA",
"description": "Our Amsterdam Node",
"homepage": "https://twitter.com/A92Syed",
"name": "THE AMSTERDAM NODE",
"ticker": "AMS",
},
"metadataJson": Object {
"hash": "4d89054c2962215694a7122dfe41bc728d3ec248f80ea9a2e0d493057d7d2338",
"url": "https://git.io/JJ7wm",
"hash": "cc019105f084aef2a956b2f7f2c0bf4e747bf7696705312c244620089429df6f",
"url": "https://git.io/JJ1dz",
},
"pledge": 100000000n,
"rewardAccount": "stake_test1upx9faamuf54pm7alg4lna5l7ll08pz833rj45tgr9m2jyceasqjt",
"vrfKeyHash": "c062fabfeb7a68c61c34532e6f441b999c6a5a30b409d24c93174f047d4d935a",
"pledge": 500000000n,
"rewardAccount": "stake_test1uz9y7juwzcyanva4x4fzpx7tft6ckntn6ulsjjd2k7a0pxgldmzp5",
"vrfKeyHash": "83a817519ec34d3c637db8f9d46fcf6f7f9e826093d1b9a8158c89da4b47a801",
},
Object {
"cost": 340000000n,
Expand Down Expand Up @@ -943,6 +943,28 @@ Array [

exports[`StakePoolBuilder queryPoolData sort by default sort 1`] = `
Array [
Object {
"cost": 340000000n,
"hexId": "5d99282bbb4840380bb98c075498ed1983aee18a4a0925b9b44d93f1",
"id": "pool1tkvjs2amfpqrszae3sr4fx8drxp6acv2fgyjtwd5fkflzguqp96",
"margin": Object {
"denominator": 1000,
"numerator": 27,
},
"metadata": Object {
"description": "Pool a of the banderini devtest staking pools",
"homepage": "http://www.banderini.net",
"name": "banderini-devtest-a",
"ticker": "BANDA",
},
"metadataJson": Object {
"hash": "4d89054c2962215694a7122dfe41bc728d3ec248f80ea9a2e0d493057d7d2338",
"url": "https://git.io/JJ7wm",
},
"pledge": 100000000n,
"rewardAccount": "stake_test1upx9faamuf54pm7alg4lna5l7ll08pz833rj45tgr9m2jyceasqjt",
"vrfKeyHash": "c062fabfeb7a68c61c34532e6f441b999c6a5a30b409d24c93174f047d4d935a",
},
Object {
"cost": 340000000n,
"hexId": "961d329fba1807eef89db767ba405aec0c5426501c6b1df20f5c0995",
Expand Down Expand Up @@ -987,28 +1009,6 @@ Array [
"rewardAccount": "stake_test1uz9y7juwzcyanva4x4fzpx7tft6ckntn6ulsjjd2k7a0pxgldmzp5",
"vrfKeyHash": "83a817519ec34d3c637db8f9d46fcf6f7f9e826093d1b9a8158c89da4b47a801",
},
Object {
"cost": 340000000n,
"hexId": "5d99282bbb4840380bb98c075498ed1983aee18a4a0925b9b44d93f1",
"id": "pool1tkvjs2amfpqrszae3sr4fx8drxp6acv2fgyjtwd5fkflzguqp96",
"margin": Object {
"denominator": 1000,
"numerator": 27,
},
"metadata": Object {
"description": "Pool a of the banderini devtest staking pools",
"homepage": "http://www.banderini.net",
"name": "banderini-devtest-a",
"ticker": "BANDA",
},
"metadataJson": Object {
"hash": "4d89054c2962215694a7122dfe41bc728d3ec248f80ea9a2e0d493057d7d2338",
"url": "https://git.io/JJ7wm",
},
"pledge": 100000000n,
"rewardAccount": "stake_test1upx9faamuf54pm7alg4lna5l7ll08pz833rj45tgr9m2jyceasqjt",
"vrfKeyHash": "c062fabfeb7a68c61c34532e6f441b999c6a5a30b409d24c93174f047d4d935a",
},
Object {
"cost": 340000000n,
"hexId": "5685f37bca393c683cf03e428280312c6c4ea485188672a2a0b3195c",
Expand Down Expand Up @@ -1046,28 +1046,6 @@ Array [

exports[`StakePoolBuilder queryPoolData sort by name desc 1`] = `
Array [
Object {
"cost": 340000000n,
"hexId": "5d99282bbb4840380bb98c075498ed1983aee18a4a0925b9b44d93f1",
"id": "pool1tkvjs2amfpqrszae3sr4fx8drxp6acv2fgyjtwd5fkflzguqp96",
"margin": Object {
"denominator": 1000,
"numerator": 27,
},
"metadata": Object {
"description": "Pool a of the banderini devtest staking pools",
"homepage": "http://www.banderini.net",
"name": "banderini-devtest-a",
"ticker": "BANDA",
},
"metadataJson": Object {
"hash": "4d89054c2962215694a7122dfe41bc728d3ec248f80ea9a2e0d493057d7d2338",
"url": "https://git.io/JJ7wm",
},
"pledge": 100000000n,
"rewardAccount": "stake_test1upx9faamuf54pm7alg4lna5l7ll08pz833rj45tgr9m2jyceasqjt",
"vrfKeyHash": "c062fabfeb7a68c61c34532e6f441b999c6a5a30b409d24c93174f047d4d935a",
},
Object {
"cost": 400000000n,
"hexId": "5ee7591bf30eaa4f5dce70b4a676eb02d5be8012d188f04fe3beffb0",
Expand Down Expand Up @@ -1112,6 +1090,28 @@ Array [
"rewardAccount": "stake_test1upzu5aw5swqmhy09e2aaa62nac468mnyjzyfww999trzavccrj7pw",
"vrfKeyHash": "0a164c03ef34f26ffda7242b36db0a57ab7b23e230ea8802e50695f1f664de42",
},
Object {
"cost": 340000000n,
"hexId": "5d99282bbb4840380bb98c075498ed1983aee18a4a0925b9b44d93f1",
"id": "pool1tkvjs2amfpqrszae3sr4fx8drxp6acv2fgyjtwd5fkflzguqp96",
"margin": Object {
"denominator": 1000,
"numerator": 27,
},
"metadata": Object {
"description": "Pool a of the banderini devtest staking pools",
"homepage": "http://www.banderini.net",
"name": "banderini-devtest-a",
"ticker": "BANDA",
},
"metadataJson": Object {
"hash": "4d89054c2962215694a7122dfe41bc728d3ec248f80ea9a2e0d493057d7d2338",
"url": "https://git.io/JJ7wm",
},
"pledge": 100000000n,
"rewardAccount": "stake_test1upx9faamuf54pm7alg4lna5l7ll08pz833rj45tgr9m2jyceasqjt",
"vrfKeyHash": "c062fabfeb7a68c61c34532e6f441b999c6a5a30b409d24c93174f047d4d935a",
},
]
`;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -546,12 +546,12 @@ describe('StakePoolHttpService', () => {
}
};
const sortByNameThenByPoolId = function (poolA: Cardano.StakePool, poolB: Cardano.StakePool) {
if ((poolA.metadata?.name || poolA.id) > (poolB.metadata?.name || poolB.id)) {
return 1;
} else if ((poolA.metadata?.name || poolA.id) < (poolB.metadata?.name || poolB.id)) {
return -1;
if (poolA.metadata?.name && !poolB.metadata?.name) return -1;
if (!poolA.metadata?.name && poolB.metadata?.name) return 1;
if (poolA.metadata?.name && poolB.metadata?.name) {
return poolA.metadata.name.toLowerCase() > poolB.metadata.name.toLowerCase() ? 1 : -1;
}
return 0;
return poolA.id > poolB.id ? 1 : -1;
};

describe('sort by name', () => {
Expand Down Expand Up @@ -614,6 +614,23 @@ describe('StakePoolHttpService', () => {
expect(pageResults[pageResults.length - 1].metadata?.name).toBeUndefined();
expect(pageResults.map(({ id }) => id)).toEqual(stakePoolIdsSorted);
});

it('with name descending', async () => {
const stakePoolIdsSorted = [
secondNamedPoolId,
firstNamedPoolId,
fistNoMetadataPoolId,
secondNoMetadataPoolId
];
const { pageResults } = await doStakePoolRequest<[StakePoolQueryOptions], StakePoolSearchResults>(url, [
{ ...reqOptions, sort: { field: 'name', order: 'desc' } }
]);

expect(pageResults.length).toEqual(4);
expect(pageResults[0].metadata?.name).toEqual('Farts');
expect(pageResults[pageResults.length - 1].metadata?.name).toBeUndefined();
expect(pageResults.map(({ id }) => id)).toEqual(stakePoolIdsSorted);
});
});

it('with applied filters', async () => {
Expand Down
Loading

0 comments on commit ed0b249

Please sign in to comment.