2018-07-31 11:36:24 -04:00
|
|
|
from twisted.internet import defer
|
|
|
|
|
|
|
|
from ._base import SQLBaseStore
|
|
|
|
|
|
|
|
|
|
|
|
class MonthlyActiveUsersStore(SQLBaseStore):
|
|
|
|
def __init__(self, hs):
|
|
|
|
super(MonthlyActiveUsersStore, self).__init__(None, hs)
|
|
|
|
self._clock = hs.get_clock()
|
|
|
|
|
|
|
|
def reap_monthly_active_users(self):
|
|
|
|
"""
|
|
|
|
Cleans out monthly active user table to ensure that no stale
|
|
|
|
entries exist.
|
|
|
|
Return:
|
|
|
|
defered, no return type
|
|
|
|
"""
|
|
|
|
def _reap_users(txn):
|
|
|
|
thirty_days_ago = (
|
|
|
|
int(self._clock.time_msec()) - (1000 * 60 * 60 * 24 * 30)
|
|
|
|
)
|
|
|
|
sql = "DELETE FROM monthly_active_users WHERE timestamp < ?"
|
|
|
|
txn.execute(sql, (thirty_days_ago,))
|
|
|
|
|
|
|
|
return self.runInteraction("reap_monthly_active_users", _reap_users)
|
|
|
|
|
|
|
|
def get_monthly_active_count(self):
|
|
|
|
"""
|
|
|
|
Generates current count of monthly active users.abs
|
|
|
|
return:
|
|
|
|
defered resolves to int
|
|
|
|
"""
|
|
|
|
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-02 05:59:58 -04:00
|
|
|
return 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
|
|
|
)
|
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
|
|
|
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 07:03:57 -04:00
|
|
|
defer.returnValue(bool(user_present))
|