countdown-bot

A Discord bot that runs countdown games and generates analytics
git clone https://git.ashermorgan.net/countdown-bot/
Log | Files | Refs | README

commit 27c5884ccf6b7887a3ea175f54a38fd3865a08e3
parent 0bee3e763e4da2a491f9197a212ba90e3dc4c910
Author: ashermorgan <59518073+ashermorgan@users.noreply.github.com>
Date:   Tue, 10 Aug 2021 16:44:30 -0700

Add longest break info to progress embed

Diffstat:
Msrc/analyticsCog.py | 5+++++
Msrc/models.py | 37+++++++++++++++++++++++++++++++++++++
2 files changed, 42 insertions(+), 0 deletions(-)

diff --git a/src/analyticsCog.py b/src/analyticsCog.py @@ -388,6 +388,7 @@ class Analytics(commands.Cog): # Get progress stats stats = countdown.progress() + breakStats = countdown.longestBreak() # Create figure fig, ax = plt.subplots() @@ -405,6 +406,9 @@ class Analytics(commands.Cog): file = discord.File(tmp.name, filename="image.png") # Calculate embed data + longestBreakDuration = timedelta(days=breakStats['duration'].days, seconds=breakStats['duration'].seconds) + longestBreakStart = breakStats['start'].date() + longestBreakEnd = breakStats['end'].date() start = (stats["start"] + timedelta(hours=countdown.timezone)).date() startDiff = (datetime.utcnow() - stats["start"]).days end = (stats["eta"] + timedelta(hours=countdown.timezone)).date() @@ -414,6 +418,7 @@ class Analytics(commands.Cog): embed.description = f"**Countdown Channel:** <#{countdown.id}>\n\n" embed.description += f"**Progress:** {stats['total'] - stats['current']:,} / {stats['total']:,} ({round(stats['percentage'], 1)}%)\n" embed.description += f"**Average Progress per Day:** {round(stats['rate']):,}\n" + embed.description += f"**Longest Break:** {longestBreakDuration} ({longestBreakStart} to {longestBreakEnd})\n" embed.description += f"**Start Date:** {start} ({startDiff:,} days ago)\n" if endDiff < timedelta(seconds=0): embed.description += f"**End Date:** {end} ({(-1 * endDiff).days:,} days ago)\n" diff --git a/src/models.py b/src/models.py @@ -362,6 +362,43 @@ class Countdown(Base): leaderboard = sorted(leaderboard, key=lambda x: x["points"], reverse=True) return leaderboard + def longestBreak(self): + """ + Get the longest countdown break + + Returns + ------- + dict + A dictionary containing information about the longest countdown break + """ + + # Make sure countdown has started + if (len(self.messages) == 0): + raise EmptyCountdownError() + + # Calculate longest break + breaks = [] + for i in range(0, len(self.messages) - 1): + breaks += [self.messages[i+1].timestamp - self.messages[i].timestamp] + if (self.messages[-1].number == 0): + breaks += [timedelta(seconds=0)] + else: + breaks += [datetime.utcnow() - self.messages[-1].timestamp] + longestBreak = max(breaks) + index = breaks.index(longestBreak) + start = self.messages[index].timestamp + timedelta(hours=self.timezone) + if (index == len(self.messages) - 1): + end = datetime.utcnow() + timedelta(hours=self.timezone) + else: + end = self.messages[index + 1].timestamp + timedelta(hours=self.timezone) + + # Return statistics + return { + 'duration': longestBreak, + 'start': start, + 'end': end, + } + def progress(self): """ Get countdown progress statistics