fix(economy): Optimise balance retrieval.
This commit is contained in:
@@ -381,13 +381,28 @@ class Economy(LionCog):
|
|||||||
if role:
|
if role:
|
||||||
query = MemModel.table.select_where(
|
query = MemModel.table.select_where(
|
||||||
(MemModel.guildid == role.guild.id) & (MemModel.coins != 0)
|
(MemModel.guildid == role.guild.id) & (MemModel.coins != 0)
|
||||||
)
|
).with_no_adapter()
|
||||||
query.order_by('coins', ORDER.DESC)
|
|
||||||
if not role.is_default():
|
if not role.is_default():
|
||||||
# Everyone role is handled differently for data efficiency
|
# Everyone role is handled differently for data efficiency
|
||||||
ids = [target.id for target in targets]
|
ids = [target.id for target in targets]
|
||||||
query = query.where(userid=ids)
|
query = query.where(userid=ids)
|
||||||
rows = await query
|
|
||||||
|
# First get a summary
|
||||||
|
summary = await query.select(
|
||||||
|
_count='COUNT(*)',
|
||||||
|
_coin_total='SUM(coins)',
|
||||||
|
)
|
||||||
|
record = summary[0]
|
||||||
|
count = record['_count']
|
||||||
|
total = record['_coin_total']
|
||||||
|
if count > 0:
|
||||||
|
# Then get the top 1000 members
|
||||||
|
query._columns = ()
|
||||||
|
query.order_by('coins', ORDER.DESC)
|
||||||
|
query.limit(1000)
|
||||||
|
rows = await query.select('userid', 'coins')
|
||||||
|
else:
|
||||||
|
rows = []
|
||||||
|
|
||||||
name = t(_p(
|
name = t(_p(
|
||||||
'cmd:economy_balance|embed:role_lb|author',
|
'cmd:economy_balance|embed:role_lb|author',
|
||||||
@@ -400,7 +415,7 @@ class Economy(LionCog):
|
|||||||
"This server has a total balance of {coin_emoji}**{total}**."
|
"This server has a total balance of {coin_emoji}**{total}**."
|
||||||
)).format(
|
)).format(
|
||||||
coin_emoji=cemoji,
|
coin_emoji=cemoji,
|
||||||
total=sum(row['coins'] for row in rows)
|
total=total
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
header = t(_p(
|
header = t(_p(
|
||||||
@@ -408,9 +423,9 @@ class Economy(LionCog):
|
|||||||
"{role_mention} has `{count}` members with non-zero balance, "
|
"{role_mention} has `{count}` members with non-zero balance, "
|
||||||
"with a total balance of {coin_emoji}**{total}**."
|
"with a total balance of {coin_emoji}**{total}**."
|
||||||
)).format(
|
)).format(
|
||||||
count=len(targets),
|
count=count,
|
||||||
role_mention=role.mention,
|
role_mention=role.mention,
|
||||||
total=sum(row['coins'] for row in rows),
|
total=total,
|
||||||
coin_emoji=cemoji
|
coin_emoji=cemoji
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user