test_stats.py 18 KB

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