MatrixSynapse/synapse/storage/monthly_active_users.py

125 lines
4.5 KiB
Python
Raw Normal View History

2018-07-31 17:36:24 +02:00
from twisted.internet import defer
2018-08-02 23:41:05 +02:00
from synapse.util.caches.descriptors import cachedInlineCallbacks
from synapse.storage.engines import PostgresEngine, Sqlite3Engine
2018-07-31 17:36:24 +02:00
from ._base import SQLBaseStore
class MonthlyActiveUsersStore(SQLBaseStore):
2018-08-02 14:47:19 +02:00
def __init__(self, dbconn, hs):
2018-07-31 17:36:24 +02:00
super(MonthlyActiveUsersStore, self).__init__(None, hs)
self._clock = hs.get_clock()
2018-08-02 00:24:38 +02:00
self.max_mau_value = hs.config.max_mau_value
2018-07-31 17:36:24 +02:00
def reap_monthly_active_users(self):
"""
Cleans out monthly active user table to ensure that no stale
entries exist.
Return:
2018-08-02 14:47:19 +02:00
Defered()
2018-07-31 17:36:24 +02:00
"""
def _reap_users(txn):
thirty_days_ago = (
int(self._clock.time_msec()) - (1000 * 60 * 60 * 24 * 30)
)
2018-08-02 23:41:05 +02:00
if isinstance(self.database_engine, PostgresEngine):
sql = """
DELETE FROM monthly_active_users
WHERE timestamp < ?
RETURNING user_id
"""
txn.execute(sql, (thirty_days_ago,))
res = txn.fetchall()
for r in res:
self.is_user_monthly_active.invalidate(r)
sql = """
DELETE FROM monthly_active_users
ORDER BY timestamp desc
LIMIT -1 OFFSET ?
RETURNING user_id
"""
txn.execute(sql, (self.max_mau_value,))
res = txn.fetchall()
for r in res:
self.is_user_monthly_active.invalidate(r)
print r
self.get_monthly_active_count.invalidate()
elif isinstance(self.database_engine, Sqlite3Engine):
sql = "DELETE FROM monthly_active_users WHERE timestamp < ?"
txn.execute(sql, (thirty_days_ago,))
sql = """
DELETE FROM monthly_active_users
ORDER BY timestamp desc
LIMIT -1 OFFSET ?
"""
txn.execute(sql, (self.max_mau_value,))
# It seems poor to invalidate the whole cache, but the alternative
2018-08-02 23:47:48 +02:00
# is to select then delete which has its own problems.
2018-08-02 23:41:05 +02:00
# It seems unlikely that anyone using this feature on large datasets
# would be using sqlite and if they are then there will be
# larger perf issues than this one to encourage an upgrade to postgres.
self.is_user_monthly_active.invalidate_all()
self.get_monthly_active_count.invalidate_all()
2018-07-31 17:36:24 +02:00
return self.runInteraction("reap_monthly_active_users", _reap_users)
2018-08-02 23:41:05 +02:00
@cachedInlineCallbacks(num_args=0)
2018-07-31 17:36:24 +02:00
def get_monthly_active_count(self):
"""
Generates current count of monthly active users.abs
2018-08-02 14:47:19 +02:00
Return:
Defered(int): Number of current monthly active users
2018-07-31 17:36:24 +02:00
"""
def _count_users(txn):
2018-08-01 13:03:57 +02:00
sql = "SELECT COALESCE(count(*), 0) FROM monthly_active_users"
2018-07-31 17:36:24 +02:00
txn.execute(sql)
count, = txn.fetchone()
return count
return self.runInteraction("count_users", _count_users)
def upsert_monthly_active_user(self, user_id):
2018-07-31 17:36:24 +02:00
"""
Updates or inserts monthly active user member
Arguments:
user_id (str): user to add/update
2018-08-02 14:47:19 +02:00
Deferred(bool): True if a new entry was created, False if an
existing one was updated.
2018-07-31 17:36:24 +02:00
"""
return self._simple_upsert(
2018-07-31 17:36:24 +02:00
desc="upsert_monthly_active_user",
table="monthly_active_users",
keyvalues={
"user_id": user_id,
2018-07-31 17:36:24 +02:00
},
values={
"timestamp": int(self._clock.time_msec()),
},
lock=False,
2018-07-31 17:36:24 +02:00
)
2018-08-02 23:41:05 +02:00
@cachedInlineCallbacks(num_args=1)
2018-07-31 17:36:24 +02:00
def is_user_monthly_active(self, user_id):
"""
Checks if a given user is part of the monthly active user group
Arguments:
user_id (str): user to add/update
Return:
bool : True if user part of group, False otherwise
"""
user_present = yield self._simple_select_onecol(
table="monthly_active_users",
keyvalues={
"user_id": user_id,
},
retcol="user_id",
desc="is_user_monthly_active",
)
2018-08-01 13:03:57 +02:00
defer.returnValue(bool(user_present))