test_sessions.py 54 KB

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