diff --git a/synapse/storage/monthly_active_users.py b/synapse/storage/monthly_active_users.py
index 0741c7fa61..2337438c58 100644
--- a/synapse/storage/monthly_active_users.py
+++ b/synapse/storage/monthly_active_users.py
@@ -21,41 +21,9 @@ class MonthlyActiveUsersStore(SQLBaseStore):
int(self._clock.time_msec()) - (1000 * 60 * 60 * 24 * 30)
)
- # Query deletes the union of users that have either:
- # * not visited in the last 30 days
- # * exceeded the total max_mau_value threshold. Where there is
- # an excess, more recent users are favoured - this is to cover
- # the case where the limit has been step change reduced.
- #
- sql = """
- DELETE FROM monthly_active_users
- WHERE user_id
- IN (
- SELECT * FROM (
- SELECT monthly_active_users.user_id
- FROM monthly_active_users
- LEFT JOIN (
- SELECT user_id, max(last_seen) AS last_seen
- FROM user_ips
- GROUP BY user_id
- ) AS uip ON uip.user_id=monthly_active_users.user_id
- ORDER BY uip.last_seen desc LIMIT -1 OFFSET ?
- )
- UNION
- SELECT * FROM (
- SELECT monthly_active_users.user_id
- FROM monthly_active_users
- LEFT JOIN (
- SELECT user_id, max(last_seen) AS last_seen
- FROM user_ips
- GROUP BY user_id
- ) AS uip ON uip.user_id=monthly_active_users.user_id
- WHERE uip.last_seen < ?
- )
- )
- """
+ sql = "DELETE FROM monthly_active_users WHERE timestamp < ?"
- txn.execute(sql, (self.max_mau_value, thirty_days_ago,))
+ txn.execute(sql, (thirty_days_ago,))
return self.runInteraction("reap_monthly_active_users", _reap_users)
@@ -73,18 +41,22 @@ class MonthlyActiveUsersStore(SQLBaseStore):
return count
return self.runInteraction("count_users", _count_users)
- def insert_monthly_active_user(self, user_id):
+ def upsert_monthly_active_user(self, user_id):
"""
Updates or inserts monthly active user member
Arguments:
user_id (str): user to add/update
"""
- return self._simple_insert(
- desc="insert_monthly_active_user",
+ return self._simple_upsert(
+ desc="upsert_monthly_active_user",
table="monthly_active_users",
- values={
+ keyvalues={
"user_id": user_id,
},
+ values={
+ "timestamp": int(self._clock.time_msec()),
+ },
+ lock=False,
)
@defer.inlineCallbacks
diff --git a/synapse/storage/schema/delta/51/monthly_active_users.sql b/synapse/storage/schema/delta/51/monthly_active_users.sql
index 590b1abab2..f2b6d3e31e 100644
--- a/synapse/storage/schema/delta/51/monthly_active_users.sql
+++ b/synapse/storage/schema/delta/51/monthly_active_users.sql
@@ -15,7 +15,9 @@
-- a table of monthly active users, for use where blocking based on mau limits
CREATE TABLE monthly_active_users (
- user_id TEXT NOT NULL
+ user_id TEXT NOT NULL,
+ timestamp BIGINT NOT NULL
);
CREATE UNIQUE INDEX monthly_active_users_users ON monthly_active_users(user_id);
+CREATE INDEX monthly_active_users_time_stamp ON monthly_active_users(timestamp);
|