Skip to content

Commit

Permalink
refactor bbt stats code
Browse files Browse the repository at this point in the history
  • Loading branch information
howardt12345 committed Jan 11, 2025
1 parent baebdc4 commit 5e0e487
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 27 deletions.
39 changes: 25 additions & 14 deletions commands/bbt_count/bbt_count.py
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,6 @@ async def bbt_count_leaderboard(interaction: discord.Interaction, year: int = No
paginator = GenericPaginator(embeds)
await interaction.followup.send(embed=embeds[0], view=paginator)


@bbt_count.command(name="stats", description="Get the stats for a user for a given year")
@app_commands.describe(user="User to get stats for (optional, default to self)")
@app_commands.describe(year="Year to get stats for (optional, default to current year)")
Expand All @@ -409,34 +408,45 @@ async def bbt_count_stats(
(datetime.datetime(year=year, month=1, day=1) if year else interaction.created_at),
group_by_location,
)

if not stats:
embed = discord.Embed(
title="No stats found",
description="No bubble tea entries found for the specified period.",
color=discord.Color.red(),
)
await interaction.followup.send(embed=embed)
return

monthly_counts = get_bubble_tea_monthly_counts(
user_id,
(datetime.datetime(year=year, month=1, day=1) if year else interaction.created_at),
)
) or []

latest = (
get_latest_bubble_tea_entry(user_id)
if not year or (year and year == datetime.datetime.now().year)
else None
)

if group_by_location and stats.get('location_stats'):
# Split location stats into chunks of 10
location_stats = list(stats['location_stats'].items())
chunks = [location_stats[i:i + 10] for i in range(0, len(location_stats), 10)]
total_entries = sum([len(entry.get("prices_list", [])) for entry in stats])

if group_by_location:
chunks = [stats[i:i + 10] for i in range(0, len(stats), 10)]
embeds = []

for i, chunk in enumerate(chunks):
embed = bbt_stats_embed(
user_id,
{**stats, 'location_stats': dict(chunk)},
year,
group_by_location,
monthly_counts if i == 0 else None, # Only show monthly counts on first page
latest if i == 0 else None, # Only show latest on first page
interaction.created_at.astimezone().tzinfo,
user_id=user_id,
entries=chunk,
year=year,
group_by_location=group_by_location,
monthly_counts=None,
latest=None,
timezone=interaction.created_at.astimezone().tzinfo,
total_entries=total_entries,
)
embed.set_footer(text=f"Page {i+1} of {len(chunks)} • Total locations: {len(location_stats)}")
embed.set_footer(text=f"Page {i+1} of {len(chunks)} • Total locations: {len(stats)}")
embeds.append(embed)

paginator = GenericPaginator(embeds)
Expand All @@ -450,6 +460,7 @@ async def bbt_count_stats(
monthly_counts,
latest,
interaction.created_at.astimezone().tzinfo,
total_entries=total_entries,
)
await interaction.followup.send(embed=embed)

Expand Down
29 changes: 16 additions & 13 deletions commands/bbt_count/embeds.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,16 +165,17 @@ def bbt_stats_embed(
monthly_counts: list[dict],
latest: dict,
timezone: datetime.tzinfo,
total_entries: int = None,
):
total_count = sum([len(entry.get("prices_list", 0)) for entry in entries])
current_count = sum([len(entry.get("prices_list", [])) for entry in entries])
embed = discord.Embed(
title=f"Bubble tea stats {f'for {year}' if year else 'for the past year'} {'grouped by location ' if group_by_location else ''}🧋",
color=discord.Color.green(),
)
embed.description = (
f"For <@{user_id}>: **{total_count} total entries**\n{average_year_string(year, total_count)}\n\n"
f"For <@{user_id}>: **{total_entries or current_count} total entries**\n{average_year_string(year, total_entries or current_count)}\n\n"
)
if total_count > 0:
if current_count > 0:
embed.description += f"{'__Total costs__' if not group_by_location else '__Costs by location__'}:\n"
embed.description += "\n".join(
[
Expand All @@ -190,16 +191,18 @@ def bbt_stats_embed(
for entry in entries
]
)
current_year = year if year else datetime.datetime.now().year
embed.description += "\n\n__Monthly counts__:\n"
embed.description += "\n".join(
[
f"**{calendar.month_name[monthly_count.get('month', 0)]}**: {monthly_count.get('entry_count')} entries ({average_month_string(current_year, monthly_count.get('month', 0), monthly_count.get('entry_count'))})"
+ ('\n- ' + rating_string(monthly_count) if monthly_count.get("average_rating") else "")
for monthly_count in monthly_counts
]
)
if latest:

if monthly_counts and not group_by_location:
current_year = year if year else datetime.datetime.now().year
embed.description += "\n\n__Monthly counts__:\n"
embed.description += "\n".join(
[
f"**{calendar.month_name[monthly_count.get('month', 0)]}**: {monthly_count.get('entry_count')} entries ({average_month_string(current_year, monthly_count.get('month', 0), monthly_count.get('entry_count'))})"
+ ('\n- ' + rating_string(monthly_count) if monthly_count.get("average_rating") else "")
for monthly_count in monthly_counts
]
)
if latest and not group_by_location:
embed.description += "\n\n**Latest entry**:\n"
embed.description += entry_string(latest, timezone)
return embed

0 comments on commit 5e0e487

Please sign in to comment.