sessions.spec.tsx 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  1. import {SessionFieldWithOperation, SessionStatus} from 'sentry/types';
  2. import {
  3. filterSessionsInTimeWindow,
  4. getCount,
  5. getCountAtIndex,
  6. getCrashFreeRate,
  7. getSessionsInterval,
  8. getSessionStatusRate,
  9. } from 'sentry/utils/sessions';
  10. const sessionsApiResponse = {
  11. start: '2021-07-09T23:00:00Z',
  12. end: '2021-07-11T14:51:00Z',
  13. query: '',
  14. intervals: [
  15. '2021-07-09T23:00:00Z',
  16. '2021-07-10T00:00:00Z',
  17. '2021-07-10T01:00:00Z',
  18. '2021-07-10T02:00:00Z',
  19. '2021-07-10T03:00:00Z',
  20. '2021-07-10T04:00:00Z',
  21. '2021-07-10T05:00:00Z',
  22. '2021-07-10T06:00:00Z',
  23. '2021-07-10T07:00:00Z',
  24. '2021-07-10T08:00:00Z',
  25. '2021-07-10T09:00:00Z',
  26. '2021-07-10T10:00:00Z',
  27. '2021-07-10T11:00:00Z',
  28. '2021-07-10T12:00:00Z',
  29. '2021-07-10T13:00:00Z',
  30. '2021-07-10T14:00:00Z',
  31. '2021-07-10T15:00:00Z',
  32. '2021-07-10T16:00:00Z',
  33. '2021-07-10T17:00:00Z',
  34. '2021-07-10T18:00:00Z',
  35. '2021-07-10T19:00:00Z',
  36. '2021-07-10T20:00:00Z',
  37. '2021-07-10T21:00:00Z',
  38. '2021-07-10T22:00:00Z',
  39. '2021-07-10T23:00:00Z',
  40. '2021-07-11T00:00:00Z',
  41. '2021-07-11T01:00:00Z',
  42. '2021-07-11T02:00:00Z',
  43. '2021-07-11T03:00:00Z',
  44. '2021-07-11T04:00:00Z',
  45. '2021-07-11T05:00:00Z',
  46. '2021-07-11T06:00:00Z',
  47. '2021-07-11T07:00:00Z',
  48. '2021-07-11T08:00:00Z',
  49. '2021-07-11T09:00:00Z',
  50. '2021-07-11T10:00:00Z',
  51. '2021-07-11T11:00:00Z',
  52. '2021-07-11T12:00:00Z',
  53. '2021-07-11T13:00:00Z',
  54. '2021-07-11T14:00:00Z',
  55. ],
  56. groups: [
  57. {
  58. by: {'session.status': 'abnormal'},
  59. totals: {'count_unique(user)': 0, 'sum(session)': 0},
  60. series: {
  61. 'count_unique(user)': [
  62. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  63. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  64. ],
  65. 'sum(session)': [
  66. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  67. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  68. ],
  69. 'p50(session.duration)': [
  70. 5432, 3999, 2632, 2624, 2587, 3525, 3666, 3783, 4059, 3882, 4022, 4490, 4052,
  71. 4157, 4166, 4502, 4260, 4713, 4474, 3802, 3199, 2296, 2737, 2259, 1560, 1659,
  72. 1997, 1975, 1777, 1897, 2783, 3310, 4414, 4012, 4230, 4618, 4773, 4814, 4821,
  73. 3893,
  74. ],
  75. },
  76. },
  77. {
  78. by: {'session.status': 'errored'},
  79. totals: {'count_unique(user)': 379, 'sum(session)': 146},
  80. series: {
  81. 'count_unique(user)': [
  82. 23, 6, 5, 7, 11, 5, 6, 8, 12, 14, 9, 16, 15, 22, 28, 11, 14, 16, 9, 11, 11, 7,
  83. 7, 4, 3, 7, 6, 12, 3, 6, 6, 4, 8, 14, 23, 16, 14, 18, 12, 8,
  84. ],
  85. 'sum(session)': [
  86. 29, 3, 0, 0, 11, 0, 0, 0, 0, 1, 2, 5, 1, 16, 40, 0, 5, 0, 1, 13, 3, 0, 0, 5, 0,
  87. 0, 3, 14, 0, 3, 2, 0, 3, 9, 16, 6, 0, 31, 20, 13,
  88. ],
  89. 'p50(session.duration)': [
  90. 0, 1, 2632, 2624, 2587, 3525, 3666, 3783, 4059, 3882, 4022, 4490, 4052, 4157,
  91. 4166, 4502, 4260, 4713, 4474, 3802, 3199, 2296, 2737, 2259, 1560, 1659, 1997,
  92. 1975, 1777, 1897, 2783, 3310, 4414, 4012, 4230, 4618, 4773, 4814, 4821, 2,
  93. ],
  94. },
  95. },
  96. {
  97. by: {'session.status': 'crashed'},
  98. totals: {'count_unique(user)': 341, 'sum(session)': 1796},
  99. series: {
  100. 'count_unique(user)': [
  101. 5, 10, 5, 6, 6, 12, 9, 16, 24, 16, 11, 13, 20, 16, 12, 18, 18, 17, 12, 8, 8, 19,
  102. 15, 5, 4, 9, 4, 7, 5, 7, 4, 12, 13, 11, 15, 9, 21, 20, 14, 11,
  103. ],
  104. 'sum(session)': [
  105. 33, 32, 31, 36, 30, 78, 56, 60, 95, 55, 52, 47, 53, 43, 61, 68, 43, 71, 47, 29,
  106. 38, 65, 55, 14, 14, 34, 30, 32, 23, 20, 21, 53, 40, 39, 56, 34, 60, 61, 62, 25,
  107. ],
  108. 'p50(session.duration)': [
  109. 3, 4, 5, 2624, 2587, 3525, 3666, 3783, 4059, 3882, 4022, 4490, 4052, 4157, 4166,
  110. 4502, 4260, 4713, 4474, 3802, 3199, 2296, 2737, 2259, 1560, 1659, 1997, 1975,
  111. 1777, 1897, 2783, 3310, 4414, 4012, 4230, 4618, 4773, 4814, 4821, 6,
  112. ],
  113. },
  114. },
  115. {
  116. by: {'session.status': 'healthy'},
  117. totals: {'count_unique(user)': 6585, 'sum(session)': 141851},
  118. series: {
  119. 'count_unique(user)': [
  120. 454, 351, 261, 239, 229, 250, 296, 329, 337, 336, 347, 368, 351, 372, 370, 391,
  121. 323, 358, 348, 307, 266, 224, 201, 168, 156, 133, 146, 164, 168, 151, 184, 276,
  122. 341, 354, 351, 379, 395, 400, 394, 333,
  123. ],
  124. 'sum(session)': [
  125. 5432, 3999, 2632, 2624, 2587, 3525, 3666, 3783, 4059, 3882, 4022, 4490, 4052,
  126. 4157, 4166, 4502, 4260, 4713, 4474, 3802, 3199, 2296, 2737, 2259, 1560, 1659,
  127. 1997, 1975, 1777, 1897, 2783, 3310, 4414, 4012, 4230, 4618, 4773, 4814, 4821,
  128. 3893,
  129. ],
  130. 'p50(session.duration)': [
  131. 7, 8, 9, 2624, 2587, 3525, 3666, 3783, 4059, 3882, 4022, 4490, 4052, 4157, 4166,
  132. 4502, 4260, 4713, 4474, 3802, 3199, 2296, 2737, 2259, 1560, 1659, 1997, 1975,
  133. 1777, 1897, 2783, 3310, 4414, 4012, 4230, 4618, 4773, 4814, 4821, 10,
  134. ],
  135. },
  136. },
  137. ],
  138. };
  139. describe('utils/sessions', () => {
  140. describe('getCount', () => {
  141. const groups = [sessionsApiResponse.groups[1], sessionsApiResponse.groups[2]];
  142. it('returns sessions count', () => {
  143. expect(getCount(groups, SessionFieldWithOperation.SESSIONS)).toBe(1942);
  144. });
  145. it('returns users count', () => {
  146. expect(getCount(groups, SessionFieldWithOperation.USERS)).toBe(720);
  147. });
  148. });
  149. describe('getCountAtIndex', () => {
  150. const groups = [sessionsApiResponse.groups[1], sessionsApiResponse.groups[2]];
  151. it('returns sessions count', () => {
  152. expect(getCountAtIndex(groups, SessionFieldWithOperation.SESSIONS, 1)).toBe(35);
  153. });
  154. it('returns users count', () => {
  155. expect(getCountAtIndex(groups, SessionFieldWithOperation.USERS, 1)).toBe(16);
  156. });
  157. });
  158. describe('getCrashFreeRate', () => {
  159. const {groups} = sessionsApiResponse;
  160. it('returns crash free sessions', () => {
  161. expect(getCrashFreeRate(groups, SessionFieldWithOperation.SESSIONS)).toBe(98.751);
  162. });
  163. it('returns crash free users', () => {
  164. expect(getCrashFreeRate(groups, SessionFieldWithOperation.USERS)).toBe(95.332);
  165. });
  166. });
  167. describe('getSessionStatusRate', () => {
  168. const {groups} = sessionsApiResponse;
  169. it('returns errored sessions rate', () => {
  170. expect(
  171. getSessionStatusRate(
  172. groups,
  173. SessionFieldWithOperation.SESSIONS,
  174. SessionStatus.ERRORED
  175. )
  176. ).toBe(0.10153484522890543);
  177. });
  178. it('returns healthy users rate', () => {
  179. expect(
  180. getSessionStatusRate(
  181. groups,
  182. SessionFieldWithOperation.USERS,
  183. SessionStatus.HEALTHY
  184. )
  185. ).toBe(90.14373716632443);
  186. });
  187. });
  188. describe('getSessionsInterval', () => {
  189. describe('with high fidelity', () => {
  190. it('>= 60 days', () => {
  191. expect(getSessionsInterval({period: '60d'}, {highFidelity: true})).toBe('1d');
  192. });
  193. it('>= 30 days', () => {
  194. expect(getSessionsInterval({period: '30d'}, {highFidelity: true})).toBe('4h');
  195. });
  196. it('14 days', () => {
  197. expect(getSessionsInterval({period: '14d'}, {highFidelity: true})).toBe('1h');
  198. });
  199. it('>= 6 hours', () => {
  200. expect(getSessionsInterval({period: '6h'}, {highFidelity: true})).toBe('1h');
  201. });
  202. it('between 6 hours and 30 minutes', () => {
  203. expect(getSessionsInterval({period: '31m'}, {highFidelity: true})).toBe('5m');
  204. });
  205. it('less or equal to 30 minutes', () => {
  206. expect(getSessionsInterval({period: '30m'}, {highFidelity: true})).toBe('1m');
  207. });
  208. it('less or equal to 10 minutes', () => {
  209. expect(
  210. getSessionsInterval(
  211. {start: '2021-10-08T12:00:00Z', end: '2021-10-08T12:05:00.000Z'},
  212. {highFidelity: true}
  213. )
  214. ).toBe('10s');
  215. });
  216. it('ignores high fidelity flag if start is older than 30d', () => {
  217. expect(
  218. getSessionsInterval(
  219. {start: '2017-09-15T02:41:20Z', end: '2017-09-15T02:42:20Z'},
  220. {highFidelity: true}
  221. )
  222. ).toBe('1h');
  223. });
  224. });
  225. describe('with low fidelity', () => {
  226. it('>= 60 days', () => {
  227. expect(getSessionsInterval({period: '60d'})).toBe('1d');
  228. expect(
  229. getSessionsInterval(
  230. {start: '2021-07-19T15:14:23Z', end: '2021-10-19T15:14:23Z'},
  231. {highFidelity: true}
  232. )
  233. ).toBe('1d');
  234. });
  235. it('>= 30 days', () => {
  236. expect(getSessionsInterval({period: '30d'})).toBe('4h');
  237. });
  238. it('14 days', () => {
  239. expect(getSessionsInterval({period: '14d'})).toBe('1h');
  240. });
  241. it('>= 6 hours', () => {
  242. expect(getSessionsInterval({period: '6h'})).toBe('1h');
  243. });
  244. it('between 6 hours and 30 minutes', () => {
  245. expect(getSessionsInterval({period: '31m'})).toBe('1h');
  246. });
  247. it('less or equal to 30 minutes', () => {
  248. expect(getSessionsInterval({period: '30m'})).toBe('1h');
  249. });
  250. });
  251. });
  252. describe('filterSessionsInTimeWindow', () => {
  253. it('filters out intervals/series out of bounds', () => {
  254. const filtered = filterSessionsInTimeWindow(
  255. sessionsApiResponse,
  256. '2021-07-09T23:12:57.265410Z',
  257. '2021-07-11T14:49:59Z'
  258. );
  259. expect(filtered).toEqual({
  260. start: '2021-07-10T00:00:00Z',
  261. end: '2021-07-11T14:00:00Z',
  262. query: '',
  263. intervals: [
  264. '2021-07-10T00:00:00Z',
  265. '2021-07-10T01:00:00Z',
  266. '2021-07-10T02:00:00Z',
  267. '2021-07-10T03:00:00Z',
  268. '2021-07-10T04:00:00Z',
  269. '2021-07-10T05:00:00Z',
  270. '2021-07-10T06:00:00Z',
  271. '2021-07-10T07:00:00Z',
  272. '2021-07-10T08:00:00Z',
  273. '2021-07-10T09:00:00Z',
  274. '2021-07-10T10:00:00Z',
  275. '2021-07-10T11:00:00Z',
  276. '2021-07-10T12:00:00Z',
  277. '2021-07-10T13:00:00Z',
  278. '2021-07-10T14:00:00Z',
  279. '2021-07-10T15:00:00Z',
  280. '2021-07-10T16:00:00Z',
  281. '2021-07-10T17:00:00Z',
  282. '2021-07-10T18:00:00Z',
  283. '2021-07-10T19:00:00Z',
  284. '2021-07-10T20:00:00Z',
  285. '2021-07-10T21:00:00Z',
  286. '2021-07-10T22:00:00Z',
  287. '2021-07-10T23:00:00Z',
  288. '2021-07-11T00:00:00Z',
  289. '2021-07-11T01:00:00Z',
  290. '2021-07-11T02:00:00Z',
  291. '2021-07-11T03:00:00Z',
  292. '2021-07-11T04:00:00Z',
  293. '2021-07-11T05:00:00Z',
  294. '2021-07-11T06:00:00Z',
  295. '2021-07-11T07:00:00Z',
  296. '2021-07-11T08:00:00Z',
  297. '2021-07-11T09:00:00Z',
  298. '2021-07-11T10:00:00Z',
  299. '2021-07-11T11:00:00Z',
  300. '2021-07-11T12:00:00Z',
  301. '2021-07-11T13:00:00Z',
  302. '2021-07-11T14:00:00Z',
  303. ],
  304. groups: [
  305. {
  306. by: {'session.status': 'abnormal'},
  307. totals: {
  308. 'count_unique(user)': 0,
  309. 'sum(session)': 0,
  310. 'p50(session.duration)': 3497.923076923077,
  311. },
  312. series: {
  313. 'count_unique(user)': [
  314. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  315. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  316. ],
  317. 'sum(session)': [
  318. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  319. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  320. ],
  321. 'p50(session.duration)': [
  322. 3999, 2632, 2624, 2587, 3525, 3666, 3783, 4059, 3882, 4022, 4490, 4052,
  323. 4157, 4166, 4502, 4260, 4713, 4474, 3802, 3199, 2296, 2737, 2259, 1560,
  324. 1659, 1997, 1975, 1777, 1897, 2783, 3310, 4414, 4012, 4230, 4618, 4773,
  325. 4814, 4821, 3893,
  326. ],
  327. },
  328. },
  329. {
  330. by: {'session.status': 'errored'},
  331. totals: {
  332. 'count_unique(user)': 379,
  333. 'sum(session)': 226,
  334. 'p50(session.duration)': 3295.641025641026,
  335. },
  336. series: {
  337. 'count_unique(user)': [
  338. 6, 5, 7, 11, 5, 6, 8, 12, 14, 9, 16, 15, 22, 28, 11, 14, 16, 9, 11, 11, 7,
  339. 7, 4, 3, 7, 6, 12, 3, 6, 6, 4, 8, 14, 23, 16, 14, 18, 12, 8,
  340. ],
  341. 'sum(session)': [
  342. 3, 0, 0, 11, 0, 0, 0, 0, 1, 2, 5, 1, 16, 40, 0, 5, 0, 1, 13, 3, 0, 0, 5,
  343. 0, 0, 3, 14, 0, 3, 2, 0, 3, 9, 16, 6, 0, 31, 20, 13,
  344. ],
  345. 'p50(session.duration)': [
  346. 1, 2632, 2624, 2587, 3525, 3666, 3783, 4059, 3882, 4022, 4490, 4052, 4157,
  347. 4166, 4502, 4260, 4713, 4474, 3802, 3199, 2296, 2737, 2259, 1560, 1659,
  348. 1997, 1975, 1777, 1897, 2783, 3310, 4414, 4012, 4230, 4618, 4773, 4814,
  349. 4821, 2,
  350. ],
  351. },
  352. },
  353. {
  354. by: {'session.status': 'crashed'},
  355. totals: {
  356. 'count_unique(user)': 341,
  357. 'sum(session)': 1763,
  358. 'p50(session.duration)': 3228.4615384615386,
  359. },
  360. series: {
  361. 'count_unique(user)': [
  362. 10, 5, 6, 6, 12, 9, 16, 24, 16, 11, 13, 20, 16, 12, 18, 18, 17, 12, 8, 8,
  363. 19, 15, 5, 4, 9, 4, 7, 5, 7, 4, 12, 13, 11, 15, 9, 21, 20, 14, 11,
  364. ],
  365. 'sum(session)': [
  366. 32, 31, 36, 30, 78, 56, 60, 95, 55, 52, 47, 53, 43, 61, 68, 43, 71, 47,
  367. 29, 38, 65, 55, 14, 14, 34, 30, 32, 23, 20, 21, 53, 40, 39, 56, 34, 60,
  368. 61, 62, 25,
  369. ],
  370. 'p50(session.duration)': [
  371. 4, 5, 2624, 2587, 3525, 3666, 3783, 4059, 3882, 4022, 4490, 4052, 4157,
  372. 4166, 4502, 4260, 4713, 4474, 3802, 3199, 2296, 2737, 2259, 1560, 1659,
  373. 1997, 1975, 1777, 1897, 2783, 3310, 4414, 4012, 4230, 4618, 4773, 4814,
  374. 4821, 6,
  375. ],
  376. },
  377. },
  378. {
  379. by: {'session.status': 'healthy'},
  380. totals: {
  381. 'count_unique(user)': 6585,
  382. 'sum(session)': 136419,
  383. 'p50(session.duration)': 3228.769230769231,
  384. },
  385. series: {
  386. 'count_unique(user)': [
  387. 351, 261, 239, 229, 250, 296, 329, 337, 336, 347, 368, 351, 372, 370, 391,
  388. 323, 358, 348, 307, 266, 224, 201, 168, 156, 133, 146, 164, 168, 151, 184,
  389. 276, 341, 354, 351, 379, 395, 400, 394, 333,
  390. ],
  391. 'sum(session)': [
  392. 3999, 2632, 2624, 2587, 3525, 3666, 3783, 4059, 3882, 4022, 4490, 4052,
  393. 4157, 4166, 4502, 4260, 4713, 4474, 3802, 3199, 2296, 2737, 2259, 1560,
  394. 1659, 1997, 1975, 1777, 1897, 2783, 3310, 4414, 4012, 4230, 4618, 4773,
  395. 4814, 4821, 3893,
  396. ],
  397. 'p50(session.duration)': [
  398. 8, 9, 2624, 2587, 3525, 3666, 3783, 4059, 3882, 4022, 4490, 4052, 4157,
  399. 4166, 4502, 4260, 4713, 4474, 3802, 3199, 2296, 2737, 2259, 1560, 1659,
  400. 1997, 1975, 1777, 1897, 2783, 3310, 4414, 4012, 4230, 4618, 4773, 4814,
  401. 4821, 10,
  402. ],
  403. },
  404. },
  405. ],
  406. });
  407. });
  408. });
  409. });