test_stats.py 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582
  1. # Copyright 2019 New Vector Ltd
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. from typing import Any, Dict, List, Optional
  15. from twisted.test.proto_helpers import MemoryReactor
  16. from synapse.rest import admin
  17. from synapse.rest.client import login, room
  18. from synapse.server import HomeServer
  19. from synapse.storage.databases.main import stats
  20. from synapse.util import Clock
  21. from tests import unittest
  22. # The expected number of state events in a fresh public room.
  23. EXPT_NUM_STATE_EVTS_IN_FRESH_PUBLIC_ROOM = 5
  24. # The expected number of state events in a fresh private room.
  25. EXPT_NUM_STATE_EVTS_IN_FRESH_PRIVATE_ROOM = 6
  26. class StatsRoomTests(unittest.HomeserverTestCase):
  27. servlets = [
  28. admin.register_servlets_for_client_rest_resource,
  29. room.register_servlets,
  30. login.register_servlets,
  31. ]
  32. def prepare(self, reactor: MemoryReactor, clock: Clock, hs: HomeServer) -> None:
  33. self.store = hs.get_datastores().main
  34. self.handler = self.hs.get_stats_handler()
  35. def _add_background_updates(self) -> None:
  36. """
  37. Add the background updates we need to run.
  38. """
  39. # Ugh, have to reset this flag
  40. self.store.db_pool.updates._all_done = False
  41. self.get_success(
  42. self.store.db_pool.simple_insert(
  43. "background_updates",
  44. {
  45. "update_name": "populate_stats_process_rooms",
  46. "progress_json": "{}",
  47. },
  48. )
  49. )
  50. self.get_success(
  51. self.store.db_pool.simple_insert(
  52. "background_updates",
  53. {
  54. "update_name": "populate_stats_process_users",
  55. "progress_json": "{}",
  56. "depends_on": "populate_stats_process_rooms",
  57. },
  58. )
  59. )
  60. async def get_all_room_state(self) -> List[Dict[str, Any]]:
  61. return await self.store.db_pool.simple_select_list(
  62. "room_stats_state", None, retcols=("name", "topic", "canonical_alias")
  63. )
  64. def _get_current_stats(
  65. self, stats_type: str, stat_id: str
  66. ) -> Optional[Dict[str, Any]]:
  67. table, id_col = stats.TYPE_TO_TABLE[stats_type]
  68. cols = list(stats.ABSOLUTE_STATS_FIELDS[stats_type])
  69. return self.get_success(
  70. self.store.db_pool.simple_select_one(
  71. table + "_current",
  72. {id_col: stat_id},
  73. cols,
  74. allow_none=True,
  75. )
  76. )
  77. def _perform_background_initial_update(self) -> None:
  78. # Do the initial population of the stats via the background update
  79. self._add_background_updates()
  80. self.wait_for_background_updates()
  81. def test_initial_room(self) -> None:
  82. """
  83. The background updates will build the table from scratch.
  84. """
  85. r = self.get_success(self.get_all_room_state())
  86. self.assertEqual(len(r), 0)
  87. # Disable stats
  88. self.hs.config.stats.stats_enabled = False
  89. self.handler.stats_enabled = False
  90. u1 = self.register_user("u1", "pass")
  91. u1_token = self.login("u1", "pass")
  92. room_1 = self.helper.create_room_as(u1, tok=u1_token)
  93. self.helper.send_state(
  94. room_1, event_type="m.room.topic", body={"topic": "foo"}, tok=u1_token
  95. )
  96. # Stats disabled, shouldn't have done anything
  97. r = self.get_success(self.get_all_room_state())
  98. self.assertEqual(len(r), 0)
  99. # Enable stats
  100. self.hs.config.stats.stats_enabled = True
  101. self.handler.stats_enabled = True
  102. # Do the initial population of the user directory via the background update
  103. self._add_background_updates()
  104. self.wait_for_background_updates()
  105. r = self.get_success(self.get_all_room_state())
  106. self.assertEqual(len(r), 1)
  107. self.assertEqual(r[0]["topic"], "foo")
  108. def test_create_user(self) -> None:
  109. """
  110. When we create a user, it should have statistics already ready.
  111. """
  112. u1 = self.register_user("u1", "pass")
  113. u1stats = self._get_current_stats("user", u1)
  114. assert u1stats is not None
  115. # not in any rooms by default
  116. self.assertEqual(u1stats["joined_rooms"], 0)
  117. def test_create_room(self) -> None:
  118. """
  119. When we create a room, it should have statistics already ready.
  120. """
  121. self._perform_background_initial_update()
  122. u1 = self.register_user("u1", "pass")
  123. u1token = self.login("u1", "pass")
  124. r1 = self.helper.create_room_as(u1, tok=u1token)
  125. r1stats = self._get_current_stats("room", r1)
  126. r2 = self.helper.create_room_as(u1, tok=u1token, is_public=False)
  127. r2stats = self._get_current_stats("room", r2)
  128. assert r1stats is not None
  129. assert r2stats is not None
  130. self.assertEqual(
  131. r1stats["current_state_events"], EXPT_NUM_STATE_EVTS_IN_FRESH_PUBLIC_ROOM
  132. )
  133. self.assertEqual(
  134. r2stats["current_state_events"], EXPT_NUM_STATE_EVTS_IN_FRESH_PRIVATE_ROOM
  135. )
  136. self.assertEqual(r1stats["joined_members"], 1)
  137. self.assertEqual(r1stats["invited_members"], 0)
  138. self.assertEqual(r1stats["banned_members"], 0)
  139. self.assertEqual(r2stats["joined_members"], 1)
  140. self.assertEqual(r2stats["invited_members"], 0)
  141. self.assertEqual(r2stats["banned_members"], 0)
  142. def test_updating_profile_information_does_not_increase_joined_members_count(
  143. self,
  144. ) -> None:
  145. """
  146. Check that the joined_members count does not increase when a user changes their
  147. profile information (which is done by sending another join membership event into
  148. the room.
  149. """
  150. self._perform_background_initial_update()
  151. # Create a user and room
  152. u1 = self.register_user("u1", "pass")
  153. u1token = self.login("u1", "pass")
  154. r1 = self.helper.create_room_as(u1, tok=u1token)
  155. # Get the current room stats
  156. r1stats_ante = self._get_current_stats("room", r1)
  157. assert r1stats_ante is not None
  158. # Send a profile update into the room
  159. new_profile = {"displayname": "bob"}
  160. self.helper.change_membership(
  161. r1, u1, u1, "join", extra_data=new_profile, tok=u1token
  162. )
  163. # Get the new room stats
  164. r1stats_post = self._get_current_stats("room", r1)
  165. assert r1stats_post is not None
  166. # Ensure that the user count did not changed
  167. self.assertEqual(r1stats_post["joined_members"], r1stats_ante["joined_members"])
  168. self.assertEqual(
  169. r1stats_post["local_users_in_room"], r1stats_ante["local_users_in_room"]
  170. )
  171. def test_send_state_event_nonoverwriting(self) -> None:
  172. """
  173. When we send a non-overwriting state event, it increments current_state_events
  174. """
  175. self._perform_background_initial_update()
  176. u1 = self.register_user("u1", "pass")
  177. u1token = self.login("u1", "pass")
  178. r1 = self.helper.create_room_as(u1, tok=u1token)
  179. self.helper.send_state(
  180. r1, "cat.hissing", {"value": True}, tok=u1token, state_key="tabby"
  181. )
  182. r1stats_ante = self._get_current_stats("room", r1)
  183. assert r1stats_ante is not None
  184. self.helper.send_state(
  185. r1, "cat.hissing", {"value": False}, tok=u1token, state_key="moggy"
  186. )
  187. r1stats_post = self._get_current_stats("room", r1)
  188. assert r1stats_post is not None
  189. self.assertEqual(
  190. r1stats_post["current_state_events"] - r1stats_ante["current_state_events"],
  191. 1,
  192. )
  193. def test_join_first_time(self) -> None:
  194. """
  195. When a user joins a room for the first time, current_state_events and
  196. joined_members should increase by exactly 1.
  197. """
  198. self._perform_background_initial_update()
  199. u1 = self.register_user("u1", "pass")
  200. u1token = self.login("u1", "pass")
  201. r1 = self.helper.create_room_as(u1, tok=u1token)
  202. u2 = self.register_user("u2", "pass")
  203. u2token = self.login("u2", "pass")
  204. r1stats_ante = self._get_current_stats("room", r1)
  205. assert r1stats_ante is not None
  206. self.helper.join(r1, u2, tok=u2token)
  207. r1stats_post = self._get_current_stats("room", r1)
  208. assert r1stats_post is not None
  209. self.assertEqual(
  210. r1stats_post["current_state_events"] - r1stats_ante["current_state_events"],
  211. 1,
  212. )
  213. self.assertEqual(
  214. r1stats_post["joined_members"] - r1stats_ante["joined_members"], 1
  215. )
  216. def test_join_after_leave(self) -> None:
  217. """
  218. When a user joins a room after being previously left,
  219. joined_members should increase by exactly 1.
  220. current_state_events should not increase.
  221. left_members should decrease by exactly 1.
  222. """
  223. self._perform_background_initial_update()
  224. u1 = self.register_user("u1", "pass")
  225. u1token = self.login("u1", "pass")
  226. r1 = self.helper.create_room_as(u1, tok=u1token)
  227. u2 = self.register_user("u2", "pass")
  228. u2token = self.login("u2", "pass")
  229. self.helper.join(r1, u2, tok=u2token)
  230. self.helper.leave(r1, u2, tok=u2token)
  231. r1stats_ante = self._get_current_stats("room", r1)
  232. assert r1stats_ante is not None
  233. self.helper.join(r1, u2, tok=u2token)
  234. r1stats_post = self._get_current_stats("room", r1)
  235. assert r1stats_post is not None
  236. self.assertEqual(
  237. r1stats_post["current_state_events"] - r1stats_ante["current_state_events"],
  238. 0,
  239. )
  240. self.assertEqual(
  241. r1stats_post["joined_members"] - r1stats_ante["joined_members"], +1
  242. )
  243. self.assertEqual(
  244. r1stats_post["left_members"] - r1stats_ante["left_members"], -1
  245. )
  246. def test_invited(self) -> None:
  247. """
  248. When a user invites another user, current_state_events and
  249. invited_members should increase by exactly 1.
  250. """
  251. self._perform_background_initial_update()
  252. u1 = self.register_user("u1", "pass")
  253. u1token = self.login("u1", "pass")
  254. r1 = self.helper.create_room_as(u1, tok=u1token)
  255. u2 = self.register_user("u2", "pass")
  256. r1stats_ante = self._get_current_stats("room", r1)
  257. assert r1stats_ante is not None
  258. self.helper.invite(r1, u1, u2, tok=u1token)
  259. r1stats_post = self._get_current_stats("room", r1)
  260. assert r1stats_post is not None
  261. self.assertEqual(
  262. r1stats_post["current_state_events"] - r1stats_ante["current_state_events"],
  263. 1,
  264. )
  265. self.assertEqual(
  266. r1stats_post["invited_members"] - r1stats_ante["invited_members"], +1
  267. )
  268. def test_join_after_invite(self) -> None:
  269. """
  270. When a user joins a room after being invited and
  271. joined_members should increase by exactly 1.
  272. current_state_events should not increase.
  273. invited_members should decrease by exactly 1.
  274. """
  275. self._perform_background_initial_update()
  276. u1 = self.register_user("u1", "pass")
  277. u1token = self.login("u1", "pass")
  278. r1 = self.helper.create_room_as(u1, tok=u1token)
  279. u2 = self.register_user("u2", "pass")
  280. u2token = self.login("u2", "pass")
  281. self.helper.invite(r1, u1, u2, tok=u1token)
  282. r1stats_ante = self._get_current_stats("room", r1)
  283. assert r1stats_ante is not None
  284. self.helper.join(r1, u2, tok=u2token)
  285. r1stats_post = self._get_current_stats("room", r1)
  286. assert r1stats_post is not None
  287. self.assertEqual(
  288. r1stats_post["current_state_events"] - r1stats_ante["current_state_events"],
  289. 0,
  290. )
  291. self.assertEqual(
  292. r1stats_post["joined_members"] - r1stats_ante["joined_members"], +1
  293. )
  294. self.assertEqual(
  295. r1stats_post["invited_members"] - r1stats_ante["invited_members"], -1
  296. )
  297. def test_left(self) -> None:
  298. """
  299. When a user leaves a room after joining and
  300. left_members should increase by exactly 1.
  301. current_state_events should not increase.
  302. joined_members should decrease by exactly 1.
  303. """
  304. self._perform_background_initial_update()
  305. u1 = self.register_user("u1", "pass")
  306. u1token = self.login("u1", "pass")
  307. r1 = self.helper.create_room_as(u1, tok=u1token)
  308. u2 = self.register_user("u2", "pass")
  309. u2token = self.login("u2", "pass")
  310. self.helper.join(r1, u2, tok=u2token)
  311. r1stats_ante = self._get_current_stats("room", r1)
  312. assert r1stats_ante is not None
  313. self.helper.leave(r1, u2, tok=u2token)
  314. r1stats_post = self._get_current_stats("room", r1)
  315. assert r1stats_post is not None
  316. self.assertEqual(
  317. r1stats_post["current_state_events"] - r1stats_ante["current_state_events"],
  318. 0,
  319. )
  320. self.assertEqual(
  321. r1stats_post["left_members"] - r1stats_ante["left_members"], +1
  322. )
  323. self.assertEqual(
  324. r1stats_post["joined_members"] - r1stats_ante["joined_members"], -1
  325. )
  326. def test_banned(self) -> None:
  327. """
  328. When a user is banned from a room after joining and
  329. left_members should increase by exactly 1.
  330. current_state_events should not increase.
  331. banned_members should decrease by exactly 1.
  332. """
  333. self._perform_background_initial_update()
  334. u1 = self.register_user("u1", "pass")
  335. u1token = self.login("u1", "pass")
  336. r1 = self.helper.create_room_as(u1, tok=u1token)
  337. u2 = self.register_user("u2", "pass")
  338. u2token = self.login("u2", "pass")
  339. self.helper.join(r1, u2, tok=u2token)
  340. r1stats_ante = self._get_current_stats("room", r1)
  341. assert r1stats_ante is not None
  342. self.helper.change_membership(r1, u1, u2, "ban", tok=u1token)
  343. r1stats_post = self._get_current_stats("room", r1)
  344. assert r1stats_post is not None
  345. self.assertEqual(
  346. r1stats_post["current_state_events"] - r1stats_ante["current_state_events"],
  347. 0,
  348. )
  349. self.assertEqual(
  350. r1stats_post["banned_members"] - r1stats_ante["banned_members"], +1
  351. )
  352. self.assertEqual(
  353. r1stats_post["joined_members"] - r1stats_ante["joined_members"], -1
  354. )
  355. def test_initial_background_update(self) -> None:
  356. """
  357. Test that statistics can be generated by the initial background update
  358. handler.
  359. This test also tests that stats rows are not created for new subjects
  360. when stats are disabled. However, it may be desirable to change this
  361. behaviour eventually to still keep current rows.
  362. """
  363. self.hs.config.stats.stats_enabled = False
  364. self.handler.stats_enabled = False
  365. u1 = self.register_user("u1", "pass")
  366. u1token = self.login("u1", "pass")
  367. r1 = self.helper.create_room_as(u1, tok=u1token)
  368. # test that these subjects, which were created during a time of disabled
  369. # stats, do not have stats.
  370. self.assertIsNone(self._get_current_stats("room", r1))
  371. self.assertIsNone(self._get_current_stats("user", u1))
  372. self.hs.config.stats.stats_enabled = True
  373. self.handler.stats_enabled = True
  374. self._perform_background_initial_update()
  375. r1stats = self._get_current_stats("room", r1)
  376. u1stats = self._get_current_stats("user", u1)
  377. assert r1stats is not None
  378. assert u1stats is not None
  379. self.assertEqual(r1stats["joined_members"], 1)
  380. self.assertEqual(
  381. r1stats["current_state_events"], EXPT_NUM_STATE_EVTS_IN_FRESH_PUBLIC_ROOM
  382. )
  383. self.assertEqual(u1stats["joined_rooms"], 1)
  384. def test_incomplete_stats(self) -> None:
  385. """
  386. This tests that we track incomplete statistics.
  387. We first test that incomplete stats are incrementally generated,
  388. following the preparation of a background regen.
  389. We then test that these incomplete rows are completed by the background
  390. regen.
  391. """
  392. u1 = self.register_user("u1", "pass")
  393. u1token = self.login("u1", "pass")
  394. u2 = self.register_user("u2", "pass")
  395. u2token = self.login("u2", "pass")
  396. u3 = self.register_user("u3", "pass")
  397. r1 = self.helper.create_room_as(u1, tok=u1token, is_public=False)
  398. # preparation stage of the initial background update
  399. # Ugh, have to reset this flag
  400. self.store.db_pool.updates._all_done = False
  401. self.get_success(
  402. self.store.db_pool.simple_delete(
  403. "room_stats_current", {"1": 1}, "test_delete_stats"
  404. )
  405. )
  406. self.get_success(
  407. self.store.db_pool.simple_delete(
  408. "user_stats_current", {"1": 1}, "test_delete_stats"
  409. )
  410. )
  411. self.helper.invite(r1, u1, u2, tok=u1token)
  412. self.helper.join(r1, u2, tok=u2token)
  413. self.helper.invite(r1, u1, u3, tok=u1token)
  414. self.helper.send(r1, "thou shalt yield", tok=u1token)
  415. # now do the background updates
  416. self.store.db_pool.updates._all_done = False
  417. self.get_success(
  418. self.store.db_pool.simple_insert(
  419. "background_updates",
  420. {
  421. "update_name": "populate_stats_process_rooms",
  422. "progress_json": "{}",
  423. },
  424. )
  425. )
  426. self.get_success(
  427. self.store.db_pool.simple_insert(
  428. "background_updates",
  429. {
  430. "update_name": "populate_stats_process_users",
  431. "progress_json": "{}",
  432. "depends_on": "populate_stats_process_rooms",
  433. },
  434. )
  435. )
  436. self.wait_for_background_updates()
  437. r1stats_complete = self._get_current_stats("room", r1)
  438. assert r1stats_complete is not None
  439. u1stats_complete = self._get_current_stats("user", u1)
  440. assert u1stats_complete is not None
  441. u2stats_complete = self._get_current_stats("user", u2)
  442. assert u2stats_complete is not None
  443. # now we make our assertions
  444. # check that _complete rows are complete and correct
  445. self.assertEqual(r1stats_complete["joined_members"], 2)
  446. self.assertEqual(r1stats_complete["invited_members"], 1)
  447. self.assertEqual(
  448. r1stats_complete["current_state_events"],
  449. 2 + EXPT_NUM_STATE_EVTS_IN_FRESH_PRIVATE_ROOM,
  450. )
  451. self.assertEqual(u1stats_complete["joined_rooms"], 1)
  452. self.assertEqual(u2stats_complete["joined_rooms"], 1)