aboutsummaryrefslogtreecommitdiffstats
path: root/pydis_site/apps/api/models
diff options
context:
space:
mode:
authorGravatar Joe Banks <[email protected]>2020-10-31 00:01:34 +0000
committerGravatar GitHub <[email protected]>2020-10-31 00:01:34 +0000
commitc28aab2ffa7c3fc5546125d92e03b43a09752ab2 (patch)
tree3c85f9e4d6eed6d3401189029773673f5c834ab2 /pydis_site/apps/api/models
parentUse referral code for Linode (diff)
parentIndent metricity block query correctly (diff)
Merge pull request #418 from python-discord/metricity/add-activity-blocks
Add activity blocks to Metricity data endpoint
Diffstat (limited to 'pydis_site/apps/api/models')
-rw-r--r--pydis_site/apps/api/models/bot/metricity.py31
1 files changed, 31 insertions, 0 deletions
diff --git a/pydis_site/apps/api/models/bot/metricity.py b/pydis_site/apps/api/models/bot/metricity.py
index eed1deb4..e7fc92fc 100644
--- a/pydis_site/apps/api/models/bot/metricity.py
+++ b/pydis_site/apps/api/models/bot/metricity.py
@@ -1,5 +1,7 @@
from django.db import connections
+BLOCK_INTERVAL = 10 * 60 # 10 minute blocks
+
class NotFound(Exception):
"""Raised when an entity cannot be found."""
@@ -43,3 +45,32 @@ class Metricity:
raise NotFound()
return values[0]
+
+ def total_message_blocks(self, user_id: str) -> int:
+ """
+ Query number of 10 minute blocks during which the user has been active.
+
+ This metric prevents users from spamming to achieve the message total threshold.
+ """
+ self.cursor.execute(
+ """
+ SELECT
+ COUNT(*)
+ FROM (
+ SELECT
+ (floor((extract('epoch' from created_at) / %d )) * %d) AS interval
+ FROM messages
+ WHERE
+ author_id='%s'
+ AND NOT is_deleted
+ GROUP BY interval
+ ) block_query;
+ """,
+ [BLOCK_INTERVAL, BLOCK_INTERVAL, user_id]
+ )
+ values = self.cursor.fetchone()
+
+ if not values:
+ raise NotFound()
+
+ return values[0]