test_stats.py 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777
  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]) + list(
  80. stats.PER_SLICE_FIELDS[stats_type]
  81. )
  82. end_ts = self.store.quantise_stats_time(self.reactor.seconds() * 1000)
  83. return self.get_success(
  84. self.store.db_pool.simple_select_one(
  85. table + "_historical",
  86. {id_col: stat_id, end_ts: end_ts},
  87. cols,
  88. allow_none=True,
  89. )
  90. )
  91. def _perform_background_initial_update(self):
  92. # Do the initial population of the stats via the background update
  93. self._add_background_updates()
  94. while not self.get_success(
  95. self.store.db_pool.updates.has_completed_background_updates()
  96. ):
  97. self.get_success(
  98. self.store.db_pool.updates.do_next_background_update(100), by=0.1
  99. )
  100. def test_initial_room(self):
  101. """
  102. The background updates will build the table from scratch.
  103. """
  104. r = self.get_success(self.get_all_room_state())
  105. self.assertEqual(len(r), 0)
  106. # Disable stats
  107. self.hs.config.stats_enabled = False
  108. self.handler.stats_enabled = False
  109. u1 = self.register_user("u1", "pass")
  110. u1_token = self.login("u1", "pass")
  111. room_1 = self.helper.create_room_as(u1, tok=u1_token)
  112. self.helper.send_state(
  113. room_1, event_type="m.room.topic", body={"topic": "foo"}, tok=u1_token
  114. )
  115. # Stats disabled, shouldn't have done anything
  116. r = self.get_success(self.get_all_room_state())
  117. self.assertEqual(len(r), 0)
  118. # Enable stats
  119. self.hs.config.stats_enabled = True
  120. self.handler.stats_enabled = True
  121. # Do the initial population of the user directory via the background update
  122. self._add_background_updates()
  123. while not self.get_success(
  124. self.store.db_pool.updates.has_completed_background_updates()
  125. ):
  126. self.get_success(
  127. self.store.db_pool.updates.do_next_background_update(100), by=0.1
  128. )
  129. r = self.get_success(self.get_all_room_state())
  130. self.assertEqual(len(r), 1)
  131. self.assertEqual(r[0]["topic"], "foo")
  132. def test_initial_earliest_token(self):
  133. """
  134. Ingestion via notify_new_event will ignore tokens that the background
  135. update have already processed.
  136. """
  137. self.reactor.advance(86401)
  138. self.hs.config.stats_enabled = False
  139. self.handler.stats_enabled = False
  140. u1 = self.register_user("u1", "pass")
  141. u1_token = self.login("u1", "pass")
  142. u2 = self.register_user("u2", "pass")
  143. u2_token = self.login("u2", "pass")
  144. u3 = self.register_user("u3", "pass")
  145. u3_token = self.login("u3", "pass")
  146. room_1 = self.helper.create_room_as(u1, tok=u1_token)
  147. self.helper.send_state(
  148. room_1, event_type="m.room.topic", body={"topic": "foo"}, tok=u1_token
  149. )
  150. # Begin the ingestion by creating the temp tables. This will also store
  151. # the position that the deltas should begin at, once they take over.
  152. self.hs.config.stats_enabled = True
  153. self.handler.stats_enabled = True
  154. self.store.db_pool.updates._all_done = False
  155. self.get_success(
  156. self.store.db_pool.simple_update_one(
  157. table="stats_incremental_position",
  158. keyvalues={},
  159. updatevalues={"stream_id": 0},
  160. )
  161. )
  162. self.get_success(
  163. self.store.db_pool.simple_insert(
  164. "background_updates",
  165. {"update_name": "populate_stats_prepare", "progress_json": "{}"},
  166. )
  167. )
  168. while not self.get_success(
  169. self.store.db_pool.updates.has_completed_background_updates()
  170. ):
  171. self.get_success(
  172. self.store.db_pool.updates.do_next_background_update(100), by=0.1
  173. )
  174. # Now, before the table is actually ingested, add some more events.
  175. self.helper.invite(room=room_1, src=u1, targ=u2, tok=u1_token)
  176. self.helper.join(room=room_1, user=u2, tok=u2_token)
  177. # orig_delta_processor = self.store.
  178. # Now do the initial ingestion.
  179. self.get_success(
  180. self.store.db_pool.simple_insert(
  181. "background_updates",
  182. {"update_name": "populate_stats_process_rooms", "progress_json": "{}"},
  183. )
  184. )
  185. self.get_success(
  186. self.store.db_pool.simple_insert(
  187. "background_updates",
  188. {
  189. "update_name": "populate_stats_cleanup",
  190. "progress_json": "{}",
  191. "depends_on": "populate_stats_process_rooms",
  192. },
  193. )
  194. )
  195. self.store.db_pool.updates._all_done = False
  196. while not self.get_success(
  197. self.store.db_pool.updates.has_completed_background_updates()
  198. ):
  199. self.get_success(
  200. self.store.db_pool.updates.do_next_background_update(100), by=0.1
  201. )
  202. self.reactor.advance(86401)
  203. # Now add some more events, triggering ingestion. Because of the stream
  204. # position being set to before the events sent in the middle, a simpler
  205. # implementation would reprocess those events, and say there were four
  206. # users, not three.
  207. self.helper.invite(room=room_1, src=u1, targ=u3, tok=u1_token)
  208. self.helper.join(room=room_1, user=u3, tok=u3_token)
  209. # self.handler.notify_new_event()
  210. # We need to let the delta processor advance…
  211. self.reactor.advance(10 * 60)
  212. # Get the slices! There should be two -- day 1, and day 2.
  213. r = self.get_success(self.store.get_statistics_for_subject("room", room_1, 0))
  214. self.assertEqual(len(r), 2)
  215. # The oldest has 2 joined members
  216. self.assertEqual(r[-1]["joined_members"], 2)
  217. # The newest has 3
  218. self.assertEqual(r[0]["joined_members"], 3)
  219. def test_create_user(self):
  220. """
  221. When we create a user, it should have statistics already ready.
  222. """
  223. u1 = self.register_user("u1", "pass")
  224. u1stats = self._get_current_stats("user", u1)
  225. self.assertIsNotNone(u1stats)
  226. # not in any rooms by default
  227. self.assertEqual(u1stats["joined_rooms"], 0)
  228. def test_create_room(self):
  229. """
  230. When we create a room, it should have statistics already ready.
  231. """
  232. self._perform_background_initial_update()
  233. u1 = self.register_user("u1", "pass")
  234. u1token = self.login("u1", "pass")
  235. r1 = self.helper.create_room_as(u1, tok=u1token)
  236. r1stats = self._get_current_stats("room", r1)
  237. r2 = self.helper.create_room_as(u1, tok=u1token, is_public=False)
  238. r2stats = self._get_current_stats("room", r2)
  239. self.assertIsNotNone(r1stats)
  240. self.assertIsNotNone(r2stats)
  241. # contains the default things you'd expect in a fresh room
  242. self.assertEqual(
  243. r1stats["total_events"],
  244. EXPT_NUM_STATE_EVTS_IN_FRESH_PUBLIC_ROOM,
  245. "Wrong number of total_events in new room's stats!"
  246. " You may need to update this if more state events are added to"
  247. " the room creation process.",
  248. )
  249. self.assertEqual(
  250. r2stats["total_events"],
  251. EXPT_NUM_STATE_EVTS_IN_FRESH_PRIVATE_ROOM,
  252. "Wrong number of total_events in new room's stats!"
  253. " You may need to update this if more state events are added to"
  254. " the room creation process.",
  255. )
  256. self.assertEqual(
  257. r1stats["current_state_events"], EXPT_NUM_STATE_EVTS_IN_FRESH_PUBLIC_ROOM
  258. )
  259. self.assertEqual(
  260. r2stats["current_state_events"], EXPT_NUM_STATE_EVTS_IN_FRESH_PRIVATE_ROOM
  261. )
  262. self.assertEqual(r1stats["joined_members"], 1)
  263. self.assertEqual(r1stats["invited_members"], 0)
  264. self.assertEqual(r1stats["banned_members"], 0)
  265. self.assertEqual(r2stats["joined_members"], 1)
  266. self.assertEqual(r2stats["invited_members"], 0)
  267. self.assertEqual(r2stats["banned_members"], 0)
  268. def test_send_message_increments_total_events(self):
  269. """
  270. When we send a message, it increments total_events.
  271. """
  272. self._perform_background_initial_update()
  273. u1 = self.register_user("u1", "pass")
  274. u1token = self.login("u1", "pass")
  275. r1 = self.helper.create_room_as(u1, tok=u1token)
  276. r1stats_ante = self._get_current_stats("room", r1)
  277. self.helper.send(r1, "hiss", tok=u1token)
  278. r1stats_post = self._get_current_stats("room", r1)
  279. self.assertEqual(r1stats_post["total_events"] - r1stats_ante["total_events"], 1)
  280. def test_updating_profile_information_does_not_increase_joined_members_count(self):
  281. """
  282. Check that the joined_members count does not increase when a user changes their
  283. profile information (which is done by sending another join membership event into
  284. the room.
  285. """
  286. self._perform_background_initial_update()
  287. # Create a user and room
  288. u1 = self.register_user("u1", "pass")
  289. u1token = self.login("u1", "pass")
  290. r1 = self.helper.create_room_as(u1, tok=u1token)
  291. # Get the current room stats
  292. r1stats_ante = self._get_current_stats("room", r1)
  293. # Send a profile update into the room
  294. new_profile = {"displayname": "bob"}
  295. self.helper.change_membership(
  296. r1, u1, u1, "join", extra_data=new_profile, tok=u1token
  297. )
  298. # Get the new room stats
  299. r1stats_post = self._get_current_stats("room", r1)
  300. # Ensure that the user count did not changed
  301. self.assertEqual(r1stats_post["joined_members"], r1stats_ante["joined_members"])
  302. self.assertEqual(
  303. r1stats_post["local_users_in_room"], r1stats_ante["local_users_in_room"]
  304. )
  305. def test_send_state_event_nonoverwriting(self):
  306. """
  307. When we send a non-overwriting state event, it increments total_events AND current_state_events
  308. """
  309. self._perform_background_initial_update()
  310. u1 = self.register_user("u1", "pass")
  311. u1token = self.login("u1", "pass")
  312. r1 = self.helper.create_room_as(u1, tok=u1token)
  313. self.helper.send_state(
  314. r1, "cat.hissing", {"value": True}, tok=u1token, state_key="tabby"
  315. )
  316. r1stats_ante = self._get_current_stats("room", r1)
  317. self.helper.send_state(
  318. r1, "cat.hissing", {"value": False}, tok=u1token, state_key="moggy"
  319. )
  320. r1stats_post = self._get_current_stats("room", r1)
  321. self.assertEqual(r1stats_post["total_events"] - r1stats_ante["total_events"], 1)
  322. self.assertEqual(
  323. r1stats_post["current_state_events"] - r1stats_ante["current_state_events"],
  324. 1,
  325. )
  326. def test_send_state_event_overwriting(self):
  327. """
  328. When we send an overwriting state event, it increments total_events ONLY
  329. """
  330. self._perform_background_initial_update()
  331. u1 = self.register_user("u1", "pass")
  332. u1token = self.login("u1", "pass")
  333. r1 = self.helper.create_room_as(u1, tok=u1token)
  334. self.helper.send_state(
  335. r1, "cat.hissing", {"value": True}, tok=u1token, state_key="tabby"
  336. )
  337. r1stats_ante = self._get_current_stats("room", r1)
  338. self.helper.send_state(
  339. r1, "cat.hissing", {"value": False}, tok=u1token, state_key="tabby"
  340. )
  341. r1stats_post = self._get_current_stats("room", r1)
  342. self.assertEqual(r1stats_post["total_events"] - r1stats_ante["total_events"], 1)
  343. self.assertEqual(
  344. r1stats_post["current_state_events"] - r1stats_ante["current_state_events"],
  345. 0,
  346. )
  347. def test_join_first_time(self):
  348. """
  349. When a user joins a room for the first time, total_events, current_state_events and
  350. joined_members should increase by exactly 1.
  351. """
  352. self._perform_background_initial_update()
  353. u1 = self.register_user("u1", "pass")
  354. u1token = self.login("u1", "pass")
  355. r1 = self.helper.create_room_as(u1, tok=u1token)
  356. u2 = self.register_user("u2", "pass")
  357. u2token = self.login("u2", "pass")
  358. r1stats_ante = self._get_current_stats("room", r1)
  359. self.helper.join(r1, u2, tok=u2token)
  360. r1stats_post = self._get_current_stats("room", r1)
  361. self.assertEqual(r1stats_post["total_events"] - r1stats_ante["total_events"], 1)
  362. self.assertEqual(
  363. r1stats_post["current_state_events"] - r1stats_ante["current_state_events"],
  364. 1,
  365. )
  366. self.assertEqual(
  367. r1stats_post["joined_members"] - r1stats_ante["joined_members"], 1
  368. )
  369. def test_join_after_leave(self):
  370. """
  371. When a user joins a room after being previously left, total_events and
  372. joined_members should increase by exactly 1.
  373. current_state_events should not increase.
  374. left_members should decrease by exactly 1.
  375. """
  376. self._perform_background_initial_update()
  377. u1 = self.register_user("u1", "pass")
  378. u1token = self.login("u1", "pass")
  379. r1 = self.helper.create_room_as(u1, tok=u1token)
  380. u2 = self.register_user("u2", "pass")
  381. u2token = self.login("u2", "pass")
  382. self.helper.join(r1, u2, tok=u2token)
  383. self.helper.leave(r1, u2, tok=u2token)
  384. r1stats_ante = self._get_current_stats("room", r1)
  385. self.helper.join(r1, u2, tok=u2token)
  386. r1stats_post = self._get_current_stats("room", r1)
  387. self.assertEqual(r1stats_post["total_events"] - r1stats_ante["total_events"], 1)
  388. self.assertEqual(
  389. r1stats_post["current_state_events"] - r1stats_ante["current_state_events"],
  390. 0,
  391. )
  392. self.assertEqual(
  393. r1stats_post["joined_members"] - r1stats_ante["joined_members"], +1
  394. )
  395. self.assertEqual(
  396. r1stats_post["left_members"] - r1stats_ante["left_members"], -1
  397. )
  398. def test_invited(self):
  399. """
  400. When a user invites another user, current_state_events, total_events and
  401. invited_members should increase by exactly 1.
  402. """
  403. self._perform_background_initial_update()
  404. u1 = self.register_user("u1", "pass")
  405. u1token = self.login("u1", "pass")
  406. r1 = self.helper.create_room_as(u1, tok=u1token)
  407. u2 = self.register_user("u2", "pass")
  408. r1stats_ante = self._get_current_stats("room", r1)
  409. self.helper.invite(r1, u1, u2, tok=u1token)
  410. r1stats_post = self._get_current_stats("room", r1)
  411. self.assertEqual(r1stats_post["total_events"] - r1stats_ante["total_events"], 1)
  412. self.assertEqual(
  413. r1stats_post["current_state_events"] - r1stats_ante["current_state_events"],
  414. 1,
  415. )
  416. self.assertEqual(
  417. r1stats_post["invited_members"] - r1stats_ante["invited_members"], +1
  418. )
  419. def test_join_after_invite(self):
  420. """
  421. When a user joins a room after being invited, total_events and
  422. joined_members should increase by exactly 1.
  423. current_state_events should not increase.
  424. invited_members should decrease by exactly 1.
  425. """
  426. self._perform_background_initial_update()
  427. u1 = self.register_user("u1", "pass")
  428. u1token = self.login("u1", "pass")
  429. r1 = self.helper.create_room_as(u1, tok=u1token)
  430. u2 = self.register_user("u2", "pass")
  431. u2token = self.login("u2", "pass")
  432. self.helper.invite(r1, u1, u2, tok=u1token)
  433. r1stats_ante = self._get_current_stats("room", r1)
  434. self.helper.join(r1, u2, tok=u2token)
  435. r1stats_post = self._get_current_stats("room", r1)
  436. self.assertEqual(r1stats_post["total_events"] - r1stats_ante["total_events"], 1)
  437. self.assertEqual(
  438. r1stats_post["current_state_events"] - r1stats_ante["current_state_events"],
  439. 0,
  440. )
  441. self.assertEqual(
  442. r1stats_post["joined_members"] - r1stats_ante["joined_members"], +1
  443. )
  444. self.assertEqual(
  445. r1stats_post["invited_members"] - r1stats_ante["invited_members"], -1
  446. )
  447. def test_left(self):
  448. """
  449. When a user leaves a room after joining, total_events and
  450. left_members should increase by exactly 1.
  451. current_state_events should not increase.
  452. joined_members should decrease by exactly 1.
  453. """
  454. self._perform_background_initial_update()
  455. u1 = self.register_user("u1", "pass")
  456. u1token = self.login("u1", "pass")
  457. r1 = self.helper.create_room_as(u1, tok=u1token)
  458. u2 = self.register_user("u2", "pass")
  459. u2token = self.login("u2", "pass")
  460. self.helper.join(r1, u2, tok=u2token)
  461. r1stats_ante = self._get_current_stats("room", r1)
  462. self.helper.leave(r1, u2, tok=u2token)
  463. r1stats_post = self._get_current_stats("room", r1)
  464. self.assertEqual(r1stats_post["total_events"] - r1stats_ante["total_events"], 1)
  465. self.assertEqual(
  466. r1stats_post["current_state_events"] - r1stats_ante["current_state_events"],
  467. 0,
  468. )
  469. self.assertEqual(
  470. r1stats_post["left_members"] - r1stats_ante["left_members"], +1
  471. )
  472. self.assertEqual(
  473. r1stats_post["joined_members"] - r1stats_ante["joined_members"], -1
  474. )
  475. def test_banned(self):
  476. """
  477. When a user is banned from a room after joining, total_events and
  478. left_members should increase by exactly 1.
  479. current_state_events should not increase.
  480. banned_members should decrease by exactly 1.
  481. """
  482. self._perform_background_initial_update()
  483. u1 = self.register_user("u1", "pass")
  484. u1token = self.login("u1", "pass")
  485. r1 = self.helper.create_room_as(u1, tok=u1token)
  486. u2 = self.register_user("u2", "pass")
  487. u2token = self.login("u2", "pass")
  488. self.helper.join(r1, u2, tok=u2token)
  489. r1stats_ante = self._get_current_stats("room", r1)
  490. self.helper.change_membership(r1, u1, u2, "ban", tok=u1token)
  491. r1stats_post = self._get_current_stats("room", r1)
  492. self.assertEqual(r1stats_post["total_events"] - r1stats_ante["total_events"], 1)
  493. self.assertEqual(
  494. r1stats_post["current_state_events"] - r1stats_ante["current_state_events"],
  495. 0,
  496. )
  497. self.assertEqual(
  498. r1stats_post["banned_members"] - r1stats_ante["banned_members"], +1
  499. )
  500. self.assertEqual(
  501. r1stats_post["joined_members"] - r1stats_ante["joined_members"], -1
  502. )
  503. def test_initial_background_update(self):
  504. """
  505. Test that statistics can be generated by the initial background update
  506. handler.
  507. This test also tests that stats rows are not created for new subjects
  508. when stats are disabled. However, it may be desirable to change this
  509. behaviour eventually to still keep current rows.
  510. """
  511. self.hs.config.stats_enabled = False
  512. self.handler.stats_enabled = False
  513. u1 = self.register_user("u1", "pass")
  514. u1token = self.login("u1", "pass")
  515. r1 = self.helper.create_room_as(u1, tok=u1token)
  516. # test that these subjects, which were created during a time of disabled
  517. # stats, do not have stats.
  518. self.assertIsNone(self._get_current_stats("room", r1))
  519. self.assertIsNone(self._get_current_stats("user", u1))
  520. self.hs.config.stats_enabled = True
  521. self.handler.stats_enabled = True
  522. self._perform_background_initial_update()
  523. r1stats = self._get_current_stats("room", r1)
  524. u1stats = self._get_current_stats("user", u1)
  525. self.assertEqual(r1stats["joined_members"], 1)
  526. self.assertEqual(
  527. r1stats["current_state_events"], EXPT_NUM_STATE_EVTS_IN_FRESH_PUBLIC_ROOM
  528. )
  529. self.assertEqual(u1stats["joined_rooms"], 1)
  530. def test_incomplete_stats(self):
  531. """
  532. This tests that we track incomplete statistics.
  533. We first test that incomplete stats are incrementally generated,
  534. following the preparation of a background regen.
  535. We then test that these incomplete rows are completed by the background
  536. regen.
  537. """
  538. u1 = self.register_user("u1", "pass")
  539. u1token = self.login("u1", "pass")
  540. u2 = self.register_user("u2", "pass")
  541. u2token = self.login("u2", "pass")
  542. u3 = self.register_user("u3", "pass")
  543. r1 = self.helper.create_room_as(u1, tok=u1token, is_public=False)
  544. # preparation stage of the initial background update
  545. # Ugh, have to reset this flag
  546. self.store.db_pool.updates._all_done = False
  547. self.get_success(
  548. self.store.db_pool.simple_delete(
  549. "room_stats_current", {"1": 1}, "test_delete_stats"
  550. )
  551. )
  552. self.get_success(
  553. self.store.db_pool.simple_delete(
  554. "user_stats_current", {"1": 1}, "test_delete_stats"
  555. )
  556. )
  557. self.helper.invite(r1, u1, u2, tok=u1token)
  558. self.helper.join(r1, u2, tok=u2token)
  559. self.helper.invite(r1, u1, u3, tok=u1token)
  560. self.helper.send(r1, "thou shalt yield", tok=u1token)
  561. # now do the background updates
  562. self.store.db_pool.updates._all_done = False
  563. self.get_success(
  564. self.store.db_pool.simple_insert(
  565. "background_updates",
  566. {
  567. "update_name": "populate_stats_process_rooms",
  568. "progress_json": "{}",
  569. "depends_on": "populate_stats_prepare",
  570. },
  571. )
  572. )
  573. self.get_success(
  574. self.store.db_pool.simple_insert(
  575. "background_updates",
  576. {
  577. "update_name": "populate_stats_process_users",
  578. "progress_json": "{}",
  579. "depends_on": "populate_stats_process_rooms",
  580. },
  581. )
  582. )
  583. self.get_success(
  584. self.store.db_pool.simple_insert(
  585. "background_updates",
  586. {
  587. "update_name": "populate_stats_cleanup",
  588. "progress_json": "{}",
  589. "depends_on": "populate_stats_process_users",
  590. },
  591. )
  592. )
  593. while not self.get_success(
  594. self.store.db_pool.updates.has_completed_background_updates()
  595. ):
  596. self.get_success(
  597. self.store.db_pool.updates.do_next_background_update(100), by=0.1
  598. )
  599. r1stats_complete = self._get_current_stats("room", r1)
  600. u1stats_complete = self._get_current_stats("user", u1)
  601. u2stats_complete = self._get_current_stats("user", u2)
  602. # now we make our assertions
  603. # check that _complete rows are complete and correct
  604. self.assertEqual(r1stats_complete["joined_members"], 2)
  605. self.assertEqual(r1stats_complete["invited_members"], 1)
  606. self.assertEqual(
  607. r1stats_complete["current_state_events"],
  608. 2 + EXPT_NUM_STATE_EVTS_IN_FRESH_PRIVATE_ROOM,
  609. )
  610. self.assertEqual(u1stats_complete["joined_rooms"], 1)
  611. self.assertEqual(u2stats_complete["joined_rooms"], 1)