1
0

test_stats.py 19 KB

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