Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Commit

Permalink
add monthly active users to phonehome stats (#5252)
Browse files Browse the repository at this point in the history
* add monthly active users to phonehome stats
  • Loading branch information
neilisfragile authored Jun 10, 2019
1 parent abce00f commit 94dac0f
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 15 deletions.
1 change: 1 addition & 0 deletions changelog.d/5252.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add monthly active users to phonehome stats.
1 change: 1 addition & 0 deletions synapse/app/homeserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -541,6 +541,7 @@ def phone_stats_home():
stats["total_room_count"] = room_count

stats["daily_active_users"] = yield hs.get_datastore().count_daily_users()
stats["monthly_active_users"] = yield hs.get_datastore().count_monthly_users()
stats["daily_active_rooms"] = yield hs.get_datastore().count_daily_active_rooms()
stats["daily_messages"] = yield hs.get_datastore().count_daily_messages()

Expand Down
44 changes: 29 additions & 15 deletions synapse/storage/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -279,23 +279,37 @@ def count_daily_users(self):
"""
Counts the number of users who used this homeserver in the last 24 hours.
"""
yesterday = int(self._clock.time_msec()) - (1000 * 60 * 60 * 24)
return self.runInteraction("count_daily_users", self._count_users, yesterday,)

def _count_users(txn):
yesterday = int(self._clock.time_msec()) - (1000 * 60 * 60 * 24)

sql = """
SELECT COALESCE(count(*), 0) FROM (
SELECT user_id FROM user_ips
WHERE last_seen > ?
GROUP BY user_id
) u
"""

txn.execute(sql, (yesterday,))
count, = txn.fetchone()
return count
def count_monthly_users(self):
"""
Counts the number of users who used this homeserver in the last 30 days.
Note this method is intended for phonehome metrics only and is different
from the mau figure in synapse.storage.monthly_active_users which,
amongst other things, includes a 3 day grace period before a user counts.
"""
thirty_days_ago = int(self._clock.time_msec()) - (1000 * 60 * 60 * 24 * 30)
return self.runInteraction(
"count_monthly_users",
self._count_users,
thirty_days_ago,
)

return self.runInteraction("count_users", _count_users)
def _count_users(self, txn, time_from):
"""
Returns number of users seen in the past time_from period
"""
sql = """
SELECT COALESCE(count(*), 0) FROM (
SELECT user_id FROM user_ips
WHERE last_seen > ?
GROUP BY user_id
) u
"""
txn.execute(sql, (time_from,))
count, = txn.fetchone()
return count

def count_r30_users(self):
"""
Expand Down

0 comments on commit 94dac0f

Please sign in to comment.