commit ca60b787c7859c8f33b9e18e5bd7b5b33a144674
parent b494099d65dc6543a0a7095f88279b043bd917ab
Author: ashermorgan <59518073+ashermorgan@users.noreply.github.com>
Date: Sun, 4 Jul 2021 18:09:34 -0700
Move historical contributor logic to models.py
Diffstat:
2 files changed, 45 insertions(+), 18 deletions(-)
diff --git a/src/analyticsCog.py b/src/analyticsCog.py
@@ -53,10 +53,6 @@ class Analytics(commands.Cog):
tmp = tempfile.NamedTemporaryFile(delete=False, suffix=".png")
tmp.close()
- # Get stats
- stats = countdown.progress()
- contributors = countdown.contributors()
-
# Create embed
embed=discord.Embed(title=":busts_in_silhouette: Countdown Contributors", color=COLORS["embed"])
@@ -68,23 +64,15 @@ class Analytics(commands.Cog):
ax.set_ylabel("Percentage of Contributions")
ax.yaxis.set_major_formatter(PercentFormatter())
- # Get historical contributor data
- authors = {}
- for author in contributors:
- authors[author["author"]] = [{"progress":0, "percentage":0, "total":0}]
- for message in countdown.messages:
- for author in authors:
- if (author == message.author_id):
- authors[author] += [{"progress":(stats["total"] - message.number), "percentage":(authors[author][-1]["total"] + 1)/(stats["total"] - message.number + 1) * 100, "total":authors[author][-1]["total"] + 1}]
- else:
- authors[author] += [{"progress":(stats["total"] - message.number), "percentage":(authors[author][-1]["total"] + 0)/(stats["total"] - message.number + 1) * 100, "total":authors[author][-1]["total"] + 0}]
+ # Get stats
+ contributors = countdown.historicalContributors()
# Plot data and add legend
- for author in list(authors.keys())[:min(len(authors), 15)]:
+ for author in list(contributors.keys())[:min(len(contributors), 15)]:
# Top 15 contributors get included in the legend
- ax.plot([x["progress"] for x in authors[author]], [x["percentage"] for x in authors[author]], label=await getUsername(self.bot, author))
- for author in list(authors.keys())[15:max(len(authors), 15)]:
- ax.plot([x["progress"] for x in authors[author]], [x["percentage"] for x in authors[author]])
+ ax.plot([x["progress"] for x in contributors[author]], [x["percentage"] * 100 for x in contributors[author]], label=await getUsername(self.bot, author))
+ for author in list(contributors.keys())[15:max(len(contributors), 15)]:
+ ax.plot([x["progress"] for x in contributors[author]], [x["percentage"] * 100 for x in contributors[author]])
ax.legend(bbox_to_anchor=(1,1.025), loc="upper left")
# Save graph
@@ -98,6 +86,9 @@ class Analytics(commands.Cog):
# Create figure
fig, ax = plt.subplots()
+ # Get stats
+ contributors = countdown.contributors()
+
# Add data to graph
x = [x["author"] for x in contributors]
y = [x["contributions"] for x in contributors]
diff --git a/src/models.py b/src/models.py
@@ -162,6 +162,42 @@ class Countdown(Base):
# Return contributors
return contributors
+ def historicalContributors(self):
+ """
+ Get countdown contributor statistics over time
+
+ Returns
+ -------
+ dict
+ A dictionary of historical contributor statistics
+ """
+
+ # Make sure countdown has at least two messages
+ if (len(self.messages) == 0):
+ raise EmptyCountdownError()
+
+ # Get contributors
+ contributors = self.contributors()
+
+ # Get countdown total
+ total = self.messages[0].number
+
+ # Initialize result dictionary
+ result = {}
+ for contributor in contributors:
+ result[contributor["author"]] = [{"progress":0, "percentage":0, "total":0}]
+
+ # Populate result dictionary
+ for message in self.messages:
+ for author in result:
+ if (author == message.author_id):
+ result[author] += [{"progress":(total - message.number), "percentage":(result[author][-1]["total"] + 1)/(total - message.number + 1), "total":result[author][-1]["total"] + 1}]
+ else:
+ result[author] += [{"progress":(total - message.number), "percentage":(result[author][-1]["total"] + 0)/(total - message.number + 1), "total":result[author][-1]["total"] + 0}]
+
+ # Return result
+ return result
+
def eta(self, period=timedelta(days=1)):
"""
Get countdown eta statistics