eventView.spec.jsx 100 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703
  1. import {COL_WIDTH_UNDEFINED} from 'sentry/components/gridEditable';
  2. import EventView, {
  3. isAPIPayloadSimilar,
  4. pickRelevantLocationQueryStrings,
  5. } from 'sentry/utils/discover/eventView';
  6. import {
  7. CHART_AXIS_OPTIONS,
  8. DiscoverDatasets,
  9. DISPLAY_MODE_OPTIONS,
  10. DisplayModes,
  11. } from 'sentry/utils/discover/types';
  12. const generateFields = fields =>
  13. fields.map(field => ({
  14. field,
  15. }));
  16. const generateSorts = sorts =>
  17. sorts.map(sortName => ({
  18. field: sortName,
  19. kind: 'desc',
  20. }));
  21. describe('EventView constructor', function () {
  22. it('instantiates default values', function () {
  23. const eventView = new EventView({});
  24. expect(eventView).toMatchObject({
  25. id: undefined,
  26. name: undefined,
  27. fields: [],
  28. sorts: [],
  29. query: '',
  30. project: [],
  31. start: undefined,
  32. end: undefined,
  33. statsPeriod: undefined,
  34. environment: [],
  35. yAxis: undefined,
  36. display: undefined,
  37. });
  38. });
  39. });
  40. describe('EventView.fromLocation()', function () {
  41. it('maps query strings', function () {
  42. const location = {
  43. query: {
  44. id: '42',
  45. name: 'best query',
  46. field: ['count()', 'id'],
  47. widths: ['123', '456'],
  48. sort: ['title', '-count'],
  49. query: 'event.type:transaction',
  50. project: [123],
  51. team: ['myteams', '1', '2'],
  52. start: '2019-10-01T00:00:00',
  53. end: '2019-10-02T00:00:00',
  54. statsPeriod: '14d',
  55. environment: ['staging'],
  56. yAxis: 'p95',
  57. display: 'previous',
  58. dataset: DiscoverDatasets.DISCOVER,
  59. },
  60. };
  61. const eventView = EventView.fromLocation(location);
  62. expect(eventView).toMatchObject({
  63. id: '42',
  64. name: 'best query',
  65. fields: [
  66. {field: 'count()', width: 123},
  67. {field: 'id', width: 456},
  68. ],
  69. sorts: generateSorts(['count']),
  70. query: 'event.type:transaction',
  71. project: [123],
  72. team: ['myteams', 1, 2],
  73. start: undefined,
  74. end: undefined,
  75. statsPeriod: '14d',
  76. environment: ['staging'],
  77. yAxis: 'p95',
  78. display: 'previous',
  79. dataset: DiscoverDatasets.DISCOVER,
  80. });
  81. });
  82. it('includes first valid statsPeriod', function () {
  83. const location = {
  84. query: {
  85. id: '42',
  86. name: 'best query',
  87. field: ['count()', 'id'],
  88. widths: ['123', '456'],
  89. sort: ['title', '-count'],
  90. query: 'event.type:transaction',
  91. project: [123],
  92. start: '2019-10-01T00:00:00',
  93. end: '2019-10-02T00:00:00',
  94. statsPeriod: ['invalid', '28d'],
  95. environment: ['staging'],
  96. },
  97. };
  98. const eventView = EventView.fromLocation(location);
  99. expect(eventView).toMatchObject({
  100. id: '42',
  101. name: 'best query',
  102. fields: [
  103. {field: 'count()', width: 123},
  104. {field: 'id', width: 456},
  105. ],
  106. sorts: generateSorts(['count']),
  107. query: 'event.type:transaction',
  108. project: [123],
  109. start: undefined,
  110. end: undefined,
  111. statsPeriod: '28d',
  112. environment: ['staging'],
  113. });
  114. });
  115. it('includes start and end', function () {
  116. const location = {
  117. query: {
  118. id: '42',
  119. name: 'best query',
  120. field: ['count()', 'id'],
  121. widths: ['123', '456'],
  122. sort: ['title', '-count'],
  123. query: 'event.type:transaction',
  124. project: [123],
  125. start: '2019-10-01T00:00:00',
  126. end: '2019-10-02T00:00:00',
  127. environment: ['staging'],
  128. },
  129. };
  130. const eventView = EventView.fromLocation(location);
  131. expect(eventView).toMatchObject({
  132. id: '42',
  133. name: 'best query',
  134. fields: [
  135. {field: 'count()', width: 123},
  136. {field: 'id', width: 456},
  137. ],
  138. sorts: generateSorts(['count']),
  139. query: 'event.type:transaction',
  140. project: [123],
  141. start: '2019-10-01T00:00:00.000',
  142. end: '2019-10-02T00:00:00.000',
  143. environment: ['staging'],
  144. });
  145. });
  146. it('generates event view when there are no query strings', function () {
  147. const location = {
  148. query: {},
  149. };
  150. const eventView = EventView.fromLocation(location);
  151. expect(eventView).toMatchObject({
  152. id: void 0,
  153. name: void 0,
  154. fields: [],
  155. sorts: [],
  156. query: '',
  157. project: [],
  158. start: void 0,
  159. end: void 0,
  160. statsPeriod: '14d',
  161. environment: [],
  162. yAxis: void 0,
  163. });
  164. });
  165. });
  166. describe('EventView.fromSavedQuery()', function () {
  167. it('maps basic properties of saved query', function () {
  168. const saved = {
  169. id: '42',
  170. name: 'best query',
  171. fields: ['count()', 'id'],
  172. query: 'event.type:transaction',
  173. projects: [123],
  174. teams: ['myteams', 1],
  175. range: '14d',
  176. start: '2019-10-01T00:00:00',
  177. end: '2019-10-02T00:00:00',
  178. orderby: '-id',
  179. environment: ['staging'],
  180. display: 'previous',
  181. dataset: DiscoverDatasets.DISCOVER,
  182. };
  183. const eventView = EventView.fromSavedQuery(saved);
  184. expect(eventView).toMatchObject({
  185. id: saved.id,
  186. name: saved.name,
  187. fields: [
  188. {field: 'count()', width: COL_WIDTH_UNDEFINED},
  189. {field: 'id', width: COL_WIDTH_UNDEFINED},
  190. ],
  191. sorts: [{field: 'id', kind: 'desc'}],
  192. query: 'event.type:transaction',
  193. project: [123],
  194. team: ['myteams', 1],
  195. start: undefined,
  196. end: undefined,
  197. // statsPeriod has precedence
  198. statsPeriod: '14d',
  199. environment: ['staging'],
  200. yAxis: undefined,
  201. display: 'previous',
  202. dataset: DiscoverDatasets.DISCOVER,
  203. });
  204. const eventView2 = EventView.fromSavedQuery({
  205. ...saved,
  206. range: undefined,
  207. });
  208. expect(eventView2).toMatchObject({
  209. id: saved.id,
  210. name: saved.name,
  211. fields: [
  212. {field: 'count()', width: COL_WIDTH_UNDEFINED},
  213. {field: 'id', width: COL_WIDTH_UNDEFINED},
  214. ],
  215. sorts: [{field: 'id', kind: 'desc'}],
  216. query: 'event.type:transaction',
  217. project: [123],
  218. team: ['myteams', 1],
  219. start: '2019-10-01T00:00:00.000',
  220. end: '2019-10-02T00:00:00.000',
  221. statsPeriod: undefined,
  222. environment: ['staging'],
  223. });
  224. });
  225. it('maps saved query with no conditions', function () {
  226. const saved = {
  227. orderby: '-count',
  228. name: 'foo bar',
  229. fields: ['release', 'count()'],
  230. widths: [111, 222],
  231. dateCreated: '2019-10-30T06:13:17.632078Z',
  232. environment: ['dev', 'production'],
  233. version: 2,
  234. createdBy: '1',
  235. dateUpdated: '2019-10-30T06:13:17.632096Z',
  236. id: '5',
  237. projects: [1],
  238. yAxis: 'count()',
  239. };
  240. const eventView = EventView.fromSavedQuery(saved);
  241. const expected = {
  242. id: '5',
  243. name: 'foo bar',
  244. fields: [
  245. {field: 'release', width: 111},
  246. {field: 'count()', width: 222},
  247. ],
  248. sorts: generateSorts(['count']),
  249. query: '',
  250. project: [1],
  251. environment: ['dev', 'production'],
  252. yAxis: 'count()',
  253. };
  254. expect(eventView).toMatchObject(expected);
  255. });
  256. it('maps properties from v2 saved query', function () {
  257. const saved = {
  258. name: 'best query',
  259. fields: ['count()', 'title'],
  260. range: '14d',
  261. start: '',
  262. end: '',
  263. };
  264. const eventView = EventView.fromSavedQuery(saved);
  265. expect(eventView.fields).toEqual([
  266. {field: 'count()', width: COL_WIDTH_UNDEFINED},
  267. {field: 'title', width: COL_WIDTH_UNDEFINED},
  268. ]);
  269. expect(eventView.name).toEqual(saved.name);
  270. expect(eventView.statsPeriod).toEqual('14d');
  271. expect(eventView.start).toEqual(undefined);
  272. expect(eventView.end).toEqual(undefined);
  273. });
  274. it('saved queries are equal when start and end datetime differ in format', function () {
  275. const saved = {
  276. orderby: '-count_timestamp',
  277. end: '2019-10-23T19:27:04+0000',
  278. name: 'release query',
  279. fields: ['release', 'count(timestamp)'],
  280. dateCreated: '2019-10-30T05:10:23.718937Z',
  281. environment: ['dev', 'production'],
  282. start: '2019-10-20T21:02:51+0000',
  283. version: 2,
  284. createdBy: '1',
  285. dateUpdated: '2019-10-30T07:25:58.291917Z',
  286. id: '3',
  287. projects: [1],
  288. };
  289. const eventView = EventView.fromSavedQuery(saved);
  290. const eventView2 = EventView.fromSavedQuery({
  291. ...saved,
  292. start: '2019-10-20T21:02:51Z',
  293. end: '2019-10-23T19:27:04Z',
  294. });
  295. expect(eventView.isEqualTo(eventView2)).toBe(true);
  296. const eventView3 = EventView.fromSavedQuery({
  297. ...saved,
  298. start: '2019-10-20T21:02:51Z',
  299. });
  300. expect(eventView.isEqualTo(eventView3)).toBe(true);
  301. const eventView4 = EventView.fromSavedQuery({
  302. ...saved,
  303. end: '2019-10-23T19:27:04Z',
  304. });
  305. expect(eventView.isEqualTo(eventView4)).toBe(true);
  306. });
  307. it('saved queries are not equal when datetime selection are invalid', function () {
  308. const saved = {
  309. orderby: '-count_timestamp',
  310. end: '2019-10-23T19:27:04+0000',
  311. name: 'release query',
  312. fields: ['release', 'count(timestamp)'],
  313. dateCreated: '2019-10-30T05:10:23.718937Z',
  314. environment: ['dev', 'production'],
  315. start: '2019-10-20T21:02:51+0000',
  316. version: 2,
  317. createdBy: '1',
  318. dateUpdated: '2019-10-30T07:25:58.291917Z',
  319. id: '3',
  320. projects: [1],
  321. };
  322. const eventView = EventView.fromSavedQuery(saved);
  323. const eventView2 = EventView.fromSavedQuery({
  324. ...saved,
  325. start: '',
  326. });
  327. expect(eventView.isEqualTo(eventView2)).toBe(false);
  328. const eventView3 = EventView.fromSavedQuery({
  329. ...saved,
  330. end: '',
  331. });
  332. expect(eventView.isEqualTo(eventView3)).toBe(false);
  333. // this is expected since datetime (start and end) are normalized
  334. expect(eventView2.isEqualTo(eventView3)).toBe(true);
  335. });
  336. it('saved queries with undefined yAxis are defaulted to count() when comparing with isEqualTo', function () {
  337. const saved = {
  338. orderby: '-count_timestamp',
  339. end: '2019-10-23T19:27:04+0000',
  340. name: 'release query',
  341. fields: ['release', 'count(timestamp)'],
  342. dateCreated: '2019-10-30T05:10:23.718937Z',
  343. environment: ['dev', 'production'],
  344. start: '2019-10-20T21:02:51+0000',
  345. version: 2,
  346. createdBy: '1',
  347. dateUpdated: '2019-10-30T07:25:58.291917Z',
  348. id: '3',
  349. projects: [1],
  350. };
  351. const eventView = EventView.fromSavedQuery(saved);
  352. const eventView2 = EventView.fromSavedQuery({
  353. ...saved,
  354. yAxis: 'count()',
  355. });
  356. expect(eventView.isEqualTo(eventView2)).toBe(true);
  357. });
  358. it('uses the first yAxis from the SavedQuery', function () {
  359. const saved = {
  360. id: '42',
  361. name: 'best query',
  362. fields: ['count()', 'id'],
  363. query: 'event.type:transaction',
  364. projects: [123],
  365. teams: ['myteams', 1],
  366. range: '14d',
  367. start: '2019-10-01T00:00:00',
  368. end: '2019-10-02T00:00:00',
  369. orderby: '-id',
  370. environment: ['staging'],
  371. display: 'previous',
  372. dataset: DiscoverDatasets.DISCOVER,
  373. yAxis: ['count()'],
  374. };
  375. const eventView = EventView.fromSavedQuery(saved);
  376. expect(eventView).toMatchObject({
  377. id: saved.id,
  378. name: saved.name,
  379. fields: [
  380. {field: 'count()', width: COL_WIDTH_UNDEFINED},
  381. {field: 'id', width: COL_WIDTH_UNDEFINED},
  382. ],
  383. sorts: [{field: 'id', kind: 'desc'}],
  384. query: 'event.type:transaction',
  385. project: [123],
  386. team: ['myteams', 1],
  387. start: undefined,
  388. end: undefined,
  389. statsPeriod: '14d',
  390. environment: ['staging'],
  391. yAxis: 'count()',
  392. dataset: DiscoverDatasets.DISCOVER,
  393. display: 'previous',
  394. });
  395. });
  396. it('preserves utc with start/end', function () {
  397. const saved = {
  398. name: 'best query',
  399. query: 'event.type:transaction',
  400. fields: ['count()', 'title'],
  401. start: '2019-10-20T21:02:51+0000',
  402. end: '2019-10-23T19:27:04+0000',
  403. utc: 'true',
  404. };
  405. const eventView = EventView.fromSavedQuery(saved);
  406. expect(eventView).toMatchObject({
  407. id: saved.id,
  408. name: saved.name,
  409. fields: [
  410. {field: 'count()', width: COL_WIDTH_UNDEFINED},
  411. {field: 'title', width: COL_WIDTH_UNDEFINED},
  412. ],
  413. query: 'event.type:transaction',
  414. start: '2019-10-20T21:02:51.000',
  415. end: '2019-10-23T19:27:04.000',
  416. utc: 'true',
  417. });
  418. });
  419. });
  420. describe('EventView.fromNewQueryWithLocation()', function () {
  421. const prebuiltQuery = {
  422. id: undefined,
  423. name: 'All Events',
  424. query: '',
  425. projects: [],
  426. fields: ['title', 'event.type', 'project', 'user', 'timestamp'],
  427. orderby: '-timestamp',
  428. version: 2,
  429. };
  430. it('maps basic properties of a prebuilt query', function () {
  431. const location = {
  432. query: {
  433. statsPeriod: '99d',
  434. },
  435. };
  436. const eventView = EventView.fromNewQueryWithLocation(prebuiltQuery, location);
  437. expect(eventView).toMatchObject({
  438. id: undefined,
  439. name: 'All Events',
  440. fields: [
  441. {field: 'title'},
  442. {field: 'event.type'},
  443. {field: 'project'},
  444. {field: 'user'},
  445. {field: 'timestamp'},
  446. ],
  447. sorts: [{field: 'timestamp', kind: 'desc'}],
  448. query: '',
  449. project: [],
  450. start: undefined,
  451. end: undefined,
  452. // statsPeriod has precedence
  453. statsPeriod: '99d',
  454. environment: [],
  455. yAxis: undefined,
  456. });
  457. });
  458. it('merges global selection values', function () {
  459. const location = {
  460. query: {
  461. statsPeriod: '99d',
  462. project: ['456'],
  463. environment: ['prod'],
  464. },
  465. };
  466. const eventView = EventView.fromNewQueryWithLocation(prebuiltQuery, location);
  467. expect(eventView).toMatchObject({
  468. id: undefined,
  469. name: 'All Events',
  470. fields: [
  471. {field: 'title'},
  472. {field: 'event.type'},
  473. {field: 'project'},
  474. {field: 'user'},
  475. {field: 'timestamp'},
  476. ],
  477. sorts: [{field: 'timestamp', kind: 'desc'}],
  478. query: '',
  479. project: [456],
  480. start: undefined,
  481. end: undefined,
  482. statsPeriod: '99d',
  483. environment: ['prod'],
  484. yAxis: undefined,
  485. });
  486. });
  487. it('new query takes precedence over global selection values', function () {
  488. const location = {
  489. query: {
  490. statsPeriod: '99d',
  491. project: ['456'],
  492. environment: ['prod'],
  493. },
  494. };
  495. const prebuiltQuery2 = {
  496. ...prebuiltQuery,
  497. range: '42d',
  498. projects: [987],
  499. environment: ['staging'],
  500. };
  501. const eventView = EventView.fromNewQueryWithLocation(prebuiltQuery2, location);
  502. expect(eventView).toMatchObject({
  503. id: undefined,
  504. name: 'All Events',
  505. fields: [
  506. {field: 'title'},
  507. {field: 'event.type'},
  508. {field: 'project'},
  509. {field: 'user'},
  510. {field: 'timestamp'},
  511. ],
  512. sorts: [{field: 'timestamp', kind: 'desc'}],
  513. query: '',
  514. project: [987],
  515. start: undefined,
  516. end: undefined,
  517. statsPeriod: '42d',
  518. environment: ['staging'],
  519. yAxis: undefined,
  520. });
  521. // also test start and end
  522. const location2 = {
  523. query: {
  524. start: '2019-10-01T00:00:00',
  525. end: '2019-10-02T00:00:00',
  526. project: ['456'],
  527. environment: ['prod'],
  528. },
  529. };
  530. const prebuiltQuery3 = {
  531. ...prebuiltQuery,
  532. start: '2019-10-01T00:00:00',
  533. end: '2019-10-02T00:00:00',
  534. projects: [987],
  535. environment: ['staging'],
  536. };
  537. const eventView2 = EventView.fromNewQueryWithLocation(prebuiltQuery3, location2);
  538. expect(eventView2).toMatchObject({
  539. id: undefined,
  540. name: 'All Events',
  541. fields: [
  542. {field: 'title'},
  543. {field: 'event.type'},
  544. {field: 'project'},
  545. {field: 'user'},
  546. {field: 'timestamp'},
  547. ],
  548. sorts: [{field: 'timestamp', kind: 'desc'}],
  549. query: '',
  550. project: [987],
  551. start: '2019-10-01T00:00:00.000',
  552. end: '2019-10-02T00:00:00.000',
  553. statsPeriod: undefined,
  554. environment: ['staging'],
  555. yAxis: undefined,
  556. });
  557. });
  558. });
  559. describe('EventView.fromSavedQueryOrLocation()', function () {
  560. it('maps basic properties of saved query', function () {
  561. const saved = {
  562. id: '42',
  563. name: 'best query',
  564. fields: ['count()', 'id'],
  565. query: 'event.type:transaction',
  566. projects: [123],
  567. range: '14d',
  568. start: '2019-10-01T00:00:00',
  569. end: '2019-10-02T00:00:00',
  570. orderby: '-id',
  571. environment: ['staging'],
  572. display: 'previous',
  573. dataset: DiscoverDatasets.DISCOVER,
  574. };
  575. const location = {
  576. query: {
  577. statsPeriod: '14d',
  578. project: ['123'],
  579. team: ['myteams', '1', '2'],
  580. environment: ['staging'],
  581. },
  582. };
  583. const eventView = EventView.fromSavedQueryOrLocation(saved, location);
  584. expect(eventView).toMatchObject({
  585. id: saved.id,
  586. name: saved.name,
  587. fields: [
  588. {field: 'count()', width: COL_WIDTH_UNDEFINED},
  589. {field: 'id', width: COL_WIDTH_UNDEFINED},
  590. ],
  591. sorts: [{field: 'id', kind: 'desc'}],
  592. query: 'event.type:transaction',
  593. project: [123],
  594. team: ['myteams', 1, 2],
  595. start: undefined,
  596. end: undefined,
  597. // statsPeriod has precedence
  598. statsPeriod: '14d',
  599. environment: ['staging'],
  600. yAxis: undefined,
  601. display: 'previous',
  602. dataset: DiscoverDatasets.DISCOVER,
  603. });
  604. const savedQuery2 = {...saved, range: undefined};
  605. const location2 = {
  606. query: {
  607. project: ['123'],
  608. environment: ['staging'],
  609. start: '2019-10-01T00:00:00',
  610. end: '2019-10-02T00:00:00',
  611. },
  612. };
  613. const eventView2 = EventView.fromSavedQueryOrLocation(savedQuery2, location2);
  614. expect(eventView2).toMatchObject({
  615. id: saved.id,
  616. name: saved.name,
  617. fields: [
  618. {field: 'count()', width: COL_WIDTH_UNDEFINED},
  619. {field: 'id', width: COL_WIDTH_UNDEFINED},
  620. ],
  621. sorts: [{field: 'id', kind: 'desc'}],
  622. query: 'event.type:transaction',
  623. project: [123],
  624. start: '2019-10-01T00:00:00.000',
  625. end: '2019-10-02T00:00:00.000',
  626. statsPeriod: undefined,
  627. environment: ['staging'],
  628. });
  629. });
  630. it('overrides saved query params with location params', function () {
  631. const saved = {
  632. id: '42',
  633. name: 'best query',
  634. fields: ['count()', 'id'],
  635. query: 'event.type:transaction',
  636. projects: [123],
  637. range: '14d',
  638. start: '2019-10-01T00:00:00',
  639. end: '2019-10-02T00:00:00',
  640. orderby: '-id',
  641. environment: ['staging'],
  642. display: 'previous',
  643. dataset: DiscoverDatasets.DISCOVER,
  644. };
  645. const location = {
  646. query: {
  647. id: '42',
  648. statsPeriod: '7d',
  649. project: ['3'],
  650. },
  651. };
  652. const eventView = EventView.fromSavedQueryOrLocation(saved, location);
  653. expect(eventView).toMatchObject({
  654. id: saved.id,
  655. name: saved.name,
  656. fields: [
  657. {field: 'count()', width: COL_WIDTH_UNDEFINED},
  658. {field: 'id', width: COL_WIDTH_UNDEFINED},
  659. ],
  660. sorts: [{field: 'id', kind: 'desc'}],
  661. query: 'event.type:transaction',
  662. project: [3],
  663. start: undefined,
  664. end: undefined,
  665. // statsPeriod has precedence
  666. statsPeriod: '7d',
  667. environment: [],
  668. yAxis: undefined,
  669. display: 'previous',
  670. dataset: DiscoverDatasets.DISCOVER,
  671. });
  672. });
  673. it('maps saved query with no conditions', function () {
  674. const saved = {
  675. orderby: '-count',
  676. name: 'foo bar',
  677. fields: ['release', 'count()'],
  678. widths: [111, 222],
  679. dateCreated: '2019-10-30T06:13:17.632078Z',
  680. query: '',
  681. environment: [],
  682. version: 2,
  683. createdBy: '1',
  684. dateUpdated: '2019-10-30T06:13:17.632096Z',
  685. id: '5',
  686. yAxis: 'count()',
  687. };
  688. const location = {
  689. query: {
  690. id: '5',
  691. project: [1],
  692. },
  693. };
  694. const eventView = EventView.fromSavedQueryOrLocation(saved, location);
  695. const expected = {
  696. id: '5',
  697. name: 'foo bar',
  698. fields: [
  699. {field: 'release', width: 111},
  700. {field: 'count()', width: 222},
  701. ],
  702. sorts: generateSorts(['count']),
  703. query: '',
  704. project: [1],
  705. yAxis: 'count()',
  706. };
  707. expect(eventView).toMatchObject(expected);
  708. });
  709. it('maps query with cleared conditions', function () {
  710. const saved = {
  711. id: '42',
  712. name: 'best query',
  713. fields: ['count()', 'id'],
  714. query: 'event.type:transaction',
  715. projects: [123],
  716. range: '14d',
  717. start: '2019-10-01T00:00:00',
  718. end: '2019-10-02T00:00:00',
  719. orderby: '-id',
  720. environment: ['staging'],
  721. display: 'previous',
  722. };
  723. const location = {
  724. query: {
  725. id: '42',
  726. statsPeriod: '7d',
  727. },
  728. };
  729. const eventView = EventView.fromSavedQueryOrLocation(saved, location);
  730. expect(eventView).toMatchObject({
  731. id: saved.id,
  732. name: saved.name,
  733. fields: [
  734. {field: 'count()', width: COL_WIDTH_UNDEFINED},
  735. {field: 'id', width: COL_WIDTH_UNDEFINED},
  736. ],
  737. sorts: [{field: 'id', kind: 'desc'}],
  738. query: 'event.type:transaction',
  739. start: undefined,
  740. end: undefined,
  741. // statsPeriod has precedence
  742. statsPeriod: '7d',
  743. environment: [],
  744. yAxis: undefined,
  745. display: 'previous',
  746. });
  747. const location2 = {
  748. query: {
  749. id: '42',
  750. statsPeriod: '7d',
  751. query: '',
  752. },
  753. };
  754. const eventView2 = EventView.fromSavedQueryOrLocation(saved, location2);
  755. expect(eventView2).toMatchObject({
  756. id: saved.id,
  757. name: saved.name,
  758. fields: [
  759. {field: 'count()', width: COL_WIDTH_UNDEFINED},
  760. {field: 'id', width: COL_WIDTH_UNDEFINED},
  761. ],
  762. sorts: [{field: 'id', kind: 'desc'}],
  763. query: '',
  764. start: undefined,
  765. end: undefined,
  766. // statsPeriod has precedence
  767. statsPeriod: '7d',
  768. environment: [],
  769. yAxis: undefined,
  770. display: 'previous',
  771. });
  772. });
  773. it('event views are equal when start and end datetime differ in format', function () {
  774. const saved = {
  775. orderby: '-count_timestamp',
  776. end: '2019-10-23T19:27:04+0000',
  777. name: 'release query',
  778. fields: ['release', 'count(timestamp)'],
  779. dateCreated: '2019-10-30T05:10:23.718937Z',
  780. environment: ['dev', 'production'],
  781. start: '2019-10-20T21:02:51+0000',
  782. version: 2,
  783. createdBy: '1',
  784. dateUpdated: '2019-10-30T07:25:58.291917Z',
  785. id: '3',
  786. };
  787. const location = {
  788. query: {
  789. id: '3',
  790. start: '2019-10-20T21:02:51+0000',
  791. end: '2019-10-23T19:27:04+0000',
  792. },
  793. };
  794. const eventView = EventView.fromSavedQueryOrLocation(saved, location);
  795. const location2 = {
  796. query: {
  797. id: '3',
  798. start: '2019-10-20T21:02:51Z',
  799. end: '2019-10-23T19:27:04Z',
  800. },
  801. };
  802. const eventView2 = EventView.fromSavedQueryOrLocation(saved, location2);
  803. expect(eventView.isEqualTo(eventView2)).toBe(true);
  804. const location3 = {
  805. query: {
  806. id: '3',
  807. start: '2019-10-20T21:02:51Z',
  808. end: '2019-10-23T19:27:04+0000',
  809. },
  810. };
  811. const eventView3 = EventView.fromSavedQueryOrLocation(saved, location3);
  812. expect(eventView.isEqualTo(eventView3)).toBe(true);
  813. const location4 = {
  814. query: {
  815. id: '3',
  816. start: '2019-10-20T21:02:51+0000',
  817. end: '2019-10-23T19:27:04Z',
  818. },
  819. };
  820. const eventView4 = EventView.fromSavedQueryOrLocation(saved, location4);
  821. expect(eventView.isEqualTo(eventView4)).toBe(true);
  822. });
  823. it('event views are not equal when datetime selection are invalid', function () {
  824. const saved = {
  825. orderby: '-count_timestamp',
  826. end: '2019-10-23T19:27:04+0000',
  827. name: 'release query',
  828. fields: ['release', 'count(timestamp)'],
  829. dateCreated: '2019-10-30T05:10:23.718937Z',
  830. environment: ['dev', 'production'],
  831. start: '2019-10-20T21:02:51+0000',
  832. version: 2,
  833. createdBy: '1',
  834. dateUpdated: '2019-10-30T07:25:58.291917Z',
  835. id: '3',
  836. projects: [1],
  837. };
  838. const location = {
  839. query: {
  840. id: '3',
  841. end: '2019-10-23T19:27:04+0000',
  842. start: '2019-10-20T21:02:51+0000',
  843. },
  844. };
  845. const eventView = EventView.fromSavedQueryOrLocation(saved, location);
  846. const location2 = {
  847. query: {
  848. id: '3',
  849. end: '2019-10-23T19:27:04+0000',
  850. start: '',
  851. },
  852. };
  853. const eventView2 = EventView.fromSavedQueryOrLocation(saved, location2);
  854. expect(eventView.isEqualTo(eventView2)).toBe(false);
  855. const location3 = {
  856. query: {
  857. id: '3',
  858. end: '',
  859. start: '2019-10-20T21:02:51+0000',
  860. },
  861. };
  862. const eventView3 = EventView.fromSavedQueryOrLocation(saved, location3);
  863. expect(eventView.isEqualTo(eventView3)).toBe(false);
  864. // this is expected since datetime (start and end) are normalized
  865. expect(eventView2.isEqualTo(eventView3)).toBe(true);
  866. });
  867. it('uses the first yAxis from the SavedQuery', function () {
  868. const saved = {
  869. id: '42',
  870. name: 'best query',
  871. fields: ['count()', 'id'],
  872. query: 'event.type:transaction',
  873. projects: [123],
  874. range: '14d',
  875. start: '2019-10-01T00:00:00',
  876. end: '2019-10-02T00:00:00',
  877. orderby: '-id',
  878. environment: ['staging'],
  879. display: 'previous',
  880. yAxis: ['count()', 'failure_count()'],
  881. };
  882. const location = {
  883. query: {
  884. statsPeriod: '14d',
  885. project: ['123'],
  886. team: ['myteams', '1', '2'],
  887. environment: ['staging'],
  888. },
  889. };
  890. const eventView = EventView.fromSavedQueryOrLocation(saved, location);
  891. expect(eventView).toMatchObject({
  892. id: saved.id,
  893. name: saved.name,
  894. fields: [
  895. {field: 'count()', width: COL_WIDTH_UNDEFINED},
  896. {field: 'id', width: COL_WIDTH_UNDEFINED},
  897. ],
  898. sorts: [{field: 'id', kind: 'desc'}],
  899. query: 'event.type:transaction',
  900. project: [123],
  901. team: ['myteams', 1, 2],
  902. start: undefined,
  903. end: undefined,
  904. statsPeriod: '14d',
  905. environment: ['staging'],
  906. yAxis: 'count()',
  907. display: 'previous',
  908. });
  909. });
  910. it('filters out invalid teams', function () {
  911. const eventView = EventView.fromSavedQueryOrLocation(undefined, {
  912. query: {
  913. statsPeriod: '14d',
  914. project: ['123'],
  915. team: ['myteams', '1', 'unassigned'],
  916. environment: ['staging'],
  917. },
  918. });
  919. expect(eventView.team).toEqual(['myteams', 1]);
  920. });
  921. });
  922. describe('EventView.generateQueryStringObject()', function () {
  923. it('skips empty values', function () {
  924. const eventView = new EventView({
  925. fields: generateFields(['id', 'title']),
  926. sorts: [],
  927. project: [],
  928. environment: '',
  929. statsPeriod: '',
  930. start: null,
  931. end: undefined,
  932. yAxis: undefined,
  933. display: 'previous',
  934. });
  935. const expected = {
  936. id: undefined,
  937. name: undefined,
  938. field: ['id', 'title'],
  939. widths: [],
  940. sort: [],
  941. query: '',
  942. project: [],
  943. environment: [],
  944. display: 'previous',
  945. yAxis: 'count()',
  946. };
  947. expect(eventView.generateQueryStringObject()).toEqual(expected);
  948. });
  949. it('generates query string object', function () {
  950. const state = {
  951. id: '1234',
  952. name: 'best query',
  953. fields: [
  954. {field: 'count()', width: 123},
  955. {field: 'project.id', width: 456},
  956. ],
  957. sorts: generateSorts(['count']),
  958. query: 'event.type:error',
  959. project: [42],
  960. start: '2019-10-01T00:00:00',
  961. end: '2019-10-02T00:00:00',
  962. statsPeriod: '14d',
  963. environment: ['staging'],
  964. yAxis: 'count()',
  965. display: 'releases',
  966. interval: '1m',
  967. };
  968. const eventView = new EventView(state);
  969. const expected = {
  970. id: '1234',
  971. name: 'best query',
  972. field: ['count()', 'project.id'],
  973. widths: [123, 456],
  974. sort: ['-count'],
  975. query: 'event.type:error',
  976. project: [42],
  977. start: '2019-10-01T00:00:00',
  978. end: '2019-10-02T00:00:00',
  979. statsPeriod: '14d',
  980. environment: ['staging'],
  981. yAxis: 'count()',
  982. display: 'releases',
  983. interval: '1m',
  984. };
  985. expect(eventView.generateQueryStringObject()).toEqual(expected);
  986. });
  987. it('encodes fields', function () {
  988. const eventView = new EventView({
  989. fields: [{field: 'id'}, {field: 'title'}],
  990. sorts: [],
  991. });
  992. const query = eventView.generateQueryStringObject();
  993. expect(query.field).toEqual(['id', 'title']);
  994. });
  995. it('returns a copy of data preventing mutation', function () {
  996. const eventView = new EventView({
  997. fields: [{field: 'id'}, {field: 'title'}],
  998. sorts: [],
  999. });
  1000. const query = eventView.generateQueryStringObject();
  1001. query.field.push('newthing');
  1002. // Getting the query again should return the original values.
  1003. const secondQuery = eventView.generateQueryStringObject();
  1004. expect(secondQuery.field).toEqual(['id', 'title']);
  1005. expect(query).not.toEqual(secondQuery);
  1006. });
  1007. });
  1008. describe('EventView.getEventsAPIPayload()', function () {
  1009. it('generates the API payload', function () {
  1010. const eventView = new EventView({
  1011. id: 34,
  1012. name: 'amazing query',
  1013. fields: generateFields(['id']),
  1014. sorts: generateSorts(['id']),
  1015. query: 'event.type:csp',
  1016. project: [567],
  1017. environment: ['prod'],
  1018. yAxis: 'users',
  1019. display: 'releases',
  1020. });
  1021. expect(eventView.getEventsAPIPayload({})).toEqual({
  1022. field: ['id'],
  1023. per_page: 50,
  1024. sort: '-id',
  1025. query: 'event.type:csp',
  1026. project: ['567'],
  1027. environment: ['prod'],
  1028. statsPeriod: '14d',
  1029. });
  1030. });
  1031. it('does not append query conditions in location', function () {
  1032. const eventView = new EventView({
  1033. fields: generateFields(['id']),
  1034. sorts: [],
  1035. query: 'event.type:csp',
  1036. });
  1037. const location = {
  1038. query: {
  1039. query: 'TypeError',
  1040. },
  1041. };
  1042. expect(eventView.getEventsAPIPayload(location).query).toEqual('event.type:csp');
  1043. });
  1044. it('only includes at most one sort key', function () {
  1045. const eventView = new EventView({
  1046. fields: generateFields(['count()', 'title']),
  1047. sorts: generateSorts(['title', 'count']),
  1048. query: 'event.type:csp',
  1049. });
  1050. const location = {
  1051. query: {},
  1052. };
  1053. expect(eventView.getEventsAPIPayload(location).sort).toEqual('-title');
  1054. });
  1055. it('only includes sort keys that are defined in fields', function () {
  1056. const eventView = new EventView({
  1057. fields: generateFields(['title', 'count()']),
  1058. sorts: generateSorts(['project', 'count']),
  1059. query: 'event.type:csp',
  1060. });
  1061. const location = {
  1062. query: {},
  1063. };
  1064. expect(eventView.getEventsAPIPayload(location).sort).toEqual('-count');
  1065. });
  1066. it('only includes relevant query strings', function () {
  1067. const eventView = new EventView({
  1068. fields: generateFields(['title', 'count()']),
  1069. sorts: generateSorts(['project', 'count']),
  1070. query: 'event.type:csp',
  1071. });
  1072. const location = {
  1073. query: {
  1074. start: '2020-08-12 12:13:14',
  1075. end: '2020-08-26 12:13:14',
  1076. utc: 'true',
  1077. statsPeriod: '14d',
  1078. cursor: 'some cursor',
  1079. yAxis: 'count()',
  1080. // irrelevant query strings
  1081. bestCountry: 'canada',
  1082. project: '1234',
  1083. environment: ['staging'],
  1084. },
  1085. };
  1086. expect(eventView.getEventsAPIPayload(location)).toEqual({
  1087. project: [],
  1088. environment: [],
  1089. utc: 'true',
  1090. statsPeriod: '14d',
  1091. field: ['title', 'count()'],
  1092. per_page: 50,
  1093. query: 'event.type:csp',
  1094. sort: '-count',
  1095. cursor: 'some cursor',
  1096. });
  1097. });
  1098. it('includes default coerced statsPeriod when omitted or is invalid', function () {
  1099. const eventView = new EventView({
  1100. fields: generateFields(['title', 'count()']),
  1101. sorts: generateSorts(['project', 'count']),
  1102. query: 'event.type:csp',
  1103. project: [1234],
  1104. environment: ['staging'],
  1105. });
  1106. const location = {
  1107. query: {
  1108. start: '',
  1109. end: '',
  1110. utc: 'true',
  1111. // invalid statsPeriod string
  1112. statsPeriod: 'invalid',
  1113. cursor: 'some cursor',
  1114. },
  1115. };
  1116. expect(eventView.getEventsAPIPayload(location)).toEqual({
  1117. project: ['1234'],
  1118. environment: ['staging'],
  1119. utc: 'true',
  1120. statsPeriod: '14d',
  1121. field: ['title', 'count()'],
  1122. per_page: 50,
  1123. query: 'event.type:csp',
  1124. sort: '-count',
  1125. cursor: 'some cursor',
  1126. });
  1127. const location2 = {
  1128. query: {
  1129. start: '',
  1130. end: '',
  1131. utc: 'true',
  1132. // statsPeriod is omitted here
  1133. cursor: 'some cursor',
  1134. },
  1135. };
  1136. expect(eventView.getEventsAPIPayload(location2)).toEqual({
  1137. project: ['1234'],
  1138. environment: ['staging'],
  1139. utc: 'true',
  1140. statsPeriod: '14d',
  1141. field: ['title', 'count()'],
  1142. per_page: 50,
  1143. query: 'event.type:csp',
  1144. sort: '-count',
  1145. cursor: 'some cursor',
  1146. });
  1147. });
  1148. it('includes default coerced statsPeriod when either start or end is only provided', function () {
  1149. const eventView = new EventView({
  1150. fields: generateFields(['title', 'count()']),
  1151. sorts: generateSorts(['project', 'count']),
  1152. query: 'event.type:csp',
  1153. project: [1234],
  1154. environment: ['staging'],
  1155. });
  1156. const location = {
  1157. query: {
  1158. start: '',
  1159. utc: 'true',
  1160. statsPeriod: 'invalid',
  1161. cursor: 'some cursor',
  1162. },
  1163. };
  1164. expect(eventView.getEventsAPIPayload(location)).toEqual({
  1165. project: ['1234'],
  1166. environment: ['staging'],
  1167. utc: 'true',
  1168. statsPeriod: '14d',
  1169. field: ['title', 'count()'],
  1170. per_page: 50,
  1171. query: 'event.type:csp',
  1172. sort: '-count',
  1173. cursor: 'some cursor',
  1174. });
  1175. const location2 = {
  1176. query: {
  1177. end: '',
  1178. utc: 'true',
  1179. statsPeriod: 'invalid',
  1180. cursor: 'some cursor',
  1181. },
  1182. };
  1183. expect(eventView.getEventsAPIPayload(location2)).toEqual({
  1184. project: ['1234'],
  1185. environment: ['staging'],
  1186. utc: 'true',
  1187. statsPeriod: '14d',
  1188. field: ['title', 'count()'],
  1189. per_page: 50,
  1190. query: 'event.type:csp',
  1191. sort: '-count',
  1192. cursor: 'some cursor',
  1193. });
  1194. });
  1195. it('includes start and end', function () {
  1196. const eventView = new EventView({
  1197. fields: generateFields(['title', 'count()']),
  1198. sorts: generateSorts(['count']),
  1199. query: 'event.type:csp',
  1200. start: '2019-10-01T00:00:00',
  1201. end: '2019-10-02T00:00:00',
  1202. environment: [],
  1203. project: [],
  1204. });
  1205. const location = {
  1206. query: {
  1207. // these should not be part of the API payload
  1208. statsPeriod: '55d',
  1209. period: '55d',
  1210. },
  1211. };
  1212. expect(eventView.getEventsAPIPayload(location)).toEqual({
  1213. field: ['title', 'count()'],
  1214. sort: '-count',
  1215. query: 'event.type:csp',
  1216. start: '2019-10-01T00:00:00.000',
  1217. end: '2019-10-02T00:00:00.000',
  1218. per_page: 50,
  1219. project: [],
  1220. environment: [],
  1221. });
  1222. });
  1223. it("an eventview's date selection has higher precedence than the date selection in the query string", function () {
  1224. const initialState = {
  1225. fields: generateFields(['title', 'count()']),
  1226. sorts: generateSorts(['count']),
  1227. query: 'event.type:csp',
  1228. environment: [],
  1229. project: [],
  1230. };
  1231. const output = {
  1232. field: ['title', 'count()'],
  1233. sort: '-count',
  1234. query: 'event.type:csp',
  1235. per_page: 50,
  1236. project: [],
  1237. environment: [],
  1238. };
  1239. // eventview's statsPeriod has highest precedence
  1240. let eventView = new EventView({
  1241. ...initialState,
  1242. statsPeriod: '90d',
  1243. start: '2019-10-01T00:00:00',
  1244. end: '2019-10-02T00:00:00',
  1245. });
  1246. let location = {
  1247. query: {
  1248. // these should not be part of the API payload
  1249. statsPeriod: '55d',
  1250. period: '30d',
  1251. start: '2020-10-01T00:00:00',
  1252. end: '2020-10-02T00:00:00',
  1253. },
  1254. };
  1255. expect(eventView.getEventsAPIPayload(location)).toEqual({
  1256. ...output,
  1257. statsPeriod: '90d',
  1258. });
  1259. // eventview's start/end has higher precedence than the date selection in the query string
  1260. eventView = new EventView({
  1261. ...initialState,
  1262. start: '2019-10-01T00:00:00',
  1263. end: '2019-10-02T00:00:00',
  1264. });
  1265. location = {
  1266. query: {
  1267. // these should not be part of the API payload
  1268. statsPeriod: '55d',
  1269. period: '30d',
  1270. start: '2020-10-01T00:00:00',
  1271. end: '2020-10-02T00:00:00',
  1272. },
  1273. };
  1274. expect(eventView.getEventsAPIPayload(location)).toEqual({
  1275. ...output,
  1276. start: '2019-10-01T00:00:00.000',
  1277. end: '2019-10-02T00:00:00.000',
  1278. });
  1279. // the date selection in the query string should be applied as expected
  1280. eventView = new EventView(initialState);
  1281. location = {
  1282. query: {
  1283. statsPeriod: '55d',
  1284. period: '30d',
  1285. start: '2020-10-01T00:00:00',
  1286. end: '2020-10-02T00:00:00',
  1287. },
  1288. };
  1289. expect(eventView.getEventsAPIPayload(location)).toEqual({
  1290. ...output,
  1291. statsPeriod: '55d',
  1292. });
  1293. location = {
  1294. query: {
  1295. period: '30d',
  1296. start: '2020-10-01T00:00:00',
  1297. end: '2020-10-02T00:00:00',
  1298. },
  1299. };
  1300. expect(eventView.getEventsAPIPayload(location)).toEqual({
  1301. ...output,
  1302. statsPeriod: '30d',
  1303. });
  1304. location = {
  1305. query: {
  1306. start: '2020-10-01T00:00:00',
  1307. end: '2020-10-02T00:00:00',
  1308. },
  1309. };
  1310. expect(eventView.getEventsAPIPayload(location)).toEqual({
  1311. ...output,
  1312. start: '2020-10-01T00:00:00.000',
  1313. end: '2020-10-02T00:00:00.000',
  1314. });
  1315. });
  1316. });
  1317. describe('EventView.getFacetsAPIPayload()', function () {
  1318. it('only includes relevant query strings', function () {
  1319. const eventView = new EventView({
  1320. fields: generateFields(['title', 'count()']),
  1321. sorts: generateSorts(['project', 'count']),
  1322. query: 'event.type:csp',
  1323. });
  1324. const location = {
  1325. query: {
  1326. start: '',
  1327. end: '',
  1328. utc: 'true',
  1329. statsPeriod: '14d',
  1330. // irrelevant query strings
  1331. bestCountry: 'canada',
  1332. cursor: 'some cursor',
  1333. sort: 'the world',
  1334. project: '1234',
  1335. environment: ['staging'],
  1336. display: 'releases',
  1337. },
  1338. };
  1339. expect(eventView.getFacetsAPIPayload(location)).toEqual({
  1340. project: [],
  1341. environment: [],
  1342. utc: 'true',
  1343. statsPeriod: '14d',
  1344. query: 'event.type:csp',
  1345. });
  1346. });
  1347. });
  1348. describe('EventView.toNewQuery()', function () {
  1349. const state = {
  1350. id: '1234',
  1351. name: 'best query',
  1352. fields: [
  1353. {field: 'count()', width: 123},
  1354. {field: 'project.id', width: 456},
  1355. ],
  1356. sorts: generateSorts(['count']),
  1357. query: 'event.type:error',
  1358. project: [42],
  1359. start: '2019-10-01T00:00:00',
  1360. end: '2019-10-02T00:00:00',
  1361. statsPeriod: '14d',
  1362. environment: ['staging'],
  1363. display: 'releases',
  1364. dataset: DiscoverDatasets.DISCOVER,
  1365. };
  1366. it('outputs the right fields', function () {
  1367. const eventView = new EventView(state);
  1368. const output = eventView.toNewQuery();
  1369. const expected = {
  1370. version: 2,
  1371. id: '1234',
  1372. name: 'best query',
  1373. fields: ['count()', 'project.id'],
  1374. widths: ['123', '456'],
  1375. orderby: '-count',
  1376. query: 'event.type:error',
  1377. projects: [42],
  1378. start: '2019-10-01T00:00:00',
  1379. end: '2019-10-02T00:00:00',
  1380. range: '14d',
  1381. environment: ['staging'],
  1382. display: 'releases',
  1383. dataset: DiscoverDatasets.DISCOVER,
  1384. };
  1385. expect(output).toEqual(expected);
  1386. });
  1387. it('omits query when query is an empty string', function () {
  1388. const modifiedState = {
  1389. ...state,
  1390. };
  1391. modifiedState.query = '';
  1392. const eventView = new EventView(modifiedState);
  1393. const output = eventView.toNewQuery();
  1394. const expected = {
  1395. version: 2,
  1396. id: '1234',
  1397. name: 'best query',
  1398. fields: ['count()', 'project.id'],
  1399. widths: ['123', '456'],
  1400. orderby: '-count',
  1401. projects: [42],
  1402. start: '2019-10-01T00:00:00',
  1403. end: '2019-10-02T00:00:00',
  1404. range: '14d',
  1405. environment: ['staging'],
  1406. display: 'releases',
  1407. dataset: DiscoverDatasets.DISCOVER,
  1408. };
  1409. expect(output).toEqual(expected);
  1410. });
  1411. it('omits query when query is not defined', function () {
  1412. const modifiedState = {
  1413. ...state,
  1414. };
  1415. delete modifiedState.query;
  1416. const eventView = new EventView(modifiedState);
  1417. const output = eventView.toNewQuery();
  1418. const expected = {
  1419. version: 2,
  1420. id: '1234',
  1421. name: 'best query',
  1422. fields: ['count()', 'project.id'],
  1423. widths: ['123', '456'],
  1424. orderby: '-count',
  1425. projects: [42],
  1426. start: '2019-10-01T00:00:00',
  1427. end: '2019-10-02T00:00:00',
  1428. range: '14d',
  1429. environment: ['staging'],
  1430. display: 'releases',
  1431. dataset: DiscoverDatasets.DISCOVER,
  1432. };
  1433. expect(output).toEqual(expected);
  1434. });
  1435. });
  1436. describe('EventView.isValid()', function () {
  1437. it('event view is valid when there is at least one field', function () {
  1438. const eventView = new EventView({
  1439. fields: [{field: 'count()'}, {field: 'project.id'}],
  1440. sorts: [],
  1441. project: [],
  1442. });
  1443. expect(eventView.isValid()).toBe(true);
  1444. });
  1445. it('event view is not valid when there are no fields', function () {
  1446. const eventView = new EventView({
  1447. fields: [],
  1448. sorts: [],
  1449. project: [],
  1450. });
  1451. expect(eventView.isValid()).toBe(false);
  1452. });
  1453. });
  1454. describe('EventView.getWidths()', function () {
  1455. it('returns widths', function () {
  1456. const eventView = new EventView({
  1457. fields: [
  1458. {field: 'count()', width: COL_WIDTH_UNDEFINED},
  1459. {field: 'project.id', width: 2020},
  1460. {field: 'title', width: COL_WIDTH_UNDEFINED},
  1461. {field: 'time', width: 420},
  1462. {field: 'lcp', width: 69},
  1463. {field: 'lcp', width: COL_WIDTH_UNDEFINED},
  1464. {field: 'fcp', width: COL_WIDTH_UNDEFINED},
  1465. {field: 'cls', width: COL_WIDTH_UNDEFINED},
  1466. ],
  1467. sorts: [],
  1468. project: [],
  1469. });
  1470. expect(eventView.getWidths()).toEqual([
  1471. COL_WIDTH_UNDEFINED,
  1472. 2020,
  1473. COL_WIDTH_UNDEFINED,
  1474. 420,
  1475. 69,
  1476. ]);
  1477. });
  1478. });
  1479. describe('EventView.getFields()', function () {
  1480. it('returns fields', function () {
  1481. const eventView = new EventView({
  1482. fields: [{field: 'count()'}, {field: 'project.id'}],
  1483. sorts: [],
  1484. project: [],
  1485. });
  1486. expect(eventView.getFields()).toEqual(['count()', 'project.id']);
  1487. });
  1488. });
  1489. describe('EventView.numOfColumns()', function () {
  1490. it('returns correct number of columns', function () {
  1491. // has columns
  1492. const eventView = new EventView({
  1493. fields: [{field: 'count()'}, {field: 'project.id'}],
  1494. sorts: [],
  1495. project: [],
  1496. });
  1497. expect(eventView.numOfColumns()).toBe(2);
  1498. // has no columns
  1499. const eventView2 = new EventView({
  1500. fields: [],
  1501. sorts: [],
  1502. project: [],
  1503. });
  1504. expect(eventView2.numOfColumns()).toBe(0);
  1505. });
  1506. });
  1507. describe('EventView.getDays()', function () {
  1508. it('returns the right number of days for statsPeriod', function () {
  1509. const eventView = new EventView({
  1510. statsPeriod: '14d',
  1511. });
  1512. expect(eventView.getDays()).toBe(14);
  1513. const eventView2 = new EventView({
  1514. statsPeriod: '12h',
  1515. });
  1516. expect(eventView2.getDays()).toBe(0.5);
  1517. });
  1518. it('returns the right number of days for start/end', function () {
  1519. const eventView = new EventView({
  1520. start: '2019-10-01T00:00:00',
  1521. end: '2019-10-02T00:00:00',
  1522. });
  1523. expect(eventView.getDays()).toBe(1);
  1524. const eventView2 = new EventView({
  1525. start: '2019-10-01T00:00:00',
  1526. end: '2019-10-15T00:00:00',
  1527. });
  1528. expect(eventView2.getDays()).toBe(14);
  1529. });
  1530. });
  1531. describe('EventView.clone()', function () {
  1532. it('returns a unique instance', function () {
  1533. const state = {
  1534. id: '1234',
  1535. name: 'best query',
  1536. fields: [{field: 'count()'}, {field: 'project.id'}],
  1537. sorts: generateSorts(['count']),
  1538. query: 'event.type:error',
  1539. project: [42],
  1540. start: '2019-10-01T00:00:00',
  1541. end: '2019-10-02T00:00:00',
  1542. statsPeriod: '14d',
  1543. environment: ['staging'],
  1544. interval: '5m',
  1545. display: 'releases',
  1546. dataset: DiscoverDatasets.DISCOVER,
  1547. };
  1548. const eventView = new EventView(state);
  1549. const eventView2 = eventView.clone();
  1550. expect(eventView2 !== eventView).toBeTruthy();
  1551. expect(eventView).toMatchObject(state);
  1552. expect(eventView2).toMatchObject(state);
  1553. expect(eventView.isEqualTo(eventView2)).toBe(true);
  1554. expect(
  1555. eventView.additionalConditions === eventView2.additionalConditions
  1556. ).toBeFalsy();
  1557. });
  1558. });
  1559. describe('EventView.withColumns()', function () {
  1560. const state = {
  1561. id: '1234',
  1562. name: 'best query',
  1563. fields: [
  1564. {field: 'count()', width: 30},
  1565. {field: 'project.id', width: 99},
  1566. {field: 'failure_count()', width: 30},
  1567. ],
  1568. yAxis: 'failure_count()',
  1569. sorts: generateSorts(['count']),
  1570. query: 'event.type:error',
  1571. project: [42],
  1572. start: '2019-10-01T00:00:00',
  1573. end: '2019-10-02T00:00:00',
  1574. statsPeriod: '14d',
  1575. environment: ['staging'],
  1576. };
  1577. const eventView = new EventView(state);
  1578. it('adds new columns, and replaces existing ones', function () {
  1579. const newView = eventView.withColumns([
  1580. {kind: 'field', field: 'title'},
  1581. {kind: 'function', function: ['count', '']},
  1582. {kind: 'field', field: 'project.id'},
  1583. {kind: 'field', field: 'culprit'},
  1584. ]);
  1585. // Views should be different.
  1586. expect(newView.isEqualTo(eventView)).toBe(false);
  1587. expect(newView.fields).toEqual([
  1588. {field: 'title', width: COL_WIDTH_UNDEFINED},
  1589. {field: 'count()', width: COL_WIDTH_UNDEFINED},
  1590. {field: 'project.id', width: COL_WIDTH_UNDEFINED},
  1591. {field: 'culprit', width: COL_WIDTH_UNDEFINED},
  1592. ]);
  1593. });
  1594. it('drops empty columns', function () {
  1595. const newView = eventView.withColumns([
  1596. {kind: 'field', field: 'issue'},
  1597. {kind: 'function', function: ['count', '']},
  1598. {kind: 'field', field: ''},
  1599. {kind: 'function', function: ['', '']},
  1600. {kind: 'function', function: ['', '', undefined]},
  1601. ]);
  1602. expect(newView.fields).toEqual([
  1603. {field: 'issue', width: COL_WIDTH_UNDEFINED},
  1604. {field: 'count()', width: COL_WIDTH_UNDEFINED},
  1605. ]);
  1606. });
  1607. it('inherits widths from existing columns when names match', function () {
  1608. const newView = eventView.withColumns([
  1609. {kind: 'function', function: ['count', '']},
  1610. {kind: 'field', field: 'project.id'},
  1611. {kind: 'field', field: 'title'},
  1612. {kind: 'field', field: 'time'},
  1613. ]);
  1614. expect(newView.fields).toEqual([
  1615. {field: 'count()', width: 30},
  1616. {field: 'project.id', width: 99},
  1617. {field: 'title', width: COL_WIDTH_UNDEFINED},
  1618. {field: 'time', width: COL_WIDTH_UNDEFINED},
  1619. ]);
  1620. });
  1621. it('retains sorts when sorted field is included', function () {
  1622. const newView = eventView.withColumns([
  1623. {kind: 'field', field: 'title'},
  1624. {kind: 'function', function: ['count', '']},
  1625. ]);
  1626. expect(newView.fields).toEqual([
  1627. {field: 'title', width: COL_WIDTH_UNDEFINED},
  1628. {field: 'count()', width: COL_WIDTH_UNDEFINED},
  1629. ]);
  1630. expect(newView.sorts).toEqual([{field: 'count', kind: 'desc'}]);
  1631. });
  1632. it('updates sorts when sorted field is removed', function () {
  1633. const newView = eventView.withColumns([{kind: 'field', field: 'title'}]);
  1634. expect(newView.fields).toEqual([{field: 'title', width: COL_WIDTH_UNDEFINED}]);
  1635. // Should pick a sortable field.
  1636. expect(newView.sorts).toEqual([{field: 'title', kind: 'desc'}]);
  1637. });
  1638. it('has no sort if no sortable fields remain', function () {
  1639. const newView = eventView.withColumns([{kind: 'field', field: 'issue'}]);
  1640. expect(newView.fields).toEqual([{field: 'issue', width: COL_WIDTH_UNDEFINED}]);
  1641. expect(newView.sorts).toEqual([]);
  1642. });
  1643. it('updates yAxis if column is dropped', function () {
  1644. const newView = eventView.withColumns([
  1645. {kind: 'field', field: 'count()'},
  1646. {kind: 'field', field: 'project.id'},
  1647. ]);
  1648. expect(newView.fields).toEqual([
  1649. {field: 'count()', width: 30},
  1650. {field: 'project.id', width: 99},
  1651. ]);
  1652. expect(eventView.yAxis).toEqual('failure_count()');
  1653. expect(newView.yAxis).toEqual('count()');
  1654. });
  1655. });
  1656. describe('EventView.withNewColumn()', function () {
  1657. const state = {
  1658. id: '1234',
  1659. name: 'best query',
  1660. fields: [
  1661. {field: 'count()', width: 30},
  1662. {field: 'project.id', width: 99},
  1663. ],
  1664. sorts: generateSorts(['count']),
  1665. query: 'event.type:error',
  1666. project: [42],
  1667. start: '2019-10-01T00:00:00',
  1668. end: '2019-10-02T00:00:00',
  1669. statsPeriod: '14d',
  1670. environment: ['staging'],
  1671. };
  1672. it('adds a field', function () {
  1673. const eventView = new EventView(state);
  1674. const newColumn = {
  1675. kind: 'field',
  1676. field: 'title',
  1677. };
  1678. const eventView2 = eventView.withNewColumn(newColumn);
  1679. expect(eventView2 !== eventView).toBeTruthy();
  1680. expect(eventView).toMatchObject(state);
  1681. const nextState = {
  1682. ...state,
  1683. fields: [...state.fields, {field: 'title'}],
  1684. };
  1685. expect(eventView2).toMatchObject(nextState);
  1686. });
  1687. it('adds an aggregate function with no arguments', function () {
  1688. const eventView = new EventView(state);
  1689. const newColumn = {
  1690. kind: 'function',
  1691. function: ['count', ''],
  1692. };
  1693. const eventView2 = eventView.withNewColumn(newColumn);
  1694. expect(eventView2 !== eventView).toBeTruthy();
  1695. expect(eventView).toMatchObject(state);
  1696. const nextState = {
  1697. ...state,
  1698. fields: [...state.fields, {field: 'count()'}],
  1699. };
  1700. expect(eventView2).toMatchObject(nextState);
  1701. });
  1702. it('add an aggregate function with field', function () {
  1703. const eventView = new EventView(state);
  1704. const newColumn = {
  1705. kind: 'function',
  1706. function: ['avg', 'transaction.duration'],
  1707. };
  1708. const eventView2 = eventView.withNewColumn(newColumn);
  1709. expect(eventView2 !== eventView).toBeTruthy();
  1710. expect(eventView).toMatchObject(state);
  1711. const nextState = {
  1712. ...state,
  1713. fields: [...state.fields, {field: 'avg(transaction.duration)'}],
  1714. };
  1715. expect(eventView2).toMatchObject(nextState);
  1716. });
  1717. it('add an aggregate function with field & refinement', function () {
  1718. const eventView = new EventView(state);
  1719. const newColumn = {
  1720. kind: 'function',
  1721. function: ['percentile', 'transaction.duration', '0.5'],
  1722. };
  1723. const updated = eventView.withNewColumn(newColumn);
  1724. expect(updated.fields).toEqual([
  1725. ...state.fields,
  1726. {field: 'percentile(transaction.duration,0.5)', width: COL_WIDTH_UNDEFINED},
  1727. ]);
  1728. });
  1729. });
  1730. describe('EventView.withResizedColumn()', function () {
  1731. const state = {
  1732. id: '1234',
  1733. name: 'best query',
  1734. fields: [{field: 'count()'}, {field: 'project.id'}],
  1735. sorts: generateSorts(['count']),
  1736. query: 'event.type:error',
  1737. project: [42],
  1738. start: '2019-10-01T00:00:00',
  1739. end: '2019-10-02T00:00:00',
  1740. statsPeriod: '14d',
  1741. environment: ['staging'],
  1742. };
  1743. const view = new EventView(state);
  1744. it('updates a column that exists', function () {
  1745. const newView = view.withResizedColumn(0, 99);
  1746. expect(view.fields[0].width).toBeUndefined();
  1747. expect(newView.fields[0].width).toEqual(99);
  1748. });
  1749. it('ignores columns that do not exist', function () {
  1750. const newView = view.withResizedColumn(100, 99);
  1751. expect(view.fields).toEqual(newView.fields);
  1752. });
  1753. });
  1754. describe('EventView.withUpdatedColumn()', function () {
  1755. const state = {
  1756. id: '1234',
  1757. name: 'best query',
  1758. fields: [{field: 'count()'}, {field: 'project.id'}],
  1759. sorts: generateSorts(['count']),
  1760. query: 'event.type:error',
  1761. project: [42],
  1762. start: '2019-10-01T00:00:00',
  1763. end: '2019-10-02T00:00:00',
  1764. statsPeriod: '14d',
  1765. environment: ['staging'],
  1766. };
  1767. const meta = {
  1768. count: 'integer',
  1769. title: 'string',
  1770. };
  1771. it('update a column with no changes', function () {
  1772. const eventView = new EventView(state);
  1773. const newColumn = {
  1774. kind: 'function',
  1775. function: ['count', ''],
  1776. };
  1777. const eventView2 = eventView.withUpdatedColumn(0, newColumn, meta);
  1778. expect(eventView2 === eventView).toBeTruthy();
  1779. expect(eventView).toMatchObject(state);
  1780. });
  1781. it('update a column to a field', function () {
  1782. const eventView = new EventView(state);
  1783. const newColumn = {
  1784. kind: 'field',
  1785. field: 'title',
  1786. };
  1787. const eventView2 = eventView.withUpdatedColumn(1, newColumn, meta);
  1788. expect(eventView2 !== eventView).toBeTruthy();
  1789. expect(eventView).toMatchObject(state);
  1790. const nextState = {
  1791. ...state,
  1792. fields: [state.fields[0], {field: 'title'}],
  1793. };
  1794. expect(eventView2).toMatchObject(nextState);
  1795. });
  1796. it('update a column to an aggregate function with no arguments', function () {
  1797. const eventView = new EventView(state);
  1798. const newColumn = {
  1799. kind: 'function',
  1800. function: ['count', ''],
  1801. };
  1802. const eventView2 = eventView.withUpdatedColumn(1, newColumn, meta);
  1803. expect(eventView2 !== eventView).toBeTruthy();
  1804. expect(eventView).toMatchObject(state);
  1805. const nextState = {
  1806. ...state,
  1807. fields: [state.fields[0], {field: 'count()'}],
  1808. };
  1809. expect(eventView2).toMatchObject(nextState);
  1810. });
  1811. it('update a column to an aggregate function with field', function () {
  1812. const eventView = new EventView(state);
  1813. const newColumn = {
  1814. kind: 'function',
  1815. function: ['avg', 'transaction.duration'],
  1816. };
  1817. const eventView2 = eventView.withUpdatedColumn(1, newColumn, meta);
  1818. expect(eventView2 !== eventView).toBeTruthy();
  1819. expect(eventView).toMatchObject(state);
  1820. const nextState = {
  1821. ...state,
  1822. fields: [state.fields[0], {field: 'avg(transaction.duration)'}],
  1823. };
  1824. expect(eventView2).toMatchObject(nextState);
  1825. });
  1826. it('update a column to an aggregate function with field & refinement', function () {
  1827. const eventView = new EventView(state);
  1828. const newColumn = {
  1829. kind: 'function',
  1830. function: ['percentile', 'transaction.duration', '0.5'],
  1831. };
  1832. const newView = eventView.withUpdatedColumn(1, newColumn, meta);
  1833. expect(newView.fields).toEqual([
  1834. state.fields[0],
  1835. {field: 'percentile(transaction.duration,0.5)', width: COL_WIDTH_UNDEFINED},
  1836. ]);
  1837. });
  1838. describe('update a column that is sorted', function () {
  1839. it('the sorted column is the only sorted column', function () {
  1840. const eventView = new EventView(state);
  1841. const newColumn = {
  1842. kind: 'field',
  1843. field: 'title',
  1844. };
  1845. const eventView2 = eventView.withUpdatedColumn(0, newColumn, meta);
  1846. expect(eventView2 !== eventView).toBeTruthy();
  1847. expect(eventView).toMatchObject(state);
  1848. const nextState = {
  1849. ...state,
  1850. sorts: [{field: 'title', kind: 'desc'}],
  1851. fields: [{field: 'title'}, state.fields[1]],
  1852. };
  1853. expect(eventView2).toMatchObject(nextState);
  1854. });
  1855. it('the sorted column occurs at least twice', function () {
  1856. const modifiedState = {
  1857. ...state,
  1858. fields: [...state.fields, {field: 'count()'}],
  1859. };
  1860. const eventView = new EventView(modifiedState);
  1861. const newColumn = {
  1862. kind: 'field',
  1863. field: 'title',
  1864. };
  1865. const eventView2 = eventView.withUpdatedColumn(0, newColumn, meta);
  1866. expect(eventView2 !== eventView).toBeTruthy();
  1867. expect(eventView).toMatchObject(modifiedState);
  1868. const nextState = {
  1869. ...state,
  1870. fields: [{field: 'title'}, state.fields[1], {field: 'count()'}],
  1871. };
  1872. expect(eventView2).toMatchObject(nextState);
  1873. });
  1874. it('using no provided table meta', function () {
  1875. // table meta may not be provided in the invalid query state;
  1876. // we will still want to be able to update columns
  1877. const eventView = new EventView(state);
  1878. const expected = {
  1879. ...state,
  1880. sorts: [{field: 'title', kind: 'desc'}],
  1881. fields: [{field: 'title'}, state.fields[1]],
  1882. };
  1883. const newColumn = {
  1884. kind: 'field',
  1885. field: 'title',
  1886. };
  1887. const eventView2 = eventView.withUpdatedColumn(0, newColumn, {});
  1888. expect(eventView2).toMatchObject(expected);
  1889. const eventView3 = eventView.withUpdatedColumn(0, newColumn);
  1890. expect(eventView3).toMatchObject(expected);
  1891. const eventView4 = eventView.withUpdatedColumn(0, newColumn, null);
  1892. expect(eventView4).toMatchObject(expected);
  1893. });
  1894. });
  1895. describe('update a column to a non-sortable column', function () {
  1896. it('default to a sortable column', function () {
  1897. const modifiedState = {
  1898. ...state,
  1899. fields: [{field: 'count()'}, {field: 'title'}],
  1900. };
  1901. const eventView = new EventView(modifiedState);
  1902. // this column is expected to be non-sortable
  1903. const newColumn = {
  1904. kind: 'field',
  1905. field: 'project.id',
  1906. };
  1907. const eventView2 = eventView.withUpdatedColumn(0, newColumn, meta);
  1908. expect(eventView2 !== eventView).toBeTruthy();
  1909. expect(eventView).toMatchObject(modifiedState);
  1910. const nextState = {
  1911. ...state,
  1912. sorts: [{field: 'title', kind: 'desc'}],
  1913. fields: [{field: 'project.id'}, {field: 'title'}],
  1914. };
  1915. expect(eventView2).toMatchObject(nextState);
  1916. });
  1917. it('has no sort if there are no sortable columns', function () {
  1918. const modifiedState = {
  1919. ...state,
  1920. fields: [{field: 'count()'}],
  1921. };
  1922. const eventView = new EventView(modifiedState);
  1923. // this column is expected to be non-sortable
  1924. const newColumn = {
  1925. kind: 'field',
  1926. field: 'project.id',
  1927. };
  1928. const eventView2 = eventView.withUpdatedColumn(0, newColumn, meta);
  1929. expect(eventView2 !== eventView).toBeTruthy();
  1930. expect(eventView).toMatchObject(modifiedState);
  1931. const nextState = {
  1932. ...state,
  1933. sorts: [],
  1934. fields: [{field: 'project.id'}],
  1935. };
  1936. expect(eventView2).toMatchObject(nextState);
  1937. });
  1938. });
  1939. });
  1940. describe('EventView.withDeletedColumn()', function () {
  1941. const state = {
  1942. id: '1234',
  1943. name: 'best query',
  1944. fields: [{field: 'count()'}, {field: 'project.id'}],
  1945. sorts: generateSorts(['count']),
  1946. query: 'event.type:error',
  1947. project: [42],
  1948. start: '2019-10-01T00:00:00',
  1949. end: '2019-10-02T00:00:00',
  1950. statsPeriod: '14d',
  1951. environment: ['staging'],
  1952. };
  1953. const meta = {
  1954. count: 'integer',
  1955. title: 'string',
  1956. };
  1957. it('returns itself when attempting to delete the last remaining column', function () {
  1958. const modifiedState = {
  1959. ...state,
  1960. fields: [{field: 'count()'}],
  1961. };
  1962. const eventView = new EventView(modifiedState);
  1963. const eventView2 = eventView.withDeletedColumn(0, meta);
  1964. expect(eventView2 === eventView).toBeTruthy();
  1965. expect(eventView).toMatchObject(modifiedState);
  1966. });
  1967. describe('deletes column, and use any remaining sortable column', function () {
  1968. it('using no provided table meta', function () {
  1969. // table meta may not be provided in the invalid query state;
  1970. // we will still want to be able to delete columns
  1971. const state2 = {
  1972. ...state,
  1973. fields: [{field: 'title'}, {field: 'timestamp'}, {field: 'count()'}],
  1974. sorts: generateSorts(['timestamp']),
  1975. };
  1976. const eventView = new EventView(state2);
  1977. const expected = {
  1978. ...state,
  1979. sorts: generateSorts(['title']),
  1980. fields: [{field: 'title'}, {field: 'count()'}],
  1981. };
  1982. const eventView2 = eventView.withDeletedColumn(1, {});
  1983. expect(eventView2).toMatchObject(expected);
  1984. const eventView3 = eventView.withDeletedColumn(1);
  1985. expect(eventView3).toMatchObject(expected);
  1986. const eventView4 = eventView.withDeletedColumn(1, null);
  1987. expect(eventView4).toMatchObject(expected);
  1988. });
  1989. it('has no remaining sortable column', function () {
  1990. const eventView = new EventView(state);
  1991. const eventView2 = eventView.withDeletedColumn(0, meta);
  1992. expect(eventView2 !== eventView).toBeTruthy();
  1993. expect(eventView).toMatchObject(state);
  1994. const nextState = {
  1995. ...state,
  1996. // we expect sorts to be empty since project.id is non-sortable
  1997. sorts: [],
  1998. fields: [state.fields[1]],
  1999. };
  2000. expect(eventView2).toMatchObject(nextState);
  2001. });
  2002. it('has a remaining sortable column', function () {
  2003. const modifiedState = {
  2004. ...state,
  2005. fields: [{field: 'count()'}, {field: 'project.id'}, {field: 'title'}],
  2006. };
  2007. const eventView = new EventView(modifiedState);
  2008. const eventView2 = eventView.withDeletedColumn(0, meta);
  2009. expect(eventView2 !== eventView).toBeTruthy();
  2010. expect(eventView).toMatchObject(modifiedState);
  2011. const nextState = {
  2012. ...state,
  2013. sorts: [{field: 'title', kind: 'desc'}],
  2014. fields: [{field: 'project.id'}, {field: 'title'}],
  2015. };
  2016. expect(eventView2).toMatchObject(nextState);
  2017. });
  2018. it('sorted column occurs at least twice', function () {
  2019. const modifiedState = {
  2020. ...state,
  2021. fields: [...state.fields, state.fields[0]],
  2022. };
  2023. const eventView = new EventView(modifiedState);
  2024. const eventView2 = eventView.withDeletedColumn(0, meta);
  2025. expect(eventView2 !== eventView).toBeTruthy();
  2026. expect(eventView).toMatchObject(modifiedState);
  2027. const nextState = {
  2028. ...state,
  2029. fields: [state.fields[1], state.fields[0]],
  2030. };
  2031. expect(eventView2).toMatchObject(nextState);
  2032. });
  2033. it('ensures there is at one auto-width column on deletion', function () {
  2034. const modifiedState = {
  2035. ...state,
  2036. fields: [
  2037. {field: 'id', width: 75},
  2038. {field: 'title', width: 100},
  2039. {field: 'project', width: 80},
  2040. {field: 'environment', width: 99},
  2041. ],
  2042. };
  2043. const eventView = new EventView(modifiedState);
  2044. let updated = eventView.withDeletedColumn(0, meta);
  2045. let updatedFields = [
  2046. {field: 'title', width: -1},
  2047. {field: 'project', width: 80},
  2048. {field: 'environment', width: 99},
  2049. ];
  2050. expect(updated.fields).toEqual(updatedFields);
  2051. updated = updated.withDeletedColumn(0, meta);
  2052. updatedFields = [
  2053. {field: 'project', width: -1},
  2054. {field: 'environment', width: 99},
  2055. ];
  2056. expect(updated.fields).toEqual(updatedFields);
  2057. });
  2058. });
  2059. });
  2060. describe('EventView.getSorts()', function () {
  2061. it('returns fields', function () {
  2062. const eventView = new EventView({
  2063. fields: [{field: 'count()'}, {field: 'project.id'}],
  2064. sorts: generateSorts(['count']),
  2065. project: [],
  2066. });
  2067. expect(eventView.getSorts()).toEqual([
  2068. {
  2069. key: 'count',
  2070. order: 'desc',
  2071. },
  2072. ]);
  2073. });
  2074. });
  2075. describe('EventView.getQuery()', function () {
  2076. it('with query', function () {
  2077. const eventView = new EventView({
  2078. fields: [],
  2079. sorts: [],
  2080. project: [],
  2081. query: 'event.type:error',
  2082. });
  2083. expect(eventView.getQuery()).toEqual('event.type:error');
  2084. expect(eventView.getQuery(null)).toEqual('event.type:error');
  2085. expect(eventView.getQuery('hello')).toEqual('event.type:error hello');
  2086. expect(eventView.getQuery(['event.type:error', 'hello'])).toEqual(
  2087. 'event.type:error hello'
  2088. );
  2089. });
  2090. it('without query', function () {
  2091. const eventView = new EventView({
  2092. fields: [],
  2093. sorts: [],
  2094. project: [],
  2095. });
  2096. expect(eventView.getQuery()).toEqual('');
  2097. expect(eventView.getQuery(null)).toEqual('');
  2098. expect(eventView.getQuery('hello')).toEqual('hello');
  2099. expect(eventView.getQuery(['event.type:error', 'hello'])).toEqual(
  2100. 'event.type:error hello'
  2101. );
  2102. });
  2103. });
  2104. describe('EventView.getQueryWithAdditionalConditions', function () {
  2105. it('with overlapping conditions', function () {
  2106. const eventView = new EventView({
  2107. fields: [],
  2108. sorts: [],
  2109. project: [],
  2110. query: 'event.type:transaction foo:bar',
  2111. });
  2112. eventView.additionalConditions.setFilterValues('event.type', ['transaction']);
  2113. expect(eventView.getQueryWithAdditionalConditions()).toEqual(
  2114. 'event.type:transaction foo:bar'
  2115. );
  2116. });
  2117. });
  2118. describe('EventView.sortForField()', function () {
  2119. const state = {
  2120. id: '1234',
  2121. name: 'best query',
  2122. fields: [{field: 'count()'}, {field: 'project.id'}],
  2123. sorts: generateSorts(['count']),
  2124. query: 'event.type:error',
  2125. project: [42],
  2126. start: '2019-10-01T00:00:00',
  2127. end: '2019-10-02T00:00:00',
  2128. statsPeriod: '14d',
  2129. environment: ['staging'],
  2130. };
  2131. const eventView = new EventView(state);
  2132. const meta = {count: 'integer'};
  2133. it('returns the sort when selected field is sorted', function () {
  2134. const field = {
  2135. field: 'count()',
  2136. };
  2137. const actual = eventView.sortForField(field, meta);
  2138. expect(actual).toEqual({
  2139. field: 'count',
  2140. kind: 'desc',
  2141. });
  2142. });
  2143. it('returns undefined when selected field is not sorted', function () {
  2144. const field = {
  2145. field: 'project.id',
  2146. };
  2147. expect(eventView.sortForField(field, meta)).toBeUndefined();
  2148. });
  2149. it('returns undefined when no meta is provided', function () {
  2150. const field = {
  2151. field: 'project.id',
  2152. };
  2153. expect(eventView.sortForField(field, undefined)).toBeUndefined();
  2154. });
  2155. });
  2156. describe('EventView.sortOnField()', function () {
  2157. const state = {
  2158. id: '1234',
  2159. name: 'best query',
  2160. fields: [{field: 'count()'}, {field: 'project.id'}],
  2161. sorts: generateSorts(['count']),
  2162. query: 'event.type:error',
  2163. project: [42],
  2164. start: '2019-10-01T00:00:00',
  2165. end: '2019-10-02T00:00:00',
  2166. statsPeriod: '14d',
  2167. environment: ['staging'],
  2168. };
  2169. const meta = {count: 'integer', title: 'string'};
  2170. it('returns itself when attempting to sort on un-sortable field', function () {
  2171. const eventView = new EventView(state);
  2172. expect(eventView).toMatchObject(state);
  2173. const field = state.fields[1];
  2174. const eventView2 = eventView.sortOnField(field, meta);
  2175. expect(eventView2 === eventView).toBe(true);
  2176. });
  2177. it('reverses the sorted field', function () {
  2178. const eventView = new EventView(state);
  2179. expect(eventView).toMatchObject(state);
  2180. const field = state.fields[0];
  2181. const eventView2 = eventView.sortOnField(field, meta);
  2182. expect(eventView2 !== eventView).toBe(true);
  2183. const nextState = {
  2184. ...state,
  2185. sorts: [{field: 'count', kind: 'asc'}],
  2186. };
  2187. expect(eventView2).toMatchObject(nextState);
  2188. });
  2189. it('enforce sort order on sorted field', function () {
  2190. const eventView = new EventView(state);
  2191. expect(eventView).toMatchObject(state);
  2192. const field = state.fields[0];
  2193. const eventView2 = eventView.sortOnField(field, meta, 'asc');
  2194. expect(eventView2).toMatchObject({
  2195. ...state,
  2196. sorts: [{field: 'count', kind: 'asc'}],
  2197. });
  2198. const eventView3 = eventView.sortOnField(field, meta, 'desc');
  2199. expect(eventView3).toMatchObject({
  2200. ...state,
  2201. sorts: [{field: 'count', kind: 'desc'}],
  2202. });
  2203. });
  2204. it('supports function format on equation sorts', function () {
  2205. const modifiedState = {
  2206. ...state,
  2207. fields: [{field: 'count()'}, {field: 'equation|count() + 100'}],
  2208. sorts: [{field: 'equation|count() + 100', kind: 'desc'}],
  2209. };
  2210. const eventView = new EventView(modifiedState);
  2211. expect(eventView).toMatchObject(modifiedState);
  2212. });
  2213. it('supports index format on equation sorts', function () {
  2214. const modifiedState = {
  2215. ...state,
  2216. fields: [{field: 'count()'}, {field: 'equation|count() + 100'}],
  2217. sorts: [{field: 'equation[0]', kind: 'desc'}],
  2218. };
  2219. const eventView = new EventView(modifiedState);
  2220. expect(eventView).toMatchObject(modifiedState);
  2221. });
  2222. it('sort on new field', function () {
  2223. const modifiedState = {
  2224. ...state,
  2225. fields: [...state.fields, {field: 'title'}],
  2226. };
  2227. const eventView = new EventView(modifiedState);
  2228. expect(eventView).toMatchObject(modifiedState);
  2229. const field = modifiedState.fields[2];
  2230. const eventView2 = eventView.sortOnField(field, meta);
  2231. expect(eventView2 !== eventView).toBe(true);
  2232. const nextState = {
  2233. ...modifiedState,
  2234. sorts: [{field: 'title', kind: 'desc'}],
  2235. };
  2236. expect(eventView2).toMatchObject(nextState);
  2237. // enforce asc sort order
  2238. const eventView3 = eventView.sortOnField(field, meta, 'asc');
  2239. expect(eventView3).toMatchObject({
  2240. ...modifiedState,
  2241. sorts: [{field: 'title', kind: 'asc'}],
  2242. });
  2243. // enforce desc sort order
  2244. const eventView4 = eventView.sortOnField(field, meta, 'desc');
  2245. expect(eventView4).toMatchObject({
  2246. ...modifiedState,
  2247. sorts: [{field: 'title', kind: 'desc'}],
  2248. });
  2249. });
  2250. it('sorts on a field using function format', function () {
  2251. const modifiedState = {
  2252. ...state,
  2253. fields: [...state.fields, {field: 'count()'}],
  2254. };
  2255. const eventView = new EventView(modifiedState);
  2256. expect(eventView).toMatchObject(modifiedState);
  2257. const field = modifiedState.fields[2];
  2258. let sortedEventView = eventView.sortOnField(field, meta, undefined, true);
  2259. expect(sortedEventView.sorts).toEqual([{field: 'count()', kind: 'asc'}]);
  2260. sortedEventView = sortedEventView.sortOnField(field, meta, undefined, true);
  2261. expect(sortedEventView.sorts).toEqual([{field: 'count()', kind: 'desc'}]);
  2262. });
  2263. });
  2264. describe('EventView.withSorts()', function () {
  2265. it('returns a clone', function () {
  2266. const eventView = new EventView({
  2267. fields: [{field: 'event.type'}],
  2268. });
  2269. const updated = eventView.withSorts([{kind: 'desc', field: 'event.type'}]);
  2270. expect(updated.sorts).not.toEqual(eventView.sorts);
  2271. });
  2272. it('only accepts sorting on fields in the view', function () {
  2273. const eventView = new EventView({
  2274. fields: [{field: 'event.type'}],
  2275. });
  2276. const updated = eventView.withSorts([
  2277. {kind: 'desc', field: 'event.type'},
  2278. {kind: 'asc', field: 'unknown'},
  2279. ]);
  2280. expect(updated.sorts).toEqual([{kind: 'desc', field: 'event.type'}]);
  2281. });
  2282. it('accepts aggregate field sorts', function () {
  2283. const eventView = new EventView({
  2284. fields: [{field: 'p50()'}],
  2285. });
  2286. const updated = eventView.withSorts([
  2287. {kind: 'desc', field: 'p50'},
  2288. {kind: 'asc', field: 'unknown'},
  2289. ]);
  2290. expect(updated.sorts).toEqual([{kind: 'desc', field: 'p50'}]);
  2291. });
  2292. });
  2293. describe('EventView.isEqualTo()', function () {
  2294. it('should be true when equal', function () {
  2295. const state = {
  2296. id: '1234',
  2297. name: 'best query',
  2298. fields: [{field: 'count()'}, {field: 'project.id'}],
  2299. sorts: generateSorts(['count']),
  2300. query: 'event.type:error',
  2301. project: [42],
  2302. start: '2019-10-01T00:00:00',
  2303. end: '2019-10-02T00:00:00',
  2304. statsPeriod: '14d',
  2305. environment: ['staging'],
  2306. yAxis: 'fam',
  2307. display: 'releases',
  2308. dataset: DiscoverDatasets.DISCOVER,
  2309. };
  2310. const eventView = new EventView(state);
  2311. const eventView2 = new EventView(state);
  2312. expect(eventView2 !== eventView).toBeTruthy();
  2313. expect(eventView).toMatchObject(state);
  2314. expect(eventView2).toMatchObject(state);
  2315. expect(eventView.isEqualTo(eventView2)).toBe(true);
  2316. // commutativity property holds
  2317. expect(eventView2.isEqualTo(eventView)).toBe(true);
  2318. });
  2319. it('should be true when datetime are equal but differ in format', function () {
  2320. const state = {
  2321. id: '1234',
  2322. name: 'best query',
  2323. fields: [{field: 'count()'}, {field: 'project.id'}],
  2324. sorts: generateSorts(['count']),
  2325. query: 'event.type:error',
  2326. project: [42],
  2327. start: '2019-10-20T21:02:51+0000',
  2328. end: '2019-10-23T19:27:04+0000',
  2329. environment: ['staging'],
  2330. };
  2331. const eventView = new EventView(state);
  2332. const eventView2 = new EventView({
  2333. ...state,
  2334. start: '2019-10-20T21:02:51Z',
  2335. end: '2019-10-23T19:27:04Z',
  2336. });
  2337. expect(eventView.isEqualTo(eventView2)).toBe(true);
  2338. });
  2339. it('should be false when not equal', function () {
  2340. const state = {
  2341. id: '1234',
  2342. name: 'best query',
  2343. fields: [{field: 'count()'}, {field: 'project.id'}],
  2344. sorts: generateSorts(['count']),
  2345. query: 'event.type:error',
  2346. project: [42],
  2347. start: '2019-10-01T00:00:00',
  2348. end: '2019-10-02T00:00:00',
  2349. statsPeriod: '14d',
  2350. environment: ['staging'],
  2351. yAxis: 'fam',
  2352. display: 'releases',
  2353. dataset: DiscoverDatasets.DISCOVER,
  2354. };
  2355. const differences = {
  2356. id: '12',
  2357. name: 'new query',
  2358. fields: [{field: 'project.id'}, {field: 'count()'}],
  2359. sorts: [{field: 'count', kind: 'asc'}],
  2360. query: 'event.type:transaction',
  2361. project: [24],
  2362. start: '2019-09-01T00:00:00',
  2363. end: '2020-09-01T00:00:00',
  2364. statsPeriod: '24d',
  2365. environment: [],
  2366. yAxis: 'ok boomer',
  2367. display: 'previous',
  2368. dataset: DiscoverDatasets.ISSUE_PLATFORM,
  2369. };
  2370. const eventView = new EventView(state);
  2371. for (const key in differences) {
  2372. const eventView2 = new EventView({...state, [key]: differences[key]});
  2373. expect(eventView.isEqualTo(eventView2)).toBe(false);
  2374. }
  2375. });
  2376. it('undefined display type equals default display type', function () {
  2377. const state = {
  2378. id: '1234',
  2379. name: 'best query',
  2380. fields: [{field: 'count()'}, {field: 'project.id'}],
  2381. sorts: generateSorts(['count']),
  2382. query: 'event.type:error',
  2383. project: [42],
  2384. start: '2019-10-01T00:00:00',
  2385. end: '2019-10-02T00:00:00',
  2386. statsPeriod: '14d',
  2387. environment: ['staging'],
  2388. yAxis: 'fam',
  2389. };
  2390. const eventView = new EventView(state);
  2391. const eventView2 = new EventView({...state, display: 'default'});
  2392. expect(eventView.isEqualTo(eventView2)).toBe(true);
  2393. });
  2394. });
  2395. describe('EventView.getResultsViewUrlTarget()', function () {
  2396. beforeEach(function () {
  2397. window.__initialData = {
  2398. customerDomain: {
  2399. subdomain: 'albertos-apples',
  2400. organizationUrl: 'https://albertos-apples.sentry.io',
  2401. sentryUrl: 'https://sentry.io',
  2402. },
  2403. };
  2404. });
  2405. afterEach(function () {
  2406. window.__initialData = {
  2407. customerDomain: null,
  2408. };
  2409. });
  2410. const state = {
  2411. id: '1234',
  2412. name: 'best query',
  2413. fields: [{field: 'count()'}, {field: 'project.id'}],
  2414. sorts: generateSorts(['count']),
  2415. query: 'event.type:error',
  2416. project: [42],
  2417. start: '2019-10-01T00:00:00',
  2418. end: '2019-10-02T00:00:00',
  2419. statsPeriod: '14d',
  2420. environment: ['staging'],
  2421. display: 'previous',
  2422. dataset: DiscoverDatasets.DISCOVER,
  2423. };
  2424. const organization = TestStubs.Organization();
  2425. it('generates a URL with non-customer domain context', function () {
  2426. window.__initialData.customerDomain = null;
  2427. const view = new EventView(state);
  2428. const result = view.getResultsViewUrlTarget(organization.slug);
  2429. expect(result.pathname).toEqual('/organizations/org-slug/discover/results/');
  2430. expect(result.query.query).toEqual(state.query);
  2431. expect(result.query.project).toEqual(state.project);
  2432. expect(result.query.display).toEqual(state.display);
  2433. });
  2434. it('generates a URL with customer domain context', function () {
  2435. const view = new EventView(state);
  2436. const result = view.getResultsViewUrlTarget(organization.slug);
  2437. expect(result.pathname).toEqual('/discover/results/');
  2438. expect(result.query.query).toEqual(state.query);
  2439. expect(result.query.project).toEqual(state.project);
  2440. expect(result.query.display).toEqual(state.display);
  2441. });
  2442. });
  2443. describe('EventView.getResultsViewShortUrlTarget()', function () {
  2444. beforeEach(function () {
  2445. window.__initialData = {
  2446. customerDomain: {
  2447. subdomain: 'albertos-apples',
  2448. organizationUrl: 'https://albertos-apples.sentry.io',
  2449. sentryUrl: 'https://sentry.io',
  2450. },
  2451. };
  2452. });
  2453. afterEach(function () {
  2454. window.__initialData = {
  2455. customerDomain: null,
  2456. };
  2457. });
  2458. const state = {
  2459. id: '1234',
  2460. name: 'best query',
  2461. fields: [{field: 'count()'}, {field: 'project.id'}],
  2462. sorts: generateSorts(['count']),
  2463. query: 'event.type:error',
  2464. project: [42],
  2465. start: '2019-10-01T00:00:00',
  2466. end: '2019-10-02T00:00:00',
  2467. statsPeriod: '14d',
  2468. environment: ['staging'],
  2469. display: 'previous',
  2470. dataset: DiscoverDatasets.DISCOVER,
  2471. };
  2472. const organization = TestStubs.Organization();
  2473. it('generates a URL with non-customer domain context', function () {
  2474. window.__initialData.customerDomain = null;
  2475. const view = new EventView(state);
  2476. const result = view.getResultsViewShortUrlTarget(organization.slug);
  2477. expect(result.pathname).toEqual('/organizations/org-slug/discover/results/');
  2478. expect(result.query).not.toHaveProperty('name');
  2479. expect(result.query).not.toHaveProperty('fields');
  2480. expect(result.query).not.toHaveProperty('query');
  2481. expect(result.query.id).toEqual(state.id);
  2482. expect(result.query.statsPeriod).toEqual(state.statsPeriod);
  2483. expect(result.query.project).toEqual(state.project);
  2484. expect(result.query.environment).toEqual(state.environment);
  2485. });
  2486. it('generates a URL with customer domain context', function () {
  2487. const view = new EventView(state);
  2488. const result = view.getResultsViewShortUrlTarget(organization.slug);
  2489. expect(result.pathname).toEqual('/discover/results/');
  2490. expect(result.query).not.toHaveProperty('name');
  2491. expect(result.query).not.toHaveProperty('fields');
  2492. expect(result.query).not.toHaveProperty('query');
  2493. expect(result.query.id).toEqual(state.id);
  2494. expect(result.query.statsPeriod).toEqual(state.statsPeriod);
  2495. expect(result.query.project).toEqual(state.project);
  2496. expect(result.query.environment).toEqual(state.environment);
  2497. });
  2498. });
  2499. describe('EventView.getPerformanceTransactionEventsViewUrlTarget()', function () {
  2500. beforeEach(function () {
  2501. window.__initialData = {
  2502. customerDomain: {
  2503. subdomain: 'albertos-apples',
  2504. organizationUrl: 'https://albertos-apples.sentry.io',
  2505. sentryUrl: 'https://sentry.io',
  2506. },
  2507. };
  2508. });
  2509. afterEach(function () {
  2510. window.__initialData = {
  2511. customerDomain: null,
  2512. };
  2513. });
  2514. const state = {
  2515. id: '1234',
  2516. name: 'best query',
  2517. fields: [{field: 'count()'}, {field: 'project.id'}],
  2518. sorts: generateSorts(['count']),
  2519. query: 'event.type:error',
  2520. project: [42],
  2521. start: '2019-10-01T00:00:00',
  2522. end: '2019-10-02T00:00:00',
  2523. statsPeriod: '14d',
  2524. environment: ['staging'],
  2525. display: 'previous',
  2526. dataset: DiscoverDatasets.DISCOVER,
  2527. };
  2528. const organization = TestStubs.Organization();
  2529. const showTransactions = 'p99';
  2530. const breakdown = 'http';
  2531. const webVital = 'measurements.lcp';
  2532. it('generates a URL with non-customer domain context', function () {
  2533. window.__initialData.customerDomain = null;
  2534. const view = new EventView(state);
  2535. const result = view.getPerformanceTransactionEventsViewUrlTarget(organization.slug, {
  2536. showTransactions,
  2537. breakdown,
  2538. webVital,
  2539. });
  2540. expect(result.pathname).toEqual(
  2541. '/organizations/org-slug/performance/summary/events/'
  2542. );
  2543. expect(result.query.query).toEqual(state.query);
  2544. expect(result.query.project).toEqual(state.project);
  2545. expect(result.query.sort).toEqual(['-count']);
  2546. expect(result.query.transaction).toEqual(state.name);
  2547. expect(result.query.showTransactions).toEqual(showTransactions);
  2548. expect(result.query.breakdown).toEqual(breakdown);
  2549. expect(result.query.webVital).toEqual(webVital);
  2550. });
  2551. it('generates a URL with customer domain context', function () {
  2552. const view = new EventView(state);
  2553. const result = view.getPerformanceTransactionEventsViewUrlTarget(organization.slug, {
  2554. showTransactions,
  2555. breakdown,
  2556. webVital,
  2557. });
  2558. expect(result.pathname).toEqual('/performance/summary/events/');
  2559. expect(result.query.query).toEqual(state.query);
  2560. expect(result.query.project).toEqual(state.project);
  2561. expect(result.query.sort).toEqual(['-count']);
  2562. expect(result.query.transaction).toEqual(state.name);
  2563. expect(result.query.showTransactions).toEqual(showTransactions);
  2564. expect(result.query.breakdown).toEqual(breakdown);
  2565. expect(result.query.webVital).toEqual(webVital);
  2566. });
  2567. });
  2568. describe('EventView.getPageFilters()', function () {
  2569. it('return default global selection', function () {
  2570. const eventView = new EventView({});
  2571. expect(eventView.getPageFilters()).toMatchObject({
  2572. projects: [],
  2573. environments: [],
  2574. datetime: {
  2575. start: null,
  2576. end: null,
  2577. period: null,
  2578. // event views currently do not support the utc option,
  2579. // see comment in EventView.getPageFilters
  2580. utc: true,
  2581. },
  2582. });
  2583. });
  2584. it('returns global selection query', function () {
  2585. const state2 = {
  2586. project: [42],
  2587. start: 'start',
  2588. end: 'end',
  2589. statsPeriod: '42d',
  2590. environment: ['prod'],
  2591. };
  2592. const eventView = new EventView(state2);
  2593. expect(eventView.getPageFilters()).toMatchObject({
  2594. projects: state2.project,
  2595. environments: state2.environment,
  2596. datetime: {
  2597. start: state2.start,
  2598. end: state2.end,
  2599. period: state2.statsPeriod,
  2600. // event views currently do not support the utc option,
  2601. // see comment in EventView.getPageFilters
  2602. utc: true,
  2603. },
  2604. });
  2605. });
  2606. });
  2607. describe('EventView.getPageFiltersQuery()', function () {
  2608. it('return default global selection query', function () {
  2609. const eventView = new EventView({});
  2610. expect(eventView.getPageFiltersQuery()).toMatchObject({
  2611. project: [],
  2612. start: undefined,
  2613. end: undefined,
  2614. statsPeriod: undefined,
  2615. environment: [],
  2616. // event views currently do not support the utc option,
  2617. // see comment in EventView.getPageFilters
  2618. utc: 'true',
  2619. });
  2620. });
  2621. it('returns global selection query', function () {
  2622. const state2 = {
  2623. project: [42],
  2624. start: 'start',
  2625. end: 'end',
  2626. statsPeriod: '42d',
  2627. environment: ['prod'],
  2628. };
  2629. const eventView = new EventView(state2);
  2630. expect(eventView.getPageFiltersQuery()).toMatchObject({
  2631. ...state2,
  2632. // when generating the query, it converts numbers to strings
  2633. project: ['42'],
  2634. // event views currently do not support the utc option,
  2635. // see comment in EventView.getPageFilters
  2636. utc: 'true',
  2637. });
  2638. });
  2639. });
  2640. describe('EventView.generateBlankQueryStringObject()', function () {
  2641. it('should return blank values', function () {
  2642. const eventView = new EventView({});
  2643. expect(eventView.generateBlankQueryStringObject()).toEqual({
  2644. id: undefined,
  2645. name: undefined,
  2646. fields: undefined,
  2647. sorts: undefined,
  2648. query: undefined,
  2649. project: undefined,
  2650. start: undefined,
  2651. end: undefined,
  2652. statsPeriod: undefined,
  2653. environment: undefined,
  2654. yAxis: undefined,
  2655. cursor: undefined,
  2656. });
  2657. });
  2658. });
  2659. describe('EventView.getYAxisOptions()', function () {
  2660. const state = {
  2661. fields: [],
  2662. sorts: [],
  2663. query: '',
  2664. project: [],
  2665. statsPeriod: '42d',
  2666. environment: [],
  2667. };
  2668. function generateYaxis(value) {
  2669. return {
  2670. value,
  2671. label: value,
  2672. };
  2673. }
  2674. it('should return default options', function () {
  2675. const thisEventView = new EventView(state);
  2676. expect(thisEventView.getYAxisOptions()).toEqual(CHART_AXIS_OPTIONS);
  2677. });
  2678. it('should add aggregate fields as options', function () {
  2679. let thisEventView = new EventView({
  2680. ...state,
  2681. fields: generateFields(['ignored-field', 'count_unique(issue)']),
  2682. });
  2683. expect(thisEventView.getYAxisOptions()).toEqual([
  2684. generateYaxis('count_unique(issue)'),
  2685. ...CHART_AXIS_OPTIONS,
  2686. ]);
  2687. // should de-duplicate entries
  2688. thisEventView = new EventView({
  2689. ...state,
  2690. fields: generateFields(['ignored-field', 'count()']),
  2691. });
  2692. expect(thisEventView.getYAxisOptions()).toEqual([...CHART_AXIS_OPTIONS]);
  2693. });
  2694. it('should exclude yAxis options that are not useful', function () {
  2695. const thisEventView = new EventView({
  2696. ...state,
  2697. fields: generateFields([
  2698. 'ignored-field',
  2699. 'count_unique(issue)',
  2700. 'last_seen()',
  2701. 'max(timestamp)',
  2702. ]),
  2703. });
  2704. expect(thisEventView.getYAxisOptions()).toEqual([
  2705. generateYaxis('count_unique(issue)'),
  2706. ...CHART_AXIS_OPTIONS,
  2707. ]);
  2708. });
  2709. });
  2710. describe('EventView.getYAxis()', function () {
  2711. const state = {
  2712. fields: [],
  2713. sorts: [],
  2714. query: '',
  2715. project: [],
  2716. statsPeriod: '42d',
  2717. environment: [],
  2718. };
  2719. it('should return first default yAxis', function () {
  2720. const thisEventView = new EventView(state);
  2721. expect(thisEventView.getYAxis()).toEqual('count()');
  2722. });
  2723. it('should return valid yAxis', function () {
  2724. const thisEventView = new EventView({
  2725. ...state,
  2726. fields: generateFields(['ignored-field', 'count_unique(user)', 'last_seen']),
  2727. yAxis: 'count_unique(user)',
  2728. });
  2729. expect(thisEventView.getYAxis()).toEqual('count_unique(user)');
  2730. });
  2731. it('should ignore invalid yAxis', function () {
  2732. const invalid = [
  2733. 'last_seen',
  2734. 'latest_event',
  2735. 'count_unique(issue)', // this is not one of the selected fields
  2736. ];
  2737. for (const option of invalid) {
  2738. const thisEventView = new EventView({
  2739. ...state,
  2740. fields: generateFields(['ignored-field', 'last_seen', 'latest_event']),
  2741. yAxis: option,
  2742. });
  2743. // yAxis defaults to the first entry of the default yAxis options
  2744. expect(thisEventView.getYAxis()).toEqual('count()');
  2745. }
  2746. });
  2747. });
  2748. describe('EventView.getDisplayOptions()', function () {
  2749. const state = {
  2750. fields: [],
  2751. sorts: [],
  2752. query: '',
  2753. project: [],
  2754. statsPeriod: '42d',
  2755. environment: [],
  2756. };
  2757. it('should return default options', function () {
  2758. const eventView = new EventView({
  2759. ...state,
  2760. // there needs to exist an aggregate or TOP 5 modes will be disabled
  2761. fields: [{field: 'count()'}],
  2762. });
  2763. expect(eventView.getDisplayOptions()).toEqual(DISPLAY_MODE_OPTIONS);
  2764. });
  2765. it('should disable previous when start/end are used.', function () {
  2766. const eventView = new EventView({
  2767. ...state,
  2768. end: '2020-04-13T12:13:14',
  2769. start: '2020-04-01T12:13:14',
  2770. });
  2771. const options = eventView.getDisplayOptions();
  2772. expect(options[1].value).toEqual('previous');
  2773. expect(options[1].disabled).toBeTruthy();
  2774. });
  2775. it('should disable top 5 period/daily if no aggregates present', function () {
  2776. const eventView = new EventView({
  2777. ...state,
  2778. });
  2779. const options = eventView.getDisplayOptions();
  2780. expect(options[2].value).toEqual('top5');
  2781. expect(options[2].disabled).toBeTruthy();
  2782. expect(options[4].value).toEqual('dailytop5');
  2783. expect(options[4].disabled).toBeTruthy();
  2784. });
  2785. });
  2786. describe('EventView.getDisplayMode()', function () {
  2787. const state = {
  2788. fields: [],
  2789. sorts: [],
  2790. query: '',
  2791. project: [],
  2792. statsPeriod: '42d',
  2793. environment: [],
  2794. };
  2795. it('should have default', function () {
  2796. const eventView = new EventView({
  2797. ...state,
  2798. });
  2799. const displayMode = eventView.getDisplayMode();
  2800. expect(displayMode).toEqual(DisplayModes.DEFAULT);
  2801. });
  2802. it('should return current mode when not disabled', function () {
  2803. const eventView = new EventView({
  2804. ...state,
  2805. display: DisplayModes.DAILY,
  2806. });
  2807. const displayMode = eventView.getDisplayMode();
  2808. expect(displayMode).toEqual(DisplayModes.DAILY);
  2809. });
  2810. it('should return default mode when disabled', function () {
  2811. const eventView = new EventView({
  2812. ...state,
  2813. // the existence of start and end will disable the PREVIOUS mode
  2814. end: '2020-04-13T12:13:14',
  2815. start: '2020-04-01T12:13:14',
  2816. display: DisplayModes.PREVIOUS,
  2817. });
  2818. const displayMode = eventView.getDisplayMode();
  2819. expect(displayMode).toEqual(DisplayModes.DEFAULT);
  2820. });
  2821. it('top 5 should fallback to default when disabled', function () {
  2822. const eventView = new EventView({
  2823. ...state,
  2824. // the lack of an aggregate will disable the TOP5 mode
  2825. display: DisplayModes.TOP5,
  2826. });
  2827. const displayMode = eventView.getDisplayMode();
  2828. expect(displayMode).toEqual(DisplayModes.DEFAULT);
  2829. });
  2830. it('top 5 daily should fallback to daily when disabled', function () {
  2831. const eventView = new EventView({
  2832. ...state,
  2833. // the lack of an aggregate will disable the DAILYTOP5 mode
  2834. display: DisplayModes.DAILYTOP5,
  2835. });
  2836. const displayMode = eventView.getDisplayMode();
  2837. expect(displayMode).toEqual(DisplayModes.DAILY);
  2838. });
  2839. it('daily mode should fall back to default when disabled', function () {
  2840. const eventView = new EventView({
  2841. ...state,
  2842. // the period being less than 24h will disable the DAILY mode
  2843. statsPeriod: '1h',
  2844. display: DisplayModes.DAILY,
  2845. });
  2846. const displayMode = eventView.getDisplayMode();
  2847. expect(displayMode).toEqual(DisplayModes.DEFAULT);
  2848. });
  2849. it('top 5 daily mode should fall back to default when daily is disabled', function () {
  2850. const eventView = new EventView({
  2851. ...state,
  2852. // the period being less than 24h will disable the DAILY mode
  2853. start: '2020-04-01T12:13:14',
  2854. end: '2020-04-02T12:10:14',
  2855. display: DisplayModes.TOP5DAILY,
  2856. });
  2857. const displayMode = eventView.getDisplayMode();
  2858. expect(displayMode).toEqual(DisplayModes.DEFAULT);
  2859. });
  2860. });
  2861. describe('EventView.getAggregateFields()', function () {
  2862. const state = {
  2863. fields: [
  2864. {field: 'title'},
  2865. {field: 'count()'},
  2866. {field: 'count_unique(user)'},
  2867. {field: 'apdex(300)'},
  2868. {field: 'transaction'},
  2869. ],
  2870. sorts: [],
  2871. query: '',
  2872. project: [],
  2873. statsPeriod: '42d',
  2874. environment: [],
  2875. };
  2876. it('getAggregateFields() returns only aggregates', function () {
  2877. const eventView = new EventView(state);
  2878. const expected = [
  2879. {field: 'count()'},
  2880. {field: 'count_unique(user)'},
  2881. {field: 'apdex(300)'},
  2882. ];
  2883. expect(eventView.getAggregateFields()).toEqual(expected);
  2884. });
  2885. });
  2886. describe('EventView.hasAggregateField', function () {
  2887. it('ensures an eventview has an aggregate field', function () {
  2888. let eventView = new EventView({
  2889. fields: [{field: 'foobar'}],
  2890. sorts: [],
  2891. query: '',
  2892. project: [],
  2893. environment: [],
  2894. });
  2895. expect(eventView.hasAggregateField()).toBe(false);
  2896. eventView = new EventView({
  2897. fields: [{field: 'count(foo.bar.is-Enterprise_42)'}],
  2898. sorts: [],
  2899. query: '',
  2900. project: [],
  2901. environment: [],
  2902. });
  2903. expect(eventView.hasAggregateField()).toBe(true);
  2904. });
  2905. });
  2906. describe('isAPIPayloadSimilar', function () {
  2907. const state = {
  2908. id: '1234',
  2909. name: 'best query',
  2910. fields: [{field: 'count()'}, {field: 'project.id'}],
  2911. sorts: generateSorts(['count']),
  2912. query: 'event.type:error',
  2913. project: [42],
  2914. start: '2019-10-01T00:00:00',
  2915. end: '2019-10-02T00:00:00',
  2916. statsPeriod: '14d',
  2917. environment: ['staging'],
  2918. };
  2919. const meta = {
  2920. count: 'integer',
  2921. title: 'string',
  2922. };
  2923. describe('getEventsAPIPayload', function () {
  2924. it('is not similar when relevant query string keys are present in the Location object', function () {
  2925. const thisEventView = new EventView(state);
  2926. const location = {
  2927. query: {
  2928. project: 'project',
  2929. environment: 'environment',
  2930. start: 'start',
  2931. end: 'end',
  2932. utc: 'utc',
  2933. statsPeriod: 'statsPeriod',
  2934. cursor: 'cursor',
  2935. },
  2936. };
  2937. const thisAPIPayload = thisEventView.getEventsAPIPayload(location);
  2938. const otherLocation = {};
  2939. const otherAPIPayload = thisEventView.getEventsAPIPayload(otherLocation);
  2940. const results = isAPIPayloadSimilar(thisAPIPayload, otherAPIPayload);
  2941. expect(results).toBe(false);
  2942. });
  2943. it('is similar when irrelevant query string keys are present in the Location object', function () {
  2944. const thisEventView = new EventView(state);
  2945. const location = {
  2946. query: {
  2947. bestCountry: 'canada',
  2948. },
  2949. };
  2950. const thisAPIPayload = thisEventView.getEventsAPIPayload(location);
  2951. const otherLocation = {};
  2952. const otherAPIPayload = thisEventView.getEventsAPIPayload(otherLocation);
  2953. const results = isAPIPayloadSimilar(thisAPIPayload, otherAPIPayload);
  2954. expect(results).toBe(true);
  2955. });
  2956. it('is not similar on sort key sorted in opposite directions', function () {
  2957. const thisEventView = new EventView(state);
  2958. const location = {};
  2959. const thisAPIPayload = thisEventView.getEventsAPIPayload(location);
  2960. const otherEventView = thisEventView.sortOnField({field: 'count()'}, meta);
  2961. const otherLocation = {};
  2962. const otherAPIPayload = otherEventView.getEventsAPIPayload(otherLocation);
  2963. const results = isAPIPayloadSimilar(thisAPIPayload, otherAPIPayload);
  2964. expect(results).toBe(false);
  2965. });
  2966. it('is not similar when a new column is added', function () {
  2967. const thisEventView = new EventView(state);
  2968. const location = {};
  2969. const thisAPIPayload = thisEventView.getEventsAPIPayload(location);
  2970. const otherEventView = thisEventView.clone();
  2971. otherEventView.fields.push({field: 'title', width: COL_WIDTH_UNDEFINED});
  2972. const otherLocation = {};
  2973. const otherAPIPayload = otherEventView.getEventsAPIPayload(otherLocation);
  2974. const results = isAPIPayloadSimilar(thisAPIPayload, otherAPIPayload);
  2975. expect(results).toBe(false);
  2976. });
  2977. it('is similar when a column is updated with no changes', function () {
  2978. const thisEventView = new EventView(state);
  2979. const location = {};
  2980. const thisAPIPayload = thisEventView.getEventsAPIPayload(location);
  2981. const newColumn = {
  2982. kind: 'function',
  2983. function: ['count', ''],
  2984. };
  2985. const otherEventView = thisEventView.withUpdatedColumn(0, newColumn, meta);
  2986. const otherLocation = {};
  2987. const otherAPIPayload = otherEventView.getEventsAPIPayload(otherLocation);
  2988. const results = isAPIPayloadSimilar(thisAPIPayload, otherAPIPayload);
  2989. expect(results).toBe(true);
  2990. });
  2991. it('is not similar when a column is updated with a replaced field', function () {
  2992. const thisEventView = new EventView(state);
  2993. const location = {};
  2994. const thisAPIPayload = thisEventView.getEventsAPIPayload(location);
  2995. const newColumn = {
  2996. kind: 'field',
  2997. field: 'title',
  2998. };
  2999. const otherEventView = thisEventView.withUpdatedColumn(0, newColumn, meta);
  3000. const otherLocation = {};
  3001. const otherAPIPayload = otherEventView.getEventsAPIPayload(otherLocation);
  3002. const results = isAPIPayloadSimilar(thisAPIPayload, otherAPIPayload);
  3003. expect(results).toBe(false);
  3004. });
  3005. it('is not similar when a column is updated with a replaced aggregation', function () {
  3006. const thisEventView = new EventView(state);
  3007. const location = {};
  3008. const thisAPIPayload = thisEventView.getEventsAPIPayload(location);
  3009. const newColumn = {
  3010. kind: 'function',
  3011. function: ['avg', ''],
  3012. };
  3013. const otherEventView = thisEventView.withUpdatedColumn(0, newColumn, meta);
  3014. const otherLocation = {};
  3015. const otherAPIPayload = otherEventView.getEventsAPIPayload(otherLocation);
  3016. const results = isAPIPayloadSimilar(thisAPIPayload, otherAPIPayload);
  3017. expect(results).toBe(false);
  3018. });
  3019. it('is similar when a column is renamed', function () {
  3020. const thisEventView = new EventView(state);
  3021. const location = {};
  3022. const thisAPIPayload = thisEventView.getEventsAPIPayload(location);
  3023. const newColumn = {
  3024. kind: 'function',
  3025. function: ['count', ''],
  3026. };
  3027. const otherEventView = thisEventView.withUpdatedColumn(0, newColumn, meta);
  3028. const otherLocation = {};
  3029. const otherAPIPayload = otherEventView.getEventsAPIPayload(otherLocation);
  3030. const results = isAPIPayloadSimilar(thisAPIPayload, otherAPIPayload);
  3031. expect(results).toBe(true);
  3032. });
  3033. it('is not similar when a column is deleted', function () {
  3034. const thisEventView = new EventView(state);
  3035. const location = {};
  3036. const thisAPIPayload = thisEventView.getEventsAPIPayload(location);
  3037. const otherEventView = thisEventView.withDeletedColumn(0, meta);
  3038. const otherLocation = {};
  3039. const otherAPIPayload = otherEventView.getEventsAPIPayload(otherLocation);
  3040. const results = isAPIPayloadSimilar(thisAPIPayload, otherAPIPayload);
  3041. expect(results).toBe(false);
  3042. });
  3043. it('is similar if column order changes', function () {
  3044. const thisEventView = new EventView(state);
  3045. const location = {};
  3046. const thisAPIPayload = thisEventView.getEventsAPIPayload(location);
  3047. state.fields.reverse();
  3048. const otherEventView = new EventView(state);
  3049. const otherLocation = {};
  3050. const otherAPIPayload = otherEventView.getEventsAPIPayload(otherLocation);
  3051. const results = isAPIPayloadSimilar(thisAPIPayload, otherAPIPayload);
  3052. expect(results).toBe(true);
  3053. });
  3054. it('is similar if equation order relatively same', function () {
  3055. const equationField = {field: 'equation|failure_count() / count()'};
  3056. const otherEquationField = {field: 'equation|failure_count() / 2'};
  3057. state.fields = [
  3058. {field: 'project.id'},
  3059. {field: 'count()'},
  3060. equationField,
  3061. otherEquationField,
  3062. ];
  3063. const thisEventView = new EventView(state);
  3064. const location = {};
  3065. const thisAPIPayload = thisEventView.getEventsAPIPayload(location);
  3066. state.fields = [
  3067. equationField,
  3068. {field: 'project.id'},
  3069. {field: 'count()'},
  3070. otherEquationField,
  3071. ];
  3072. const otherEventView = new EventView(state);
  3073. const otherLocation = {};
  3074. const otherAPIPayload = otherEventView.getEventsAPIPayload(otherLocation);
  3075. const results = isAPIPayloadSimilar(thisAPIPayload, otherAPIPayload);
  3076. expect(results).toBe(true);
  3077. });
  3078. it('is not similar if equation order changes', function () {
  3079. const equationField = {field: 'equation|failure_count() / count()'};
  3080. const otherEquationField = {field: 'equation|failure_count() / 2'};
  3081. state.fields = [
  3082. {field: 'project.id'},
  3083. {field: 'count()'},
  3084. equationField,
  3085. otherEquationField,
  3086. ];
  3087. const thisEventView = new EventView(state);
  3088. const location = {};
  3089. const thisAPIPayload = thisEventView.getEventsAPIPayload(location);
  3090. state.fields = [
  3091. {field: 'project.id'},
  3092. {field: 'count()'},
  3093. otherEquationField,
  3094. equationField,
  3095. ];
  3096. const otherEventView = new EventView(state);
  3097. const otherLocation = {};
  3098. const otherAPIPayload = otherEventView.getEventsAPIPayload(otherLocation);
  3099. const results = isAPIPayloadSimilar(thisAPIPayload, otherAPIPayload);
  3100. expect(results).toBe(false);
  3101. });
  3102. });
  3103. describe('getFacetsAPIPayload', function () {
  3104. it('only includes relevant parameters', function () {
  3105. const thisEventView = new EventView(state);
  3106. const location = {};
  3107. const results = thisEventView.getFacetsAPIPayload(location);
  3108. const expected = {
  3109. query: state.query,
  3110. project: ['42'],
  3111. statsPeriod: state.statsPeriod,
  3112. environment: state.environment,
  3113. };
  3114. expect(results).toEqual(expected);
  3115. });
  3116. it('is similar on sort key sorted in opposite directions', function () {
  3117. const thisEventView = new EventView(state);
  3118. const location = {};
  3119. const thisAPIPayload = thisEventView.getFacetsAPIPayload(location);
  3120. const newColumn = {
  3121. kind: 'function',
  3122. function: ['count', ''],
  3123. };
  3124. const otherEventView = thisEventView.withUpdatedColumn(0, newColumn, meta);
  3125. const otherLocation = {};
  3126. const otherAPIPayload = otherEventView.getFacetsAPIPayload(otherLocation);
  3127. const results = isAPIPayloadSimilar(thisAPIPayload, otherAPIPayload);
  3128. expect(results).toBe(true);
  3129. });
  3130. it('is similar when a columns are different', function () {
  3131. const thisEventView = new EventView(state);
  3132. const location = {};
  3133. const thisAPIPayload = thisEventView.getFacetsAPIPayload(location);
  3134. const otherEventView = thisEventView.clone();
  3135. otherEventView.fields.push({field: 'title', width: COL_WIDTH_UNDEFINED});
  3136. const otherLocation = {};
  3137. const otherAPIPayload = otherEventView.getFacetsAPIPayload(otherLocation);
  3138. const results = isAPIPayloadSimilar(thisAPIPayload, otherAPIPayload);
  3139. expect(results).toBe(true);
  3140. });
  3141. });
  3142. });
  3143. describe('pickRelevantLocationQueryStrings', function () {
  3144. it('picks relevant query strings', function () {
  3145. const location = {
  3146. query: {
  3147. project: 'project',
  3148. environment: 'environment',
  3149. start: 'start',
  3150. end: 'end',
  3151. utc: 'utc',
  3152. statsPeriod: 'statsPeriod',
  3153. cursor: 'cursor',
  3154. // irrelevant query strings
  3155. bestCountry: 'canada',
  3156. },
  3157. };
  3158. const actual = pickRelevantLocationQueryStrings(location);
  3159. const expected = {
  3160. start: 'start',
  3161. end: 'end',
  3162. utc: 'utc',
  3163. statsPeriod: 'statsPeriod',
  3164. cursor: 'cursor',
  3165. };
  3166. expect(actual).toEqual(expected);
  3167. });
  3168. });