test_stats.py 19 KB

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