From 0da1659cd088d03669edc55f147c118f751fc00b Mon Sep 17 00:00:00 2001 From: Will Hunt Date: Fri, 25 Sep 2020 14:53:23 +0100 Subject: [PATCH 1/5] (not working) filter out appservices from mau count --- .../storage/databases/main/monthly_active_users.py | 7 ++++++- tests/storage/test_monthly_active_users.py | 11 +++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/synapse/storage/databases/main/monthly_active_users.py b/synapse/storage/databases/main/monthly_active_users.py index e0cedd1aacc9..138129ad30af 100644 --- a/synapse/storage/databases/main/monthly_active_users.py +++ b/synapse/storage/databases/main/monthly_active_users.py @@ -41,7 +41,12 @@ async def get_monthly_active_count(self) -> int: """ def _count_users(txn): - sql = "SELECT COALESCE(count(*), 0) FROM monthly_active_users" + # Ensure we only fetch native users + sql = """ + SELECT COALESCE(count(*), 0) + FROM monthly_active_users + INNER JOIN users ON monthly_active_users.user_id=users.name AND appservice_id IS NULL; + """ txn.execute(sql) (count,) = txn.fetchone() return count diff --git a/tests/storage/test_monthly_active_users.py b/tests/storage/test_monthly_active_users.py index 643072bbaf18..4869103c2fd9 100644 --- a/tests/storage/test_monthly_active_users.py +++ b/tests/storage/test_monthly_active_users.py @@ -137,6 +137,17 @@ 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("@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" From 419dbd7d8ed06f928b376bac8bba5473c40787a7 Mon Sep 17 00:00:00 2001 From: Will Hunt Date: Mon, 28 Sep 2020 17:14:04 +0100 Subject: [PATCH 2/5] changelog --- changelog.d/8404.misc | 1 + 1 file changed, 1 insertion(+) create mode 100644 changelog.d/8404.misc diff --git a/changelog.d/8404.misc b/changelog.d/8404.misc new file mode 100644 index 000000000000..0249063e351e --- /dev/null +++ b/changelog.d/8404.misc @@ -0,0 +1 @@ +Do not include appservice users when calculating the total MAU for a server \ No newline at end of file From f0d1109109e1209034704362edf5b6e97cd19a29 Mon Sep 17 00:00:00 2001 From: Will Hunt Date: Mon, 28 Sep 2020 17:26:20 +0100 Subject: [PATCH 3/5] (now working) fix sql statement --- synapse/storage/databases/main/monthly_active_users.py | 4 +++- tests/storage/test_monthly_active_users.py | 10 +++++++--- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/synapse/storage/databases/main/monthly_active_users.py b/synapse/storage/databases/main/monthly_active_users.py index 138129ad30af..db6ad9331be2 100644 --- a/synapse/storage/databases/main/monthly_active_users.py +++ b/synapse/storage/databases/main/monthly_active_users.py @@ -45,7 +45,9 @@ def _count_users(txn): sql = """ SELECT COALESCE(count(*), 0) FROM monthly_active_users - INNER JOIN users ON monthly_active_users.user_id=users.name AND appservice_id IS NULL; + 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() diff --git a/tests/storage/test_monthly_active_users.py b/tests/storage/test_monthly_active_users.py index 4869103c2fd9..8d97b6d4cdf4 100644 --- a/tests/storage/test_monthly_active_users.py +++ b/tests/storage/test_monthly_active_users.py @@ -138,11 +138,15 @@ def test_can_insert_and_count_mau(self): 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")) + 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("@user:server") + d = self.store.upsert_monthly_active_user("@appservice_user:server") self.get_success(d) count = self.get_success(self.store.get_monthly_active_count()) @@ -394,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) From 791a7539b25f36a1cf9c34b0135268ca26d700d6 Mon Sep 17 00:00:00 2001 From: Andrew Morgan Date: Mon, 28 Sep 2020 17:43:28 +0100 Subject: [PATCH 4/5] Add a period to the changelog --- changelog.d/8404.misc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/changelog.d/8404.misc b/changelog.d/8404.misc index 0249063e351e..7aadded6c1dd 100644 --- a/changelog.d/8404.misc +++ b/changelog.d/8404.misc @@ -1 +1 @@ -Do not include appservice users when calculating the total MAU for a server \ No newline at end of file +Do not include appservice users when calculating the total MAU for a server. From 143a0cd193b9765230fca5cc46c4348ef09f755c Mon Sep 17 00:00:00 2001 From: Will Hunt Date: Tue, 29 Sep 2020 12:20:53 +0100 Subject: [PATCH 5/5] Update synapse/storage/databases/main/monthly_active_users.py Co-authored-by: Andrew Morgan <1342360+anoadragon453@users.noreply.github.com> --- synapse/storage/databases/main/monthly_active_users.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/synapse/storage/databases/main/monthly_active_users.py b/synapse/storage/databases/main/monthly_active_users.py index db6ad9331be2..e93aad33cd89 100644 --- a/synapse/storage/databases/main/monthly_active_users.py +++ b/synapse/storage/databases/main/monthly_active_users.py @@ -41,7 +41,7 @@ async def get_monthly_active_count(self) -> int: """ def _count_users(txn): - # Ensure we only fetch native users + # Exclude app service users sql = """ SELECT COALESCE(count(*), 0) FROM monthly_active_users