feat: allow passing min,max to sortedSetsCardSum

to get rid of multiple db calls in profile page
This commit is contained in:
Barış Soner Uşaklı
2024-06-07 19:14:13 -04:00
parent 6bbe3d1c4c
commit 70b4a0e2ae
5 changed files with 99 additions and 71 deletions

View File

@@ -221,16 +221,42 @@ SELECT o."_key" k,
return keys.map(k => parseInt((res.rows.find(r => r.k === k) || { c: 0 }).c, 10));
};
module.sortedSetsCardSum = async function (keys) {
module.sortedSetsCardSum = async function (keys, min = '-inf', max = '+inf') {
if (!keys || (Array.isArray(keys) && !keys.length)) {
return 0;
}
if (!Array.isArray(keys)) {
keys = [keys];
}
const counts = await module.sortedSetsCard(keys);
const sum = counts.reduce((acc, val) => acc + val, 0);
return sum;
let counts = [];
if (min !== '-inf' || max !== '+inf') {
if (min === '-inf') {
min = null;
}
if (max === '+inf') {
max = null;
}
const res = await module.pool.query({
name: 'sortedSetsCardSum',
text: `
SELECT o."_key" k,
COUNT(*) c
FROM "legacy_object_live" o
INNER JOIN "legacy_zset" z
ON o."_key" = z."_key"
AND o."type" = z."type"
WHERE o."_key" = ANY($1::TEXT[])
AND (z."score" >= $2::NUMERIC OR $2::NUMERIC IS NULL)
AND (z."score" <= $3::NUMERIC OR $3::NUMERIC IS NULL)
GROUP BY o."_key"`,
values: [keys, min, max],
});
counts = keys.map(k => parseInt((res.rows.find(r => r.k === k) || { c: 0 }).c, 10));
} else {
counts = await module.sortedSetsCard(keys);
}
return counts.reduce((acc, val) => acc + val, 0);
};
module.sortedSetRank = async function (key, value) {