You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

778 lines
27 KiB

  1. # Copyright 2018, 2019 New Vector Ltd
  2. # Copyright 2019 The Matrix.org Foundation C.I.C.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. import logging
  16. from enum import Enum
  17. from itertools import chain
  18. from typing import (
  19. TYPE_CHECKING,
  20. Any,
  21. Counter,
  22. Dict,
  23. Iterable,
  24. List,
  25. Optional,
  26. Tuple,
  27. Union,
  28. cast,
  29. )
  30. from twisted.internet.defer import DeferredLock
  31. from synapse.api.constants import Direction, EventContentFields, EventTypes, Membership
  32. from synapse.api.errors import StoreError
  33. from synapse.storage.database import (
  34. DatabasePool,
  35. LoggingDatabaseConnection,
  36. LoggingTransaction,
  37. )
  38. from synapse.storage.databases.main.events_worker import InvalidEventError
  39. from synapse.storage.databases.main.state_deltas import StateDeltasStore
  40. from synapse.types import JsonDict
  41. from synapse.util.caches.descriptors import cached
  42. if TYPE_CHECKING:
  43. from synapse.server import HomeServer
  44. logger = logging.getLogger(__name__)
  45. # these fields track absolutes (e.g. total number of rooms on the server)
  46. # You can think of these as Prometheus Gauges.
  47. # You can draw these stats on a line graph.
  48. # Example: number of users in a room
  49. ABSOLUTE_STATS_FIELDS = {
  50. "room": (
  51. "current_state_events",
  52. "joined_members",
  53. "invited_members",
  54. "knocked_members",
  55. "left_members",
  56. "banned_members",
  57. "local_users_in_room",
  58. ),
  59. "user": ("joined_rooms",),
  60. }
  61. TYPE_TO_TABLE = {"room": ("room_stats", "room_id"), "user": ("user_stats", "user_id")}
  62. # these are the tables (& ID columns) which contain our actual subjects
  63. TYPE_TO_ORIGIN_TABLE = {"room": ("rooms", "room_id"), "user": ("users", "name")}
  64. class UserSortOrder(Enum):
  65. """
  66. Enum to define the sorting method used when returning users
  67. with get_users_paginate in __init__.py
  68. and get_users_media_usage_paginate in stats.py
  69. When moves this to __init__.py gets `builtins.ImportError` with
  70. `most likely due to a circular import`
  71. MEDIA_LENGTH = ordered by size of uploaded media.
  72. MEDIA_COUNT = ordered by number of uploaded media.
  73. USER_ID = ordered alphabetically by `user_id`.
  74. NAME = ordered alphabetically by `user_id`. This is for compatibility reasons,
  75. as the user_id is returned in the name field in the response in list users admin API.
  76. DISPLAYNAME = ordered alphabetically by `displayname`
  77. GUEST = ordered by `is_guest`
  78. ADMIN = ordered by `admin`
  79. DEACTIVATED = ordered by `deactivated`
  80. USER_TYPE = ordered alphabetically by `user_type`
  81. AVATAR_URL = ordered alphabetically by `avatar_url`
  82. SHADOW_BANNED = ordered by `shadow_banned`
  83. CREATION_TS = ordered by `creation_ts`
  84. """
  85. MEDIA_LENGTH = "media_length"
  86. MEDIA_COUNT = "media_count"
  87. USER_ID = "user_id"
  88. NAME = "name"
  89. DISPLAYNAME = "displayname"
  90. GUEST = "is_guest"
  91. ADMIN = "admin"
  92. DEACTIVATED = "deactivated"
  93. USER_TYPE = "user_type"
  94. AVATAR_URL = "avatar_url"
  95. SHADOW_BANNED = "shadow_banned"
  96. CREATION_TS = "creation_ts"
  97. class StatsStore(StateDeltasStore):
  98. def __init__(
  99. self,
  100. database: DatabasePool,
  101. db_conn: LoggingDatabaseConnection,
  102. hs: "HomeServer",
  103. ):
  104. super().__init__(database, db_conn, hs)
  105. self.server_name: str = hs.hostname
  106. self.clock = self.hs.get_clock()
  107. self.stats_enabled = hs.config.stats.stats_enabled
  108. self.stats_delta_processing_lock = DeferredLock()
  109. self.db_pool.updates.register_background_update_handler(
  110. "populate_stats_process_rooms", self._populate_stats_process_rooms
  111. )
  112. self.db_pool.updates.register_background_update_handler(
  113. "populate_stats_process_users", self._populate_stats_process_users
  114. )
  115. async def _populate_stats_process_users(
  116. self, progress: JsonDict, batch_size: int
  117. ) -> int:
  118. """
  119. This is a background update which regenerates statistics for users.
  120. """
  121. if not self.stats_enabled:
  122. await self.db_pool.updates._end_background_update(
  123. "populate_stats_process_users"
  124. )
  125. return 1
  126. last_user_id = progress.get("last_user_id", "")
  127. def _get_next_batch(txn: LoggingTransaction) -> List[str]:
  128. sql = """
  129. SELECT DISTINCT name FROM users
  130. WHERE name > ?
  131. ORDER BY name ASC
  132. LIMIT ?
  133. """
  134. txn.execute(sql, (last_user_id, batch_size))
  135. return [r for r, in txn]
  136. users_to_work_on = await self.db_pool.runInteraction(
  137. "_populate_stats_process_users", _get_next_batch
  138. )
  139. # No more rooms -- complete the transaction.
  140. if not users_to_work_on:
  141. await self.db_pool.updates._end_background_update(
  142. "populate_stats_process_users"
  143. )
  144. return 1
  145. for user_id in users_to_work_on:
  146. await self._calculate_and_set_initial_state_for_user(user_id)
  147. progress["last_user_id"] = user_id
  148. await self.db_pool.runInteraction(
  149. "populate_stats_process_users",
  150. self.db_pool.updates._background_update_progress_txn,
  151. "populate_stats_process_users",
  152. progress,
  153. )
  154. return len(users_to_work_on)
  155. async def _populate_stats_process_rooms(
  156. self, progress: JsonDict, batch_size: int
  157. ) -> int:
  158. """This is a background update which regenerates statistics for rooms."""
  159. if not self.stats_enabled:
  160. await self.db_pool.updates._end_background_update(
  161. "populate_stats_process_rooms"
  162. )
  163. return 1
  164. last_room_id = progress.get("last_room_id", "")
  165. def _get_next_batch(txn: LoggingTransaction) -> List[str]:
  166. sql = """
  167. SELECT DISTINCT room_id FROM current_state_events
  168. WHERE room_id > ?
  169. ORDER BY room_id ASC
  170. LIMIT ?
  171. """
  172. txn.execute(sql, (last_room_id, batch_size))
  173. return [r for r, in txn]
  174. rooms_to_work_on = await self.db_pool.runInteraction(
  175. "populate_stats_rooms_get_batch", _get_next_batch
  176. )
  177. # No more rooms -- complete the transaction.
  178. if not rooms_to_work_on:
  179. await self.db_pool.updates._end_background_update(
  180. "populate_stats_process_rooms"
  181. )
  182. return 1
  183. for room_id in rooms_to_work_on:
  184. await self._calculate_and_set_initial_state_for_room(room_id)
  185. progress["last_room_id"] = room_id
  186. await self.db_pool.runInteraction(
  187. "_populate_stats_process_rooms",
  188. self.db_pool.updates._background_update_progress_txn,
  189. "populate_stats_process_rooms",
  190. progress,
  191. )
  192. return len(rooms_to_work_on)
  193. async def get_stats_positions(self) -> int:
  194. """
  195. Returns the stats processor positions.
  196. """
  197. return await self.db_pool.simple_select_one_onecol(
  198. table="stats_incremental_position",
  199. keyvalues={},
  200. retcol="stream_id",
  201. desc="stats_incremental_position",
  202. )
  203. async def update_room_state(self, room_id: str, fields: Dict[str, Any]) -> None:
  204. """Update the state of a room.
  205. fields can contain the following keys with string values:
  206. * join_rules
  207. * history_visibility
  208. * encryption
  209. * name
  210. * topic
  211. * avatar
  212. * canonical_alias
  213. * guest_access
  214. * room_type
  215. A is_federatable key can also be included with a boolean value.
  216. Args:
  217. room_id: The room ID to update the state of.
  218. fields: The fields to update. This can include a partial list of the
  219. above fields to only update some room information.
  220. """
  221. # Ensure that the values to update are valid, they should be strings and
  222. # not contain any null bytes.
  223. #
  224. # Invalid data gets overwritten with null.
  225. #
  226. # Note that a missing value should not be overwritten (it keeps the
  227. # previous value).
  228. sentinel = object()
  229. for col in (
  230. "join_rules",
  231. "history_visibility",
  232. "encryption",
  233. "name",
  234. "topic",
  235. "avatar",
  236. "canonical_alias",
  237. "guest_access",
  238. "room_type",
  239. ):
  240. field = fields.get(col, sentinel)
  241. if field is not sentinel and (not isinstance(field, str) or "\0" in field):
  242. fields[col] = None
  243. await self.db_pool.simple_upsert(
  244. table="room_stats_state",
  245. keyvalues={"room_id": room_id},
  246. values=fields,
  247. desc="update_room_state",
  248. )
  249. @cached()
  250. async def get_earliest_token_for_stats(
  251. self, stats_type: str, id: str
  252. ) -> Optional[int]:
  253. """
  254. Fetch the "earliest token". This is used by the room stats delta
  255. processor to ignore deltas that have been processed between the
  256. start of the background task and any particular room's stats
  257. being calculated.
  258. Returns:
  259. The earliest token.
  260. """
  261. table, id_col = TYPE_TO_TABLE[stats_type]
  262. return await self.db_pool.simple_select_one_onecol(
  263. "%s_current" % (table,),
  264. keyvalues={id_col: id},
  265. retcol="completed_delta_stream_id",
  266. allow_none=True,
  267. desc="get_earliest_token_for_stats",
  268. )
  269. async def bulk_update_stats_delta(
  270. self, ts: int, updates: Dict[str, Dict[str, Counter[str]]], stream_id: int
  271. ) -> None:
  272. """Bulk update stats tables for a given stream_id and updates the stats
  273. incremental position.
  274. Args:
  275. ts: Current timestamp in ms
  276. updates: The updates to commit as a mapping of
  277. stats_type -> stats_id -> field -> delta.
  278. stream_id: Current position.
  279. """
  280. def _bulk_update_stats_delta_txn(txn: LoggingTransaction) -> None:
  281. for stats_type, stats_updates in updates.items():
  282. for stats_id, fields in stats_updates.items():
  283. logger.debug(
  284. "Updating %s stats for %s: %s", stats_type, stats_id, fields
  285. )
  286. self._update_stats_delta_txn(
  287. txn,
  288. ts=ts,
  289. stats_type=stats_type,
  290. stats_id=stats_id,
  291. fields=fields,
  292. complete_with_stream_id=stream_id,
  293. )
  294. self.db_pool.simple_update_one_txn(
  295. txn,
  296. table="stats_incremental_position",
  297. keyvalues={},
  298. updatevalues={"stream_id": stream_id},
  299. )
  300. await self.db_pool.runInteraction(
  301. "bulk_update_stats_delta", _bulk_update_stats_delta_txn
  302. )
  303. async def update_stats_delta(
  304. self,
  305. ts: int,
  306. stats_type: str,
  307. stats_id: str,
  308. fields: Dict[str, int],
  309. complete_with_stream_id: int,
  310. absolute_field_overrides: Optional[Dict[str, int]] = None,
  311. ) -> None:
  312. """
  313. Updates the statistics for a subject, with a delta (difference/relative
  314. change).
  315. Args:
  316. ts: timestamp of the change
  317. stats_type: "room" or "user" – the kind of subject
  318. stats_id: the subject's ID (room ID or user ID)
  319. fields: Deltas of stats values.
  320. complete_with_stream_id:
  321. If supplied, converts an incomplete row into a complete row,
  322. with the supplied stream_id marked as the stream_id where the
  323. row was completed.
  324. absolute_field_overrides: Current stats values (i.e. not deltas) of
  325. absolute fields. Does not work with per-slice fields.
  326. """
  327. await self.db_pool.runInteraction(
  328. "update_stats_delta",
  329. self._update_stats_delta_txn,
  330. ts,
  331. stats_type,
  332. stats_id,
  333. fields,
  334. complete_with_stream_id=complete_with_stream_id,
  335. absolute_field_overrides=absolute_field_overrides,
  336. )
  337. def _update_stats_delta_txn(
  338. self,
  339. txn: LoggingTransaction,
  340. ts: int,
  341. stats_type: str,
  342. stats_id: str,
  343. fields: Dict[str, int],
  344. complete_with_stream_id: int,
  345. absolute_field_overrides: Optional[Dict[str, int]] = None,
  346. ) -> None:
  347. if absolute_field_overrides is None:
  348. absolute_field_overrides = {}
  349. table, id_col = TYPE_TO_TABLE[stats_type]
  350. # Lets be paranoid and check that all the given field names are known
  351. abs_field_names = ABSOLUTE_STATS_FIELDS[stats_type]
  352. for field in chain(fields.keys(), absolute_field_overrides.keys()):
  353. if field not in abs_field_names:
  354. # guard against potential SQL injection dodginess
  355. raise ValueError(
  356. "%s is not a recognised field"
  357. " for stats type %s" % (field, stats_type)
  358. )
  359. # Per slice fields do not get added to the _current table
  360. # This calculates the deltas (`field = field + ?` values)
  361. # for absolute fields,
  362. # * defaulting to 0 if not specified
  363. # (required for the INSERT part of upserting to work)
  364. # * omitting overrides specified in `absolute_field_overrides`
  365. deltas_of_absolute_fields = {
  366. key: fields.get(key, 0)
  367. for key in abs_field_names
  368. if key not in absolute_field_overrides
  369. }
  370. # Keep the delta stream ID field up to date
  371. absolute_field_overrides = absolute_field_overrides.copy()
  372. absolute_field_overrides["completed_delta_stream_id"] = complete_with_stream_id
  373. # first upsert the `_current` table
  374. self._upsert_with_additive_relatives_txn(
  375. txn=txn,
  376. table=table + "_current",
  377. keyvalues={id_col: stats_id},
  378. absolutes=absolute_field_overrides,
  379. additive_relatives=deltas_of_absolute_fields,
  380. )
  381. def _upsert_with_additive_relatives_txn(
  382. self,
  383. txn: LoggingTransaction,
  384. table: str,
  385. keyvalues: Dict[str, Any],
  386. absolutes: Dict[str, Any],
  387. additive_relatives: Dict[str, int],
  388. ) -> None:
  389. """Used to update values in the stats tables.
  390. This is basically a slightly convoluted upsert that *adds* to any
  391. existing rows.
  392. Args:
  393. table: Table name
  394. keyvalues: Row-identifying key values
  395. absolutes: Absolute (set) fields
  396. additive_relatives: Fields that will be added onto if existing row present.
  397. """
  398. absolute_updates = [
  399. "%(field)s = EXCLUDED.%(field)s" % {"field": field}
  400. for field in absolutes.keys()
  401. ]
  402. relative_updates = [
  403. "%(field)s = EXCLUDED.%(field)s + COALESCE(%(table)s.%(field)s, 0)"
  404. % {"table": table, "field": field}
  405. for field in additive_relatives.keys()
  406. ]
  407. insert_cols = []
  408. qargs = []
  409. for key, val in chain(
  410. keyvalues.items(), absolutes.items(), additive_relatives.items()
  411. ):
  412. insert_cols.append(key)
  413. qargs.append(val)
  414. sql = """
  415. INSERT INTO %(table)s (%(insert_cols_cs)s)
  416. VALUES (%(insert_vals_qs)s)
  417. ON CONFLICT (%(key_columns)s) DO UPDATE SET %(updates)s
  418. """ % {
  419. "table": table,
  420. "insert_cols_cs": ", ".join(insert_cols),
  421. "insert_vals_qs": ", ".join(
  422. ["?"] * (len(keyvalues) + len(absolutes) + len(additive_relatives))
  423. ),
  424. "key_columns": ", ".join(keyvalues),
  425. "updates": ", ".join(chain(absolute_updates, relative_updates)),
  426. }
  427. txn.execute(sql, qargs)
  428. async def _calculate_and_set_initial_state_for_room(self, room_id: str) -> None:
  429. """Calculate and insert an entry into room_stats_current.
  430. Args:
  431. room_id: The room ID under calculation.
  432. """
  433. def _fetch_current_state_stats(
  434. txn: LoggingTransaction,
  435. ) -> Tuple[List[str], Dict[str, int], int, List[str], int]:
  436. pos = self.get_room_max_stream_ordering() # type: ignore[attr-defined]
  437. rows = self.db_pool.simple_select_many_txn(
  438. txn,
  439. table="current_state_events",
  440. column="type",
  441. iterable=[
  442. EventTypes.Create,
  443. EventTypes.JoinRules,
  444. EventTypes.RoomHistoryVisibility,
  445. EventTypes.RoomEncryption,
  446. EventTypes.Name,
  447. EventTypes.Topic,
  448. EventTypes.RoomAvatar,
  449. EventTypes.CanonicalAlias,
  450. ],
  451. keyvalues={"room_id": room_id, "state_key": ""},
  452. retcols=["event_id"],
  453. )
  454. event_ids = cast(List[str], [row["event_id"] for row in rows])
  455. txn.execute(
  456. """
  457. SELECT membership, count(*) FROM current_state_events
  458. WHERE room_id = ? AND type = 'm.room.member'
  459. GROUP BY membership
  460. """,
  461. (room_id,),
  462. )
  463. membership_counts = dict(cast(Iterable[Tuple[str, int]], txn))
  464. txn.execute(
  465. """
  466. SELECT COUNT(*) FROM current_state_events
  467. WHERE room_id = ?
  468. """,
  469. (room_id,),
  470. )
  471. current_state_events_count = cast(Tuple[int], txn.fetchone())[0]
  472. users_in_room = self.get_users_in_room_txn(txn, room_id) # type: ignore[attr-defined]
  473. return (
  474. event_ids,
  475. membership_counts,
  476. current_state_events_count,
  477. users_in_room,
  478. pos,
  479. )
  480. (
  481. event_ids,
  482. membership_counts,
  483. current_state_events_count,
  484. users_in_room,
  485. pos,
  486. ) = await self.db_pool.runInteraction(
  487. "get_initial_state_for_room", _fetch_current_state_stats
  488. )
  489. try:
  490. state_event_map = await self.get_events(event_ids, get_prev_content=False) # type: ignore[attr-defined]
  491. except InvalidEventError as e:
  492. # If an exception occurs fetching events then the room is broken;
  493. # skip process it to avoid being stuck on a room.
  494. logger.warning(
  495. "Failed to fetch events for room %s, skipping stats calculation: %r.",
  496. room_id,
  497. e,
  498. )
  499. return
  500. room_state: Dict[str, Union[None, bool, str]] = {
  501. "join_rules": None,
  502. "history_visibility": None,
  503. "encryption": None,
  504. "name": None,
  505. "topic": None,
  506. "avatar": None,
  507. "canonical_alias": None,
  508. "is_federatable": True,
  509. "room_type": None,
  510. }
  511. for event in state_event_map.values():
  512. if event.type == EventTypes.JoinRules:
  513. room_state["join_rules"] = event.content.get("join_rule")
  514. elif event.type == EventTypes.RoomHistoryVisibility:
  515. room_state["history_visibility"] = event.content.get(
  516. "history_visibility"
  517. )
  518. elif event.type == EventTypes.RoomEncryption:
  519. room_state["encryption"] = event.content.get("algorithm")
  520. elif event.type == EventTypes.Name:
  521. room_state["name"] = event.content.get("name")
  522. elif event.type == EventTypes.Topic:
  523. room_state["topic"] = event.content.get("topic")
  524. elif event.type == EventTypes.RoomAvatar:
  525. room_state["avatar"] = event.content.get("url")
  526. elif event.type == EventTypes.CanonicalAlias:
  527. room_state["canonical_alias"] = event.content.get("alias")
  528. elif event.type == EventTypes.Create:
  529. room_state["is_federatable"] = (
  530. event.content.get(EventContentFields.FEDERATE, True) is True
  531. )
  532. room_type = event.content.get(EventContentFields.ROOM_TYPE)
  533. if isinstance(room_type, str):
  534. room_state["room_type"] = room_type
  535. await self.update_room_state(room_id, room_state)
  536. local_users_in_room = [u for u in users_in_room if self.hs.is_mine_id(u)]
  537. await self.update_stats_delta(
  538. ts=self.clock.time_msec(),
  539. stats_type="room",
  540. stats_id=room_id,
  541. fields={},
  542. complete_with_stream_id=pos,
  543. absolute_field_overrides={
  544. "current_state_events": current_state_events_count,
  545. "joined_members": membership_counts.get(Membership.JOIN, 0),
  546. "invited_members": membership_counts.get(Membership.INVITE, 0),
  547. "left_members": membership_counts.get(Membership.LEAVE, 0),
  548. "banned_members": membership_counts.get(Membership.BAN, 0),
  549. "knocked_members": membership_counts.get(Membership.KNOCK, 0),
  550. "local_users_in_room": len(local_users_in_room),
  551. },
  552. )
  553. async def _calculate_and_set_initial_state_for_user(self, user_id: str) -> None:
  554. def _calculate_and_set_initial_state_for_user_txn(
  555. txn: LoggingTransaction,
  556. ) -> Tuple[int, int]:
  557. pos = self._get_max_stream_id_in_current_state_deltas_txn(txn)
  558. txn.execute(
  559. """
  560. SELECT COUNT(distinct room_id) FROM current_state_events
  561. WHERE type = 'm.room.member' AND state_key = ?
  562. AND membership = 'join'
  563. """,
  564. (user_id,),
  565. )
  566. count = cast(Tuple[int], txn.fetchone())[0]
  567. return count, pos
  568. joined_rooms, pos = await self.db_pool.runInteraction(
  569. "calculate_and_set_initial_state_for_user",
  570. _calculate_and_set_initial_state_for_user_txn,
  571. )
  572. await self.update_stats_delta(
  573. ts=self.clock.time_msec(),
  574. stats_type="user",
  575. stats_id=user_id,
  576. fields={},
  577. complete_with_stream_id=pos,
  578. absolute_field_overrides={"joined_rooms": joined_rooms},
  579. )
  580. async def get_users_media_usage_paginate(
  581. self,
  582. start: int,
  583. limit: int,
  584. from_ts: Optional[int] = None,
  585. until_ts: Optional[int] = None,
  586. order_by: Optional[str] = UserSortOrder.USER_ID.value,
  587. direction: Direction = Direction.FORWARDS,
  588. search_term: Optional[str] = None,
  589. ) -> Tuple[List[JsonDict], int]:
  590. """Function to retrieve a paginated list of users and their uploaded local media
  591. (size and number). This will return a json list of users and the
  592. total number of users matching the filter criteria.
  593. Args:
  594. start: offset to begin the query from
  595. limit: number of rows to retrieve
  596. from_ts: request only media that are created later than this timestamp (ms)
  597. until_ts: request only media that are created earlier than this timestamp (ms)
  598. order_by: the sort order of the returned list
  599. direction: sort ascending or descending
  600. search_term: a string to filter user names by
  601. Returns:
  602. A list of user dicts and an integer representing the total number of
  603. users that exist given this query
  604. """
  605. def get_users_media_usage_paginate_txn(
  606. txn: LoggingTransaction,
  607. ) -> Tuple[List[JsonDict], int]:
  608. filters = []
  609. args: list = []
  610. if search_term:
  611. filters.append("(lmr.user_id LIKE ? OR displayname LIKE ?)")
  612. args.extend(["@%" + search_term + "%:%", "%" + search_term + "%"])
  613. if from_ts:
  614. filters.append("created_ts >= ?")
  615. args.extend([from_ts])
  616. if until_ts:
  617. filters.append("created_ts <= ?")
  618. args.extend([until_ts])
  619. # Set ordering
  620. if UserSortOrder(order_by) == UserSortOrder.MEDIA_LENGTH:
  621. order_by_column = "media_length"
  622. elif UserSortOrder(order_by) == UserSortOrder.MEDIA_COUNT:
  623. order_by_column = "media_count"
  624. elif UserSortOrder(order_by) == UserSortOrder.USER_ID:
  625. order_by_column = "lmr.user_id"
  626. elif UserSortOrder(order_by) == UserSortOrder.DISPLAYNAME:
  627. order_by_column = "displayname"
  628. else:
  629. raise StoreError(
  630. 500, "Incorrect value for order_by provided: %s" % order_by
  631. )
  632. if direction == Direction.BACKWARDS:
  633. order = "DESC"
  634. else:
  635. order = "ASC"
  636. where_clause = "WHERE " + " AND ".join(filters) if len(filters) > 0 else ""
  637. sql_base = """
  638. FROM local_media_repository as lmr
  639. LEFT JOIN profiles AS p ON lmr.user_id = p.full_user_id
  640. {}
  641. GROUP BY lmr.user_id, displayname
  642. """.format(
  643. where_clause
  644. )
  645. # SQLite does not support SELECT COUNT(*) OVER()
  646. sql = """
  647. SELECT COUNT(*) FROM (
  648. SELECT lmr.user_id
  649. {sql_base}
  650. ) AS count_user_ids
  651. """.format(
  652. sql_base=sql_base,
  653. )
  654. txn.execute(sql, args)
  655. count = cast(Tuple[int], txn.fetchone())[0]
  656. sql = """
  657. SELECT
  658. lmr.user_id,
  659. displayname,
  660. COUNT(lmr.user_id) as media_count,
  661. SUM(media_length) as media_length
  662. {sql_base}
  663. ORDER BY {order_by_column} {order}
  664. LIMIT ? OFFSET ?
  665. """.format(
  666. sql_base=sql_base,
  667. order_by_column=order_by_column,
  668. order=order,
  669. )
  670. args += [limit, start]
  671. txn.execute(sql, args)
  672. users = self.db_pool.cursor_to_dict(txn)
  673. return users, count
  674. return await self.db_pool.runInteraction(
  675. "get_users_media_usage_paginate_txn", get_users_media_usage_paginate_txn
  676. )