diff --git a/PresenceTracker.py b/PresenceTracker.py index e7e3eb2..b1d0337 100644 --- a/PresenceTracker.py +++ b/PresenceTracker.py @@ -116,6 +116,17 @@ class PresenceTracker: return {row[0]: row[1:3] for row in res} + def top_games(self) -> dict[str, int]: + """Returns keys - names, values - spent hours""" + res = self.db.execute( + """select a.name, round(sum(end_time - presence_journal.start_time) / 3600.0) as total + from presence_journal left join activities a on a.id = presence_journal.activity_name_id + group by activity_name_id + order by total desc + limit 50;""").fetchall() + + return {row[0]: row[1] for row in res} + @staticmethod def default_end_time(end_time: datetime | None) -> datetime: return datetime.now() if end_time is None else end_time diff --git a/extensions/Frontend.py b/extensions/Frontend.py index db3877a..13c4152 100644 --- a/extensions/Frontend.py +++ b/extensions/Frontend.py @@ -79,6 +79,26 @@ class Frontend(commands.Cog): ) await interaction.response.send_message(embed=embed) + @discord.app_commands.command() + async def top_games(self, interaction: discord.Interaction): + """ + Mostly played games by all users + + :param interaction: + :return: + """ + + logger.trace(f'Invoking /top_games for {interaction.user}') + + data = self.bot.activity_tracker.top_games() + embed = build_table_embed( + ('Games', *data.keys()), + ('Spent hours', *data.values()), + name='Total spent hours per games by all users', + description=f'Total spent {sum(data.values())} hours' + ) + await interaction.response.send_message(embed=embed) + async def setup(bot: 'Bot'): await bot.add_cog(Frontend(bot))