test_organization_stats_summary.py 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742
  1. import functools
  2. from datetime import datetime, timedelta, timezone
  3. from django.urls import reverse
  4. from sentry.constants import DataCategory
  5. from sentry.testutils.cases import APITestCase, OutcomesSnubaTest
  6. from sentry.testutils.helpers.datetime import freeze_time
  7. from sentry.testutils.silo import region_silo_test
  8. from sentry.utils.outcomes import Outcome
  9. @region_silo_test
  10. class OrganizationStatsSummaryTest(APITestCase, OutcomesSnubaTest):
  11. def setUp(self):
  12. super().setUp()
  13. self.now = datetime(2021, 3, 14, 12, 27, 28, tzinfo=timezone.utc)
  14. self.login_as(user=self.user)
  15. self.org = self.organization
  16. self.org.flags.allow_joinleave = False
  17. self.org.save()
  18. self.org2 = self.create_organization()
  19. self.org3 = self.create_organization()
  20. self.project = self.create_project(
  21. name="bar", teams=[self.create_team(organization=self.org, members=[self.user])]
  22. )
  23. self.project2 = self.create_project(
  24. name="foo", teams=[self.create_team(organization=self.org, members=[self.user])]
  25. )
  26. self.project3 = self.create_project(organization=self.org2)
  27. self.user2 = self.create_user(is_superuser=False)
  28. self.create_member(user=self.user2, organization=self.organization, role="member", teams=[])
  29. self.create_member(user=self.user2, organization=self.org3, role="member", teams=[])
  30. self.project4 = self.create_project(
  31. name="users2sproj",
  32. teams=[self.create_team(organization=self.org, members=[self.user2])],
  33. )
  34. self.store_outcomes(
  35. {
  36. "org_id": self.org.id,
  37. "timestamp": self.now - timedelta(hours=1),
  38. "project_id": self.project.id,
  39. "outcome": Outcome.ACCEPTED,
  40. "reason": "none",
  41. "category": DataCategory.ERROR,
  42. "quantity": 1,
  43. },
  44. 5,
  45. )
  46. self.store_outcomes(
  47. {
  48. "org_id": self.org.id,
  49. "timestamp": self.now - timedelta(hours=1),
  50. "project_id": self.project.id,
  51. "outcome": Outcome.ACCEPTED,
  52. "reason": "none",
  53. "category": DataCategory.DEFAULT, # test that this shows up under error
  54. "quantity": 1,
  55. }
  56. )
  57. self.store_outcomes(
  58. {
  59. "org_id": self.org.id,
  60. "timestamp": self.now - timedelta(hours=1),
  61. "project_id": self.project.id,
  62. "outcome": Outcome.RATE_LIMITED,
  63. "reason": "smart_rate_limit",
  64. "category": DataCategory.ATTACHMENT,
  65. "quantity": 1024,
  66. }
  67. )
  68. self.store_outcomes(
  69. {
  70. "org_id": self.org.id,
  71. "timestamp": self.now - timedelta(hours=1),
  72. "project_id": self.project2.id,
  73. "outcome": Outcome.RATE_LIMITED,
  74. "reason": "smart_rate_limit",
  75. "category": DataCategory.TRANSACTION,
  76. "quantity": 1,
  77. }
  78. )
  79. def do_request(self, query, user=None, org=None):
  80. self.login_as(user=user or self.user)
  81. url = reverse(
  82. "sentry-api-0-organization-stats-summary",
  83. kwargs={"organization_slug": (org or self.organization).slug},
  84. )
  85. return self.client.get(url, query, format="json")
  86. def test_empty_request(self):
  87. response = self.do_request({})
  88. assert response.status_code == 400, response.content
  89. assert response.data == {"detail": 'At least one "field" is required.'}
  90. def test_inaccessible_project(self):
  91. response = self.do_request({"project": [self.project3.id]})
  92. assert response.status_code == 403, response.content
  93. assert response.data == {"detail": "You do not have permission to perform this action."}
  94. def test_no_projects_available(self):
  95. response = self.do_request(
  96. {
  97. "groupBy": ["project"],
  98. "statsPeriod": "1d",
  99. "interval": "1d",
  100. "field": ["sum(quantity)"],
  101. "category": ["error", "transaction"],
  102. },
  103. user=self.user2,
  104. org=self.org3,
  105. )
  106. assert response.status_code == 400, response.content
  107. assert response.data == {
  108. "detail": "No projects available",
  109. }
  110. def test_unknown_field(self):
  111. response = self.do_request(
  112. {
  113. "field": ["summ(qarntenty)"],
  114. "statsPeriod": "1d",
  115. "interval": "1d",
  116. }
  117. )
  118. assert response.status_code == 400, response.content
  119. assert response.data == {
  120. "detail": 'Invalid field: "summ(qarntenty)"',
  121. }
  122. def test_no_end_param(self):
  123. response = self.do_request(
  124. {"field": ["sum(quantity)"], "interval": "1d", "start": "2021-03-14T00:00:00Z"}
  125. )
  126. assert response.status_code == 400, response.content
  127. assert response.data == {"detail": "start and end are both required"}
  128. @freeze_time(datetime(2021, 3, 14, 12, 27, 28, tzinfo=timezone.utc))
  129. def test_future_request(self):
  130. response = self.do_request(
  131. {
  132. "field": ["sum(quantity)"],
  133. "interval": "1h",
  134. "category": ["error"],
  135. "start": "2021-03-14T15:30:00",
  136. "end": "2021-03-14T16:30:00",
  137. }
  138. )
  139. assert response.status_code == 200, response.content
  140. assert response.data == {
  141. "start": "2021-03-14T12:00:00Z",
  142. "end": "2021-03-14T12:28:00Z",
  143. "projects": [],
  144. }
  145. def test_unknown_category(self):
  146. response = self.do_request(
  147. {
  148. "field": ["sum(quantity)"],
  149. "statsPeriod": "1d",
  150. "interval": "1d",
  151. "category": "scoobydoo",
  152. }
  153. )
  154. assert response.status_code == 400, response.content
  155. assert response.data == {
  156. "detail": 'Invalid category: "scoobydoo"',
  157. }
  158. def test_unknown_outcome(self):
  159. response = self.do_request(
  160. {
  161. "field": ["sum(quantity)"],
  162. "statsPeriod": "1d",
  163. "interval": "1d",
  164. "category": "error",
  165. "outcome": "scoobydoo",
  166. }
  167. )
  168. assert response.status_code == 400, response.content
  169. assert response.data == {
  170. "detail": 'Invalid outcome: "scoobydoo"',
  171. }
  172. def test_resolution_invalid(self):
  173. self.login_as(user=self.user)
  174. make_request = functools.partial(
  175. self.client.get,
  176. reverse("sentry-api-0-organization-stats-summary", args=[self.org.slug]),
  177. )
  178. response = make_request(
  179. {
  180. "statsPeriod": "1d",
  181. "interval": "bad_interval",
  182. }
  183. )
  184. assert response.status_code == 400, response.content
  185. @freeze_time("2021-03-14T12:27:28.303Z")
  186. def test_attachment_filter_only(self):
  187. response = self.do_request(
  188. {
  189. "project": [-1],
  190. "statsPeriod": "1d",
  191. "interval": "1d",
  192. "field": ["sum(quantity)"],
  193. "category": ["error", "attachment"],
  194. }
  195. )
  196. assert response.status_code == 400, response.content
  197. assert response.data == {
  198. "detail": "if filtering by attachment no other category may be present"
  199. }
  200. @freeze_time("2021-03-14T12:27:28.303Z")
  201. def test_user_all_accessible(self):
  202. response = self.do_request(
  203. {
  204. "project": self.project.id,
  205. "statsPeriod": "1d",
  206. "interval": "1d",
  207. "field": ["sum(quantity)"],
  208. "category": ["error", "transaction"],
  209. },
  210. user=self.user2,
  211. )
  212. assert response.status_code == 403
  213. response = self.do_request(
  214. {
  215. "project": [-1],
  216. "statsPeriod": "1d",
  217. "interval": "1d",
  218. "field": ["sum(quantity)"],
  219. "category": ["error", "transaction"],
  220. },
  221. user=self.user2,
  222. )
  223. assert response.status_code == 200
  224. assert response.data == {
  225. "start": "2021-03-14T00:00:00Z",
  226. "end": "2021-03-14T12:28:00Z",
  227. "projects": [],
  228. }
  229. @freeze_time("2021-03-14T12:27:28.303Z")
  230. def test_no_project_access(self):
  231. user = self.create_user(is_superuser=False)
  232. self.create_member(user=user, organization=self.organization, role="member", teams=[])
  233. response = self.do_request(
  234. {
  235. "project": [self.project.id],
  236. "statsPeriod": "1d",
  237. "interval": "1d",
  238. "category": ["error", "transaction"],
  239. "field": ["sum(quantity)"],
  240. },
  241. org=self.organization,
  242. user=user,
  243. )
  244. assert response.status_code == 403, response.content
  245. assert response.data == {"detail": "You do not have permission to perform this action."}
  246. @freeze_time("2021-03-14T12:27:28.303Z")
  247. def test_open_membership_semantics(self):
  248. self.org.flags.allow_joinleave = True
  249. self.org.save()
  250. response = self.do_request(
  251. {
  252. "project": [-1],
  253. "statsPeriod": "1d",
  254. "interval": "1d",
  255. "field": ["sum(quantity)"],
  256. "category": ["error", "transaction"],
  257. "groupBy": ["project"],
  258. },
  259. user=self.user2,
  260. )
  261. assert response.status_code == 200
  262. assert response.data == {
  263. "start": "2021-03-14T00:00:00Z",
  264. "end": "2021-03-14T12:28:00Z",
  265. "projects": [
  266. {
  267. "id": self.project.id,
  268. "slug": self.project.slug,
  269. "stats": [
  270. {
  271. "category": "error",
  272. "outcomes": {
  273. "abuse": 0,
  274. "accepted": 6,
  275. "client_discard": 0,
  276. "filtered": 0,
  277. "invalid": 0,
  278. "rate_limited": 0,
  279. },
  280. "totals": {"dropped": 0, "sum(quantity)": 6},
  281. }
  282. ],
  283. },
  284. {
  285. "id": self.project2.id,
  286. "slug": self.project2.slug,
  287. "stats": [
  288. {
  289. "category": "transaction",
  290. "outcomes": {
  291. "abuse": 0,
  292. "accepted": 0,
  293. "client_discard": 0,
  294. "filtered": 0,
  295. "invalid": 0,
  296. "rate_limited": 1,
  297. },
  298. "totals": {"dropped": 1, "sum(quantity)": 1},
  299. }
  300. ],
  301. },
  302. ],
  303. }
  304. @freeze_time("2021-03-14T12:27:28.303Z")
  305. def test_org_simple(self):
  306. make_request = functools.partial(
  307. self.client.get,
  308. reverse("sentry-api-0-organization-stats-summary", args=[self.org.slug]),
  309. )
  310. response = make_request(
  311. {
  312. "statsPeriod": "2d",
  313. "interval": "1d",
  314. "field": ["sum(quantity)"],
  315. }
  316. )
  317. assert response.status_code == 200, response.content
  318. assert response.data == {
  319. "start": "2021-03-13T00:00:00Z",
  320. "end": "2021-03-14T12:28:00Z",
  321. "projects": [
  322. {
  323. "id": self.project.id,
  324. "slug": self.project.slug,
  325. "stats": [
  326. {
  327. "category": "attachment",
  328. "outcomes": {
  329. "accepted": 0,
  330. "filtered": 0,
  331. "rate_limited": 1024,
  332. "invalid": 0,
  333. "abuse": 0,
  334. "client_discard": 0,
  335. },
  336. "totals": {"dropped": 1024, "sum(quantity)": 1024},
  337. },
  338. {
  339. "category": "error",
  340. "outcomes": {
  341. "accepted": 6,
  342. "filtered": 0,
  343. "rate_limited": 0,
  344. "invalid": 0,
  345. "abuse": 0,
  346. "client_discard": 0,
  347. },
  348. "totals": {"dropped": 0, "sum(quantity)": 6},
  349. },
  350. ],
  351. },
  352. {
  353. "id": self.project2.id,
  354. "slug": self.project2.slug,
  355. "stats": [
  356. {
  357. "category": "transaction",
  358. "outcomes": {
  359. "accepted": 0,
  360. "filtered": 0,
  361. "rate_limited": 1,
  362. "invalid": 0,
  363. "abuse": 0,
  364. "client_discard": 0,
  365. },
  366. "totals": {"dropped": 1, "sum(quantity)": 1},
  367. }
  368. ],
  369. },
  370. ],
  371. }
  372. @freeze_time("2021-03-14T12:27:28.303Z")
  373. def test_org_multiple_fields(self):
  374. make_request = functools.partial(
  375. self.client.get,
  376. reverse("sentry-api-0-organization-stats-summary", args=[self.org.slug]),
  377. )
  378. response = make_request(
  379. {
  380. "statsPeriod": "2d",
  381. "interval": "1d",
  382. "field": ["sum(quantity)", "sum(times_seen)"],
  383. }
  384. )
  385. assert response.status_code == 200, response.content
  386. assert response.data == {
  387. "start": "2021-03-13T00:00:00Z",
  388. "end": "2021-03-14T12:28:00Z",
  389. "projects": [
  390. {
  391. "id": self.project.id,
  392. "slug": self.project.slug,
  393. "stats": [
  394. {
  395. "category": "attachment",
  396. "outcomes": {
  397. "accepted": 0,
  398. "filtered": 0,
  399. "rate_limited": 1025,
  400. "invalid": 0,
  401. "abuse": 0,
  402. "client_discard": 0,
  403. },
  404. "totals": {
  405. "dropped": 1025,
  406. "sum(quantity)": 1024,
  407. "sum(times_seen)": 1,
  408. },
  409. },
  410. {
  411. "category": "error",
  412. "outcomes": {
  413. "accepted": 12,
  414. "filtered": 0,
  415. "rate_limited": 0,
  416. "invalid": 0,
  417. "abuse": 0,
  418. "client_discard": 0,
  419. },
  420. "totals": {"dropped": 0, "sum(quantity)": 6, "sum(times_seen)": 6},
  421. },
  422. ],
  423. },
  424. {
  425. "id": self.project2.id,
  426. "slug": self.project2.slug,
  427. "stats": [
  428. {
  429. "category": "transaction",
  430. "outcomes": {
  431. "accepted": 0,
  432. "filtered": 0,
  433. "rate_limited": 2,
  434. "invalid": 0,
  435. "abuse": 0,
  436. "client_discard": 0,
  437. },
  438. "totals": {"dropped": 2, "sum(quantity)": 1, "sum(times_seen)": 1},
  439. }
  440. ],
  441. },
  442. ],
  443. }
  444. @freeze_time("2021-03-14T12:27:28.303Z")
  445. def test_org_project_totals_per_project(self):
  446. make_request = functools.partial(
  447. self.client.get,
  448. reverse("sentry-api-0-organization-stats-summary", args=[self.org.slug]),
  449. )
  450. response_per_group = make_request(
  451. {
  452. "statsPeriod": "1d",
  453. "interval": "1h",
  454. "field": ["sum(times_seen)"],
  455. "category": ["error", "transaction"],
  456. }
  457. )
  458. assert response_per_group.status_code == 200, response_per_group.content
  459. assert response_per_group.data == {
  460. "start": "2021-03-13T13:00:00Z",
  461. "end": "2021-03-14T12:28:00Z",
  462. "projects": [
  463. {
  464. "id": self.project.id,
  465. "slug": self.project.slug,
  466. "stats": [
  467. {
  468. "category": "error",
  469. "outcomes": {
  470. "abuse": 0,
  471. "accepted": 6,
  472. "client_discard": 0,
  473. "filtered": 0,
  474. "invalid": 0,
  475. "rate_limited": 0,
  476. },
  477. "totals": {"dropped": 0, "sum(times_seen)": 6},
  478. }
  479. ],
  480. },
  481. {
  482. "id": self.project2.id,
  483. "slug": self.project2.slug,
  484. "stats": [
  485. {
  486. "category": "transaction",
  487. "outcomes": {
  488. "abuse": 0,
  489. "accepted": 0,
  490. "client_discard": 0,
  491. "filtered": 0,
  492. "invalid": 0,
  493. "rate_limited": 1,
  494. },
  495. "totals": {"dropped": 1, "sum(times_seen)": 1},
  496. }
  497. ],
  498. },
  499. ],
  500. }
  501. @freeze_time("2021-03-14T12:27:28.303Z")
  502. def test_project_filter(self):
  503. make_request = functools.partial(
  504. self.client.get,
  505. reverse("sentry-api-0-organization-stats-summary", args=[self.org.slug]),
  506. )
  507. response = make_request(
  508. {
  509. "project": self.project.id,
  510. "statsPeriod": "1d",
  511. "interval": "1d",
  512. "field": ["sum(quantity)"],
  513. "category": ["error", "transaction"],
  514. }
  515. )
  516. assert response.status_code == 200, response.content
  517. assert response.data == {
  518. "start": "2021-03-14T00:00:00Z",
  519. "end": "2021-03-14T12:28:00Z",
  520. "projects": [
  521. {
  522. "id": self.project.id,
  523. "slug": self.project.slug,
  524. "stats": [
  525. {
  526. "category": "error",
  527. "outcomes": {
  528. "abuse": 0,
  529. "accepted": 6,
  530. "client_discard": 0,
  531. "filtered": 0,
  532. "invalid": 0,
  533. "rate_limited": 0,
  534. },
  535. "totals": {"dropped": 0, "sum(quantity)": 6},
  536. },
  537. ],
  538. },
  539. ],
  540. }
  541. @freeze_time("2021-03-14T12:27:28.303Z")
  542. def test_reason_filter(self):
  543. make_request = functools.partial(
  544. self.client.get,
  545. reverse("sentry-api-0-organization-stats-summary", args=[self.org.slug]),
  546. )
  547. response = make_request(
  548. {
  549. "statsPeriod": "1d",
  550. "interval": "1d",
  551. "field": ["sum(times_seen)"],
  552. "reason": ["spike_protection"],
  553. "groupBy": ["category"],
  554. }
  555. )
  556. assert response.status_code == 200, response.content
  557. assert response.data == {
  558. "start": "2021-03-14T00:00:00Z",
  559. "end": "2021-03-14T12:28:00Z",
  560. "projects": [
  561. {
  562. "id": self.project.id,
  563. "slug": self.project.slug,
  564. "stats": [
  565. {
  566. "category": "attachment",
  567. "reason": "spike_protection",
  568. "outcomes": {
  569. "accepted": 0,
  570. "filtered": 0,
  571. "rate_limited": 1,
  572. "invalid": 0,
  573. "abuse": 0,
  574. "client_discard": 0,
  575. },
  576. "totals": {"dropped": 1, "sum(times_seen)": 1},
  577. }
  578. ],
  579. },
  580. {
  581. "id": self.project2.id,
  582. "slug": self.project2.slug,
  583. "stats": [
  584. {
  585. "category": "transaction",
  586. "reason": "spike_protection",
  587. "outcomes": {
  588. "accepted": 0,
  589. "filtered": 0,
  590. "rate_limited": 1,
  591. "invalid": 0,
  592. "abuse": 0,
  593. "client_discard": 0,
  594. },
  595. "totals": {"dropped": 1, "sum(times_seen)": 1},
  596. }
  597. ],
  598. },
  599. ],
  600. }
  601. @freeze_time("2021-03-14T12:27:28.303Z")
  602. def test_outcome_filter(self):
  603. make_request = functools.partial(
  604. self.client.get,
  605. reverse("sentry-api-0-organization-stats-summary", args=[self.org.slug]),
  606. )
  607. response = make_request(
  608. {
  609. "statsPeriod": "1d",
  610. "interval": "1d",
  611. "field": ["sum(quantity)"],
  612. "outcome": "accepted",
  613. "category": ["error", "transaction"],
  614. }
  615. )
  616. assert response.status_code == 200, response.content
  617. assert response.data == {
  618. "start": "2021-03-14T00:00:00Z",
  619. "end": "2021-03-14T12:28:00Z",
  620. "projects": [
  621. {
  622. "id": self.project.id,
  623. "slug": self.project.slug,
  624. "stats": [
  625. {
  626. "category": "error",
  627. "outcomes": {
  628. "accepted": 6,
  629. },
  630. "totals": {"sum(quantity)": 6},
  631. }
  632. ],
  633. }
  634. ],
  635. }
  636. @freeze_time("2021-03-14T12:27:28.303Z")
  637. def test_category_filter(self):
  638. make_request = functools.partial(
  639. self.client.get,
  640. reverse("sentry-api-0-organization-stats-summary", args=[self.org.slug]),
  641. )
  642. response = make_request(
  643. {
  644. "statsPeriod": "1d",
  645. "interval": "1d",
  646. "field": ["sum(quantity)"],
  647. "category": "error",
  648. }
  649. )
  650. assert response.status_code == 200, response.content
  651. assert response.data == {
  652. "start": "2021-03-14T00:00:00Z",
  653. "end": "2021-03-14T12:28:00Z",
  654. "projects": [
  655. {
  656. "id": self.project.id,
  657. "slug": self.project.slug,
  658. "stats": [
  659. {
  660. "category": "error",
  661. "outcomes": {
  662. "accepted": 6,
  663. "filtered": 0,
  664. "rate_limited": 0,
  665. "invalid": 0,
  666. "abuse": 0,
  667. "client_discard": 0,
  668. },
  669. "totals": {"dropped": 0, "sum(quantity)": 6},
  670. }
  671. ],
  672. }
  673. ],
  674. }
  675. def test_download(self):
  676. make_request = functools.partial(
  677. self.client.get,
  678. reverse("sentry-api-0-organization-stats-summary", args=[self.org.slug]),
  679. )
  680. response = make_request(
  681. {
  682. "statsPeriod": "2d",
  683. "interval": "1d",
  684. "field": ["sum(quantity)", "sum(times_seen)"],
  685. "download": True,
  686. }
  687. )
  688. assert response.headers["Content-Type"] == "text/csv"
  689. assert response.headers["Content-Disposition"] == 'attachment; filename="stats_summary.csv"'
  690. assert response.status_code == 200