test_sessions.py 54 KB

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