test_organization_events_v2.py 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747
  1. import copy
  2. from datetime import timedelta, timezone
  3. from unittest.mock import patch
  4. from urllib.parse import urlencode
  5. import pytest
  6. from selenium.webdriver.common.by import By
  7. from selenium.webdriver.common.keys import Keys
  8. from sentry.discover.models import DiscoverSavedQuery
  9. from sentry.testutils.cases import AcceptanceTestCase, SnubaTestCase
  10. from sentry.testutils.helpers.datetime import before_now, iso_format, timestamp_format
  11. from sentry.testutils.silo import no_silo_test
  12. from sentry.utils.samples import load_data
  13. FEATURE_NAMES = [
  14. "organizations:discover-basic",
  15. "organizations:discover-query",
  16. "organizations:performance-view",
  17. ]
  18. def all_events_query(**kwargs):
  19. options = {
  20. "sort": ["-timestamp"],
  21. "field": ["title", "event.type", "project", "user.display", "timestamp"],
  22. "name": ["All Events"],
  23. }
  24. options.update(kwargs)
  25. return urlencode(options, doseq=True)
  26. def errors_query(**kwargs):
  27. options = {
  28. "sort": ["-title"],
  29. "name": ["Errors"],
  30. "field": ["title", "count(id)", "count_unique(user)", "project"],
  31. "query": ["event.type:error"],
  32. }
  33. options.update(kwargs)
  34. return urlencode(options, doseq=True)
  35. def transactions_query(**kwargs):
  36. options = {
  37. "sort": ["-count"],
  38. "name": ["Transactions"],
  39. "field": ["transaction", "project", "count()"],
  40. "statsPeriod": ["14d"],
  41. "query": ["event.type:transaction"],
  42. }
  43. options.update(kwargs)
  44. return urlencode(options, doseq=True)
  45. # Sorted by transactions to avoid sorting issues caused by storing events
  46. def transactions_sorted_query(**kwargs):
  47. options = {
  48. "sort": ["transaction"],
  49. "name": ["Transactions"],
  50. "field": ["transaction", "project", "count()"],
  51. "statsPeriod": ["14d"],
  52. "query": ["event.type:transaction"],
  53. }
  54. options.update(kwargs)
  55. return urlencode(options, doseq=True)
  56. def generate_transaction(trace=None, span=None):
  57. end_datetime = before_now(minutes=10)
  58. start_datetime = end_datetime - timedelta(milliseconds=500)
  59. event_data = load_data(
  60. "transaction",
  61. timestamp=end_datetime,
  62. start_timestamp=start_datetime,
  63. trace=trace,
  64. span_id=span,
  65. )
  66. event_data.update({"event_id": "a" * 32})
  67. # generate and build up span tree
  68. reference_span = event_data["spans"][0]
  69. parent_span_id = reference_span["parent_span_id"]
  70. span_tree_blueprint = {
  71. "a": {},
  72. "b": {"bb": {"bbb": {"bbbb": "bbbbb"}}},
  73. "c": {},
  74. "d": {},
  75. "e": {},
  76. }
  77. time_offsets = {
  78. "a": (timedelta(), timedelta(milliseconds=10)),
  79. "b": (timedelta(milliseconds=120), timedelta(milliseconds=250)),
  80. "bb": (timedelta(milliseconds=130), timedelta(milliseconds=10)),
  81. "bbb": (timedelta(milliseconds=140), timedelta(milliseconds=10)),
  82. "bbbb": (timedelta(milliseconds=150), timedelta(milliseconds=10)),
  83. "bbbbb": (timedelta(milliseconds=160), timedelta(milliseconds=90)),
  84. "c": (timedelta(milliseconds=260), timedelta(milliseconds=100)),
  85. "d": (timedelta(milliseconds=375), timedelta(milliseconds=50)),
  86. "e": (timedelta(milliseconds=400), timedelta(milliseconds=100)),
  87. }
  88. def build_span_tree(span_tree, spans, parent_span_id):
  89. for span_id, child in sorted(span_tree.items(), key=lambda item: item[0]):
  90. span = copy.deepcopy(reference_span)
  91. # non-leaf node span
  92. span["parent_span_id"] = parent_span_id.ljust(16, "0")
  93. span["span_id"] = span_id.ljust(16, "0")
  94. (start_delta, span_length) = time_offsets.get(span_id, (timedelta(), timedelta()))
  95. span_start_time = start_datetime + start_delta
  96. span["start_timestamp"] = timestamp_format(span_start_time)
  97. span["timestamp"] = timestamp_format(span_start_time + span_length)
  98. spans.append(span)
  99. if isinstance(child, dict):
  100. spans = build_span_tree(child, spans, span_id)
  101. elif isinstance(child, str):
  102. parent_span_id = span_id
  103. span_id = child
  104. span = copy.deepcopy(reference_span)
  105. # leaf node span
  106. span["parent_span_id"] = parent_span_id.ljust(16, "0")
  107. span["span_id"] = span_id.ljust(16, "0")
  108. (start_delta, span_length) = time_offsets.get(span_id, (timedelta(), timedelta()))
  109. span_start_time = start_datetime + start_delta
  110. span["start_timestamp"] = timestamp_format(span_start_time)
  111. span["timestamp"] = timestamp_format(span_start_time + span_length)
  112. spans.append(span)
  113. return spans
  114. event_data["spans"] = build_span_tree(span_tree_blueprint, [], parent_span_id)
  115. return event_data
  116. @no_silo_test(stable=True)
  117. class OrganizationEventsV2Test(AcceptanceTestCase, SnubaTestCase):
  118. def setUp(self):
  119. super().setUp()
  120. self.user = self.create_user("foo@example.com", is_superuser=True)
  121. self.org = self.create_organization(name="Rowdy Tiger")
  122. self.team = self.create_team(organization=self.org, name="Mariachi Band")
  123. self.project = self.create_project(organization=self.org, teams=[self.team], name="Bengal")
  124. self.create_member(user=self.user, organization=self.org, role="owner", teams=[self.team])
  125. self.login_as(self.user)
  126. self.landing_path = f"/organizations/{self.org.slug}/discover/queries/"
  127. self.result_path = f"/organizations/{self.org.slug}/discover/results/"
  128. def wait_until_loaded(self):
  129. self.browser.wait_until_not('[data-test-id="loading-indicator"]')
  130. self.browser.wait_until_not('[data-test-id="loading-placeholder"]')
  131. def test_events_default_landing(self):
  132. with self.feature(FEATURE_NAMES):
  133. self.browser.get(self.landing_path)
  134. self.wait_until_loaded()
  135. self.browser.snapshot("events-v2 - default landing")
  136. def test_all_events_query_empty_state(self):
  137. with self.feature(FEATURE_NAMES):
  138. self.browser.get(self.result_path + "?" + all_events_query())
  139. self.wait_until_loaded()
  140. self.browser.snapshot("events-v2 - all events query - empty state")
  141. with self.feature(FEATURE_NAMES):
  142. # expect table to expand to the right when no tags are provided
  143. self.browser.get(self.result_path + "?" + all_events_query(tag=[]))
  144. self.wait_until_loaded()
  145. self.browser.snapshot("events-v2 - all events query - empty state - no tags")
  146. @patch("django.utils.timezone.now")
  147. def test_all_events_query(self, mock_now):
  148. now = before_now().replace(tzinfo=timezone.utc)
  149. mock_now.return_value = now
  150. five_mins_ago = iso_format(now - timedelta(minutes=5))
  151. ten_mins_ago = iso_format(now - timedelta(minutes=10))
  152. self.store_event(
  153. data={
  154. "event_id": "a" * 32,
  155. "message": "oh no",
  156. "timestamp": five_mins_ago,
  157. "fingerprint": ["group-1"],
  158. },
  159. project_id=self.project.id,
  160. assert_no_errors=False,
  161. )
  162. self.store_event(
  163. data={
  164. "event_id": "b" * 32,
  165. "message": "this is bad.",
  166. "timestamp": ten_mins_ago,
  167. "fingerprint": ["group-2"],
  168. "user": {
  169. "id": "123",
  170. "email": "someone@example.com",
  171. "username": "haveibeenpwned",
  172. "ip_address": "8.8.8.8",
  173. "name": "Someone",
  174. },
  175. },
  176. project_id=self.project.id,
  177. assert_no_errors=False,
  178. )
  179. self.wait_for_event_count(self.project.id, 2)
  180. with self.feature(FEATURE_NAMES):
  181. self.browser.get(self.result_path + "?" + all_events_query())
  182. self.wait_until_loaded()
  183. # This test is flakey in that we sometimes load this page before the event is processed
  184. # depend on pytest-retry to reload the page
  185. self.browser.wait_until('[data-test-id="grid-editable"] > tbody > tr:nth-child(2)')
  186. self.browser.snapshot("events-v2 - all events query - list")
  187. with self.feature(FEATURE_NAMES):
  188. # expect table to expand to the right when no tags are provided
  189. self.browser.get(self.result_path + "?" + all_events_query(tag=[]))
  190. self.wait_until_loaded()
  191. self.browser.wait_until('[data-test-id="grid-editable"] > tbody > tr:nth-child(2)')
  192. self.browser.snapshot("events-v2 - all events query - list - no tags")
  193. def test_errors_query_empty_state(self):
  194. with self.feature(FEATURE_NAMES):
  195. self.browser.get(self.result_path + "?" + errors_query())
  196. self.wait_until_loaded()
  197. self.browser.snapshot("events-v2 - errors query - empty state")
  198. self.browser.click_when_visible('[data-test-id="grid-edit-enable"]')
  199. self.browser.snapshot(
  200. "events-v2 - errors query - empty state - querybuilder - column edit state"
  201. )
  202. @patch("django.utils.timezone.now")
  203. def test_errors_query(self, mock_now):
  204. now = before_now().replace(tzinfo=timezone.utc)
  205. mock_now.return_value = now
  206. ten_mins_ago = iso_format(now - timedelta(minutes=10))
  207. self.store_event(
  208. data={
  209. "event_id": "a" * 32,
  210. "message": "oh no",
  211. "timestamp": ten_mins_ago,
  212. "fingerprint": ["group-1"],
  213. "type": "error",
  214. },
  215. project_id=self.project.id,
  216. assert_no_errors=False,
  217. )
  218. self.store_event(
  219. data={
  220. "event_id": "b" * 32,
  221. "message": "oh no",
  222. "timestamp": ten_mins_ago,
  223. "fingerprint": ["group-1"],
  224. "type": "error",
  225. },
  226. project_id=self.project.id,
  227. assert_no_errors=False,
  228. )
  229. self.store_event(
  230. data={
  231. "event_id": "c" * 32,
  232. "message": "this is bad.",
  233. "timestamp": ten_mins_ago,
  234. "fingerprint": ["group-2"],
  235. "type": "error",
  236. },
  237. project_id=self.project.id,
  238. assert_no_errors=False,
  239. )
  240. with self.feature(FEATURE_NAMES):
  241. self.browser.get(self.result_path + "?" + errors_query())
  242. self.wait_until_loaded()
  243. self.browser.snapshot("events-v2 - errors")
  244. def test_transactions_query_empty_state(self):
  245. with self.feature(FEATURE_NAMES):
  246. self.browser.get(self.result_path + "?" + transactions_query())
  247. self.wait_until_loaded()
  248. self.browser.snapshot("events-v2 - transactions query - empty state")
  249. with self.feature(FEATURE_NAMES):
  250. # expect table to expand to the right when no tags are provided
  251. self.browser.get(self.result_path + "?" + transactions_query(tag=[]))
  252. self.wait_until_loaded()
  253. self.browser.snapshot("events-v2 - transactions query - empty state - no tags")
  254. @patch("django.utils.timezone.now")
  255. def test_transactions_query(self, mock_now):
  256. mock_now.return_value = before_now().replace(tzinfo=timezone.utc)
  257. event_data = generate_transaction()
  258. self.store_event(data=event_data, project_id=self.project.id, assert_no_errors=True)
  259. with self.feature(FEATURE_NAMES):
  260. self.browser.get(self.result_path + "?" + transactions_query())
  261. self.wait_until_loaded()
  262. self.browser.wait_until_not(
  263. '[data-test-id="grid-editable"] [data-test-id="empty-state"]', timeout=2
  264. )
  265. self.browser.snapshot("events-v2 - transactions query - list")
  266. @patch("django.utils.timezone.now")
  267. def test_event_detail_view_from_all_events(self, mock_now):
  268. now = before_now().replace(tzinfo=timezone.utc)
  269. mock_now.return_value = now
  270. ten_mins_ago = iso_format(now - timedelta(minutes=10))
  271. event_data = load_data("python")
  272. event_data.update(
  273. {
  274. "event_id": "a" * 32,
  275. "timestamp": ten_mins_ago,
  276. "received": ten_mins_ago,
  277. "fingerprint": ["group-1"],
  278. }
  279. )
  280. if "contexts" not in event_data:
  281. event_data["contexts"] = {}
  282. event_data["contexts"]["trace"] = {
  283. "type": "trace",
  284. "trace_id": "a" * 32,
  285. "span_id": "b" * 16,
  286. }
  287. self.store_event(data=event_data, project_id=self.project.id, assert_no_errors=False)
  288. with self.feature(FEATURE_NAMES):
  289. # Get the list page.
  290. self.browser.get(self.result_path + "?" + all_events_query())
  291. self.wait_until_loaded()
  292. # View Event
  293. self.browser.elements('[data-test-id="view-event"]')[0].click()
  294. self.wait_until_loaded()
  295. # header = self.browser.element('[data-test-id="event-header"] div div span')
  296. # assert event_data["message"] in header.text
  297. self.browser.snapshot("events-v2 - single error details view")
  298. @patch("django.utils.timezone.now")
  299. def test_event_detail_view_from_errors_view(self, mock_now):
  300. now = before_now().replace(tzinfo=timezone.utc)
  301. mock_now.return_value = now
  302. event_data = load_data("javascript")
  303. event_data.update(
  304. {
  305. "timestamp": iso_format(now - timedelta(minutes=5)),
  306. "event_id": "d" * 32,
  307. "fingerprint": ["group-1"],
  308. }
  309. )
  310. event_data["contexts"]["trace"] = {
  311. "type": "trace",
  312. "trace_id": "a" * 32,
  313. "span_id": "b" * 16,
  314. }
  315. self.store_event(data=event_data, project_id=self.project.id)
  316. self.wait_for_event_count(self.project.id, 1)
  317. with self.feature(FEATURE_NAMES):
  318. # Get the list page
  319. self.browser.get(self.result_path + "?" + errors_query() + "&statsPeriod=24h")
  320. self.wait_until_loaded()
  321. # Open the stack
  322. self.browser.element('[data-test-id="open-group"]').click()
  323. self.wait_until_loaded()
  324. # View Event
  325. self.browser.elements('[data-test-id="view-event"]')[0].click()
  326. self.wait_until_loaded()
  327. self.browser.snapshot("events-v2 - error event detail view")
  328. @patch("django.utils.timezone.now")
  329. def test_event_detail_view_from_transactions_query(self, mock_now):
  330. mock_now.return_value = before_now().replace(tzinfo=timezone.utc)
  331. event_data = generate_transaction(trace="a" * 32, span="ab" * 8)
  332. self.store_event(data=event_data, project_id=self.project.id, assert_no_errors=True)
  333. # Create a child event that is linked to the parent so we have coverage
  334. # of traversal buttons.
  335. child_event = generate_transaction(
  336. trace=event_data["contexts"]["trace"]["trace_id"], span="bc" * 8
  337. )
  338. child_event["event_id"] = "b" * 32
  339. child_event["contexts"]["trace"]["parent_span_id"] = event_data["spans"][4]["span_id"]
  340. child_event["transaction"] = "z-child-transaction"
  341. child_event["spans"] = child_event["spans"][0:3]
  342. self.store_event(data=child_event, project_id=self.project.id, assert_no_errors=True)
  343. with self.feature(FEATURE_NAMES):
  344. # Get the list page
  345. self.browser.get(self.result_path + "?" + transactions_sorted_query())
  346. self.wait_until_loaded()
  347. # Open the stack
  348. self.browser.elements('[data-test-id="open-group"]')[0].click()
  349. self.wait_until_loaded()
  350. # View Event
  351. self.browser.elements('[data-test-id="view-event"]')[0].click()
  352. self.wait_until_loaded()
  353. self.browser.snapshot("events-v2 - transactions event with auto-grouped spans")
  354. # Expand auto-grouped spans
  355. self.browser.element('[data-test-id="span-row-5"]').click()
  356. # Open a span detail so we can check the search by trace link.
  357. # Click on the 6th one as a missing instrumentation span is inserted.
  358. self.browser.element('[data-test-id="span-row-7"]').click()
  359. # Wait until the child event loads.
  360. child_button = '[data-test-id="view-child-transaction"]'
  361. self.browser.wait_until(child_button)
  362. self.browser.snapshot("events-v2 - transactions event detail view")
  363. # Click on the child transaction.
  364. self.browser.click(child_button)
  365. self.wait_until_loaded()
  366. @patch("django.utils.timezone.now")
  367. def test_event_detail_view_from_transactions_query_siblings(self, mock_now):
  368. mock_now.return_value = before_now().replace(tzinfo=timezone.utc)
  369. event_data = generate_transaction(trace="a" * 32, span="ab" * 8)
  370. # Arranges sibling spans to be autogrouped in a way that will cover many edgecases
  371. last_span = copy.deepcopy(event_data["spans"][-1])
  372. for i in range(5):
  373. clone = copy.deepcopy(last_span)
  374. # If range > 9 this might no longer work because of constraints on span_id (hex 16)
  375. clone["span_id"] = (str("ac" * 6) + str(i)).ljust(16, "0")
  376. event_data["spans"].append(clone)
  377. combo_breaker_span = copy.deepcopy(last_span)
  378. combo_breaker_span["span_id"] = (str("af" * 6)).ljust(16, "0")
  379. combo_breaker_span["op"] = "combo.breaker"
  380. event_data["spans"].append(combo_breaker_span)
  381. for i in range(5):
  382. clone = copy.deepcopy(last_span)
  383. clone["op"] = "django.middleware"
  384. clone["span_id"] = (str("de" * 6) + str(i)).ljust(16, "0")
  385. event_data["spans"].append(clone)
  386. for i in range(5):
  387. clone = copy.deepcopy(last_span)
  388. clone["op"] = "http"
  389. clone["description"] = "test"
  390. clone["span_id"] = (str("bd" * 6) + str(i)).ljust(16, "0")
  391. event_data["spans"].append(clone)
  392. self.store_event(data=event_data, project_id=self.project.id, assert_no_errors=True)
  393. # Create a child event that is linked to the parent so we have coverage
  394. # of traversal buttons.
  395. child_event = generate_transaction(
  396. trace=event_data["contexts"]["trace"]["trace_id"], span="bc" * 8
  397. )
  398. child_event["event_id"] = "b" * 32
  399. child_event["contexts"]["trace"]["parent_span_id"] = event_data["spans"][4]["span_id"]
  400. child_event["transaction"] = "z-child-transaction"
  401. child_event["spans"] = child_event["spans"][0:3]
  402. self.store_event(data=child_event, project_id=self.project.id, assert_no_errors=True)
  403. with self.feature(FEATURE_NAMES):
  404. # Get the list page
  405. self.browser.get(self.result_path + "?" + transactions_sorted_query())
  406. self.wait_until_loaded()
  407. # Open the stack
  408. self.browser.elements('[data-test-id="open-group"]')[0].click()
  409. self.wait_until_loaded()
  410. # View Event
  411. self.browser.elements('[data-test-id="view-event"]')[0].click()
  412. self.wait_until_loaded()
  413. self.browser.snapshot(
  414. "events-v2 - transactions event with descendant and sibling auto-grouped spans"
  415. )
  416. # Expand auto-grouped descendant spans
  417. self.browser.element('[data-test-id="span-row-5"]').click()
  418. # Expand all autogrouped rows
  419. self.browser.element('[data-test-id="span-row-9"]').click()
  420. self.browser.element('[data-test-id="span-row-18"]').click()
  421. self.browser.element('[data-test-id="span-row-23"]').click()
  422. self.browser.snapshot(
  423. "events-v2 - transactions event with expanded descendant and sibling auto-grouped spans"
  424. )
  425. # Click to collapse all of these spans back into autogroups, we expect the span tree to look like it did initially
  426. first_row = self.browser.element('[data-test-id="span-row-23"]')
  427. first_row.find_element(By.CSS_SELECTOR, "a").click()
  428. second_row = self.browser.element('[data-test-id="span-row-18"]')
  429. second_row.find_element(By.CSS_SELECTOR, "a").click()
  430. third_row = self.browser.element('[data-test-id="span-row-9"]')
  431. third_row.find_element(By.CSS_SELECTOR, "a").click()
  432. self.browser.snapshot(
  433. "events-v2 - transactions event after regrouping expanded sibling auto-grouped spans"
  434. )
  435. @patch("django.utils.timezone.now")
  436. def test_transaction_event_detail_view_ops_filtering(self, mock_now):
  437. mock_now.return_value = before_now().replace(tzinfo=timezone.utc)
  438. event_data = generate_transaction(trace="a" * 32, span="ab" * 8)
  439. self.store_event(data=event_data, project_id=self.project.id, assert_no_errors=True)
  440. with self.feature(FEATURE_NAMES):
  441. # Get the list page
  442. self.browser.get(self.result_path + "?" + transactions_query())
  443. self.wait_until_loaded()
  444. # Open the stack
  445. self.browser.elements('[data-test-id="open-group"]')[0].click()
  446. self.wait_until_loaded()
  447. # View Event
  448. self.browser.elements('[data-test-id="view-event"]')[0].click()
  449. self.wait_until_loaded()
  450. # Interact with ops filter dropdown
  451. self.browser.elements('[aria-label="Filter by operation"]')[0].click()
  452. # select django.middleware
  453. self.browser.elements('[data-test-id="django\\\\.middleware"]')[0].click()
  454. self.browser.snapshot("events-v2 - transactions event detail view - ops filtering")
  455. def test_create_saved_query(self):
  456. # Simulate a custom query
  457. query = {"field": ["project.id", "count()"], "query": "event.type:error"}
  458. query_name = "A new custom query"
  459. with self.feature(FEATURE_NAMES):
  460. # Go directly to the query builder view
  461. self.browser.get(self.result_path + "?" + urlencode(query, doseq=True))
  462. self.wait_until_loaded()
  463. # Open the save as drawer
  464. self.browser.element('[aria-label="Save as"]').click()
  465. # Fill out name and submit form.
  466. self.browser.element('input[name="query_name"]').send_keys(query_name)
  467. self.browser.element('[aria-label="Save for Org"]').click()
  468. self.browser.wait_until(f'[data-test-id="discover2-query-name-{query_name}"]')
  469. # Page title should update.
  470. editable_text_label = self.browser.element('[data-test-id="editable-text-label"]').text
  471. assert editable_text_label == query_name
  472. # Saved query should exist.
  473. assert DiscoverSavedQuery.objects.filter(name=query_name).exists()
  474. def test_view_and_rename_saved_query(self):
  475. # Create saved query to rename
  476. query = DiscoverSavedQuery.objects.create(
  477. name="Custom query",
  478. organization=self.org,
  479. version=2,
  480. query={"fields": ["title", "project.id", "count()"], "query": "event.type:error"},
  481. )
  482. with self.feature(FEATURE_NAMES):
  483. # View the query list
  484. self.browser.get(self.landing_path)
  485. self.wait_until_loaded()
  486. # Look at the results for our query.
  487. self.browser.element(f'[data-test-id="card-{query.name}"]').click()
  488. self.wait_until_loaded()
  489. self.browser.element('[data-test-id="editable-text-label"]').click()
  490. self.browser.wait_until('[data-test-id="editable-text-input"]')
  491. editable_text_input = self.browser.element('[data-test-id="editable-text-input"] input')
  492. editable_text_input.click()
  493. editable_text_input.send_keys(Keys.END + "updated!")
  494. # Move focus somewhere else to trigger a blur and update the query
  495. self.browser.element("table").click()
  496. self.browser.wait_until('[data-test-id="editable-text-label"]')
  497. new_name = "Custom queryupdated!"
  498. # new_card_selector = f'div[name="discover2-query-name"][value="{new_name}"]'
  499. # self.browser.wait_until(new_card_selector)
  500. self.browser.wait_until(f'[data-test-id="discover2-query-name-{new_name}"]')
  501. # Assert the name was updated.
  502. assert DiscoverSavedQuery.objects.filter(name=new_name).exists()
  503. def test_delete_saved_query(self):
  504. # Create saved query with ORM
  505. query = DiscoverSavedQuery.objects.create(
  506. name="Custom query",
  507. organization=self.org,
  508. version=2,
  509. query={"fields": ["title", "project.id", "count()"], "query": "event.type:error"},
  510. )
  511. with self.feature(FEATURE_NAMES):
  512. # View the query list
  513. self.browser.get(self.landing_path)
  514. self.wait_until_loaded()
  515. # Get the card with the new query
  516. card_selector = f'[data-test-id="card-{query.name}"]'
  517. card = self.browser.element(card_selector)
  518. # Open the context menu
  519. card.find_element(by=By.CSS_SELECTOR, value='[data-test-id="menu-trigger"]').click()
  520. # Delete the query
  521. card.find_element(by=By.CSS_SELECTOR, value='[data-test-id="delete"]').click()
  522. # Wait for card to clear
  523. self.browser.wait_until_not(card_selector)
  524. assert DiscoverSavedQuery.objects.filter(name=query.name).exists() is False
  525. def test_duplicate_query(self):
  526. # Create saved query with ORM
  527. query = DiscoverSavedQuery.objects.create(
  528. name="Custom query",
  529. organization=self.org,
  530. version=2,
  531. query={"fields": ["title", "project.id", "count()"], "query": "event.type:error"},
  532. )
  533. with self.feature(FEATURE_NAMES):
  534. # View the query list
  535. self.browser.get(self.landing_path)
  536. self.wait_until_loaded()
  537. # Get the card with the new query
  538. card_selector = f'[data-test-id="card-{query.name}"]'
  539. card = self.browser.element(card_selector)
  540. # Open the context menu, and duplicate
  541. card.find_element(by=By.CSS_SELECTOR, value='[data-test-id="menu-trigger"]').click()
  542. card.find_element(by=By.CSS_SELECTOR, value='[data-test-id="duplicate"]').click()
  543. duplicate_name = f"{query.name} copy"
  544. # Reload the page
  545. self.browser.get(self.landing_path)
  546. # Wait for new element to show up.
  547. self.browser.element(f'[data-test-id="card-{duplicate_name}"]')
  548. # Assert the new query exists and has 'copy' added to the name.
  549. assert DiscoverSavedQuery.objects.filter(name=duplicate_name).exists()
  550. @pytest.mark.skip(reason="causing timeouts in github actions and travis")
  551. @patch("django.utils.timezone.now")
  552. def test_drilldown_result(self, mock_now):
  553. now = before_now().replace(tzinfo=timezone.utc)
  554. mock_now.return_value = now
  555. ten_mins_ago = iso_format(now - timedelta(minutes=10))
  556. events = (
  557. ("a" * 32, "oh no", "group-1"),
  558. ("b" * 32, "oh no", "group-1"),
  559. ("c" * 32, "this is bad", "group-2"),
  560. )
  561. for event in events:
  562. self.store_event(
  563. data={
  564. "event_id": event[0],
  565. "message": event[1],
  566. "timestamp": ten_mins_ago,
  567. "fingerprint": [event[2]],
  568. "type": "error",
  569. },
  570. project_id=self.project.id,
  571. )
  572. query = {"field": ["message", "project", "count()"], "query": "event.type:error"}
  573. with self.feature(FEATURE_NAMES):
  574. # Go directly to the query builder view
  575. self.browser.get(self.result_path + "?" + urlencode(query, doseq=True))
  576. self.wait_until_loaded()
  577. # Click the first drilldown
  578. self.browser.element('[data-test-id="expand-count"]').click()
  579. self.wait_until_loaded()
  580. assert self.browser.element_exists_by_test_id("grid-editable"), "table should exist."
  581. headers = self.browser.elements('[data-test-id="grid-editable"] thead th')
  582. expected = ["", "MESSAGE", "PROJECT", "ID"]
  583. actual = [header.text for header in headers]
  584. assert expected == actual
  585. @pytest.mark.skip(reason="not done")
  586. @patch("django.utils.timezone.now")
  587. def test_usage(self, mock_now):
  588. mock_now.return_value = before_now().replace(tzinfo=timezone.utc)
  589. # TODO: load events
  590. # go to landing
  591. # go to a precanned query
  592. # save query 1
  593. # add environment column
  594. # update query
  595. # add condition from facet map
  596. # delete a column
  597. # click and drag a column
  598. # save as query 2
  599. # load save query 1
  600. # sort column
  601. # update query
  602. # delete save query 1