test_stats.py 26 KB

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