Fetch membership counts all at once

pull/5294/head
Erik Johnston 2019-05-30 15:22:32 +01:00
parent 54d50fbfdf
commit 04710cc2d7
2 changed files with 18 additions and 38 deletions

View File

@ -142,26 +142,9 @@ class RoomMemberWorkerStore(EventsWorkerStore):
return self.runInteraction("get_room_summary", _get_room_summary_txn) return self.runInteraction("get_room_summary", _get_room_summary_txn)
def _get_user_count_in_room_txn(self, txn, room_id, membership): def _get_user_counts_in_room_txn(self, txn, room_id):
""" """
See get_user_count_in_room. Get the user count in a room by membership.
"""
sql = (
"SELECT count(*) FROM room_memberships as m"
" INNER JOIN current_state_events as c"
" ON m.event_id = c.event_id "
" AND m.room_id = c.room_id "
" AND m.user_id = c.state_key"
" WHERE c.type = 'm.room.member' AND c.room_id = ? AND m.membership = ?"
)
txn.execute(sql, (room_id, membership))
row = txn.fetchone()
return row[0]
def get_user_count_in_room(self, room_id, membership):
"""
Get the user count in a room with a particular membership.
Args: Args:
room_id (str) room_id (str)
@ -170,9 +153,15 @@ class RoomMemberWorkerStore(EventsWorkerStore):
Returns: Returns:
Deferred[int] Deferred[int]
""" """
return self.runInteraction( sql = """
"get_users_in_room", self._get_user_count_in_room_txn, room_id, membership SELECT m.membership, count(*) FROM room_memberships as m
) INNER JOIN current_state_events as c USING(event_id)
WHERE c.type = 'm.room.member' AND c.room_id = ?
GROUP BY m.membership
"""
txn.execute(sql, (room_id,))
return {row[0]: row[1] for row in txn}
@cached() @cached()
def get_invited_rooms_for_user(self, user_id): def get_invited_rooms_for_user(self, user_id):

View File

@ -226,18 +226,9 @@ class StatsStore(StateDeltasStore):
current_token = self._get_max_stream_id_in_current_state_deltas_txn(txn) current_token = self._get_max_stream_id_in_current_state_deltas_txn(txn)
current_state_events = len(current_state_ids) current_state_events = len(current_state_ids)
joined_members = self._get_user_count_in_room_txn(
txn, room_id, Membership.JOIN membership_counts = self._get_user_counts_in_room_txn(txn, room_id)
)
invited_members = self._get_user_count_in_room_txn(
txn, room_id, Membership.INVITE
)
left_members = self._get_user_count_in_room_txn(
txn, room_id, Membership.LEAVE
)
banned_members = self._get_user_count_in_room_txn(
txn, room_id, Membership.BAN
)
total_state_events = self._get_total_state_event_counts_txn( total_state_events = self._get_total_state_event_counts_txn(
txn, room_id txn, room_id
) )
@ -250,10 +241,10 @@ class StatsStore(StateDeltasStore):
{ {
"bucket_size": self.stats_bucket_size, "bucket_size": self.stats_bucket_size,
"current_state_events": current_state_events, "current_state_events": current_state_events,
"joined_members": joined_members, "joined_members": membership_counts.get(Membership.JOIN, 0),
"invited_members": invited_members, "invited_members": membership_counts.get(Membership.INVITE, 0),
"left_members": left_members, "left_members": membership_counts.get(Membership.LEAVE, 0),
"banned_members": banned_members, "banned_members": membership_counts.get(Membership.BAN, 0),
"state_events": total_state_events, "state_events": total_state_events,
}, },
) )