Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Filter out appservices from mau count #8404

Merged
merged 5 commits into from
Sep 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/8404.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Do not include appservice users when calculating the total MAU for a server.
9 changes: 8 additions & 1 deletion synapse/storage/databases/main/monthly_active_users.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,14 @@ async def get_monthly_active_count(self) -> int:
"""

def _count_users(txn):
sql = "SELECT COALESCE(count(*), 0) FROM monthly_active_users"
# Exclude app service users
sql = """
SELECT COALESCE(count(*), 0)
FROM monthly_active_users
LEFT JOIN users
ON monthly_active_users.user_id=users.name
WHERE (users.appservice_id IS NULL OR users.appservice_id = '');
"""
txn.execute(sql)
(count,) = txn.fetchone()
return count
Expand Down
17 changes: 16 additions & 1 deletion tests/storage/test_monthly_active_users.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,21 @@ def test_can_insert_and_count_mau(self):
count = self.get_success(self.store.get_monthly_active_count())
self.assertEqual(count, 1)

def test_appservice_user_not_counted_in_mau(self):
self.get_success(
self.store.register_user(
user_id="@appservice_user:server", appservice_id="wibble"
)
)
count = self.get_success(self.store.get_monthly_active_count())
self.assertEqual(count, 0)

d = self.store.upsert_monthly_active_user("@appservice_user:server")
self.get_success(d)

count = self.get_success(self.store.get_monthly_active_count())
self.assertEqual(count, 0)

def test_user_last_seen_monthly_active(self):
user_id1 = "@user1:server"
user_id2 = "@user2:server"
Expand Down Expand Up @@ -383,7 +398,7 @@ def test_get_monthly_active_count_by_service(self):
self.get_success(self.store.upsert_monthly_active_user(appservice2_user1))

count = self.get_success(self.store.get_monthly_active_count())
self.assertEqual(count, 4)
self.assertEqual(count, 1)

d = self.store.get_monthly_active_count_by_service()
result = self.get_success(d)
Expand Down