diff --git a/synapse/storage/databases/main/monthly_active_users.py b/synapse/storage/databases/main/monthly_active_users.py
index 5beb8f1d4b..9a63f953fb 100644
--- a/synapse/storage/databases/main/monthly_active_users.py
+++ b/synapse/storage/databases/main/monthly_active_users.py
@@ -122,6 +122,51 @@ class MonthlyActiveUsersWorkerStore(RegistrationWorkerStore):
"count_users_by_service", _count_users_by_service
)
+ async def get_monthly_active_users_by_service(
+ self, start_timestamp: Optional[int] = None, end_timestamp: Optional[int] = None
+ ) -> List[Tuple[str, str]]:
+ """Generates list of monthly active users and their services.
+ Please see "get_monthly_active_count_by_service" docstring for more details
+ about services.
+
+ Arguments:
+ start_timestamp: If specified, only include users that were first active
+ at or after this point
+ end_timestamp: If specified, only include users that were first active
+ at or before this point
+
+ Returns:
+ A list of tuples (appservice_id, user_id). "native" is emitted as the
+ appservice for users that don't come from appservices (i.e. native Matrix
+ users).
+
+ """
+ if start_timestamp is not None and end_timestamp is not None:
+ where_clause = 'WHERE "timestamp" >= ? and "timestamp" <= ?'
+ query_params = [start_timestamp, end_timestamp]
+ elif start_timestamp is not None:
+ where_clause = 'WHERE "timestamp" >= ?'
+ query_params = [start_timestamp]
+ elif end_timestamp is not None:
+ where_clause = 'WHERE "timestamp" <= ?'
+ query_params = [end_timestamp]
+ else:
+ where_clause = ""
+ query_params = []
+
+ def _list_users(txn: LoggingTransaction) -> List[Tuple[str, str]]:
+ sql = f"""
+ SELECT COALESCE(appservice_id, 'native'), user_id
+ FROM monthly_active_users
+ LEFT JOIN users ON monthly_active_users.user_id=users.name
+ {where_clause};
+ """
+
+ txn.execute(sql, query_params)
+ return cast(List[Tuple[str, str]], txn.fetchall())
+
+ return await self.db_pool.runInteraction("list_users", _list_users)
+
async def get_registered_reserved_users(self) -> List[str]:
"""Of the reserved threepids defined in config, retrieve those that are associated
with registered users
|