2018-08-03 08:49:53 -04:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# Copyright 2018 New Vector
|
|
|
|
#
|
|
|
|
# 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.
|
|
|
|
|
2018-07-31 11:36:24 -04:00
|
|
|
from twisted.internet import defer
|
2018-08-03 08:49:53 -04:00
|
|
|
|
|
|
|
from synapse.util.caches.descriptors import cached, cachedInlineCallbacks
|
2018-07-31 11:36:24 -04:00
|
|
|
|
|
|
|
from ._base import SQLBaseStore
|
|
|
|
|
2018-08-03 12:55:50 -04:00
|
|
|
# Number of msec of granularity to store the monthly_active_user timestamp
|
|
|
|
# This means it is not necessary to update the table on every request
|
|
|
|
LAST_SEEN_GRANULARITY = 60 * 60 * 1000
|
|
|
|
|
2018-07-31 11:36:24 -04:00
|
|
|
|
|
|
|
class MonthlyActiveUsersStore(SQLBaseStore):
|
2018-08-02 08:47:19 -04:00
|
|
|
def __init__(self, dbconn, hs):
|
2018-07-31 11:36:24 -04:00
|
|
|
super(MonthlyActiveUsersStore, self).__init__(None, hs)
|
|
|
|
self._clock = hs.get_clock()
|
2018-08-03 08:49:53 -04:00
|
|
|
self.hs = hs
|
2018-07-31 11:36:24 -04:00
|
|
|
|
|
|
|
def reap_monthly_active_users(self):
|
|
|
|
"""
|
|
|
|
Cleans out monthly active user table to ensure that no stale
|
|
|
|
entries exist.
|
2018-08-03 12:55:50 -04:00
|
|
|
|
|
|
|
Returns:
|
|
|
|
Deferred[]
|
2018-07-31 11:36:24 -04:00
|
|
|
"""
|
|
|
|
def _reap_users(txn):
|
2018-08-03 08:49:53 -04:00
|
|
|
|
2018-07-31 11:36:24 -04:00
|
|
|
thirty_days_ago = (
|
|
|
|
int(self._clock.time_msec()) - (1000 * 60 * 60 * 24 * 30)
|
|
|
|
)
|
2018-08-02 17:41:05 -04:00
|
|
|
|
2018-08-03 08:49:53 -04:00
|
|
|
sql = "DELETE FROM monthly_active_users WHERE timestamp < ?"
|
2018-08-02 17:41:05 -04:00
|
|
|
|
2018-08-03 08:49:53 -04:00
|
|
|
txn.execute(sql, (thirty_days_ago,))
|
|
|
|
sql = """
|
|
|
|
DELETE FROM monthly_active_users
|
|
|
|
ORDER BY timestamp desc
|
|
|
|
LIMIT -1 OFFSET ?
|
|
|
|
"""
|
|
|
|
txn.execute(sql, (self.hs.config.max_mau_value,))
|
2018-08-02 17:41:05 -04:00
|
|
|
|
2018-08-03 12:55:50 -04:00
|
|
|
res = yield self.runInteraction("reap_monthly_active_users", _reap_users)
|
2018-08-03 08:49:53 -04:00
|
|
|
# It seems poor to invalidate the whole cache, Postgres supports
|
|
|
|
# 'Returning' which would allow me to invalidate only the
|
|
|
|
# specific users, but sqlite has no way to do this and instead
|
|
|
|
# I would need to SELECT and the DELETE which without locking
|
|
|
|
# is racy.
|
|
|
|
# Have resolved to invalidate the whole cache for now and do
|
|
|
|
# something about it if and when the perf becomes significant
|
2018-08-03 12:55:50 -04:00
|
|
|
self._user_last_seen_monthly_active.invalidate_all()
|
2018-08-03 08:49:53 -04:00
|
|
|
self.get_monthly_active_count.invalidate_all()
|
|
|
|
return res
|
2018-08-02 17:41:05 -04:00
|
|
|
|
2018-08-03 08:49:53 -04:00
|
|
|
@cached(num_args=0)
|
2018-07-31 11:36:24 -04:00
|
|
|
def get_monthly_active_count(self):
|
2018-08-03 12:55:50 -04:00
|
|
|
"""Generates current count of monthly active users.abs
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
Defered[int]: Number of current monthly active users
|
2018-07-31 11:36:24 -04:00
|
|
|
"""
|
2018-08-03 08:49:53 -04:00
|
|
|
|
2018-07-31 11:36:24 -04:00
|
|
|
def _count_users(txn):
|
2018-08-01 07:03:57 -04:00
|
|
|
sql = "SELECT COALESCE(count(*), 0) FROM monthly_active_users"
|
|
|
|
|
2018-07-31 11:36:24 -04:00
|
|
|
txn.execute(sql)
|
|
|
|
count, = txn.fetchone()
|
|
|
|
return count
|
|
|
|
return self.runInteraction("count_users", _count_users)
|
|
|
|
|
2018-08-02 05:59:58 -04:00
|
|
|
def upsert_monthly_active_user(self, user_id):
|
2018-07-31 11:36:24 -04:00
|
|
|
"""
|
|
|
|
Updates or inserts monthly active user member
|
|
|
|
Arguments:
|
|
|
|
user_id (str): user to add/update
|
2018-08-03 12:55:50 -04:00
|
|
|
Deferred[bool]: True if a new entry was created, False if an
|
2018-08-02 08:47:19 -04:00
|
|
|
existing one was updated.
|
2018-07-31 11:36:24 -04:00
|
|
|
"""
|
2018-08-03 12:55:50 -04:00
|
|
|
is_insert = self._simple_upsert(
|
2018-07-31 11:36:24 -04:00
|
|
|
desc="upsert_monthly_active_user",
|
|
|
|
table="monthly_active_users",
|
2018-08-02 05:59:58 -04:00
|
|
|
keyvalues={
|
2018-08-01 12:54:37 -04:00
|
|
|
"user_id": user_id,
|
2018-07-31 11:36:24 -04:00
|
|
|
},
|
2018-08-02 05:59:58 -04:00
|
|
|
values={
|
|
|
|
"timestamp": int(self._clock.time_msec()),
|
|
|
|
},
|
|
|
|
lock=False,
|
2018-07-31 11:36:24 -04:00
|
|
|
)
|
2018-08-03 12:55:50 -04:00
|
|
|
if is_insert:
|
|
|
|
self._user_last_seen_monthly_active.invalidate((user_id,))
|
|
|
|
self.get_monthly_active_count.invalidate(())
|
2018-07-31 11:36:24 -04:00
|
|
|
|
2018-08-02 17:41:05 -04:00
|
|
|
@cachedInlineCallbacks(num_args=1)
|
2018-08-03 12:55:50 -04:00
|
|
|
def _user_last_seen_monthly_active(self, user_id):
|
2018-07-31 11:36:24 -04:00
|
|
|
"""
|
|
|
|
Checks if a given user is part of the monthly active user group
|
|
|
|
Arguments:
|
|
|
|
user_id (str): user to add/update
|
|
|
|
Return:
|
2018-08-03 12:55:50 -04:00
|
|
|
int : timestamp since last seen, None if never seen
|
|
|
|
|
2018-07-31 11:36:24 -04:00
|
|
|
"""
|
2018-08-03 12:55:50 -04:00
|
|
|
result = yield self._simple_select_onecol(
|
2018-07-31 11:36:24 -04:00
|
|
|
table="monthly_active_users",
|
|
|
|
keyvalues={
|
|
|
|
"user_id": user_id,
|
|
|
|
},
|
2018-08-03 12:55:50 -04:00
|
|
|
retcol="timestamp",
|
|
|
|
desc="_user_last_seen_monthly_active",
|
2018-07-31 11:36:24 -04:00
|
|
|
)
|
2018-08-03 12:55:50 -04:00
|
|
|
timestamp = None
|
|
|
|
if len(result) > 0:
|
|
|
|
timestamp = result[0]
|
|
|
|
defer.returnValue(timestamp)
|
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
|
|
|
def populate_monthly_active_users(self, user_id):
|
|
|
|
"""Checks on the state of monthly active user limits and optionally
|
|
|
|
add the user to the monthly active tables
|
|
|
|
|
|
|
|
Args:
|
|
|
|
user_id(str): the user_id to query
|
|
|
|
"""
|
|
|
|
|
|
|
|
if self.hs.config.limit_usage_by_mau:
|
|
|
|
last_seen_timestamp = yield self._user_last_seen_monthly_active(user_id)
|
|
|
|
now = self.hs.get_clock().time_msec()
|
|
|
|
|
|
|
|
if last_seen_timestamp is None:
|
|
|
|
count = yield self.get_monthly_active_count()
|
|
|
|
if count < self.hs.config.max_mau_value:
|
|
|
|
yield self.upsert_monthly_active_user(user_id)
|
|
|
|
elif now - last_seen_timestamp > LAST_SEEN_GRANULARITY:
|
|
|
|
yield self.upsert_monthly_active_user(user_id)
|