test_organization_events_v2.py 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713
  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. def test_all_events_query_empty_state(self):
  136. with self.feature(FEATURE_NAMES):
  137. self.browser.get(self.result_path + "?" + all_events_query())
  138. self.wait_until_loaded()
  139. with self.feature(FEATURE_NAMES):
  140. # expect table to expand to the right when no tags are provided
  141. self.browser.get(self.result_path + "?" + all_events_query(tag=[]))
  142. self.wait_until_loaded()
  143. @patch("django.utils.timezone.now")
  144. def test_all_events_query(self, mock_now):
  145. now = before_now().replace(tzinfo=timezone.utc)
  146. mock_now.return_value = now
  147. five_mins_ago = iso_format(now - timedelta(minutes=5))
  148. ten_mins_ago = iso_format(now - timedelta(minutes=10))
  149. self.store_event(
  150. data={
  151. "event_id": "a" * 32,
  152. "message": "oh no",
  153. "timestamp": five_mins_ago,
  154. "fingerprint": ["group-1"],
  155. },
  156. project_id=self.project.id,
  157. assert_no_errors=False,
  158. )
  159. self.store_event(
  160. data={
  161. "event_id": "b" * 32,
  162. "message": "this is bad.",
  163. "timestamp": ten_mins_ago,
  164. "fingerprint": ["group-2"],
  165. "user": {
  166. "id": "123",
  167. "email": "someone@example.com",
  168. "username": "haveibeenpwned",
  169. "ip_address": "8.8.8.8",
  170. "name": "Someone",
  171. },
  172. },
  173. project_id=self.project.id,
  174. assert_no_errors=False,
  175. )
  176. self.wait_for_event_count(self.project.id, 2)
  177. with self.feature(FEATURE_NAMES):
  178. self.browser.get(self.result_path + "?" + all_events_query())
  179. self.wait_until_loaded()
  180. # This test is flakey in that we sometimes load this page before the event is processed
  181. # depend on pytest-retry to reload the page
  182. self.browser.wait_until('[data-test-id="grid-editable"] > tbody > tr:nth-child(2)')
  183. with self.feature(FEATURE_NAMES):
  184. # expect table to expand to the right when no tags are provided
  185. self.browser.get(self.result_path + "?" + all_events_query(tag=[]))
  186. self.wait_until_loaded()
  187. self.browser.wait_until('[data-test-id="grid-editable"] > tbody > tr:nth-child(2)')
  188. def test_errors_query_empty_state(self):
  189. with self.feature(FEATURE_NAMES):
  190. self.browser.get(self.result_path + "?" + errors_query())
  191. self.wait_until_loaded()
  192. self.browser.click_when_visible('[data-test-id="grid-edit-enable"]')
  193. @patch("django.utils.timezone.now")
  194. def test_errors_query(self, mock_now):
  195. now = before_now().replace(tzinfo=timezone.utc)
  196. mock_now.return_value = now
  197. ten_mins_ago = iso_format(now - timedelta(minutes=10))
  198. self.store_event(
  199. data={
  200. "event_id": "a" * 32,
  201. "message": "oh no",
  202. "timestamp": ten_mins_ago,
  203. "fingerprint": ["group-1"],
  204. "type": "error",
  205. },
  206. project_id=self.project.id,
  207. assert_no_errors=False,
  208. )
  209. self.store_event(
  210. data={
  211. "event_id": "b" * 32,
  212. "message": "oh no",
  213. "timestamp": ten_mins_ago,
  214. "fingerprint": ["group-1"],
  215. "type": "error",
  216. },
  217. project_id=self.project.id,
  218. assert_no_errors=False,
  219. )
  220. self.store_event(
  221. data={
  222. "event_id": "c" * 32,
  223. "message": "this is bad.",
  224. "timestamp": ten_mins_ago,
  225. "fingerprint": ["group-2"],
  226. "type": "error",
  227. },
  228. project_id=self.project.id,
  229. assert_no_errors=False,
  230. )
  231. with self.feature(FEATURE_NAMES):
  232. self.browser.get(self.result_path + "?" + errors_query())
  233. self.wait_until_loaded()
  234. def test_transactions_query_empty_state(self):
  235. with self.feature(FEATURE_NAMES):
  236. self.browser.get(self.result_path + "?" + transactions_query())
  237. self.wait_until_loaded()
  238. with self.feature(FEATURE_NAMES):
  239. # expect table to expand to the right when no tags are provided
  240. self.browser.get(self.result_path + "?" + transactions_query(tag=[]))
  241. self.wait_until_loaded()
  242. @patch("django.utils.timezone.now")
  243. def test_transactions_query(self, mock_now):
  244. mock_now.return_value = before_now().replace(tzinfo=timezone.utc)
  245. event_data = generate_transaction()
  246. self.store_event(data=event_data, project_id=self.project.id, assert_no_errors=True)
  247. with self.feature(FEATURE_NAMES):
  248. self.browser.get(self.result_path + "?" + transactions_query())
  249. self.wait_until_loaded()
  250. self.browser.wait_until_not(
  251. '[data-test-id="grid-editable"] [data-test-id="empty-state"]', timeout=2
  252. )
  253. @patch("django.utils.timezone.now")
  254. def test_event_detail_view_from_all_events(self, mock_now):
  255. now = before_now().replace(tzinfo=timezone.utc)
  256. mock_now.return_value = now
  257. ten_mins_ago = iso_format(now - timedelta(minutes=10))
  258. event_data = load_data("python")
  259. event_data.update(
  260. {
  261. "event_id": "a" * 32,
  262. "timestamp": ten_mins_ago,
  263. "received": ten_mins_ago,
  264. "fingerprint": ["group-1"],
  265. }
  266. )
  267. if "contexts" not in event_data:
  268. event_data["contexts"] = {}
  269. event_data["contexts"]["trace"] = {
  270. "type": "trace",
  271. "trace_id": "a" * 32,
  272. "span_id": "b" * 16,
  273. }
  274. self.store_event(data=event_data, project_id=self.project.id, assert_no_errors=False)
  275. with self.feature(FEATURE_NAMES):
  276. # Get the list page.
  277. self.browser.get(self.result_path + "?" + all_events_query())
  278. self.wait_until_loaded()
  279. # View Event
  280. self.browser.elements('[data-test-id="view-event"]')[0].click()
  281. self.wait_until_loaded()
  282. # header = self.browser.element('[data-test-id="event-header"] div div span')
  283. # assert event_data["message"] in header.text
  284. @patch("django.utils.timezone.now")
  285. def test_event_detail_view_from_errors_view(self, mock_now):
  286. now = before_now().replace(tzinfo=timezone.utc)
  287. mock_now.return_value = now
  288. event_data = load_data("javascript")
  289. event_data.update(
  290. {
  291. "timestamp": iso_format(now - timedelta(minutes=5)),
  292. "event_id": "d" * 32,
  293. "fingerprint": ["group-1"],
  294. }
  295. )
  296. event_data["contexts"]["trace"] = {
  297. "type": "trace",
  298. "trace_id": "a" * 32,
  299. "span_id": "b" * 16,
  300. }
  301. self.store_event(data=event_data, project_id=self.project.id)
  302. self.wait_for_event_count(self.project.id, 1)
  303. with self.feature(FEATURE_NAMES):
  304. # Get the list page
  305. self.browser.get(self.result_path + "?" + errors_query() + "&statsPeriod=24h")
  306. self.wait_until_loaded()
  307. # Open the stack
  308. self.browser.element('[data-test-id="open-group"]').click()
  309. self.wait_until_loaded()
  310. # View Event
  311. self.browser.elements('[data-test-id="view-event"]')[0].click()
  312. self.wait_until_loaded()
  313. @patch("django.utils.timezone.now")
  314. def test_event_detail_view_from_transactions_query(self, mock_now):
  315. mock_now.return_value = before_now().replace(tzinfo=timezone.utc)
  316. event_data = generate_transaction(trace="a" * 32, span="ab" * 8)
  317. self.store_event(data=event_data, project_id=self.project.id, assert_no_errors=True)
  318. # Create a child event that is linked to the parent so we have coverage
  319. # of traversal buttons.
  320. child_event = generate_transaction(
  321. trace=event_data["contexts"]["trace"]["trace_id"], span="bc" * 8
  322. )
  323. child_event["event_id"] = "b" * 32
  324. child_event["contexts"]["trace"]["parent_span_id"] = event_data["spans"][4]["span_id"]
  325. child_event["transaction"] = "z-child-transaction"
  326. child_event["spans"] = child_event["spans"][0:3]
  327. self.store_event(data=child_event, project_id=self.project.id, assert_no_errors=True)
  328. with self.feature(FEATURE_NAMES):
  329. # Get the list page
  330. self.browser.get(self.result_path + "?" + transactions_sorted_query())
  331. self.wait_until_loaded()
  332. # Open the stack
  333. self.browser.elements('[data-test-id="open-group"]')[0].click()
  334. self.wait_until_loaded()
  335. # View Event
  336. self.browser.elements('[data-test-id="view-event"]')[0].click()
  337. self.wait_until_loaded()
  338. # Expand auto-grouped spans
  339. self.browser.element('[data-test-id="span-row-5"]').click()
  340. # Open a span detail so we can check the search by trace link.
  341. # Click on the 6th one as a missing instrumentation span is inserted.
  342. self.browser.element('[data-test-id="span-row-7"]').click()
  343. # Wait until the child event loads.
  344. child_button = '[data-test-id="view-child-transaction"]'
  345. self.browser.wait_until(child_button)
  346. # Click on the child transaction.
  347. self.browser.click(child_button)
  348. self.wait_until_loaded()
  349. @patch("django.utils.timezone.now")
  350. def test_event_detail_view_from_transactions_query_siblings(self, mock_now):
  351. mock_now.return_value = before_now().replace(tzinfo=timezone.utc)
  352. event_data = generate_transaction(trace="a" * 32, span="ab" * 8)
  353. # Arranges sibling spans to be autogrouped in a way that will cover many edgecases
  354. last_span = copy.deepcopy(event_data["spans"][-1])
  355. for i in range(5):
  356. clone = copy.deepcopy(last_span)
  357. # If range > 9 this might no longer work because of constraints on span_id (hex 16)
  358. clone["span_id"] = (str("ac" * 6) + str(i)).ljust(16, "0")
  359. event_data["spans"].append(clone)
  360. combo_breaker_span = copy.deepcopy(last_span)
  361. combo_breaker_span["span_id"] = (str("af" * 6)).ljust(16, "0")
  362. combo_breaker_span["op"] = "combo.breaker"
  363. event_data["spans"].append(combo_breaker_span)
  364. for i in range(5):
  365. clone = copy.deepcopy(last_span)
  366. clone["op"] = "django.middleware"
  367. clone["span_id"] = (str("de" * 6) + str(i)).ljust(16, "0")
  368. event_data["spans"].append(clone)
  369. for i in range(5):
  370. clone = copy.deepcopy(last_span)
  371. clone["op"] = "http"
  372. clone["description"] = "test"
  373. clone["span_id"] = (str("bd" * 6) + str(i)).ljust(16, "0")
  374. event_data["spans"].append(clone)
  375. self.store_event(data=event_data, project_id=self.project.id, assert_no_errors=True)
  376. # Create a child event that is linked to the parent so we have coverage
  377. # of traversal buttons.
  378. child_event = generate_transaction(
  379. trace=event_data["contexts"]["trace"]["trace_id"], span="bc" * 8
  380. )
  381. child_event["event_id"] = "b" * 32
  382. child_event["contexts"]["trace"]["parent_span_id"] = event_data["spans"][4]["span_id"]
  383. child_event["transaction"] = "z-child-transaction"
  384. child_event["spans"] = child_event["spans"][0:3]
  385. self.store_event(data=child_event, project_id=self.project.id, assert_no_errors=True)
  386. with self.feature(FEATURE_NAMES):
  387. # Get the list page
  388. self.browser.get(self.result_path + "?" + transactions_sorted_query())
  389. self.wait_until_loaded()
  390. # Open the stack
  391. self.browser.elements('[data-test-id="open-group"]')[0].click()
  392. self.wait_until_loaded()
  393. # View Event
  394. self.browser.elements('[data-test-id="view-event"]')[0].click()
  395. self.wait_until_loaded()
  396. # Expand auto-grouped descendant spans
  397. self.browser.element('[data-test-id="span-row-5"]').click()
  398. # Expand all autogrouped rows
  399. self.browser.element('[data-test-id="span-row-9"]').click()
  400. self.browser.element('[data-test-id="span-row-18"]').click()
  401. self.browser.element('[data-test-id="span-row-23"]').click()
  402. # Click to collapse all of these spans back into autogroups, we expect the span tree to look like it did initially
  403. first_row = self.browser.element('[data-test-id="span-row-23"]')
  404. first_row.find_element(By.CSS_SELECTOR, "a").click()
  405. second_row = self.browser.element('[data-test-id="span-row-18"]')
  406. second_row.find_element(By.CSS_SELECTOR, "a").click()
  407. third_row = self.browser.element('[data-test-id="span-row-9"]')
  408. third_row.find_element(By.CSS_SELECTOR, "a").click()
  409. @patch("django.utils.timezone.now")
  410. def test_transaction_event_detail_view_ops_filtering(self, mock_now):
  411. mock_now.return_value = before_now().replace(tzinfo=timezone.utc)
  412. event_data = generate_transaction(trace="a" * 32, span="ab" * 8)
  413. self.store_event(data=event_data, project_id=self.project.id, assert_no_errors=True)
  414. with self.feature(FEATURE_NAMES):
  415. # Get the list page
  416. self.browser.get(self.result_path + "?" + transactions_query())
  417. self.wait_until_loaded()
  418. # Open the stack
  419. self.browser.elements('[data-test-id="open-group"]')[0].click()
  420. self.wait_until_loaded()
  421. # View Event
  422. self.browser.elements('[data-test-id="view-event"]')[0].click()
  423. self.wait_until_loaded()
  424. # Interact with ops filter dropdown
  425. self.browser.elements('[aria-label="Filter by operation"]')[0].click()
  426. # select django.middleware
  427. self.browser.elements('[data-test-id="django\\\\.middleware"]')[0].click()
  428. def test_create_saved_query(self):
  429. # Simulate a custom query
  430. query = {"field": ["project.id", "count()"], "query": "event.type:error"}
  431. query_name = "A new custom query"
  432. with self.feature(FEATURE_NAMES):
  433. # Go directly to the query builder view
  434. self.browser.get(self.result_path + "?" + urlencode(query, doseq=True))
  435. self.wait_until_loaded()
  436. # Open the save as drawer
  437. self.browser.element('[aria-label="Save as"]').click()
  438. # Fill out name and submit form.
  439. self.browser.element('input[name="query_name"]').send_keys(query_name)
  440. self.browser.element('[aria-label="Save for Org"]').click()
  441. self.browser.wait_until(f'[data-test-id="discover2-query-name-{query_name}"]')
  442. # Page title should update.
  443. editable_text_label = self.browser.element('[data-test-id="editable-text-label"]').text
  444. assert editable_text_label == query_name
  445. # Saved query should exist.
  446. assert DiscoverSavedQuery.objects.filter(name=query_name).exists()
  447. def test_view_and_rename_saved_query(self):
  448. # Create saved query to rename
  449. query = DiscoverSavedQuery.objects.create(
  450. name="Custom query",
  451. organization=self.org,
  452. version=2,
  453. query={"fields": ["title", "project.id", "count()"], "query": "event.type:error"},
  454. )
  455. with self.feature(FEATURE_NAMES):
  456. # View the query list
  457. self.browser.get(self.landing_path)
  458. self.wait_until_loaded()
  459. # Look at the results for our query.
  460. self.browser.element(f'[data-test-id="card-{query.name}"]').click()
  461. self.wait_until_loaded()
  462. self.browser.element('[data-test-id="editable-text-label"]').click()
  463. self.browser.wait_until('[data-test-id="editable-text-input"]')
  464. editable_text_input = self.browser.element('[data-test-id="editable-text-input"] input')
  465. editable_text_input.click()
  466. editable_text_input.send_keys(Keys.END + "updated!")
  467. # Move focus somewhere else to trigger a blur and update the query
  468. self.browser.element("table").click()
  469. self.browser.wait_until('[data-test-id="editable-text-label"]')
  470. new_name = "Custom queryupdated!"
  471. # new_card_selector = f'div[name="discover2-query-name"][value="{new_name}"]'
  472. # self.browser.wait_until(new_card_selector)
  473. self.browser.wait_until(f'[data-test-id="discover2-query-name-{new_name}"]')
  474. # Assert the name was updated.
  475. assert DiscoverSavedQuery.objects.filter(name=new_name).exists()
  476. def test_delete_saved_query(self):
  477. # Create saved query with ORM
  478. query = DiscoverSavedQuery.objects.create(
  479. name="Custom query",
  480. organization=self.org,
  481. version=2,
  482. query={"fields": ["title", "project.id", "count()"], "query": "event.type:error"},
  483. )
  484. with self.feature(FEATURE_NAMES):
  485. # View the query list
  486. self.browser.get(self.landing_path)
  487. self.wait_until_loaded()
  488. # Get the card with the new query
  489. card_selector = f'[data-test-id="card-{query.name}"]'
  490. card = self.browser.element(card_selector)
  491. # Open the context menu
  492. card.find_element(by=By.CSS_SELECTOR, value='[data-test-id="menu-trigger"]').click()
  493. # Delete the query
  494. card.find_element(by=By.CSS_SELECTOR, value='[data-test-id="delete"]').click()
  495. # Wait for card to clear
  496. self.browser.wait_until_not(card_selector)
  497. assert DiscoverSavedQuery.objects.filter(name=query.name).exists() is False
  498. def test_duplicate_query(self):
  499. # Create saved query with ORM
  500. query = DiscoverSavedQuery.objects.create(
  501. name="Custom query",
  502. organization=self.org,
  503. version=2,
  504. query={"fields": ["title", "project.id", "count()"], "query": "event.type:error"},
  505. )
  506. with self.feature(FEATURE_NAMES):
  507. # View the query list
  508. self.browser.get(self.landing_path)
  509. self.wait_until_loaded()
  510. # Get the card with the new query
  511. card_selector = f'[data-test-id="card-{query.name}"]'
  512. card = self.browser.element(card_selector)
  513. # Open the context menu, and duplicate
  514. card.find_element(by=By.CSS_SELECTOR, value='[data-test-id="menu-trigger"]').click()
  515. card.find_element(by=By.CSS_SELECTOR, value='[data-test-id="duplicate"]').click()
  516. duplicate_name = f"{query.name} copy"
  517. # Reload the page
  518. self.browser.get(self.landing_path)
  519. # Wait for new element to show up.
  520. self.browser.element(f'[data-test-id="card-{duplicate_name}"]')
  521. # Assert the new query exists and has 'copy' added to the name.
  522. assert DiscoverSavedQuery.objects.filter(name=duplicate_name).exists()
  523. @pytest.mark.skip(reason="causing timeouts in github actions and travis")
  524. @patch("django.utils.timezone.now")
  525. def test_drilldown_result(self, mock_now):
  526. now = before_now().replace(tzinfo=timezone.utc)
  527. mock_now.return_value = now
  528. ten_mins_ago = iso_format(now - timedelta(minutes=10))
  529. events = (
  530. ("a" * 32, "oh no", "group-1"),
  531. ("b" * 32, "oh no", "group-1"),
  532. ("c" * 32, "this is bad", "group-2"),
  533. )
  534. for event in events:
  535. self.store_event(
  536. data={
  537. "event_id": event[0],
  538. "message": event[1],
  539. "timestamp": ten_mins_ago,
  540. "fingerprint": [event[2]],
  541. "type": "error",
  542. },
  543. project_id=self.project.id,
  544. )
  545. query = {"field": ["message", "project", "count()"], "query": "event.type:error"}
  546. with self.feature(FEATURE_NAMES):
  547. # Go directly to the query builder view
  548. self.browser.get(self.result_path + "?" + urlencode(query, doseq=True))
  549. self.wait_until_loaded()
  550. # Click the first drilldown
  551. self.browser.element('[data-test-id="expand-count"]').click()
  552. self.wait_until_loaded()
  553. assert self.browser.element_exists_by_test_id("grid-editable"), "table should exist."
  554. headers = self.browser.elements('[data-test-id="grid-editable"] thead th')
  555. expected = ["", "MESSAGE", "PROJECT", "ID"]
  556. actual = [header.text for header in headers]
  557. assert expected == actual
  558. @pytest.mark.skip(reason="not done")
  559. @patch("django.utils.timezone.now")
  560. def test_usage(self, mock_now):
  561. mock_now.return_value = before_now().replace(tzinfo=timezone.utc)
  562. # TODO: load events
  563. # go to landing
  564. # go to a precanned query
  565. # save query 1
  566. # add environment column
  567. # update query
  568. # add condition from facet map
  569. # delete a column
  570. # click and drag a column
  571. # save as query 2
  572. # load save query 1
  573. # sort column
  574. # update query
  575. # delete save query 1