test_sessions.py 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584
  1. from __future__ import annotations
  2. import time
  3. from datetime import UTC, datetime, timedelta
  4. from unittest import mock
  5. import pytest
  6. from django.utils import timezone
  7. from sentry.release_health.base import OverviewStat
  8. from sentry.release_health.metrics import MetricsReleaseHealthBackend
  9. from sentry.sentry_metrics.use_case_id_registry import UseCaseID
  10. from sentry.snuba.sessions import _make_stats
  11. from sentry.testutils.cases import BaseMetricsTestCase, TestCase
  12. pytestmark = pytest.mark.sentry_metrics
  13. def format_timestamp(dt):
  14. if not isinstance(dt, datetime):
  15. dt = datetime.fromtimestamp(dt)
  16. return dt.strftime("%Y-%m-%dT%H:%M:%S+00:00")
  17. def make_24h_stats(ts, adjust_start=False):
  18. ret_val = _make_stats(datetime.fromtimestamp(ts, UTC), 3600, 24)
  19. if adjust_start:
  20. # HACK this adds another interval at the beginning in accordance with the new way of calculating intervals
  21. # https://www.notion.so/sentry/Metrics-Layer-get_intervals-bug-dce140607d054201a5e6629b070cb969
  22. ret_val.insert(0, [ret_val[0][0] - 3600, 0])
  23. return ret_val
  24. class SnubaSessionsTest(TestCase, BaseMetricsTestCase):
  25. backend = MetricsReleaseHealthBackend()
  26. def setUp(self):
  27. super().setUp()
  28. self.received = time.time()
  29. self.session_started = time.time() // 60 * 60
  30. self.session_release = "foo@1.0.0"
  31. self.session_crashed_release = "foo@2.0.0"
  32. session_1 = "5d52fd05-fcc9-4bf3-9dc9-267783670341"
  33. session_2 = "5e910c1a-6941-460e-9843-24103fb6a63c"
  34. session_3 = "a148c0c5-06a2-423b-8901-6b43b812cf82"
  35. user_1 = "39887d89-13b2-4c84-8c23-5d13d2102666"
  36. self.store_session(
  37. self.build_session(
  38. distinct_id=user_1,
  39. session_id=session_1,
  40. status="exited",
  41. release=self.session_release,
  42. environment="prod",
  43. started=self.session_started,
  44. received=self.received,
  45. )
  46. )
  47. self.store_session(
  48. self.build_session(
  49. distinct_id=user_1,
  50. session_id=session_2,
  51. release=self.session_release,
  52. environment="prod",
  53. duration=None,
  54. started=self.session_started,
  55. received=self.received,
  56. )
  57. )
  58. self.store_session(
  59. self.build_session(
  60. distinct_id=user_1,
  61. session_id=session_2,
  62. seq=1,
  63. duration=30,
  64. status="exited",
  65. release=self.session_release,
  66. environment="prod",
  67. started=self.session_started,
  68. received=self.received,
  69. )
  70. )
  71. self.store_session(
  72. self.build_session(
  73. distinct_id=user_1,
  74. session_id=session_3,
  75. status="crashed",
  76. release=self.session_crashed_release,
  77. environment="prod",
  78. started=self.session_started,
  79. received=self.received,
  80. )
  81. )
  82. def test_get_oldest_health_data_for_releases(self):
  83. data = self.backend.get_oldest_health_data_for_releases(
  84. [(self.project.id, self.session_release)]
  85. )
  86. assert data == {
  87. (self.project.id, self.session_release): format_timestamp(
  88. self.session_started // 3600 * 3600
  89. )
  90. }
  91. def test_check_has_health_data(self):
  92. data = self.backend.check_has_health_data(
  93. [(self.project.id, self.session_release), (self.project.id, "dummy-release")]
  94. )
  95. assert data == {(self.project.id, self.session_release)}
  96. def test_check_has_health_data_without_releases_should_include_sessions_lte_90_days(self):
  97. """
  98. Test that ensures that `check_has_health_data` returns a set of projects that has health
  99. data within the last 90d if only a list of project ids is provided and any project with
  100. session data earlier than 90 days should be included
  101. """
  102. project2 = self.create_project(
  103. name="Bar2",
  104. slug="bar2",
  105. teams=[self.team],
  106. fire_project_created=True,
  107. organization=self.organization,
  108. )
  109. self.store_session(
  110. self.build_session(
  111. **{
  112. "project_id": project2.id,
  113. "org_id": project2.organization_id,
  114. "status": "exited",
  115. }
  116. )
  117. )
  118. data = self.backend.check_has_health_data([self.project.id, project2.id])
  119. assert data == {self.project.id, project2.id}
  120. def test_check_has_health_data_does_not_crash_when_sending_projects_list_as_set(self):
  121. data = self.backend.check_has_health_data({self.project.id})
  122. assert data == {self.project.id}
  123. def test_get_project_releases_by_stability(self):
  124. # Add an extra session with a different `distinct_id` so that sorting by users
  125. # is stable
  126. self.store_session(
  127. self.build_session(
  128. release=self.session_release,
  129. environment="prod",
  130. started=self.session_started,
  131. received=self.received,
  132. )
  133. )
  134. for scope in "sessions", "users":
  135. data = self.backend.get_project_releases_by_stability(
  136. [self.project.id], offset=0, limit=100, scope=scope, stats_period="24h"
  137. )
  138. assert data == [
  139. (self.project.id, self.session_release),
  140. (self.project.id, self.session_crashed_release),
  141. ]
  142. def test_get_project_releases_by_stability_for_crash_free_sort(self):
  143. """
  144. Test that ensures that using crash free rate sort options, returns a list of ASC releases
  145. according to the chosen crash_free sort option
  146. """
  147. # add another user to session_release to make sure that they are sorted correctly
  148. self.store_session(
  149. self.build_session(
  150. status="exited",
  151. release=self.session_release,
  152. environment="prod",
  153. started=self.session_started,
  154. received=self.received,
  155. )
  156. )
  157. for scope in "crash_free_sessions", "crash_free_users":
  158. data = self.backend.get_project_releases_by_stability(
  159. [self.project.id], offset=0, limit=100, scope=scope, stats_period="24h"
  160. )
  161. assert data == [
  162. (self.project.id, self.session_crashed_release),
  163. (self.project.id, self.session_release),
  164. ]
  165. def test_get_project_releases_by_stability_for_releases_with_users_data(self):
  166. """
  167. Test that ensures if releases contain no users data, then those releases should not be
  168. returned on `users` and `crash_free_users` sorts
  169. """
  170. self.store_session(
  171. self.build_session(
  172. distinct_id=None,
  173. release="release-with-no-users",
  174. environment="prod",
  175. started=self.session_started,
  176. received=self.received,
  177. )
  178. )
  179. data = self.backend.get_project_releases_by_stability(
  180. [self.project.id], offset=0, limit=100, scope="users", stats_period="24h"
  181. )
  182. assert set(data) == {
  183. (self.project.id, self.session_release),
  184. (self.project.id, self.session_crashed_release),
  185. }
  186. data = self.backend.get_project_releases_by_stability(
  187. [self.project.id], offset=0, limit=100, scope="crash_free_users", stats_period="24h"
  188. )
  189. assert set(data) == {
  190. (self.project.id, self.session_crashed_release),
  191. (self.project.id, self.session_release),
  192. }
  193. def test_get_release_adoption(self):
  194. data = self.backend.get_release_adoption(
  195. [
  196. (self.project.id, self.session_release),
  197. (self.project.id, self.session_crashed_release),
  198. (self.project.id, "dummy-release"),
  199. ]
  200. )
  201. assert data == {
  202. (self.project.id, self.session_release): {
  203. "sessions_24h": 2,
  204. "users_24h": 1,
  205. "adoption": 100.0,
  206. "sessions_adoption": 66.66666666666666,
  207. "project_sessions_24h": 3,
  208. "project_users_24h": 1,
  209. },
  210. (self.project.id, self.session_crashed_release): {
  211. "sessions_24h": 1,
  212. "users_24h": 1,
  213. "adoption": 100.0,
  214. "sessions_adoption": 33.33333333333333,
  215. "project_sessions_24h": 3,
  216. "project_users_24h": 1,
  217. },
  218. }
  219. def test_get_release_adoption_lowered(self):
  220. self.store_session(
  221. self.build_session(
  222. release=self.session_crashed_release,
  223. environment="prod",
  224. status="crashed",
  225. started=self.session_started,
  226. received=self.received,
  227. )
  228. )
  229. data = self.backend.get_release_adoption(
  230. [
  231. (self.project.id, self.session_release),
  232. (self.project.id, self.session_crashed_release),
  233. (self.project.id, "dummy-release"),
  234. ]
  235. )
  236. assert data == {
  237. (self.project.id, self.session_release): {
  238. "sessions_24h": 2,
  239. "users_24h": 1,
  240. "adoption": 50.0,
  241. "sessions_adoption": 50.0,
  242. "project_sessions_24h": 4,
  243. "project_users_24h": 2,
  244. },
  245. (self.project.id, self.session_crashed_release): {
  246. "sessions_24h": 2,
  247. "users_24h": 2,
  248. "adoption": 100.0,
  249. "sessions_adoption": 50.0,
  250. "project_sessions_24h": 4,
  251. "project_users_24h": 2,
  252. },
  253. }
  254. def test_fetching_release_sessions_time_bounds_for_different_release(self):
  255. """
  256. Test that ensures only session bounds for releases are calculated according
  257. to their respective release
  258. """
  259. # Same release session
  260. self.store_session(
  261. self.build_session(
  262. release=self.session_release,
  263. environment="prod",
  264. status="exited",
  265. started=self.session_started - 3600 * 2,
  266. received=self.received - 3600 * 2,
  267. )
  268. )
  269. # Different release session
  270. self.store_session(
  271. self.build_session(
  272. release=self.session_crashed_release,
  273. environment="prod",
  274. status="crashed",
  275. started=self.session_started - 3600 * 2,
  276. received=self.received - 3600 * 2,
  277. )
  278. )
  279. expected_formatted_lower_bound = (
  280. datetime.fromtimestamp(self.session_started - 3600 * 2)
  281. .replace(minute=0)
  282. .isoformat()[:19]
  283. + "Z"
  284. )
  285. expected_formatted_upper_bound = (
  286. datetime.fromtimestamp(self.session_started).replace(minute=0).isoformat()[:19] + "Z"
  287. )
  288. # Test for self.session_release
  289. data = self.backend.get_release_sessions_time_bounds(
  290. project_id=self.project.id,
  291. release=self.session_release,
  292. org_id=self.organization.id,
  293. environments=["prod"],
  294. )
  295. assert data == {
  296. "sessions_lower_bound": expected_formatted_lower_bound,
  297. "sessions_upper_bound": expected_formatted_upper_bound,
  298. }
  299. # Test for self.session_crashed_release
  300. data = self.backend.get_release_sessions_time_bounds(
  301. project_id=self.project.id,
  302. release=self.session_crashed_release,
  303. org_id=self.organization.id,
  304. environments=["prod"],
  305. )
  306. assert data == {
  307. "sessions_lower_bound": expected_formatted_lower_bound,
  308. "sessions_upper_bound": expected_formatted_upper_bound,
  309. }
  310. def test_fetching_release_sessions_time_bounds_for_different_release_with_no_sessions(self):
  311. """
  312. Test that ensures if no sessions are available for a specific release then the bounds
  313. should be returned as None
  314. """
  315. data = self.backend.get_release_sessions_time_bounds(
  316. project_id=self.project.id,
  317. release="different_release",
  318. org_id=self.organization.id,
  319. environments=["prod"],
  320. )
  321. assert data == {
  322. "sessions_lower_bound": None,
  323. "sessions_upper_bound": None,
  324. }
  325. def test_get_crash_free_breakdown(self):
  326. start = timezone.now() - timedelta(days=4)
  327. # it should work with and without environments
  328. for environments in [None, ["prod"]]:
  329. data = self.backend.get_crash_free_breakdown(
  330. project_id=self.project.id,
  331. release=self.session_release,
  332. start=start,
  333. environments=environments,
  334. )
  335. # Last returned date is generated within function, should be close to now:
  336. last_date = data[-1]["date"]
  337. assert timezone.now() - last_date < timedelta(seconds=1)
  338. assert data == [
  339. {
  340. "crash_free_sessions": None,
  341. "crash_free_users": None,
  342. "date": start + timedelta(days=1),
  343. "total_sessions": 0,
  344. "total_users": 0,
  345. },
  346. {
  347. "crash_free_sessions": None,
  348. "crash_free_users": None,
  349. "date": start + timedelta(days=2),
  350. "total_sessions": 0,
  351. "total_users": 0,
  352. },
  353. {
  354. "crash_free_sessions": 100.0,
  355. "crash_free_users": 100.0,
  356. "total_sessions": 2,
  357. "total_users": 1,
  358. "date": mock.ANY, # tested above
  359. },
  360. ]
  361. data = self.backend.get_crash_free_breakdown(
  362. project_id=self.project.id,
  363. release=self.session_crashed_release,
  364. start=start,
  365. environments=["prod"],
  366. )
  367. assert data == [
  368. {
  369. "crash_free_sessions": None,
  370. "crash_free_users": None,
  371. "date": start + timedelta(days=1),
  372. "total_sessions": 0,
  373. "total_users": 0,
  374. },
  375. {
  376. "crash_free_sessions": None,
  377. "crash_free_users": None,
  378. "date": start + timedelta(days=2),
  379. "total_sessions": 0,
  380. "total_users": 0,
  381. },
  382. {
  383. "crash_free_sessions": 0.0,
  384. "crash_free_users": 0.0,
  385. "total_sessions": 1,
  386. "total_users": 1,
  387. "date": mock.ANY,
  388. },
  389. ]
  390. data = self.backend.get_crash_free_breakdown(
  391. project_id=self.project.id,
  392. release="non-existing",
  393. start=start,
  394. environments=["prod"],
  395. )
  396. assert data == [
  397. {
  398. "crash_free_sessions": None,
  399. "crash_free_users": None,
  400. "date": start + timedelta(days=1),
  401. "total_sessions": 0,
  402. "total_users": 0,
  403. },
  404. {
  405. "crash_free_sessions": None,
  406. "crash_free_users": None,
  407. "date": start + timedelta(days=2),
  408. "total_sessions": 0,
  409. "total_users": 0,
  410. },
  411. {
  412. "crash_free_sessions": None,
  413. "crash_free_users": None,
  414. "total_sessions": 0,
  415. "total_users": 0,
  416. "date": mock.ANY,
  417. },
  418. ]
  419. def test_basic_release_model_adoptions(self):
  420. """
  421. Test that the basic (project,release) data is returned
  422. """
  423. proj_id = self.project.id
  424. data = self.backend.get_changed_project_release_model_adoptions([proj_id])
  425. assert set(data) == {(proj_id, "foo@1.0.0"), (proj_id, "foo@2.0.0")}
  426. def test_old_release_model_adoptions(self):
  427. """
  428. Test that old entries (older that 72 h) are not returned
  429. """
  430. _100h = 100 * 60 * 60 # 100 hours in seconds
  431. proj_id = self.project.id
  432. self.store_session(
  433. self.build_session(
  434. release="foo@3.0.0",
  435. environment="prod",
  436. status="crashed",
  437. started=self.session_started - _100h,
  438. received=self.received - 3600 * 2,
  439. )
  440. )
  441. data = self.backend.get_changed_project_release_model_adoptions([proj_id])
  442. assert set(data) == {(proj_id, "foo@1.0.0"), (proj_id, "foo@2.0.0")}
  443. def test_multi_proj_release_model_adoptions(self):
  444. """Test that the api works with multiple projects"""
  445. proj_id = self.project.id
  446. new_proj_id = proj_id + 1
  447. self.store_session(
  448. self.build_session(
  449. project_id=new_proj_id,
  450. release="foo@3.0.0",
  451. environment="prod",
  452. status="crashed",
  453. started=self.session_started,
  454. received=self.received - 3600 * 2,
  455. )
  456. )
  457. data = self.backend.get_changed_project_release_model_adoptions([proj_id, new_proj_id])
  458. assert set(data) == {
  459. (proj_id, "foo@1.0.0"),
  460. (proj_id, "foo@2.0.0"),
  461. (new_proj_id, "foo@3.0.0"),
  462. }
  463. @staticmethod
  464. def _add_timestamps_to_series(series, start: datetime):
  465. one_day = 24 * 60 * 60
  466. day0 = one_day * int(start.timestamp() / one_day)
  467. def ts(days: int) -> int:
  468. return day0 + days * one_day
  469. return [(ts(i + 1), data) for i, data in enumerate(series)]
  470. def _test_get_project_release_stats(
  471. self, stat: OverviewStat, release: str, expected_series, expected_totals
  472. ):
  473. end = timezone.now()
  474. start = end - timedelta(days=4)
  475. stats, totals = self.backend.get_project_release_stats(
  476. self.project.id,
  477. release=release,
  478. stat=stat,
  479. rollup=86400,
  480. start=start,
  481. end=end,
  482. )
  483. # one system returns lists instead of tuples
  484. normed = [(ts, data) for ts, data in stats]
  485. assert normed == self._add_timestamps_to_series(expected_series, start)
  486. assert totals == expected_totals
  487. def test_get_project_release_stats_users(self):
  488. self._test_get_project_release_stats(
  489. "users",
  490. self.session_release,
  491. [
  492. {
  493. "duration_p50": None,
  494. "duration_p90": None,
  495. "users": 0,
  496. "users_abnormal": 0,
  497. "users_crashed": 0,
  498. "users_errored": 0,
  499. "users_healthy": 0,
  500. },
  501. {
  502. "duration_p50": None,
  503. "duration_p90": None,
  504. "users": 0,
  505. "users_abnormal": 0,
  506. "users_crashed": 0,
  507. "users_errored": 0,
  508. "users_healthy": 0,
  509. },
  510. {
  511. "duration_p50": None,
  512. "duration_p90": None,
  513. "users": 0,
  514. "users_abnormal": 0,
  515. "users_crashed": 0,
  516. "users_errored": 0,
  517. "users_healthy": 0,
  518. },
  519. {
  520. "duration_p50": 45.0,
  521. "duration_p90": 57.0,
  522. "users": 1,
  523. "users_abnormal": 0,
  524. "users_crashed": 0,
  525. "users_errored": 0,
  526. "users_healthy": 1,
  527. },
  528. ],
  529. {
  530. "users": 1,
  531. "users_abnormal": 0,
  532. "users_crashed": 0,
  533. "users_errored": 0,
  534. "users_healthy": 1,
  535. },
  536. )
  537. def test_get_project_release_stats_users_crashed(self):
  538. self._test_get_project_release_stats(
  539. "users",
  540. self.session_crashed_release,
  541. [
  542. {
  543. "duration_p50": None,
  544. "duration_p90": None,
  545. "users": 0,
  546. "users_abnormal": 0,
  547. "users_crashed": 0,
  548. "users_errored": 0,
  549. "users_healthy": 0,
  550. },
  551. {
  552. "duration_p50": None,
  553. "duration_p90": None,
  554. "users": 0,
  555. "users_abnormal": 0,
  556. "users_crashed": 0,
  557. "users_errored": 0,
  558. "users_healthy": 0,
  559. },
  560. {
  561. "duration_p50": None,
  562. "duration_p90": None,
  563. "users": 0,
  564. "users_abnormal": 0,
  565. "users_crashed": 0,
  566. "users_errored": 0,
  567. "users_healthy": 0,
  568. },
  569. {
  570. "duration_p50": None,
  571. "duration_p90": None,
  572. "users": 1,
  573. "users_abnormal": 0,
  574. "users_crashed": 1,
  575. "users_errored": 0,
  576. "users_healthy": 0,
  577. },
  578. ],
  579. {
  580. "users": 1,
  581. "users_abnormal": 0,
  582. "users_crashed": 1,
  583. "users_errored": 0,
  584. "users_healthy": 0,
  585. },
  586. )
  587. def test_get_project_release_stats_sessions(self):
  588. self._test_get_project_release_stats(
  589. "sessions",
  590. self.session_release,
  591. [
  592. {
  593. "duration_p50": None,
  594. "duration_p90": None,
  595. "sessions": 0,
  596. "sessions_abnormal": 0,
  597. "sessions_crashed": 0,
  598. "sessions_errored": 0,
  599. "sessions_healthy": 0,
  600. },
  601. {
  602. "duration_p50": None,
  603. "duration_p90": None,
  604. "sessions": 0,
  605. "sessions_abnormal": 0,
  606. "sessions_crashed": 0,
  607. "sessions_errored": 0,
  608. "sessions_healthy": 0,
  609. },
  610. {
  611. "duration_p50": None,
  612. "duration_p90": None,
  613. "sessions": 0,
  614. "sessions_abnormal": 0,
  615. "sessions_crashed": 0,
  616. "sessions_errored": 0,
  617. "sessions_healthy": 0,
  618. },
  619. {
  620. "duration_p50": 45.0,
  621. "duration_p90": 57.0,
  622. "sessions": 2,
  623. "sessions_abnormal": 0,
  624. "sessions_crashed": 0,
  625. "sessions_errored": 0,
  626. "sessions_healthy": 2,
  627. },
  628. ],
  629. {
  630. "sessions": 2,
  631. "sessions_abnormal": 0,
  632. "sessions_crashed": 0,
  633. "sessions_errored": 0,
  634. "sessions_healthy": 2,
  635. },
  636. )
  637. def test_get_project_release_stats_sessions_crashed(self):
  638. self._test_get_project_release_stats(
  639. "sessions",
  640. self.session_crashed_release,
  641. [
  642. {
  643. "duration_p50": None,
  644. "duration_p90": None,
  645. "sessions": 0,
  646. "sessions_abnormal": 0,
  647. "sessions_crashed": 0,
  648. "sessions_errored": 0,
  649. "sessions_healthy": 0,
  650. },
  651. {
  652. "duration_p50": None,
  653. "duration_p90": None,
  654. "sessions": 0,
  655. "sessions_abnormal": 0,
  656. "sessions_crashed": 0,
  657. "sessions_errored": 0,
  658. "sessions_healthy": 0,
  659. },
  660. {
  661. "duration_p50": None,
  662. "duration_p90": None,
  663. "sessions": 0,
  664. "sessions_abnormal": 0,
  665. "sessions_crashed": 0,
  666. "sessions_errored": 0,
  667. "sessions_healthy": 0,
  668. },
  669. {
  670. "duration_p50": None,
  671. "duration_p90": None,
  672. "sessions": 1,
  673. "sessions_abnormal": 0,
  674. "sessions_crashed": 1,
  675. "sessions_errored": 0,
  676. "sessions_healthy": 0,
  677. },
  678. ],
  679. {
  680. "sessions": 1,
  681. "sessions_abnormal": 0,
  682. "sessions_crashed": 1,
  683. "sessions_errored": 0,
  684. "sessions_healthy": 0,
  685. },
  686. )
  687. def test_get_project_release_stats_no_sessions(self):
  688. """
  689. Test still returning correct data when no sessions are available
  690. :return:
  691. """
  692. self._test_get_project_release_stats(
  693. "sessions",
  694. "INEXISTENT-RELEASE",
  695. [
  696. {
  697. "duration_p50": None,
  698. "duration_p90": None,
  699. "sessions": 0,
  700. "sessions_abnormal": 0,
  701. "sessions_crashed": 0,
  702. "sessions_errored": 0,
  703. "sessions_healthy": 0,
  704. },
  705. {
  706. "duration_p50": None,
  707. "duration_p90": None,
  708. "sessions": 0,
  709. "sessions_abnormal": 0,
  710. "sessions_crashed": 0,
  711. "sessions_errored": 0,
  712. "sessions_healthy": 0,
  713. },
  714. {
  715. "duration_p50": None,
  716. "duration_p90": None,
  717. "sessions": 0,
  718. "sessions_abnormal": 0,
  719. "sessions_crashed": 0,
  720. "sessions_errored": 0,
  721. "sessions_healthy": 0,
  722. },
  723. {
  724. "duration_p50": None,
  725. "duration_p90": None,
  726. "sessions": 0,
  727. "sessions_abnormal": 0,
  728. "sessions_crashed": 0,
  729. "sessions_errored": 0,
  730. "sessions_healthy": 0,
  731. },
  732. ],
  733. {
  734. "sessions": 0,
  735. "sessions_abnormal": 0,
  736. "sessions_crashed": 0,
  737. "sessions_errored": 0,
  738. "sessions_healthy": 0,
  739. },
  740. )
  741. def test_get_project_release_stats_no_users(self):
  742. self._test_get_project_release_stats(
  743. "users",
  744. "INEXISTENT-RELEASE",
  745. [
  746. {
  747. "duration_p50": None,
  748. "duration_p90": None,
  749. "users": 0,
  750. "users_abnormal": 0,
  751. "users_crashed": 0,
  752. "users_errored": 0,
  753. "users_healthy": 0,
  754. },
  755. {
  756. "duration_p50": None,
  757. "duration_p90": None,
  758. "users": 0,
  759. "users_abnormal": 0,
  760. "users_crashed": 0,
  761. "users_errored": 0,
  762. "users_healthy": 0,
  763. },
  764. {
  765. "duration_p50": None,
  766. "duration_p90": None,
  767. "users": 0,
  768. "users_abnormal": 0,
  769. "users_crashed": 0,
  770. "users_errored": 0,
  771. "users_healthy": 0,
  772. },
  773. {
  774. "duration_p50": None,
  775. "duration_p90": None,
  776. "users": 0,
  777. "users_abnormal": 0,
  778. "users_crashed": 0,
  779. "users_errored": 0,
  780. "users_healthy": 0,
  781. },
  782. ],
  783. {
  784. "users": 0,
  785. "users_abnormal": 0,
  786. "users_crashed": 0,
  787. "users_errored": 0,
  788. "users_healthy": 0,
  789. },
  790. )
  791. class GetCrashFreeRateTestCase(TestCase, BaseMetricsTestCase):
  792. """
  793. TestClass that tests that `get_current_and_previous_crash_free_rates` returns the correct
  794. `currentCrashFreeRate` and `previousCrashFreeRate` for each project
  795. TestData:
  796. Project 1:
  797. In the last 24h -> 2 Exited Sessions / 2 Total Sessions -> 100% Crash free rate
  798. In the previous 24h (>24h & <48h) -> 2 Exited + 1 Crashed Sessions / 3 Sessions -> 66.7%
  799. Project 2:
  800. In the last 24h -> 1 Exited + 1 Crashed / 2 Total Sessions -> 50% Crash free rate
  801. In the previous 24h (>24h & <48h) -> 0 Sessions -> None
  802. Project 3:
  803. In the last 24h -> 0 Sessions -> None
  804. In the previous 24h (>24h & <48h) -> 4 Exited + 1 Crashed / 5 Total Sessions -> 80%
  805. """
  806. backend = MetricsReleaseHealthBackend()
  807. def setUp(self):
  808. super().setUp()
  809. self.session_started = time.time() // 60 * 60
  810. self.session_started_gt_24_lt_48 = self.session_started - 30 * 60 * 60
  811. self.project2 = self.create_project(
  812. name="Bar2",
  813. slug="bar2",
  814. teams=[self.team],
  815. fire_project_created=True,
  816. organization=self.organization,
  817. )
  818. self.project3 = self.create_project(
  819. name="Bar3",
  820. slug="bar3",
  821. teams=[self.team],
  822. fire_project_created=True,
  823. organization=self.organization,
  824. )
  825. # Project 1
  826. for _ in range(0, 2):
  827. self.store_session(
  828. self.build_session(
  829. **{
  830. "project_id": self.project.id,
  831. "org_id": self.project.organization_id,
  832. "status": "exited",
  833. }
  834. )
  835. )
  836. for idx in range(0, 3):
  837. status = "exited"
  838. if idx == 2:
  839. status = "crashed"
  840. self.store_session(
  841. self.build_session(
  842. **{
  843. "project_id": self.project.id,
  844. "org_id": self.project.organization_id,
  845. "status": status,
  846. "started": self.session_started_gt_24_lt_48,
  847. }
  848. )
  849. )
  850. # Project 2
  851. for i in range(0, 2):
  852. status = "exited"
  853. if i == 1:
  854. status = "crashed"
  855. self.store_session(
  856. self.build_session(
  857. **{
  858. "project_id": self.project2.id,
  859. "org_id": self.project2.organization_id,
  860. "status": status,
  861. }
  862. )
  863. )
  864. # Project 3
  865. for i in range(0, 5):
  866. status = "exited"
  867. if i == 4:
  868. status = "crashed"
  869. self.store_session(
  870. self.build_session(
  871. **{
  872. "project_id": self.project3.id,
  873. "org_id": self.project3.organization_id,
  874. "status": status,
  875. "started": self.session_started_gt_24_lt_48,
  876. }
  877. )
  878. )
  879. def test_get_current_and_previous_crash_free_rates(self):
  880. now = timezone.now().replace(minute=15, second=23)
  881. last_24h_start = now - 24 * timedelta(hours=1)
  882. last_48h_start = now - 2 * 24 * timedelta(hours=1)
  883. data = self.backend.get_current_and_previous_crash_free_rates(
  884. org_id=self.organization.id,
  885. project_ids=[self.project.id, self.project2.id, self.project3.id],
  886. current_start=last_24h_start,
  887. current_end=now,
  888. previous_start=last_48h_start,
  889. previous_end=last_24h_start,
  890. rollup=3600,
  891. )
  892. assert data == {
  893. self.project.id: {
  894. "currentCrashFreeRate": 100,
  895. "previousCrashFreeRate": 66.66666666666667,
  896. },
  897. self.project2.id: {"currentCrashFreeRate": 50.0, "previousCrashFreeRate": None},
  898. self.project3.id: {"currentCrashFreeRate": None, "previousCrashFreeRate": 80.0},
  899. }
  900. def test_get_current_and_previous_crash_free_rates_with_zero_sessions(self):
  901. now = timezone.now().replace(minute=15, second=23)
  902. last_48h_start = now - 2 * 24 * timedelta(hours=1)
  903. last_72h_start = now - 3 * 24 * timedelta(hours=1)
  904. last_96h_start = now - 4 * 24 * timedelta(hours=1)
  905. data = self.backend.get_current_and_previous_crash_free_rates(
  906. org_id=self.organization.id,
  907. project_ids=[self.project.id],
  908. current_start=last_72h_start,
  909. current_end=last_48h_start,
  910. previous_start=last_96h_start,
  911. previous_end=last_72h_start,
  912. rollup=3600,
  913. )
  914. assert data == {
  915. self.project.id: {
  916. "currentCrashFreeRate": None,
  917. "previousCrashFreeRate": None,
  918. },
  919. }
  920. def test_extract_crash_free_rate_from_result_groups(self):
  921. result_groups = [
  922. {"by": {"project_id": 1}, "totals": {"rate": 0.66}},
  923. {"by": {"project_id": 2}, "totals": {"rate": 0.8}},
  924. ]
  925. crash_free_rates = self.backend._extract_crash_free_rates_from_result_groups(result_groups)
  926. assert crash_free_rates[1] == 0.66 * 100
  927. assert crash_free_rates[2] == 0.8 * 100
  928. def test_extract_crash_free_rate_from_result_groups_with_none(self):
  929. result_groups = [
  930. {"by": {"project_id": 1}, "totals": {"rate": 0.66}},
  931. {"by": {"project_id": 2}, "totals": {"rate": None}},
  932. ]
  933. crash_free_rates = self.backend._extract_crash_free_rates_from_result_groups(result_groups)
  934. assert crash_free_rates[1] == 0.66 * 100
  935. assert crash_free_rates[2] is None
  936. def test_extract_crash_free_rates_from_result_groups_only_none(self):
  937. result_groups = [
  938. {"by": {"project_id": 2}, "totals": {"rate": None}},
  939. ]
  940. crash_free_rates = self.backend._extract_crash_free_rates_from_result_groups(result_groups)
  941. assert crash_free_rates[2] is None
  942. class GetProjectReleasesCountTest(TestCase, BaseMetricsTestCase):
  943. backend = MetricsReleaseHealthBackend()
  944. def test_empty(self):
  945. # Test no errors when no session data
  946. org = self.create_organization()
  947. proj = self.create_project(organization=org)
  948. assert (
  949. self.backend.get_project_releases_count(
  950. org.id, [proj.id], "crash_free_users", stats_period="14d"
  951. )
  952. == 0
  953. )
  954. def test_with_other_metrics(self):
  955. if not self.backend.is_metrics_based():
  956. return
  957. assert isinstance(self, BaseMetricsTestCase)
  958. # Test no errors when no session data
  959. org = self.create_organization()
  960. proj = self.create_project(organization=org)
  961. # Insert a different set metric:
  962. for value in 1, 2, 3:
  963. self.store_metric(
  964. org_id=org.id,
  965. project_id=proj.id,
  966. name="foobarbaz", # any other metric ID
  967. timestamp=int(time.time()),
  968. tags={},
  969. type="set",
  970. value=value,
  971. use_case_id=UseCaseID.SESSIONS,
  972. )
  973. assert (
  974. self.backend.get_project_releases_count(
  975. org.id, [proj.id], "crash_free_users", stats_period="14d"
  976. )
  977. == 0
  978. )
  979. def test(self):
  980. project_release_1 = self.create_release(self.project)
  981. other_project = self.create_project()
  982. other_project_release_1 = self.create_release(other_project)
  983. self.bulk_store_sessions(
  984. [
  985. self.build_session(
  986. environment=self.environment.name, release=project_release_1.version
  987. ),
  988. self.build_session(
  989. environment="staging",
  990. project_id=other_project.id,
  991. release=other_project_release_1.version,
  992. ),
  993. ]
  994. )
  995. assert (
  996. self.backend.get_project_releases_count(
  997. self.organization.id, [self.project.id], "sessions"
  998. )
  999. == 1
  1000. )
  1001. assert (
  1002. self.backend.get_project_releases_count(
  1003. self.organization.id, [self.project.id], "users"
  1004. )
  1005. == 1
  1006. )
  1007. assert (
  1008. self.backend.get_project_releases_count(
  1009. self.organization.id, [self.project.id, other_project.id], "sessions"
  1010. )
  1011. == 2
  1012. )
  1013. assert (
  1014. self.backend.get_project_releases_count(
  1015. self.organization.id,
  1016. [self.project.id, other_project.id],
  1017. "users",
  1018. )
  1019. == 2
  1020. )
  1021. assert (
  1022. self.backend.get_project_releases_count(
  1023. self.organization.id,
  1024. [self.project.id, other_project.id],
  1025. "sessions",
  1026. environments=[self.environment.name],
  1027. )
  1028. == 1
  1029. )
  1030. class CheckReleasesHaveHealthDataTest(TestCase, BaseMetricsTestCase):
  1031. backend = MetricsReleaseHealthBackend()
  1032. def run_test(self, expected, projects, releases, start=None, end=None):
  1033. if not start:
  1034. start = datetime.now() - timedelta(days=1)
  1035. if not end:
  1036. end = datetime.now()
  1037. assert self.backend.check_releases_have_health_data(
  1038. self.organization.id,
  1039. [p.id for p in projects],
  1040. [r.version for r in releases],
  1041. start,
  1042. end,
  1043. ) == {v.version for v in expected}
  1044. def test_empty(self):
  1045. # Test no errors when no session data
  1046. project_release_1 = self.create_release(self.project)
  1047. self.run_test([], [self.project], [project_release_1])
  1048. def test(self):
  1049. other_project = self.create_project()
  1050. release_1 = self.create_release(
  1051. self.project, version="1", additional_projects=[other_project]
  1052. )
  1053. release_2 = self.create_release(other_project, version="2")
  1054. self.bulk_store_sessions(
  1055. [
  1056. self.build_session(release=release_1),
  1057. self.build_session(project_id=other_project, release=release_1),
  1058. self.build_session(project_id=other_project, release=release_2),
  1059. ]
  1060. )
  1061. self.run_test([release_1], [self.project], [release_1])
  1062. self.run_test([release_1], [self.project], [release_1, release_2])
  1063. self.run_test([release_1], [other_project], [release_1])
  1064. self.run_test([release_1, release_2], [other_project], [release_1, release_2])
  1065. self.run_test([release_1, release_2], [self.project, other_project], [release_1, release_2])
  1066. class CheckNumberOfSessions(TestCase, BaseMetricsTestCase):
  1067. backend = MetricsReleaseHealthBackend()
  1068. def setUp(self):
  1069. super().setUp()
  1070. self.dev_env = self.create_environment(name="development", project=self.project)
  1071. self.prod_env = self.create_environment(name="production", project=self.project)
  1072. self.test_env = self.create_environment(name="test", project=self.project)
  1073. self.another_project = self.create_project()
  1074. self.third_project = self.create_project()
  1075. # now_dt should be set to 17:40 of some day not in the future and (system time - now_dt)
  1076. # must be less than 90 days for the metrics DB TTL
  1077. ONE_DAY_AGO = timezone.now() - timedelta(days=1)
  1078. self.now_dt = ONE_DAY_AGO.replace(hour=17, minute=40, second=0)
  1079. self._5_min_ago_dt = self.now_dt - timedelta(minutes=5)
  1080. self._30_min_ago_dt = self.now_dt - timedelta(minutes=30)
  1081. self._1_h_ago_dt = self.now_dt - timedelta(hours=1)
  1082. self._2_h_ago_dt = self.now_dt - timedelta(hours=2)
  1083. self._3_h_ago_dt = self.now_dt - timedelta(hours=3)
  1084. self.now = self.now_dt.timestamp()
  1085. self._5_min_ago = self._5_min_ago_dt.timestamp()
  1086. self._30_min_ago = self._30_min_ago_dt.timestamp()
  1087. self._1_h_ago = self._1_h_ago_dt.timestamp()
  1088. self._2_h_ago = self._2_h_ago_dt.timestamp()
  1089. self._3_h_ago = self._3_h_ago_dt.timestamp()
  1090. def test_no_sessions(self):
  1091. """
  1092. Tests that when there are no sessions the function behaves and returns 0
  1093. """
  1094. actual = self.backend.get_project_sessions_count(
  1095. project_id=self.project.id,
  1096. environment_id=None,
  1097. rollup=60,
  1098. start=self._30_min_ago_dt,
  1099. end=self.now_dt,
  1100. )
  1101. assert 0 == actual
  1102. def test_sessions_in_environment(self):
  1103. """
  1104. Tests that it correctly picks up the sessions for the selected environment
  1105. in the selected time, not counting other environments and other times
  1106. """
  1107. dev = self.dev_env.name
  1108. prod = self.prod_env.name
  1109. self.bulk_store_sessions(
  1110. [
  1111. self.build_session(
  1112. environment=dev, received=self._5_min_ago, started=self._5_min_ago
  1113. ),
  1114. self.build_session(
  1115. environment=prod, received=self._5_min_ago, started=self._5_min_ago
  1116. ),
  1117. self.build_session(
  1118. environment=prod, received=self._5_min_ago, started=self._5_min_ago
  1119. ),
  1120. self.build_session(environment=prod, received=self._2_h_ago, started=self._2_h_ago),
  1121. ]
  1122. )
  1123. actual = self.backend.get_project_sessions_count(
  1124. project_id=self.project.id,
  1125. environment_id=self.prod_env.id,
  1126. rollup=60,
  1127. start=self._1_h_ago_dt,
  1128. end=self.now_dt,
  1129. )
  1130. assert actual == 2
  1131. def test_environment_without_sessions(self):
  1132. """
  1133. We should get zero sessions, even if the environment name has not been indexed
  1134. by the metrics indexer.
  1135. """
  1136. env_without_sessions = self.create_environment(
  1137. name="this_has_no_sessions", project=self.project
  1138. )
  1139. self.bulk_store_sessions(
  1140. [
  1141. self.build_session(
  1142. environment=self.prod_env.name,
  1143. received=self._5_min_ago,
  1144. started=self._5_min_ago,
  1145. ),
  1146. self.build_session(
  1147. environment=None, received=self._5_min_ago, started=self._5_min_ago
  1148. ),
  1149. ]
  1150. )
  1151. count_env_all = self.backend.get_project_sessions_count(
  1152. project_id=self.project.id,
  1153. environment_id=None,
  1154. rollup=60,
  1155. start=self._1_h_ago_dt,
  1156. end=self.now_dt,
  1157. )
  1158. assert count_env_all == 2
  1159. count_env_new = self.backend.get_project_sessions_count(
  1160. project_id=self.project.id,
  1161. environment_id=env_without_sessions.id,
  1162. rollup=60,
  1163. start=self._1_h_ago_dt,
  1164. end=self.now_dt,
  1165. )
  1166. assert count_env_new == 0
  1167. def test_sessions_in_all_environments(self):
  1168. """
  1169. When the environment is not specified sessions from all environments are counted
  1170. """
  1171. dev = self.dev_env.name
  1172. prod = self.prod_env.name
  1173. self.bulk_store_sessions(
  1174. [
  1175. self.build_session(
  1176. environment=dev, received=self._5_min_ago, started=self._5_min_ago
  1177. ),
  1178. self.build_session(
  1179. environment=prod, received=self._5_min_ago, started=self._5_min_ago
  1180. ),
  1181. self.build_session(
  1182. environment=prod, received=self._5_min_ago, started=self._5_min_ago
  1183. ),
  1184. self.build_session(environment=prod, received=self._2_h_ago, started=self._2_h_ago),
  1185. self.build_session(environment=dev, received=self._2_h_ago, started=self._2_h_ago),
  1186. ]
  1187. )
  1188. actual = self.backend.get_project_sessions_count(
  1189. project_id=self.project.id,
  1190. environment_id=None,
  1191. rollup=60,
  1192. start=self._1_h_ago_dt,
  1193. end=self.now_dt,
  1194. )
  1195. assert actual == 3
  1196. def test_sessions_from_multiple_projects(self):
  1197. """
  1198. Only sessions from the specified project are considered
  1199. """
  1200. dev = self.dev_env.name
  1201. prod = self.prod_env.name
  1202. self.bulk_store_sessions(
  1203. [
  1204. self.build_session(
  1205. environment=dev, received=self._5_min_ago, started=self._5_min_ago
  1206. ),
  1207. self.build_session(
  1208. environment=prod, received=self._5_min_ago, started=self._5_min_ago
  1209. ),
  1210. self.build_session(
  1211. environment=prod,
  1212. received=self._5_min_ago,
  1213. project_id=self.another_project.id,
  1214. started=self._5_min_ago,
  1215. ),
  1216. ]
  1217. )
  1218. actual = self.backend.get_project_sessions_count(
  1219. project_id=self.project.id,
  1220. environment_id=None,
  1221. rollup=60,
  1222. start=self._1_h_ago_dt,
  1223. end=self.now_dt,
  1224. )
  1225. assert actual == 2
  1226. def test_sessions_per_project_no_sessions(self):
  1227. """
  1228. Tests that no sessions are returned
  1229. """
  1230. actual = self.backend.get_num_sessions_per_project(
  1231. project_ids=[self.project.id, self.another_project.id],
  1232. environment_ids=None,
  1233. rollup=60,
  1234. start=self._30_min_ago_dt,
  1235. end=self.now_dt,
  1236. )
  1237. assert [] == actual
  1238. def test_sesions_per_project_multiple_projects(self):
  1239. dev = self.dev_env.name
  1240. prod = self.prod_env.name
  1241. test = self.test_env.name
  1242. p1 = self.project
  1243. p2 = self.another_project
  1244. p3 = self.third_project
  1245. self.bulk_store_sessions(
  1246. [
  1247. # counted in p1
  1248. self.build_session(
  1249. environment=dev, received=self._5_min_ago, started=self._5_min_ago
  1250. ),
  1251. self.build_session(
  1252. environment=prod, received=self._5_min_ago, started=self._5_min_ago
  1253. ),
  1254. self.build_session(
  1255. environment=dev, received=self._30_min_ago, started=self._30_min_ago
  1256. ),
  1257. # ignored in p1
  1258. # ignored env
  1259. self.build_session(
  1260. environment=test, received=self._30_min_ago, started=self._30_min_ago
  1261. ),
  1262. # too old
  1263. self.build_session(environment=prod, received=self._3_h_ago, started=self._3_h_ago),
  1264. # counted in p2
  1265. self.build_session(
  1266. environment=dev,
  1267. received=self._5_min_ago,
  1268. project_id=p2.id,
  1269. started=self._5_min_ago,
  1270. ),
  1271. # ignored in p2
  1272. # ignored env
  1273. self.build_session(
  1274. environment=test,
  1275. received=self._5_min_ago,
  1276. project_id=p2.id,
  1277. started=self._5_min_ago,
  1278. ),
  1279. # too old
  1280. self.build_session(
  1281. environment=prod,
  1282. received=self._3_h_ago,
  1283. project_id=p2.id,
  1284. started=self._3_h_ago,
  1285. ),
  1286. # ignored p3
  1287. self.build_session(
  1288. environment=dev,
  1289. received=self._5_min_ago,
  1290. project_id=p3.id,
  1291. started=self._5_min_ago,
  1292. ),
  1293. ]
  1294. )
  1295. actual = self.backend.get_num_sessions_per_project(
  1296. project_ids=[self.project.id, self.another_project.id],
  1297. environment_ids=[self.dev_env.id, self.prod_env.id],
  1298. rollup=60,
  1299. start=self._2_h_ago_dt,
  1300. end=self.now_dt,
  1301. )
  1302. assert set(actual) == {(p1.id, 3), (p2.id, 1)}
  1303. eids_tests: tuple[list[int] | None, ...] = ([], None)
  1304. for eids in eids_tests:
  1305. actual = self.backend.get_num_sessions_per_project(
  1306. project_ids=[self.project.id, self.another_project.id],
  1307. environment_ids=eids,
  1308. rollup=60,
  1309. start=self._2_h_ago_dt,
  1310. end=self.now_dt,
  1311. )
  1312. assert set(actual) == {(p1.id, 4), (p2.id, 2)}
  1313. class InitWithoutUserTestCase(TestCase, BaseMetricsTestCase):
  1314. backend = MetricsReleaseHealthBackend()
  1315. def setUp(self):
  1316. super().setUp()
  1317. self.received = time.time()
  1318. self.session_started = time.time() // 60 * 60
  1319. self.session_release = "foo@1.0.0"
  1320. session_1 = "5d52fd05-fcc9-4bf3-9dc9-267783670341"
  1321. session_2 = "5e910c1a-6941-460e-9843-24103fb6a63c"
  1322. session_3 = "a148c0c5-06a2-423b-8901-6b43b812cf82"
  1323. user_1 = "39887d89-13b2-4c84-8c23-5d13d2102666"
  1324. user_2 = "39887d89-13b2-4c84-8c23-5d13d2102667"
  1325. user_3 = "39887d89-13b2-4c84-8c23-5d13d2102668"
  1326. self.bulk_store_sessions(
  1327. [
  1328. self.build_session(
  1329. distinct_id=user_1,
  1330. session_id=session_1,
  1331. status="exited",
  1332. release=self.session_release,
  1333. environment="prod",
  1334. started=self.session_started,
  1335. received=self.received,
  1336. ),
  1337. self.build_session(
  1338. distinct_id=user_2,
  1339. session_id=session_2,
  1340. status="crashed",
  1341. release=self.session_release,
  1342. environment="prod",
  1343. started=self.session_started,
  1344. received=self.received,
  1345. ),
  1346. # session_3 initial update: no user ID
  1347. self.build_session(
  1348. distinct_id=None,
  1349. session_id=session_3,
  1350. status="ok",
  1351. seq=0,
  1352. release=self.session_release,
  1353. environment="prod",
  1354. started=self.session_started,
  1355. received=self.received,
  1356. ),
  1357. # session_3 subsequent update: user ID is here!
  1358. self.build_session(
  1359. distinct_id=user_3,
  1360. session_id=session_3,
  1361. status="ok",
  1362. seq=123,
  1363. release=self.session_release,
  1364. environment="prod",
  1365. started=self.session_started,
  1366. received=self.received,
  1367. ),
  1368. ]
  1369. )
  1370. def test_get_release_adoption(self):
  1371. data = self.backend.get_release_adoption(
  1372. [
  1373. (self.project.id, self.session_release),
  1374. ]
  1375. )
  1376. inner = data[(self.project.id, self.session_release)]
  1377. assert inner["users_24h"] == 3
  1378. def test_get_release_health_data_overview_users(self):
  1379. data = self.backend.get_release_health_data_overview(
  1380. [
  1381. (self.project.id, self.session_release),
  1382. ],
  1383. summary_stats_period="24h",
  1384. health_stats_period="24h",
  1385. stat="users",
  1386. )
  1387. inner = data[(self.project.id, self.session_release)]
  1388. assert inner["total_users"] == 3
  1389. assert inner["crash_free_users"] == 66.66666666666667
  1390. def test_get_crash_free_breakdown(self):
  1391. start = timezone.now() - timedelta(days=4)
  1392. data = self.backend.get_crash_free_breakdown(
  1393. project_id=self.project.id,
  1394. release=self.session_release,
  1395. start=start,
  1396. environments=["prod"],
  1397. )
  1398. # Last returned date is generated within function, should be close to now:
  1399. last_date = data[-1]["date"]
  1400. assert timezone.now() - last_date < timedelta(seconds=1)
  1401. assert data == [
  1402. {
  1403. "crash_free_sessions": None,
  1404. "crash_free_users": None,
  1405. "date": start + timedelta(days=1),
  1406. "total_sessions": 0,
  1407. "total_users": 0,
  1408. },
  1409. {
  1410. "crash_free_sessions": None,
  1411. "crash_free_users": None,
  1412. "date": start + timedelta(days=2),
  1413. "total_sessions": 0,
  1414. "total_users": 0,
  1415. },
  1416. {
  1417. "crash_free_sessions": 66.66666666666667,
  1418. "crash_free_users": 66.66666666666667,
  1419. "total_sessions": 3,
  1420. "total_users": 3,
  1421. "date": mock.ANY, # tested above
  1422. },
  1423. ]
  1424. def test_get_project_release_stats_users(self):
  1425. end = timezone.now()
  1426. start = end - timedelta(days=4)
  1427. stats, totals = self.backend.get_project_release_stats(
  1428. self.project.id,
  1429. release=self.session_release,
  1430. stat="users",
  1431. rollup=86400,
  1432. start=start,
  1433. end=end,
  1434. )
  1435. assert stats[3][1] == {
  1436. "duration_p50": 60.0,
  1437. "duration_p90": 60.0,
  1438. "users": 3,
  1439. "users_abnormal": 0,
  1440. "users_crashed": 1,
  1441. "users_errored": 0,
  1442. "users_healthy": 2,
  1443. }