diff options
author | Matthew Hodgson <matthew@matrix.org> | 2018-08-22 18:31:51 +0200 |
---|---|---|
committer | Matthew Hodgson <matthew@matrix.org> | 2018-08-22 18:31:51 +0200 |
commit | b61f299e7d6d817baa269562146030c443966678 (patch) | |
tree | 20ecb6d9a59c7789ed9981b027da8b37375e5a59 /synapse | |
parent | Merge branch 'master' into develop (diff) | |
download | synapse-b61f299e7d6d817baa269562146030c443966678.tar.xz |
Add mau_trial_days config option
Lets users have N days before they get counted as an MAU
Diffstat (limited to 'synapse')
-rw-r--r-- | synapse/config/server.py | 4 | ||||
-rw-r--r-- | synapse/storage/monthly_active_users.py | 16 | ||||
-rw-r--r-- | synapse/storage/prepare_database.py | 2 | ||||
-rw-r--r-- | synapse/storage/schema/delta/52/free_mau.sql | 18 |
4 files changed, 36 insertions, 4 deletions
diff --git a/synapse/config/server.py b/synapse/config/server.py index 68a612e594..40bbe9133c 100644 --- a/synapse/config/server.py +++ b/synapse/config/server.py @@ -80,6 +80,9 @@ class ServerConfig(Config): self.mau_limits_reserved_threepids = config.get( "mau_limit_reserved_threepids", [] ) + self.mau_trial_days = config.get( + "mau_trial_days", 0, + ) # Options to disable HS self.hs_disabled = config.get("hs_disabled", False) @@ -365,6 +368,7 @@ class ServerConfig(Config): # Enables monthly active user checking # limit_usage_by_mau: False # max_mau_value: 50 + # mau_trial_days: 2 # # Sometimes the server admin will want to ensure certain accounts are # never blocked by mau checking. These accounts are specified here. diff --git a/synapse/storage/monthly_active_users.py b/synapse/storage/monthly_active_users.py index 06f9a75a97..fa985bc005 100644 --- a/synapse/storage/monthly_active_users.py +++ b/synapse/storage/monthly_active_users.py @@ -139,10 +139,16 @@ class MonthlyActiveUsersStore(SQLBaseStore): Defered[int]: Number of current monthly active users """ + mau_trial_ms = self.hs.config.mau_trial_days * 24 * 60 * 60 * 1000 + def _count_users(txn): - sql = "SELECT COALESCE(count(*), 0) FROM monthly_active_users" + sql = """ + SELECT COALESCE(count(*), 0) + FROM monthly_active_users + WHERE timestamp - last_active > ? + """ - txn.execute(sql) + txn.execute(sql, mau_trial_ms) count, = txn.fetchone() return count return self.runInteraction("count_users", _count_users) @@ -155,6 +161,7 @@ class MonthlyActiveUsersStore(SQLBaseStore): Deferred[bool]: True if a new entry was created, False if an existing one was updated. """ + now = int(self._clock.time_msec()) is_insert = self._simple_upsert( desc="upsert_monthly_active_user", table="monthly_active_users", @@ -162,7 +169,10 @@ class MonthlyActiveUsersStore(SQLBaseStore): "user_id": user_id, }, values={ - "timestamp": int(self._clock.time_msec()), + "timestamp": now, + }, + insertion_values={ + "first_active": now, }, lock=False, ) diff --git a/synapse/storage/prepare_database.py b/synapse/storage/prepare_database.py index b364719312..bd740e1e45 100644 --- a/synapse/storage/prepare_database.py +++ b/synapse/storage/prepare_database.py @@ -25,7 +25,7 @@ logger = logging.getLogger(__name__) # Remember to update this number every time a change is made to database # schema files, so the users will be informed on server restarts. -SCHEMA_VERSION = 51 +SCHEMA_VERSION = 52 dir_path = os.path.abspath(os.path.dirname(__file__)) diff --git a/synapse/storage/schema/delta/52/free_mau.sql b/synapse/storage/schema/delta/52/free_mau.sql new file mode 100644 index 0000000000..c7b1d17fcc --- /dev/null +++ b/synapse/storage/schema/delta/52/free_mau.sql @@ -0,0 +1,18 @@ +/* Copyright 2018 New Vector Ltd + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +ALTER TABLE monthly_active_users ADD COLUMN first_active BIGINT NOT NULL; + +CREATE INDEX monthly_active_users_first_active ON monthly_active_users(first_active); |