test_sessions.py 58 KB

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