aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar Johannes Christ <[email protected]>2021-11-23 13:00:11 +0100
committerGravatar GitHub <[email protected]>2021-11-23 13:00:11 +0100
commitd1a051e6aa7df063552d626318ba236de11661b4 (patch)
treef7f0355bd539a9bdd9622c6de9877d0949fc3aeb
parentMerge pull request #625 from python-discord/filter-redirects (diff)
parentAdd metricity test for users >1k messages (diff)
Merge pull request #627 from python-discord/don't-always-calc-activity-blocks
Don't always calc activity blocks
-rw-r--r--pydis_site/apps/api/tests/test_users.py23
-rw-r--r--pydis_site/apps/api/viewsets/bot/user.py8
2 files changed, 28 insertions, 3 deletions
diff --git a/pydis_site/apps/api/tests/test_users.py b/pydis_site/apps/api/tests/test_users.py
index 295bcf64..81bfd43b 100644
--- a/pydis_site/apps/api/tests/test_users.py
+++ b/pydis_site/apps/api/tests/test_users.py
@@ -408,7 +408,7 @@ class UserMetricityTests(AuthenticatedAPITestCase):
in_guild=True,
)
- def test_get_metricity_data(self):
+ def test_get_metricity_data_under_1k(self):
# Given
joined_at = "foo"
total_messages = 1
@@ -421,13 +421,32 @@ class UserMetricityTests(AuthenticatedAPITestCase):
# Then
self.assertEqual(response.status_code, 200)
- self.assertEqual(response.json(), {
+ self.assertCountEqual(response.json(), {
"joined_at": joined_at,
"total_messages": total_messages,
"voice_banned": False,
"activity_blocks": total_blocks
})
+ def test_get_metricity_data_over_1k(self):
+ # Given
+ joined_at = "foo"
+ total_messages = 1001
+ total_blocks = 1001
+ self.mock_metricity_user(joined_at, total_messages, total_blocks, [])
+
+ # When
+ url = reverse('api:bot:user-metricity-data', args=[0])
+ response = self.client.get(url)
+
+ # Then
+ self.assertEqual(response.status_code, 200)
+ self.assertCountEqual(response.json(), {
+ "joined_at": joined_at,
+ "total_messages": total_messages,
+ "voice_banned": False,
+ })
+
def test_no_metricity_user(self):
# Given
self.mock_no_metricity_user()
diff --git a/pydis_site/apps/api/viewsets/bot/user.py b/pydis_site/apps/api/viewsets/bot/user.py
index 22d13dc4..ed661323 100644
--- a/pydis_site/apps/api/viewsets/bot/user.py
+++ b/pydis_site/apps/api/viewsets/bot/user.py
@@ -271,9 +271,15 @@ class UserViewSet(ModelViewSet):
with Metricity() as metricity:
try:
data = metricity.user(user.id)
+
data["total_messages"] = metricity.total_messages(user.id)
+ if data["total_messages"] < 1000:
+ # Only calculate and return activity_blocks if the user has a small amount
+ # of messages, as calculating activity_blocks is expensive.
+ # 1000 message chosen as an arbitrarily large number.
+ data["activity_blocks"] = metricity.total_message_blocks(user.id)
+
data["voice_banned"] = voice_banned
- data["activity_blocks"] = metricity.total_message_blocks(user.id)
return Response(data, status=status.HTTP_200_OK)
except NotFoundError:
return Response(dict(detail="User not found in metricity"),