eventView.spec.jsx 94 KB

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