test_sessions_v2.py 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804
  1. import math
  2. from datetime import datetime
  3. import pytest
  4. import pytz
  5. from django.http import QueryDict
  6. from freezegun import freeze_time
  7. from sentry.release_health.base import SessionsQueryConfig
  8. # from sentry.testutils import TestCase
  9. from sentry.snuba.sessions_v2 import (
  10. AllowedResolution,
  11. InvalidParams,
  12. QueryDefinition,
  13. get_constrained_date_range,
  14. get_timestamps,
  15. massage_sessions_result,
  16. )
  17. def _make_query(qs, allow_minute_resolution=True, params=None):
  18. query_config = SessionsQueryConfig(
  19. (AllowedResolution.one_minute if allow_minute_resolution else AllowedResolution.one_hour),
  20. allow_session_status_query=False,
  21. restrict_date_range=True,
  22. )
  23. return QueryDefinition(QueryDict(qs), params or {}, query_config)
  24. def result_sorted(result):
  25. """sort the groups of the results array by the `by` object, ensuring a stable order"""
  26. def stable_dict(d):
  27. return tuple(sorted(d.items(), key=lambda t: t[0]))
  28. result["groups"].sort(key=lambda group: stable_dict(group["by"]))
  29. return result
  30. @freeze_time("2018-12-11 03:21:00")
  31. def test_round_range():
  32. start, end, interval = get_constrained_date_range({"statsPeriod": "2d"})
  33. assert start == datetime(2018, 12, 9, 4, tzinfo=pytz.utc)
  34. assert end == datetime(2018, 12, 11, 3, 22, tzinfo=pytz.utc)
  35. start, end, interval = get_constrained_date_range({"statsPeriod": "2d", "interval": "1d"})
  36. assert start == datetime(2018, 12, 10, tzinfo=pytz.utc)
  37. assert end == datetime(2018, 12, 11, 3, 22, tzinfo=pytz.utc)
  38. def test_invalid_interval():
  39. with pytest.raises(InvalidParams):
  40. start, end, interval = get_constrained_date_range({"interval": "0d"})
  41. def test_round_exact():
  42. start, end, interval = get_constrained_date_range(
  43. {"start": "2021-01-12T04:06:16", "end": "2021-01-17T08:26:13", "interval": "1d"},
  44. )
  45. assert start == datetime(2021, 1, 12, tzinfo=pytz.utc)
  46. assert end == datetime(2021, 1, 18, tzinfo=pytz.utc)
  47. def test_inclusive_end():
  48. start, end, interval = get_constrained_date_range(
  49. {"start": "2021-02-24T00:00:00", "end": "2021-02-25T00:00:00", "interval": "1h"},
  50. )
  51. assert start == datetime(2021, 2, 24, tzinfo=pytz.utc)
  52. assert end == datetime(2021, 2, 25, 1, tzinfo=pytz.utc)
  53. @freeze_time("2021-03-05T11:14:17.105Z")
  54. def test_interval_restrictions():
  55. # making sure intervals are cleanly divisible
  56. with pytest.raises(InvalidParams, match="The interval has to be less than one day."):
  57. _make_query("statsPeriod=4d&interval=2d&field=sum(session)")
  58. with pytest.raises(
  59. InvalidParams, match="The interval should divide one day without a remainder."
  60. ):
  61. _make_query("statsPeriod=6h&interval=59m&field=sum(session)")
  62. with pytest.raises(
  63. InvalidParams, match="The interval should divide one day without a remainder."
  64. ):
  65. _make_query("statsPeriod=4d&interval=5h&field=sum(session)")
  66. _make_query("statsPeriod=6h&interval=90m&field=sum(session)")
  67. with pytest.raises(
  68. InvalidParams,
  69. match="The interval has to be a multiple of the minimum interval of one hour.",
  70. ):
  71. _make_query("statsPeriod=6h&interval=90m&field=sum(session)", False)
  72. with pytest.raises(
  73. InvalidParams,
  74. match="The interval has to be a multiple of the minimum interval of one minute.",
  75. ):
  76. _make_query("statsPeriod=1h&interval=90s&field=sum(session)")
  77. # restrictions for minute resolution time range
  78. with pytest.raises(
  79. InvalidParams,
  80. match="The time-range when using one-minute resolution intervals is restricted to 6 hours.",
  81. ):
  82. _make_query("statsPeriod=7h&interval=15m&field=sum(session)")
  83. with pytest.raises(
  84. InvalidParams,
  85. match="The time-range when using one-minute resolution intervals is restricted to the last 30 days.",
  86. ):
  87. _make_query(
  88. "start=2021-01-05T11:14:17&end=2021-01-05T12:14:17&interval=15m&field=sum(session)"
  89. )
  90. with pytest.raises(
  91. InvalidParams, match="Your interval and date range would create too many results."
  92. ):
  93. _make_query("statsPeriod=90d&interval=1h&field=sum(session)")
  94. @freeze_time("2020-12-18T11:14:17.105Z")
  95. def test_timestamps():
  96. query = _make_query("statsPeriod=1d&interval=12h&field=sum(session)")
  97. expected_timestamps = ["2020-12-17T12:00:00Z", "2020-12-18T00:00:00Z"]
  98. actual_timestamps = get_timestamps(query)
  99. assert actual_timestamps == expected_timestamps
  100. @freeze_time("2021-03-08T09:34:00.000Z")
  101. def test_hourly_rounded_start():
  102. query = _make_query("statsPeriod=30m&interval=1m&field=sum(session)")
  103. actual_timestamps = get_timestamps(query)
  104. assert actual_timestamps[0] == "2021-03-08T09:00:00Z"
  105. assert actual_timestamps[-1] == "2021-03-08T09:34:00Z"
  106. assert len(actual_timestamps) == 35
  107. # in this case "45m" means from 08:49:00-09:34:00, but since we round start/end
  108. # to hours, we extend the start time to 08:00:00.
  109. query = _make_query("statsPeriod=45m&interval=1m&field=sum(session)")
  110. actual_timestamps = get_timestamps(query)
  111. assert actual_timestamps[0] == "2021-03-08T08:00:00Z"
  112. assert actual_timestamps[-1] == "2021-03-08T09:34:00Z"
  113. assert len(actual_timestamps) == 95
  114. def test_rounded_end():
  115. query = _make_query(
  116. "field=sum(session)&interval=1h&start=2021-02-24T00:00:00Z&end=2021-02-25T00:00:00Z"
  117. )
  118. expected_timestamps = [
  119. "2021-02-24T00:00:00Z",
  120. "2021-02-24T01:00:00Z",
  121. "2021-02-24T02:00:00Z",
  122. "2021-02-24T03:00:00Z",
  123. "2021-02-24T04:00:00Z",
  124. "2021-02-24T05:00:00Z",
  125. "2021-02-24T06:00:00Z",
  126. "2021-02-24T07:00:00Z",
  127. "2021-02-24T08:00:00Z",
  128. "2021-02-24T09:00:00Z",
  129. "2021-02-24T10:00:00Z",
  130. "2021-02-24T11:00:00Z",
  131. "2021-02-24T12:00:00Z",
  132. "2021-02-24T13:00:00Z",
  133. "2021-02-24T14:00:00Z",
  134. "2021-02-24T15:00:00Z",
  135. "2021-02-24T16:00:00Z",
  136. "2021-02-24T17:00:00Z",
  137. "2021-02-24T18:00:00Z",
  138. "2021-02-24T19:00:00Z",
  139. "2021-02-24T20:00:00Z",
  140. "2021-02-24T21:00:00Z",
  141. "2021-02-24T22:00:00Z",
  142. "2021-02-24T23:00:00Z",
  143. "2021-02-25T00:00:00Z",
  144. ]
  145. actual_timestamps = get_timestamps(query)
  146. assert len(actual_timestamps) == 25
  147. assert actual_timestamps == expected_timestamps
  148. def test_simple_query():
  149. query = _make_query("statsPeriod=1d&interval=12h&field=sum(session)")
  150. assert query.query_columns == ["sessions"]
  151. def test_groupby_query():
  152. query = _make_query("statsPeriod=1d&interval=12h&field=sum(session)&groupBy=release")
  153. assert sorted(query.query_columns) == ["release", "sessions"]
  154. assert query.query_groupby == ["release"]
  155. def test_virtual_groupby_query():
  156. query = _make_query("statsPeriod=1d&interval=12h&field=sum(session)&groupBy=session.status")
  157. assert sorted(query.query_columns) == [
  158. "sessions",
  159. "sessions_abnormal",
  160. "sessions_crashed",
  161. "sessions_errored",
  162. ]
  163. assert query.query_groupby == []
  164. query = _make_query(
  165. "statsPeriod=1d&interval=12h&field=count_unique(user)&groupBy=session.status"
  166. )
  167. assert sorted(query.query_columns) == [
  168. "users",
  169. "users_abnormal",
  170. "users_crashed",
  171. "users_errored",
  172. ]
  173. assert query.query_groupby == []
  174. @freeze_time("2022-05-04T09:00:00.000Z")
  175. def _get_query_maker_params(project):
  176. # These parameters are computed in the API endpoint, before the
  177. # QueryDefinition is built. Since we're only testing the query
  178. # definition here, we can safely mock these.
  179. return {
  180. "start": datetime.now(),
  181. "end": datetime.now(),
  182. "organization_id": project.organization_id,
  183. }
  184. @pytest.mark.django_db(databases="__all__")
  185. def test_filter_proj_slug_in_query(default_project):
  186. params = _get_query_maker_params(default_project)
  187. params["project_id"] = [default_project.id]
  188. query_def = _make_query(
  189. f"field=sum(session)&interval=2h&statsPeriod=2h&query=project%3A{default_project.slug}",
  190. params=params,
  191. )
  192. assert query_def.query == f"project:{default_project.slug}"
  193. assert query_def.params["project_id"] == [default_project.id]
  194. @pytest.mark.django_db(databases="__all__")
  195. def test_filter_proj_slug_in_top_filter(default_project):
  196. params = _get_query_maker_params(default_project)
  197. params["project_id"] = [default_project.id]
  198. query_def = _make_query(
  199. f"field=sum(session)&interval=2h&statsPeriod=2h&project={default_project.id}",
  200. params=params,
  201. )
  202. assert query_def.query == ""
  203. assert query_def.params["project_id"] == [default_project.id]
  204. @pytest.mark.django_db(databases="__all__")
  205. def test_filter_proj_slug_in_top_filter_and_query(default_project):
  206. params = _get_query_maker_params(default_project)
  207. params["project_id"] = [default_project.id]
  208. query_def = _make_query(
  209. f"field=sum(session)&interval=2h&statsPeriod=2h&project={default_project.id}&query=project%3A{default_project.slug}",
  210. params=params,
  211. )
  212. assert query_def.query == f"project:{default_project.slug}"
  213. assert query_def.params["project_id"] == [default_project.id]
  214. @pytest.mark.django_db(databases="__all__")
  215. def test_proj_neither_in_top_filter_nor_query(default_project):
  216. params = _get_query_maker_params(default_project)
  217. query_def = _make_query(
  218. "field=sum(session)&interval=2h&statsPeriod=2h",
  219. params=params,
  220. )
  221. assert query_def.query == ""
  222. assert "project_id" not in query_def.params
  223. @pytest.mark.django_db(databases="__all__")
  224. def test_filter_env_in_query(default_project):
  225. env = "prod"
  226. params = _get_query_maker_params(default_project)
  227. query_def = _make_query(
  228. f"field=sum(session)&interval=2h&statsPeriod=2h&query=environment%3A{env}",
  229. params=params,
  230. )
  231. assert query_def.query == f"environment:{env}"
  232. @pytest.mark.django_db(databases="__all__")
  233. def test_filter_env_in_top_filter(default_project):
  234. env = "prod"
  235. params = _get_query_maker_params(default_project)
  236. params["environment"] = "prod"
  237. query_def = _make_query(
  238. f"field=sum(session)&interval=2h&statsPeriod=2h&environment={env}",
  239. params=params,
  240. )
  241. assert query_def.query == ""
  242. @pytest.mark.django_db(databases="__all__")
  243. def test_filter_env_in_top_filter_and_query(default_project):
  244. env = "prod"
  245. params = _get_query_maker_params(default_project)
  246. params["environment"] = "prod"
  247. query_def = _make_query(
  248. f"field=sum(session)&interval=2h&statsPeriod=2h&environment={env}&query=environment%3A{env}",
  249. params=params,
  250. )
  251. assert query_def.query == f"environment:{env}"
  252. @pytest.mark.django_db(databases="__all__")
  253. def test_env_neither_in_top_filter_nor_query(default_project):
  254. params = _get_query_maker_params(default_project)
  255. query_def = _make_query(
  256. "field=sum(session)&interval=2h&statsPeriod=2h",
  257. params=params,
  258. )
  259. assert query_def.query == ""
  260. @freeze_time("2020-12-18T11:14:17.105Z")
  261. def test_massage_empty():
  262. query = _make_query("statsPeriod=1d&interval=1d&field=sum(session)")
  263. result_totals = []
  264. result_timeseries = []
  265. expected_result = {
  266. "start": "2020-12-18T00:00:00Z",
  267. "end": "2020-12-18T11:15:00Z",
  268. "query": "",
  269. "intervals": ["2020-12-18T00:00:00Z"],
  270. "groups": [],
  271. }
  272. actual_result = result_sorted(massage_sessions_result(query, result_totals, result_timeseries))
  273. assert actual_result == expected_result
  274. @freeze_time("2020-12-18T11:14:17.105Z")
  275. def test_massage_unbalanced_results():
  276. query = _make_query("statsPeriod=1d&interval=1d&field=sum(session)&groupBy=release")
  277. result_totals = [
  278. {"release": "test-example-release", "sessions": 1},
  279. ]
  280. result_timeseries = []
  281. expected_result = {
  282. "start": "2020-12-18T00:00:00Z",
  283. "end": "2020-12-18T11:15:00Z",
  284. "query": "",
  285. "intervals": ["2020-12-18T00:00:00Z"],
  286. "groups": [
  287. {
  288. "by": {"release": "test-example-release"},
  289. "series": {"sum(session)": [0]},
  290. "totals": {"sum(session)": 1},
  291. }
  292. ],
  293. }
  294. actual_result = result_sorted(massage_sessions_result(query, result_totals, result_timeseries))
  295. assert actual_result == expected_result
  296. result_totals = []
  297. result_timeseries = [
  298. {
  299. "release": "test-example-release",
  300. "sessions": 1,
  301. "bucketed_started": "2020-12-18T00:00:00+00:00",
  302. },
  303. ]
  304. expected_result = {
  305. "start": "2020-12-18T00:00:00Z",
  306. "end": "2020-12-18T11:15:00Z",
  307. "query": "",
  308. "intervals": ["2020-12-18T00:00:00Z"],
  309. "groups": [
  310. {
  311. "by": {"release": "test-example-release"},
  312. "series": {"sum(session)": [1]},
  313. "totals": {"sum(session)": 0},
  314. }
  315. ],
  316. }
  317. actual_result = result_sorted(massage_sessions_result(query, result_totals, result_timeseries))
  318. assert actual_result == expected_result
  319. @freeze_time("2020-12-18T11:14:17.105Z")
  320. def test_massage_simple_timeseries():
  321. """A timeseries is filled up when it only receives partial data"""
  322. query = _make_query("statsPeriod=1d&interval=6h&field=sum(session)")
  323. result_totals = [{"sessions": 4}]
  324. # snuba returns the datetimes as strings for now
  325. result_timeseries = [
  326. {"sessions": 2, "bucketed_started": "2020-12-18T06:00:00+00:00"},
  327. {"sessions": 2, "bucketed_started": "2020-12-17T12:00:00+00:00"},
  328. ]
  329. expected_result = {
  330. "start": "2020-12-17T12:00:00Z",
  331. "end": "2020-12-18T11:15:00Z",
  332. "query": "",
  333. "intervals": [
  334. "2020-12-17T12:00:00Z",
  335. "2020-12-17T18:00:00Z",
  336. "2020-12-18T00:00:00Z",
  337. "2020-12-18T06:00:00Z",
  338. ],
  339. "groups": [
  340. {"by": {}, "series": {"sum(session)": [2, 0, 0, 2]}, "totals": {"sum(session)": 4}}
  341. ],
  342. }
  343. actual_result = result_sorted(massage_sessions_result(query, result_totals, result_timeseries))
  344. assert actual_result == expected_result
  345. @freeze_time("2020-12-18T11:14:17.105Z")
  346. def test_massage_unordered_timeseries():
  347. query = _make_query("statsPeriod=1d&interval=6h&field=sum(session)")
  348. result_totals = [{"sessions": 10}]
  349. # snuba returns the datetimes as strings for now
  350. result_timeseries = [
  351. {"sessions": 3, "bucketed_started": "2020-12-18T00:00:00+00:00"},
  352. {"sessions": 2, "bucketed_started": "2020-12-17T18:00:00+00:00"},
  353. {"sessions": 4, "bucketed_started": "2020-12-18T06:00:00+00:00"},
  354. {"sessions": 1, "bucketed_started": "2020-12-17T12:00:00+00:00"},
  355. ]
  356. expected_result = {
  357. "start": "2020-12-17T12:00:00Z",
  358. "end": "2020-12-18T11:15:00Z",
  359. "query": "",
  360. "intervals": [
  361. "2020-12-17T12:00:00Z",
  362. "2020-12-17T18:00:00Z",
  363. "2020-12-18T00:00:00Z",
  364. "2020-12-18T06:00:00Z",
  365. ],
  366. "groups": [
  367. {"by": {}, "series": {"sum(session)": [1, 2, 3, 4]}, "totals": {"sum(session)": 10}}
  368. ],
  369. }
  370. actual_result = result_sorted(massage_sessions_result(query, result_totals, result_timeseries))
  371. assert actual_result == expected_result
  372. @freeze_time("2020-12-18T11:14:17.105Z")
  373. def test_massage_no_timeseries():
  374. query = _make_query("statsPeriod=1d&interval=6h&field=sum(session)&groupby=projects")
  375. result_totals = [{"sessions": 4}]
  376. # snuba returns the datetimes as strings for now
  377. result_timeseries = None
  378. expected_result = {
  379. "start": "2020-12-17T12:00:00Z",
  380. "end": "2020-12-18T11:15:00Z",
  381. "query": "",
  382. "intervals": [
  383. "2020-12-17T12:00:00Z",
  384. "2020-12-17T18:00:00Z",
  385. "2020-12-18T00:00:00Z",
  386. "2020-12-18T06:00:00Z",
  387. ],
  388. "groups": [{"by": {}, "totals": {"sum(session)": 4}}],
  389. }
  390. actual_result = result_sorted(massage_sessions_result(query, result_totals, result_timeseries))
  391. assert actual_result == expected_result
  392. def test_massage_exact_timeseries():
  393. query = _make_query(
  394. "start=2020-12-17T15:12:34Z&end=2020-12-18T11:14:17Z&interval=6h&field=sum(session)"
  395. )
  396. result_totals = [{"sessions": 4}]
  397. result_timeseries = [
  398. {"sessions": 2, "bucketed_started": "2020-12-18T06:00:00+00:00"},
  399. {"sessions": 2, "bucketed_started": "2020-12-17T12:00:00+00:00"},
  400. ]
  401. expected_result = {
  402. "start": "2020-12-17T12:00:00Z",
  403. "end": "2020-12-18T12:00:00Z",
  404. "query": "",
  405. "intervals": [
  406. "2020-12-17T12:00:00Z",
  407. "2020-12-17T18:00:00Z",
  408. "2020-12-18T00:00:00Z",
  409. "2020-12-18T06:00:00Z",
  410. ],
  411. "groups": [
  412. {"by": {}, "series": {"sum(session)": [2, 0, 0, 2]}, "totals": {"sum(session)": 4}}
  413. ],
  414. }
  415. actual_result = result_sorted(massage_sessions_result(query, result_totals, result_timeseries))
  416. assert actual_result == expected_result
  417. @freeze_time("2020-12-18T11:14:17.105Z")
  418. def test_massage_groupby_timeseries():
  419. query = _make_query("statsPeriod=1d&interval=6h&field=sum(session)&groupBy=release")
  420. result_totals = [
  421. {"release": "test-example-release", "sessions": 4},
  422. {"release": "test-example-release-2", "sessions": 1},
  423. ]
  424. # snuba returns the datetimes as strings for now
  425. result_timeseries = [
  426. {
  427. "release": "test-example-release",
  428. "sessions": 2,
  429. "bucketed_started": "2020-12-18T06:00:00+00:00",
  430. },
  431. {
  432. "release": "test-example-release-2",
  433. "sessions": 1,
  434. "bucketed_started": "2020-12-18T06:00:00+00:00",
  435. },
  436. {
  437. "release": "test-example-release",
  438. "sessions": 2,
  439. "bucketed_started": "2020-12-17T12:00:00+00:00",
  440. },
  441. ]
  442. expected_result = {
  443. "start": "2020-12-17T12:00:00Z",
  444. "end": "2020-12-18T11:15:00Z",
  445. "query": "",
  446. "intervals": [
  447. "2020-12-17T12:00:00Z",
  448. "2020-12-17T18:00:00Z",
  449. "2020-12-18T00:00:00Z",
  450. "2020-12-18T06:00:00Z",
  451. ],
  452. "groups": [
  453. {
  454. "by": {"release": "test-example-release"},
  455. "series": {"sum(session)": [2, 0, 0, 2]},
  456. "totals": {"sum(session)": 4},
  457. },
  458. {
  459. "by": {"release": "test-example-release-2"},
  460. "series": {"sum(session)": [0, 0, 0, 1]},
  461. "totals": {"sum(session)": 1},
  462. },
  463. ],
  464. }
  465. actual_result = result_sorted(massage_sessions_result(query, result_totals, result_timeseries))
  466. assert actual_result == expected_result
  467. @freeze_time("2020-12-18T13:25:15.769Z")
  468. def test_massage_virtual_groupby_timeseries():
  469. query = _make_query(
  470. "statsPeriod=1d&interval=6h&field=sum(session)&field=count_unique(user)&groupBy=session.status"
  471. )
  472. result_totals = [
  473. {
  474. "users": 1,
  475. "users_crashed": 1,
  476. "sessions": 31,
  477. "sessions_errored": 15,
  478. "users_errored": 1,
  479. "sessions_abnormal": 6,
  480. "sessions_crashed": 8,
  481. "users_abnormal": 0,
  482. }
  483. ]
  484. # snuba returns the datetimes as strings for now
  485. result_timeseries = [
  486. {
  487. "sessions_errored": 1,
  488. "users": 1,
  489. "users_crashed": 1,
  490. "sessions_abnormal": 0,
  491. "sessions": 3,
  492. "users_errored": 1,
  493. "users_abnormal": 0,
  494. "sessions_crashed": 1,
  495. "bucketed_started": "2020-12-18T12:00:00+00:00",
  496. },
  497. {
  498. "sessions_errored": 0,
  499. "users": 1,
  500. "users_crashed": 0,
  501. "sessions_abnormal": 0,
  502. "sessions": 3,
  503. "users_errored": 0,
  504. "users_abnormal": 0,
  505. "sessions_crashed": 0,
  506. "bucketed_started": "2020-12-18T06:00:00+00:00",
  507. },
  508. {
  509. "sessions_errored": 10,
  510. "users": 1,
  511. "users_crashed": 0,
  512. "sessions_abnormal": 2,
  513. "sessions": 15,
  514. "users_errored": 0,
  515. "users_abnormal": 0,
  516. "sessions_crashed": 4,
  517. "bucketed_started": "2020-12-18T00:00:00+00:00",
  518. },
  519. {
  520. "sessions_errored": 4,
  521. "users": 1,
  522. "users_crashed": 0,
  523. "sessions_abnormal": 4,
  524. "sessions": 10,
  525. "users_errored": 0,
  526. "users_abnormal": 0,
  527. "sessions_crashed": 3,
  528. "bucketed_started": "2020-12-17T18:00:00+00:00",
  529. },
  530. ]
  531. expected_result = {
  532. "start": "2020-12-17T18:00:00Z",
  533. "end": "2020-12-18T13:26:00Z",
  534. "query": "",
  535. "intervals": [
  536. "2020-12-17T18:00:00Z",
  537. "2020-12-18T00:00:00Z",
  538. "2020-12-18T06:00:00Z",
  539. "2020-12-18T12:00:00Z",
  540. ],
  541. "groups": [
  542. {
  543. "by": {"session.status": "abnormal"},
  544. "series": {"count_unique(user)": [0, 0, 0, 0], "sum(session)": [4, 2, 0, 0]},
  545. "totals": {"count_unique(user)": 0, "sum(session)": 6},
  546. },
  547. {
  548. "by": {"session.status": "crashed"},
  549. "series": {"count_unique(user)": [0, 0, 0, 1], "sum(session)": [3, 4, 0, 1]},
  550. "totals": {"count_unique(user)": 1, "sum(session)": 8},
  551. },
  552. {
  553. "by": {"session.status": "errored"},
  554. "series": {"count_unique(user)": [0, 0, 0, 0], "sum(session)": [0, 4, 0, 0]},
  555. "totals": {"count_unique(user)": 0, "sum(session)": 1},
  556. },
  557. {
  558. "by": {"session.status": "healthy"},
  559. "series": {"count_unique(user)": [1, 1, 1, 0], "sum(session)": [6, 5, 3, 2]},
  560. # while in one of the time slots, we have a healthy user, it is
  561. # the *same* user as the one experiencing a crash later on,
  562. # so in the *whole* time window, that one user is not counted as healthy,
  563. # so the `0` here is expected, as that's an example of the `count_unique` behavior.
  564. "totals": {"count_unique(user)": 0, "sum(session)": 16},
  565. },
  566. ],
  567. }
  568. actual_result = result_sorted(massage_sessions_result(query, result_totals, result_timeseries))
  569. assert actual_result == expected_result
  570. @freeze_time("2020-12-18T13:25:15.769Z")
  571. def test_clamping_in_massage_sessions_results_with_groupby_timeseries():
  572. query = _make_query(
  573. "statsPeriod=12h&interval=6h&field=sum(session)&field=count_unique(user)&groupBy=session.status"
  574. )
  575. # snuba returns the datetimes as strings for now
  576. result_timeseries = [
  577. {
  578. "sessions": 7,
  579. "sessions_errored": 3,
  580. "sessions_crashed": 2,
  581. "sessions_abnormal": 2,
  582. "users": 7,
  583. "users_errored": 3,
  584. "users_crashed": 2,
  585. "users_abnormal": 2,
  586. "bucketed_started": "2020-12-18T12:00:00+00:00",
  587. },
  588. {
  589. "sessions": 5,
  590. "sessions_errored": 10,
  591. "sessions_crashed": 0,
  592. "sessions_abnormal": 0,
  593. "users": 5,
  594. "users_errored": 10,
  595. "users_crashed": 0,
  596. "users_abnormal": 0,
  597. "bucketed_started": "2020-12-18T06:00:00+00:00",
  598. },
  599. ]
  600. expected_result = {
  601. "start": "2020-12-18T06:00:00Z",
  602. "end": "2020-12-18T13:26:00Z",
  603. "query": "",
  604. "intervals": [
  605. "2020-12-18T06:00:00Z",
  606. "2020-12-18T12:00:00Z",
  607. ],
  608. "groups": [
  609. {
  610. "by": {"session.status": "abnormal"},
  611. "series": {"count_unique(user)": [0, 2], "sum(session)": [0, 2]},
  612. "totals": {"count_unique(user)": 0, "sum(session)": 0},
  613. },
  614. {
  615. "by": {"session.status": "crashed"},
  616. "series": {"count_unique(user)": [0, 2], "sum(session)": [0, 2]},
  617. "totals": {"count_unique(user)": 0, "sum(session)": 0},
  618. },
  619. {
  620. "by": {"session.status": "errored"},
  621. "series": {"count_unique(user)": [10, 0], "sum(session)": [10, 0]},
  622. "totals": {"count_unique(user)": 0, "sum(session)": 0},
  623. },
  624. {
  625. "by": {"session.status": "healthy"},
  626. "series": {"count_unique(user)": [0, 4], "sum(session)": [0, 4]},
  627. "totals": {"count_unique(user)": 0, "sum(session)": 0},
  628. },
  629. ],
  630. }
  631. actual_result = result_sorted(massage_sessions_result(query, [], result_timeseries))
  632. assert actual_result == expected_result
  633. @freeze_time("2020-12-18T11:14:17.105Z")
  634. def test_nan_duration():
  635. query = _make_query(
  636. "statsPeriod=1d&interval=6h&field=avg(session.duration)&field=p50(session.duration)"
  637. )
  638. result_totals = [
  639. {
  640. "duration_avg": math.nan,
  641. "duration_quantiles": [math.inf, math.inf, math.inf, math.inf, math.inf, math.inf],
  642. },
  643. ]
  644. result_timeseries = [
  645. {
  646. "duration_avg": math.inf,
  647. "duration_quantiles": [math.inf, math.inf, math.inf, math.inf, math.inf, math.inf],
  648. "bucketed_started": "2020-12-18T06:00:00+00:00",
  649. },
  650. {
  651. "duration_avg": math.nan,
  652. "duration_quantiles": [math.nan, math.nan, math.nan, math.nan, math.nan, math.nan],
  653. "bucketed_started": "2020-12-17T12:00:00+00:00",
  654. },
  655. ]
  656. expected_result = {
  657. "start": "2020-12-17T12:00:00Z",
  658. "end": "2020-12-18T11:15:00Z",
  659. "query": "",
  660. "intervals": [
  661. "2020-12-17T12:00:00Z",
  662. "2020-12-17T18:00:00Z",
  663. "2020-12-18T00:00:00Z",
  664. "2020-12-18T06:00:00Z",
  665. ],
  666. "groups": [
  667. {
  668. "by": {},
  669. "series": {
  670. "avg(session.duration)": [None, None, None, None],
  671. "p50(session.duration)": [None, None, None, None],
  672. },
  673. "totals": {"avg(session.duration)": None, "p50(session.duration)": None},
  674. },
  675. ],
  676. }
  677. actual_result = result_sorted(massage_sessions_result(query, result_totals, result_timeseries))
  678. assert actual_result == expected_result