mirror of
https://git.anonymousland.org/anonymousland/synapse.git
synced 2025-05-02 19:54:53 -04:00
api into monthly_active_users table
This commit is contained in:
parent
82977477e3
commit
6ef983ce5c
6 changed files with 160 additions and 2 deletions
89
synapse/storage/monthly_active_users.py
Normal file
89
synapse/storage/monthly_active_users.py
Normal file
|
@ -0,0 +1,89 @@
|
|||
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):
|
||||
sql = """
|
||||
SELECT COALESCE(count(*), 0) FROM (
|
||||
SELECT user_id FROM monthly_active_users
|
||||
) u
|
||||
"""
|
||||
txn.execute(sql)
|
||||
count, = txn.fetchone()
|
||||
return count
|
||||
return self.runInteraction("count_users", _count_users)
|
||||
|
||||
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_upsert(
|
||||
desc="upsert_monthly_active_user",
|
||||
table="monthly_active_users",
|
||||
keyvalues={
|
||||
"user_id": user_id,
|
||||
},
|
||||
values={
|
||||
"timestamp": int(self._clock.time_msec()),
|
||||
},
|
||||
lock=False,
|
||||
)
|
||||
|
||||
def clean_out_monthly_active_users(self):
|
||||
pass
|
||||
|
||||
@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",
|
||||
)
|
||||
# jeff = self.cursor_to_dict(res)
|
||||
result = False
|
||||
if user_present:
|
||||
result = True
|
||||
|
||||
defer.returnValue(
|
||||
result
|
||||
)
|
Loading…
Add table
Add a link
Reference in a new issue