eventView.spec.jsx 75 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840
  1. import EventView, {
  2. isAPIPayloadSimilar,
  3. pickRelevantLocationQueryStrings,
  4. } from 'app/utils/discover/eventView';
  5. import {COL_WIDTH_UNDEFINED} from 'app/components/gridEditable/utils';
  6. import {
  7. CHART_AXIS_OPTIONS,
  8. DisplayModes,
  9. DISPLAY_MODE_OPTIONS,
  10. } from 'app/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. start: '2019-10-01T00:00:00',
  51. end: '2019-10-02T00:00:00',
  52. statsPeriod: '14d',
  53. environment: ['staging'],
  54. yAxis: 'p95',
  55. display: 'previous',
  56. },
  57. };
  58. const eventView = EventView.fromLocation(location);
  59. expect(eventView).toMatchObject({
  60. id: '42',
  61. name: 'best query',
  62. fields: [
  63. {field: 'count()', width: 123},
  64. {field: 'id', width: 456},
  65. ],
  66. sorts: generateSorts(['count']),
  67. query: 'event.type:transaction',
  68. project: [123],
  69. start: undefined,
  70. end: undefined,
  71. statsPeriod: '14d',
  72. environment: ['staging'],
  73. yAxis: 'p95',
  74. display: 'previous',
  75. });
  76. });
  77. it('includes first valid statsPeriod', function() {
  78. const location = {
  79. query: {
  80. id: '42',
  81. name: 'best query',
  82. field: ['count()', 'id'],
  83. widths: ['123', '456'],
  84. sort: ['title', '-count'],
  85. query: 'event.type:transaction',
  86. project: [123],
  87. start: '2019-10-01T00:00:00',
  88. end: '2019-10-02T00:00:00',
  89. statsPeriod: ['invalid', '28d'],
  90. environment: ['staging'],
  91. },
  92. };
  93. const eventView = EventView.fromLocation(location);
  94. expect(eventView).toMatchObject({
  95. id: '42',
  96. name: 'best query',
  97. fields: [
  98. {field: 'count()', width: 123},
  99. {field: 'id', width: 456},
  100. ],
  101. sorts: generateSorts(['count']),
  102. query: 'event.type:transaction',
  103. project: [123],
  104. start: undefined,
  105. end: undefined,
  106. statsPeriod: '28d',
  107. environment: ['staging'],
  108. });
  109. });
  110. it('includes start and end', function() {
  111. const location = {
  112. query: {
  113. id: '42',
  114. name: 'best query',
  115. field: ['count()', 'id'],
  116. widths: ['123', '456'],
  117. sort: ['title', '-count'],
  118. query: 'event.type:transaction',
  119. project: [123],
  120. start: '2019-10-01T00:00:00',
  121. end: '2019-10-02T00:00:00',
  122. environment: ['staging'],
  123. },
  124. };
  125. const eventView = EventView.fromLocation(location);
  126. expect(eventView).toMatchObject({
  127. id: '42',
  128. name: 'best query',
  129. fields: [
  130. {field: 'count()', width: 123},
  131. {field: 'id', width: 456},
  132. ],
  133. sorts: generateSorts(['count']),
  134. query: 'event.type:transaction',
  135. project: [123],
  136. start: '2019-10-01T00:00:00.000',
  137. end: '2019-10-02T00:00:00.000',
  138. environment: ['staging'],
  139. });
  140. });
  141. it('generates event view when there are no query strings', function() {
  142. const location = {
  143. query: {},
  144. };
  145. const eventView = EventView.fromLocation(location);
  146. expect(eventView).toMatchObject({
  147. id: void 0,
  148. name: void 0,
  149. fields: [],
  150. sorts: [],
  151. query: '',
  152. project: [],
  153. start: void 0,
  154. end: void 0,
  155. statsPeriod: '14d',
  156. environment: [],
  157. yAxis: void 0,
  158. });
  159. });
  160. });
  161. describe('EventView.fromSavedQuery()', function() {
  162. it('maps basic properties of saved query', function() {
  163. const saved = {
  164. id: '42',
  165. name: 'best query',
  166. fields: ['count()', 'id'],
  167. query: 'event.type:transaction',
  168. projects: [123],
  169. range: '14d',
  170. start: '2019-10-01T00:00:00',
  171. end: '2019-10-02T00:00:00',
  172. orderby: '-id',
  173. environment: ['staging'],
  174. display: 'previous',
  175. };
  176. const eventView = EventView.fromSavedQuery(saved);
  177. expect(eventView).toMatchObject({
  178. id: saved.id,
  179. name: saved.name,
  180. fields: [
  181. {field: 'count()', width: COL_WIDTH_UNDEFINED},
  182. {field: 'id', width: COL_WIDTH_UNDEFINED},
  183. ],
  184. sorts: [{field: 'id', kind: 'desc'}],
  185. query: 'event.type:transaction',
  186. project: [123],
  187. start: undefined,
  188. end: undefined,
  189. // statsPeriod has precedence
  190. statsPeriod: '14d',
  191. environment: ['staging'],
  192. yAxis: undefined,
  193. display: 'previous',
  194. });
  195. const eventView2 = EventView.fromSavedQuery({
  196. ...saved,
  197. range: undefined,
  198. });
  199. expect(eventView2).toMatchObject({
  200. id: saved.id,
  201. name: saved.name,
  202. fields: [
  203. {field: 'count()', width: COL_WIDTH_UNDEFINED},
  204. {field: 'id', width: COL_WIDTH_UNDEFINED},
  205. ],
  206. sorts: [{field: 'id', kind: 'desc'}],
  207. query: 'event.type:transaction',
  208. project: [123],
  209. start: '2019-10-01T00:00:00.000',
  210. end: '2019-10-02T00:00:00.000',
  211. statsPeriod: undefined,
  212. environment: ['staging'],
  213. });
  214. });
  215. it('maps saved query with no conditions', function() {
  216. const saved = {
  217. orderby: '-count',
  218. name: 'foo bar',
  219. fields: ['release', 'count()'],
  220. widths: [111, 222],
  221. dateCreated: '2019-10-30T06:13:17.632078Z',
  222. environment: ['dev', 'production'],
  223. version: 2,
  224. createdBy: '1',
  225. dateUpdated: '2019-10-30T06:13:17.632096Z',
  226. id: '5',
  227. projects: [1],
  228. yAxis: 'count()',
  229. };
  230. const eventView = EventView.fromSavedQuery(saved);
  231. const expected = {
  232. id: '5',
  233. name: 'foo bar',
  234. fields: [
  235. {field: 'release', width: 111},
  236. {field: 'count()', width: 222},
  237. ],
  238. sorts: generateSorts(['count']),
  239. query: '',
  240. project: [1],
  241. environment: ['dev', 'production'],
  242. yAxis: 'count()',
  243. };
  244. expect(eventView).toMatchObject(expected);
  245. });
  246. it('maps properties from v2 saved query', function() {
  247. const saved = {
  248. name: 'best query',
  249. fields: ['count()', 'title'],
  250. range: '14d',
  251. start: '',
  252. end: '',
  253. };
  254. const eventView = EventView.fromSavedQuery(saved);
  255. expect(eventView.fields).toEqual([
  256. {field: 'count()', width: COL_WIDTH_UNDEFINED},
  257. {field: 'title', width: COL_WIDTH_UNDEFINED},
  258. ]);
  259. expect(eventView.name).toEqual(saved.name);
  260. expect(eventView.statsPeriod).toEqual('14d');
  261. expect(eventView.start).toEqual(undefined);
  262. expect(eventView.end).toEqual(undefined);
  263. });
  264. it('saved queries are equal when start and end datetime differ in format', function() {
  265. const saved = {
  266. orderby: '-count_timestamp',
  267. end: '2019-10-23T19:27:04+0000',
  268. name: 'release query',
  269. fields: ['release', 'count(timestamp)'],
  270. dateCreated: '2019-10-30T05:10:23.718937Z',
  271. environment: ['dev', 'production'],
  272. start: '2019-10-20T21:02:51+0000',
  273. version: 2,
  274. createdBy: '1',
  275. dateUpdated: '2019-10-30T07:25:58.291917Z',
  276. id: '3',
  277. projects: [1],
  278. };
  279. const eventView = EventView.fromSavedQuery(saved);
  280. const eventView2 = EventView.fromSavedQuery({
  281. ...saved,
  282. start: '2019-10-20T21:02:51Z',
  283. end: '2019-10-23T19:27:04Z',
  284. });
  285. expect(eventView.isEqualTo(eventView2)).toBe(true);
  286. const eventView3 = EventView.fromSavedQuery({
  287. ...saved,
  288. start: '2019-10-20T21:02:51Z',
  289. });
  290. expect(eventView.isEqualTo(eventView3)).toBe(true);
  291. const eventView4 = EventView.fromSavedQuery({
  292. ...saved,
  293. end: '2019-10-23T19:27:04Z',
  294. });
  295. expect(eventView.isEqualTo(eventView4)).toBe(true);
  296. });
  297. it('saved queries are not equal when datetime selection are invalid', function() {
  298. const saved = {
  299. orderby: '-count_timestamp',
  300. end: '2019-10-23T19:27:04+0000',
  301. name: 'release query',
  302. fields: ['release', 'count(timestamp)'],
  303. dateCreated: '2019-10-30T05:10:23.718937Z',
  304. environment: ['dev', 'production'],
  305. start: '2019-10-20T21:02:51+0000',
  306. version: 2,
  307. createdBy: '1',
  308. dateUpdated: '2019-10-30T07:25:58.291917Z',
  309. id: '3',
  310. projects: [1],
  311. };
  312. const eventView = EventView.fromSavedQuery(saved);
  313. const eventView2 = EventView.fromSavedQuery({
  314. ...saved,
  315. start: '',
  316. });
  317. expect(eventView.isEqualTo(eventView2)).toBe(false);
  318. const eventView3 = EventView.fromSavedQuery({
  319. ...saved,
  320. end: '',
  321. });
  322. expect(eventView.isEqualTo(eventView3)).toBe(false);
  323. // this is expected since datetime (start and end) are normalized
  324. expect(eventView2.isEqualTo(eventView3)).toBe(true);
  325. });
  326. });
  327. describe('EventView.fromNewQueryWithLocation()', function() {
  328. const prebuiltQuery = {
  329. id: undefined,
  330. name: 'All Events',
  331. query: '',
  332. projects: [],
  333. fields: ['title', 'event.type', 'project', 'user', 'timestamp'],
  334. orderby: '-timestamp',
  335. version: 2,
  336. };
  337. it('maps basic properties of a prebuilt query', function() {
  338. const location = {
  339. query: {
  340. statsPeriod: '99d',
  341. },
  342. };
  343. const eventView = EventView.fromNewQueryWithLocation(prebuiltQuery, location);
  344. expect(eventView).toMatchObject({
  345. id: undefined,
  346. name: 'All Events',
  347. fields: [
  348. {field: 'title'},
  349. {field: 'event.type'},
  350. {field: 'project'},
  351. {field: 'user'},
  352. {field: 'timestamp'},
  353. ],
  354. sorts: [{field: 'timestamp', kind: 'desc'}],
  355. query: '',
  356. project: [],
  357. start: undefined,
  358. end: undefined,
  359. // statsPeriod has precedence
  360. statsPeriod: '99d',
  361. environment: [],
  362. yAxis: undefined,
  363. });
  364. });
  365. it('merges global selection values', function() {
  366. const location = {
  367. query: {
  368. statsPeriod: '99d',
  369. project: ['456'],
  370. environment: ['prod'],
  371. },
  372. };
  373. const eventView = EventView.fromNewQueryWithLocation(prebuiltQuery, location);
  374. expect(eventView).toMatchObject({
  375. id: undefined,
  376. name: 'All Events',
  377. fields: [
  378. {field: 'title'},
  379. {field: 'event.type'},
  380. {field: 'project'},
  381. {field: 'user'},
  382. {field: 'timestamp'},
  383. ],
  384. sorts: [{field: 'timestamp', kind: 'desc'}],
  385. query: '',
  386. project: [456],
  387. start: undefined,
  388. end: undefined,
  389. statsPeriod: '99d',
  390. environment: ['prod'],
  391. yAxis: undefined,
  392. });
  393. });
  394. it('new query takes precedence over global selection values', function() {
  395. const location = {
  396. query: {
  397. statsPeriod: '99d',
  398. project: ['456'],
  399. environment: ['prod'],
  400. },
  401. };
  402. const prebuiltQuery2 = {
  403. ...prebuiltQuery,
  404. range: '42d',
  405. projects: [987],
  406. environment: ['staging'],
  407. };
  408. const eventView = EventView.fromNewQueryWithLocation(prebuiltQuery2, location);
  409. expect(eventView).toMatchObject({
  410. id: undefined,
  411. name: 'All Events',
  412. fields: [
  413. {field: 'title'},
  414. {field: 'event.type'},
  415. {field: 'project'},
  416. {field: 'user'},
  417. {field: 'timestamp'},
  418. ],
  419. sorts: [{field: 'timestamp', kind: 'desc'}],
  420. query: '',
  421. project: [987],
  422. start: undefined,
  423. end: undefined,
  424. statsPeriod: '42d',
  425. environment: ['staging'],
  426. yAxis: undefined,
  427. });
  428. // also test start and end
  429. const location2 = {
  430. query: {
  431. start: '2019-10-01T00:00:00',
  432. end: '2019-10-02T00:00:00',
  433. project: ['456'],
  434. environment: ['prod'],
  435. },
  436. };
  437. const prebuiltQuery3 = {
  438. ...prebuiltQuery,
  439. start: '2019-10-01T00:00:00',
  440. end: '2019-10-02T00:00:00',
  441. projects: [987],
  442. environment: ['staging'],
  443. };
  444. const eventView2 = EventView.fromNewQueryWithLocation(prebuiltQuery3, location2);
  445. expect(eventView2).toMatchObject({
  446. id: undefined,
  447. name: 'All Events',
  448. fields: [
  449. {field: 'title'},
  450. {field: 'event.type'},
  451. {field: 'project'},
  452. {field: 'user'},
  453. {field: 'timestamp'},
  454. ],
  455. sorts: [{field: 'timestamp', kind: 'desc'}],
  456. query: '',
  457. project: [987],
  458. start: '2019-10-01T00:00:00.000',
  459. end: '2019-10-02T00:00:00.000',
  460. statsPeriod: undefined,
  461. environment: ['staging'],
  462. yAxis: undefined,
  463. });
  464. });
  465. });
  466. describe('EventView.generateQueryStringObject()', function() {
  467. it('skips empty values', function() {
  468. const eventView = new EventView({
  469. fields: generateFields(['id', 'title']),
  470. sorts: [],
  471. project: [],
  472. environment: '',
  473. statsPeriod: '',
  474. start: null,
  475. end: undefined,
  476. yAxis: undefined,
  477. display: 'previous',
  478. });
  479. const expected = {
  480. id: undefined,
  481. name: undefined,
  482. field: ['id', 'title'],
  483. widths: [COL_WIDTH_UNDEFINED, COL_WIDTH_UNDEFINED],
  484. sort: [],
  485. query: '',
  486. project: [],
  487. environment: [],
  488. display: 'previous',
  489. };
  490. expect(eventView.generateQueryStringObject()).toEqual(expected);
  491. });
  492. it('generates query string object', function() {
  493. const state = {
  494. id: '1234',
  495. name: 'best query',
  496. fields: [
  497. {field: 'count()', width: 123},
  498. {field: 'project.id', width: 456},
  499. ],
  500. sorts: generateSorts(['count']),
  501. query: 'event.type:error',
  502. project: [42],
  503. start: '2019-10-01T00:00:00',
  504. end: '2019-10-02T00:00:00',
  505. statsPeriod: '14d',
  506. environment: ['staging'],
  507. yAxis: 'count()',
  508. display: 'releases',
  509. interval: '1m',
  510. };
  511. const eventView = new EventView(state);
  512. const expected = {
  513. id: '1234',
  514. name: 'best query',
  515. field: ['count()', 'project.id'],
  516. widths: [123, 456],
  517. sort: ['-count'],
  518. query: 'event.type:error',
  519. project: [42],
  520. start: '2019-10-01T00:00:00',
  521. end: '2019-10-02T00:00:00',
  522. statsPeriod: '14d',
  523. environment: ['staging'],
  524. yAxis: 'count()',
  525. display: 'releases',
  526. interval: '1m',
  527. };
  528. expect(eventView.generateQueryStringObject()).toEqual(expected);
  529. });
  530. it('encodes fields', function() {
  531. const eventView = new EventView({
  532. fields: [{field: 'id'}, {field: 'title'}],
  533. sorts: [],
  534. });
  535. const query = eventView.generateQueryStringObject();
  536. expect(query.field).toEqual(['id', 'title']);
  537. });
  538. it('returns a copy of data preventing mutation', function() {
  539. const eventView = new EventView({
  540. fields: [{field: 'id'}, {field: 'title'}],
  541. sorts: [],
  542. });
  543. const query = eventView.generateQueryStringObject();
  544. query.field.push('newthing');
  545. // Getting the query again should return the original values.
  546. const secondQuery = eventView.generateQueryStringObject();
  547. expect(secondQuery.field).toEqual(['id', 'title']);
  548. expect(query).not.toEqual(secondQuery);
  549. });
  550. });
  551. describe('EventView.getEventsAPIPayload()', function() {
  552. it('generates the API payload', function() {
  553. const eventView = new EventView({
  554. id: 34,
  555. name: 'amazing query',
  556. fields: generateFields(['id']),
  557. sorts: generateSorts(['id']),
  558. query: 'event.type:csp',
  559. project: [567],
  560. environment: ['prod'],
  561. yAxis: 'users',
  562. display: 'releases',
  563. });
  564. expect(eventView.getEventsAPIPayload({})).toEqual({
  565. field: ['id'],
  566. per_page: 50,
  567. sort: '-id',
  568. query: 'event.type:csp',
  569. project: ['567'],
  570. environment: ['prod'],
  571. statsPeriod: '14d',
  572. });
  573. });
  574. it('does not append query conditions in location', function() {
  575. const eventView = new EventView({
  576. fields: generateFields(['id']),
  577. sorts: [],
  578. query: 'event.type:csp',
  579. });
  580. const location = {
  581. query: {
  582. query: 'TypeError',
  583. },
  584. };
  585. expect(eventView.getEventsAPIPayload(location).query).toEqual('event.type:csp');
  586. });
  587. it('only includes at most one sort key', function() {
  588. const eventView = new EventView({
  589. fields: generateFields(['count()', 'title']),
  590. sorts: generateSorts(['title', 'count']),
  591. query: 'event.type:csp',
  592. });
  593. const location = {
  594. query: {},
  595. };
  596. expect(eventView.getEventsAPIPayload(location).sort).toEqual('-title');
  597. });
  598. it('only includes sort keys that are defined in fields', function() {
  599. const eventView = new EventView({
  600. fields: generateFields(['title', 'count()']),
  601. sorts: generateSorts(['project', 'count']),
  602. query: 'event.type:csp',
  603. });
  604. const location = {
  605. query: {},
  606. };
  607. expect(eventView.getEventsAPIPayload(location).sort).toEqual('-count');
  608. });
  609. it('only includes relevant query strings', function() {
  610. const eventView = new EventView({
  611. fields: generateFields(['title', 'count()']),
  612. sorts: generateSorts(['project', 'count']),
  613. query: 'event.type:csp',
  614. });
  615. const location = {
  616. query: {
  617. start: '2020-08-12 12:13:14',
  618. end: '2020-08-26 12:13:14',
  619. utc: 'true',
  620. statsPeriod: '14d',
  621. cursor: 'some cursor',
  622. yAxis: 'count()',
  623. // irrelevant query strings
  624. bestCountry: 'canada',
  625. project: '1234',
  626. environment: ['staging'],
  627. },
  628. };
  629. expect(eventView.getEventsAPIPayload(location)).toEqual({
  630. project: [],
  631. environment: [],
  632. utc: 'true',
  633. statsPeriod: '14d',
  634. field: ['title', 'count()'],
  635. per_page: 50,
  636. query: 'event.type:csp',
  637. sort: '-count',
  638. cursor: 'some cursor',
  639. });
  640. });
  641. it('includes default coerced statsPeriod when omitted or is invalid', function() {
  642. const eventView = new EventView({
  643. fields: generateFields(['title', 'count()']),
  644. sorts: generateSorts(['project', 'count']),
  645. query: 'event.type:csp',
  646. project: [1234],
  647. environment: ['staging'],
  648. });
  649. const location = {
  650. query: {
  651. start: '',
  652. end: '',
  653. utc: 'true',
  654. // invalid statsPeriod string
  655. statsPeriod: 'invalid',
  656. cursor: 'some cursor',
  657. },
  658. };
  659. expect(eventView.getEventsAPIPayload(location)).toEqual({
  660. project: ['1234'],
  661. environment: ['staging'],
  662. utc: 'true',
  663. statsPeriod: '14d',
  664. field: ['title', 'count()'],
  665. per_page: 50,
  666. query: 'event.type:csp',
  667. sort: '-count',
  668. cursor: 'some cursor',
  669. });
  670. const location2 = {
  671. query: {
  672. start: '',
  673. end: '',
  674. utc: 'true',
  675. // statsPeriod is omitted here
  676. cursor: 'some cursor',
  677. },
  678. };
  679. expect(eventView.getEventsAPIPayload(location2)).toEqual({
  680. project: ['1234'],
  681. environment: ['staging'],
  682. utc: 'true',
  683. statsPeriod: '14d',
  684. field: ['title', 'count()'],
  685. per_page: 50,
  686. query: 'event.type:csp',
  687. sort: '-count',
  688. cursor: 'some cursor',
  689. });
  690. });
  691. it('includes default coerced statsPeriod when either start or end is only provided', function() {
  692. const eventView = new EventView({
  693. fields: generateFields(['title', 'count()']),
  694. sorts: generateSorts(['project', 'count']),
  695. query: 'event.type:csp',
  696. project: [1234],
  697. environment: ['staging'],
  698. });
  699. const location = {
  700. query: {
  701. start: '',
  702. utc: 'true',
  703. statsPeriod: 'invalid',
  704. cursor: 'some cursor',
  705. },
  706. };
  707. expect(eventView.getEventsAPIPayload(location)).toEqual({
  708. project: ['1234'],
  709. environment: ['staging'],
  710. utc: 'true',
  711. statsPeriod: '14d',
  712. field: ['title', 'count()'],
  713. per_page: 50,
  714. query: 'event.type:csp',
  715. sort: '-count',
  716. cursor: 'some cursor',
  717. });
  718. const location2 = {
  719. query: {
  720. end: '',
  721. utc: 'true',
  722. statsPeriod: 'invalid',
  723. cursor: 'some cursor',
  724. },
  725. };
  726. expect(eventView.getEventsAPIPayload(location2)).toEqual({
  727. project: ['1234'],
  728. environment: ['staging'],
  729. utc: 'true',
  730. statsPeriod: '14d',
  731. field: ['title', 'count()'],
  732. per_page: 50,
  733. query: 'event.type:csp',
  734. sort: '-count',
  735. cursor: 'some cursor',
  736. });
  737. });
  738. it('includes start and end', function() {
  739. const eventView = new EventView({
  740. fields: generateFields(['title', 'count()']),
  741. sorts: generateSorts(['count']),
  742. query: 'event.type:csp',
  743. start: '2019-10-01T00:00:00',
  744. end: '2019-10-02T00:00:00',
  745. environment: [],
  746. project: [],
  747. });
  748. const location = {
  749. query: {
  750. // these should not be part of the API payload
  751. statsPeriod: '55d',
  752. period: '55d',
  753. },
  754. };
  755. expect(eventView.getEventsAPIPayload(location)).toEqual({
  756. field: ['title', 'count()'],
  757. sort: '-count',
  758. query: 'event.type:csp',
  759. start: '2019-10-01T00:00:00.000',
  760. end: '2019-10-02T00:00:00.000',
  761. per_page: 50,
  762. project: [],
  763. environment: [],
  764. });
  765. });
  766. it("an eventview's date selection has higher precedence than the date selection in the query string", function() {
  767. const initialState = {
  768. fields: generateFields(['title', 'count()']),
  769. sorts: generateSorts(['count']),
  770. query: 'event.type:csp',
  771. environment: [],
  772. project: [],
  773. };
  774. const output = {
  775. field: ['title', 'count()'],
  776. sort: '-count',
  777. query: 'event.type:csp',
  778. per_page: 50,
  779. project: [],
  780. environment: [],
  781. };
  782. // eventview's statsPeriod has highest precedence
  783. let eventView = new EventView({
  784. ...initialState,
  785. statsPeriod: '90d',
  786. start: '2019-10-01T00:00:00',
  787. end: '2019-10-02T00:00:00',
  788. });
  789. let location = {
  790. query: {
  791. // these should not be part of the API payload
  792. statsPeriod: '55d',
  793. period: '30d',
  794. start: '2020-10-01T00:00:00',
  795. end: '2020-10-02T00:00:00',
  796. },
  797. };
  798. expect(eventView.getEventsAPIPayload(location)).toEqual({
  799. ...output,
  800. statsPeriod: '90d',
  801. });
  802. // eventview's start/end has higher precedence than the date selection in the query string
  803. eventView = new EventView({
  804. ...initialState,
  805. start: '2019-10-01T00:00:00',
  806. end: '2019-10-02T00:00:00',
  807. });
  808. location = {
  809. query: {
  810. // these should not be part of the API payload
  811. statsPeriod: '55d',
  812. period: '30d',
  813. start: '2020-10-01T00:00:00',
  814. end: '2020-10-02T00:00:00',
  815. },
  816. };
  817. expect(eventView.getEventsAPIPayload(location)).toEqual({
  818. ...output,
  819. start: '2019-10-01T00:00:00.000',
  820. end: '2019-10-02T00:00:00.000',
  821. });
  822. // the date selection in the query string should be applied as expected
  823. eventView = new EventView(initialState);
  824. location = {
  825. query: {
  826. statsPeriod: '55d',
  827. period: '30d',
  828. start: '2020-10-01T00:00:00',
  829. end: '2020-10-02T00:00:00',
  830. },
  831. };
  832. expect(eventView.getEventsAPIPayload(location)).toEqual({
  833. ...output,
  834. statsPeriod: '55d',
  835. });
  836. location = {
  837. query: {
  838. period: '30d',
  839. start: '2020-10-01T00:00:00',
  840. end: '2020-10-02T00:00:00',
  841. },
  842. };
  843. expect(eventView.getEventsAPIPayload(location)).toEqual({
  844. ...output,
  845. statsPeriod: '30d',
  846. });
  847. location = {
  848. query: {
  849. start: '2020-10-01T00:00:00',
  850. end: '2020-10-02T00:00:00',
  851. },
  852. };
  853. expect(eventView.getEventsAPIPayload(location)).toEqual({
  854. ...output,
  855. start: '2020-10-01T00:00:00.000',
  856. end: '2020-10-02T00:00:00.000',
  857. });
  858. });
  859. });
  860. describe('EventView.getFacetsAPIPayload()', function() {
  861. it('only includes relevant query strings', function() {
  862. const eventView = new EventView({
  863. fields: generateFields(['title', 'count()']),
  864. sorts: generateSorts(['project', 'count']),
  865. query: 'event.type:csp',
  866. });
  867. const location = {
  868. query: {
  869. start: '',
  870. end: '',
  871. utc: 'true',
  872. statsPeriod: '14d',
  873. // irrelevant query strings
  874. bestCountry: 'canada',
  875. cursor: 'some cursor',
  876. sort: 'the world',
  877. project: '1234',
  878. environment: ['staging'],
  879. display: 'releases',
  880. },
  881. };
  882. expect(eventView.getFacetsAPIPayload(location)).toEqual({
  883. project: [],
  884. environment: [],
  885. utc: 'true',
  886. statsPeriod: '14d',
  887. query: 'event.type:csp',
  888. });
  889. });
  890. });
  891. describe('EventView.toNewQuery()', function() {
  892. const state = {
  893. id: '1234',
  894. name: 'best query',
  895. fields: [
  896. {field: 'count()', width: 123},
  897. {field: 'project.id', width: 456},
  898. ],
  899. sorts: generateSorts(['count']),
  900. query: 'event.type:error',
  901. project: [42],
  902. start: '2019-10-01T00:00:00',
  903. end: '2019-10-02T00:00:00',
  904. statsPeriod: '14d',
  905. environment: ['staging'],
  906. display: 'releases',
  907. };
  908. it('outputs the right fields', function() {
  909. const eventView = new EventView(state);
  910. const output = eventView.toNewQuery();
  911. const expected = {
  912. version: 2,
  913. id: '1234',
  914. name: 'best query',
  915. fields: ['count()', 'project.id'],
  916. widths: ['123', '456'],
  917. orderby: '-count',
  918. query: 'event.type:error',
  919. projects: [42],
  920. start: '2019-10-01T00:00:00',
  921. end: '2019-10-02T00:00:00',
  922. range: '14d',
  923. environment: ['staging'],
  924. display: 'releases',
  925. };
  926. expect(output).toEqual(expected);
  927. });
  928. it('omits query when query is an empty string', function() {
  929. const modifiedState = {
  930. ...state,
  931. };
  932. modifiedState.query = '';
  933. const eventView = new EventView(modifiedState);
  934. const output = eventView.toNewQuery();
  935. const expected = {
  936. version: 2,
  937. id: '1234',
  938. name: 'best query',
  939. fields: ['count()', 'project.id'],
  940. widths: ['123', '456'],
  941. orderby: '-count',
  942. projects: [42],
  943. start: '2019-10-01T00:00:00',
  944. end: '2019-10-02T00:00:00',
  945. range: '14d',
  946. environment: ['staging'],
  947. display: 'releases',
  948. };
  949. expect(output).toEqual(expected);
  950. });
  951. it('omits query when query is not defined', function() {
  952. const modifiedState = {
  953. ...state,
  954. };
  955. delete modifiedState.query;
  956. const eventView = new EventView(modifiedState);
  957. const output = eventView.toNewQuery();
  958. const expected = {
  959. version: 2,
  960. id: '1234',
  961. name: 'best query',
  962. fields: ['count()', 'project.id'],
  963. widths: ['123', '456'],
  964. orderby: '-count',
  965. projects: [42],
  966. start: '2019-10-01T00:00:00',
  967. end: '2019-10-02T00:00:00',
  968. range: '14d',
  969. environment: ['staging'],
  970. display: 'releases',
  971. };
  972. expect(output).toEqual(expected);
  973. });
  974. });
  975. describe('EventView.isValid()', function() {
  976. it('event view is valid when there is at least one field', function() {
  977. const eventView = new EventView({
  978. fields: [{field: 'count()'}, {field: 'project.id'}],
  979. sorts: [],
  980. project: [],
  981. });
  982. expect(eventView.isValid()).toBe(true);
  983. });
  984. it('event view is not valid when there are no fields', function() {
  985. const eventView = new EventView({
  986. fields: [],
  987. sorts: [],
  988. project: [],
  989. });
  990. expect(eventView.isValid()).toBe(false);
  991. });
  992. });
  993. describe('EventView.getFields()', function() {
  994. it('returns fields', function() {
  995. const eventView = new EventView({
  996. fields: [{field: 'count()'}, {field: 'project.id'}],
  997. sorts: [],
  998. project: [],
  999. });
  1000. expect(eventView.getFields()).toEqual(['count()', 'project.id']);
  1001. });
  1002. });
  1003. describe('EventView.numOfColumns()', function() {
  1004. it('returns correct number of columns', function() {
  1005. // has columns
  1006. const eventView = new EventView({
  1007. fields: [{field: 'count()'}, {field: 'project.id'}],
  1008. sorts: [],
  1009. project: [],
  1010. });
  1011. expect(eventView.numOfColumns()).toBe(2);
  1012. // has no columns
  1013. const eventView2 = new EventView({
  1014. fields: [],
  1015. sorts: [],
  1016. project: [],
  1017. });
  1018. expect(eventView2.numOfColumns()).toBe(0);
  1019. });
  1020. });
  1021. describe('EventView.getDays()', function() {
  1022. it('returns the right number of days for statsPeriod', function() {
  1023. const eventView = new EventView({
  1024. statsPeriod: '14d',
  1025. });
  1026. expect(eventView.getDays()).toBe(14);
  1027. const eventView2 = new EventView({
  1028. statsPeriod: '12h',
  1029. });
  1030. expect(eventView2.getDays()).toBe(0.5);
  1031. });
  1032. it('returns the right number of days for start/end', function() {
  1033. const eventView = new EventView({
  1034. start: '2019-10-01T00:00:00',
  1035. end: '2019-10-02T00:00:00',
  1036. });
  1037. expect(eventView.getDays()).toBe(1);
  1038. const eventView2 = new EventView({
  1039. start: '2019-10-01T00:00:00',
  1040. end: '2019-10-15T00:00:00',
  1041. });
  1042. expect(eventView2.getDays()).toBe(14);
  1043. });
  1044. });
  1045. describe('EventView.clone()', function() {
  1046. it('returns a unique instance', function() {
  1047. const state = {
  1048. id: '1234',
  1049. name: 'best query',
  1050. fields: [{field: 'count()'}, {field: 'project.id'}],
  1051. sorts: generateSorts(['count']),
  1052. query: 'event.type:error',
  1053. project: [42],
  1054. start: '2019-10-01T00:00:00',
  1055. end: '2019-10-02T00:00:00',
  1056. statsPeriod: '14d',
  1057. environment: ['staging'],
  1058. interval: '5m',
  1059. display: 'releases',
  1060. };
  1061. const eventView = new EventView(state);
  1062. const eventView2 = eventView.clone();
  1063. expect(eventView2 !== eventView).toBeTruthy();
  1064. expect(eventView).toMatchObject(state);
  1065. expect(eventView2).toMatchObject(state);
  1066. expect(eventView.isEqualTo(eventView2)).toBe(true);
  1067. });
  1068. });
  1069. describe('EventView.withColumns()', function() {
  1070. const state = {
  1071. id: '1234',
  1072. name: 'best query',
  1073. fields: [
  1074. {field: 'count()', width: 30},
  1075. {field: 'project.id', width: 99},
  1076. ],
  1077. sorts: generateSorts(['count']),
  1078. query: 'event.type:error',
  1079. project: [42],
  1080. start: '2019-10-01T00:00:00',
  1081. end: '2019-10-02T00:00:00',
  1082. statsPeriod: '14d',
  1083. environment: ['staging'],
  1084. };
  1085. const eventView = new EventView(state);
  1086. it('adds new columns, and replaces existing ones', function() {
  1087. const newView = eventView.withColumns([
  1088. {kind: 'field', field: 'title'},
  1089. {kind: 'function', function: ['count', '']},
  1090. {kind: 'field', field: 'project.id'},
  1091. {kind: 'field', field: 'culprit'},
  1092. ]);
  1093. // Views should be different.
  1094. expect(newView.isEqualTo(eventView)).toBe(false);
  1095. expect(newView.fields).toEqual([
  1096. {field: 'title', width: COL_WIDTH_UNDEFINED},
  1097. {field: 'count()', width: COL_WIDTH_UNDEFINED},
  1098. {field: 'project.id', width: COL_WIDTH_UNDEFINED},
  1099. {field: 'culprit', width: COL_WIDTH_UNDEFINED},
  1100. ]);
  1101. });
  1102. it('drops empty columns', function() {
  1103. const newView = eventView.withColumns([
  1104. {kind: 'field', field: 'issue'},
  1105. {kind: 'function', function: ['count', '']},
  1106. {kind: 'field', field: ''},
  1107. {kind: 'function', function: ['', '']},
  1108. {kind: 'function', function: ['', '', undefined]},
  1109. ]);
  1110. expect(newView.fields).toEqual([
  1111. {field: 'issue', width: COL_WIDTH_UNDEFINED},
  1112. {field: 'count()', width: COL_WIDTH_UNDEFINED},
  1113. ]);
  1114. });
  1115. it('inherits widths from existing columns when names match', function() {
  1116. const newView = eventView.withColumns([
  1117. {kind: 'function', function: ['count', '']},
  1118. {kind: 'field', field: 'project.id'},
  1119. {kind: 'field', field: 'title'},
  1120. {kind: 'field', field: 'time'},
  1121. ]);
  1122. expect(newView.fields).toEqual([
  1123. {field: 'count()', width: 30},
  1124. {field: 'project.id', width: 99},
  1125. {field: 'title', width: COL_WIDTH_UNDEFINED},
  1126. {field: 'time', width: COL_WIDTH_UNDEFINED},
  1127. ]);
  1128. });
  1129. it('retains sorts when sorted field is included', function() {
  1130. const newView = eventView.withColumns([
  1131. {kind: 'field', field: 'title'},
  1132. {kind: 'function', function: ['count', '']},
  1133. ]);
  1134. expect(newView.fields).toEqual([
  1135. {field: 'title', width: COL_WIDTH_UNDEFINED},
  1136. {field: 'count()', width: COL_WIDTH_UNDEFINED},
  1137. ]);
  1138. expect(newView.sorts).toEqual([{field: 'count', kind: 'desc'}]);
  1139. });
  1140. it('updates sorts when sorted field is removed', function() {
  1141. const newView = eventView.withColumns([{kind: 'field', field: 'title'}]);
  1142. expect(newView.fields).toEqual([{field: 'title', width: COL_WIDTH_UNDEFINED}]);
  1143. // Should pick a sortable field.
  1144. expect(newView.sorts).toEqual([{field: 'title', kind: 'desc'}]);
  1145. });
  1146. it('has no sort if no sortable fields remain', function() {
  1147. const newView = eventView.withColumns([{kind: 'field', field: 'issue'}]);
  1148. expect(newView.fields).toEqual([{field: 'issue', width: COL_WIDTH_UNDEFINED}]);
  1149. expect(newView.sorts).toEqual([]);
  1150. });
  1151. });
  1152. describe('EventView.withNewColumn()', function() {
  1153. const state = {
  1154. id: '1234',
  1155. name: 'best query',
  1156. fields: [
  1157. {field: 'count()', width: 30},
  1158. {field: 'project.id', width: 99},
  1159. ],
  1160. sorts: generateSorts(['count']),
  1161. query: 'event.type:error',
  1162. project: [42],
  1163. start: '2019-10-01T00:00:00',
  1164. end: '2019-10-02T00:00:00',
  1165. statsPeriod: '14d',
  1166. environment: ['staging'],
  1167. };
  1168. it('adds a field', function() {
  1169. const eventView = new EventView(state);
  1170. const newColumn = {
  1171. kind: 'field',
  1172. field: 'title',
  1173. };
  1174. const eventView2 = eventView.withNewColumn(newColumn);
  1175. expect(eventView2 !== eventView).toBeTruthy();
  1176. expect(eventView).toMatchObject(state);
  1177. const nextState = {
  1178. ...state,
  1179. fields: [...state.fields, {field: 'title'}],
  1180. };
  1181. expect(eventView2).toMatchObject(nextState);
  1182. });
  1183. it('adds an aggregate function with no arguments', function() {
  1184. const eventView = new EventView(state);
  1185. const newColumn = {
  1186. kind: 'function',
  1187. function: ['count', ''],
  1188. };
  1189. const eventView2 = eventView.withNewColumn(newColumn);
  1190. expect(eventView2 !== eventView).toBeTruthy();
  1191. expect(eventView).toMatchObject(state);
  1192. const nextState = {
  1193. ...state,
  1194. fields: [...state.fields, {field: 'count()'}],
  1195. };
  1196. expect(eventView2).toMatchObject(nextState);
  1197. });
  1198. it('add an aggregate function with field', function() {
  1199. const eventView = new EventView(state);
  1200. const newColumn = {
  1201. kind: 'function',
  1202. function: ['avg', 'transaction.duration'],
  1203. };
  1204. const eventView2 = eventView.withNewColumn(newColumn);
  1205. expect(eventView2 !== eventView).toBeTruthy();
  1206. expect(eventView).toMatchObject(state);
  1207. const nextState = {
  1208. ...state,
  1209. fields: [...state.fields, {field: 'avg(transaction.duration)'}],
  1210. };
  1211. expect(eventView2).toMatchObject(nextState);
  1212. });
  1213. it('add an aggregate function with field & refinement', function() {
  1214. const eventView = new EventView(state);
  1215. const newColumn = {
  1216. kind: 'function',
  1217. function: ['percentile', 'transaction.duration', '0.5'],
  1218. };
  1219. const updated = eventView.withNewColumn(newColumn);
  1220. expect(updated.fields).toEqual([
  1221. ...state.fields,
  1222. {field: 'percentile(transaction.duration,0.5)', width: COL_WIDTH_UNDEFINED},
  1223. ]);
  1224. });
  1225. });
  1226. describe('EventView.withResizedColumn()', function() {
  1227. const state = {
  1228. id: '1234',
  1229. name: 'best query',
  1230. fields: [{field: 'count()'}, {field: 'project.id'}],
  1231. sorts: generateSorts(['count']),
  1232. query: 'event.type:error',
  1233. project: [42],
  1234. start: '2019-10-01T00:00:00',
  1235. end: '2019-10-02T00:00:00',
  1236. statsPeriod: '14d',
  1237. environment: ['staging'],
  1238. };
  1239. const view = new EventView(state);
  1240. it('updates a column that exists', function() {
  1241. const newView = view.withResizedColumn(0, 99);
  1242. expect(view.fields[0].width).toBeUndefined();
  1243. expect(newView.fields[0].width).toEqual(99);
  1244. });
  1245. it('ignores columns that do not exist', function() {
  1246. const newView = view.withResizedColumn(100, 99);
  1247. expect(view.fields).toEqual(newView.fields);
  1248. });
  1249. });
  1250. describe('EventView.withUpdatedColumn()', function() {
  1251. const state = {
  1252. id: '1234',
  1253. name: 'best query',
  1254. fields: [{field: 'count()'}, {field: 'project.id'}],
  1255. sorts: generateSorts(['count']),
  1256. query: 'event.type:error',
  1257. project: [42],
  1258. start: '2019-10-01T00:00:00',
  1259. end: '2019-10-02T00:00:00',
  1260. statsPeriod: '14d',
  1261. environment: ['staging'],
  1262. };
  1263. const meta = {
  1264. count: 'integer',
  1265. title: 'string',
  1266. };
  1267. it('update a column with no changes', function() {
  1268. const eventView = new EventView(state);
  1269. const newColumn = {
  1270. kind: 'function',
  1271. function: ['count', ''],
  1272. };
  1273. const eventView2 = eventView.withUpdatedColumn(0, newColumn, meta);
  1274. expect(eventView2 === eventView).toBeTruthy();
  1275. expect(eventView).toMatchObject(state);
  1276. });
  1277. it('update a column to a field', function() {
  1278. const eventView = new EventView(state);
  1279. const newColumn = {
  1280. kind: 'field',
  1281. field: 'title',
  1282. };
  1283. const eventView2 = eventView.withUpdatedColumn(1, newColumn, meta);
  1284. expect(eventView2 !== eventView).toBeTruthy();
  1285. expect(eventView).toMatchObject(state);
  1286. const nextState = {
  1287. ...state,
  1288. fields: [state.fields[0], {field: 'title'}],
  1289. };
  1290. expect(eventView2).toMatchObject(nextState);
  1291. });
  1292. it('update a column to an aggregate function with no arguments', function() {
  1293. const eventView = new EventView(state);
  1294. const newColumn = {
  1295. kind: 'function',
  1296. function: ['count', ''],
  1297. };
  1298. const eventView2 = eventView.withUpdatedColumn(1, newColumn, meta);
  1299. expect(eventView2 !== eventView).toBeTruthy();
  1300. expect(eventView).toMatchObject(state);
  1301. const nextState = {
  1302. ...state,
  1303. fields: [state.fields[0], {field: 'count()'}],
  1304. };
  1305. expect(eventView2).toMatchObject(nextState);
  1306. });
  1307. it('update a column to an aggregate function with field', function() {
  1308. const eventView = new EventView(state);
  1309. const newColumn = {
  1310. kind: 'function',
  1311. function: ['avg', 'transaction.duration'],
  1312. };
  1313. const eventView2 = eventView.withUpdatedColumn(1, newColumn, meta);
  1314. expect(eventView2 !== eventView).toBeTruthy();
  1315. expect(eventView).toMatchObject(state);
  1316. const nextState = {
  1317. ...state,
  1318. fields: [state.fields[0], {field: 'avg(transaction.duration)'}],
  1319. };
  1320. expect(eventView2).toMatchObject(nextState);
  1321. });
  1322. it('update a column to an aggregate function with field & refinement', function() {
  1323. const eventView = new EventView(state);
  1324. const newColumn = {
  1325. kind: 'function',
  1326. function: ['percentile', 'transaction.duration', '0.5'],
  1327. };
  1328. const newView = eventView.withUpdatedColumn(1, newColumn, meta);
  1329. expect(newView.fields).toEqual([
  1330. state.fields[0],
  1331. {field: 'percentile(transaction.duration,0.5)', width: COL_WIDTH_UNDEFINED},
  1332. ]);
  1333. });
  1334. describe('update a column that is sorted', function() {
  1335. it('the sorted column is the only sorted column', function() {
  1336. const eventView = new EventView(state);
  1337. const newColumn = {
  1338. kind: 'field',
  1339. field: 'title',
  1340. };
  1341. const eventView2 = eventView.withUpdatedColumn(0, newColumn, meta);
  1342. expect(eventView2 !== eventView).toBeTruthy();
  1343. expect(eventView).toMatchObject(state);
  1344. const nextState = {
  1345. ...state,
  1346. sorts: [{field: 'title', kind: 'desc'}],
  1347. fields: [{field: 'title'}, state.fields[1]],
  1348. };
  1349. expect(eventView2).toMatchObject(nextState);
  1350. });
  1351. it('the sorted column occurs at least twice', function() {
  1352. const modifiedState = {
  1353. ...state,
  1354. fields: [...state.fields, {field: 'count()'}],
  1355. };
  1356. const eventView = new EventView(modifiedState);
  1357. const newColumn = {
  1358. kind: 'field',
  1359. field: 'title',
  1360. };
  1361. const eventView2 = eventView.withUpdatedColumn(0, newColumn, meta);
  1362. expect(eventView2 !== eventView).toBeTruthy();
  1363. expect(eventView).toMatchObject(modifiedState);
  1364. const nextState = {
  1365. ...state,
  1366. fields: [{field: 'title'}, state.fields[1], {field: 'count()'}],
  1367. };
  1368. expect(eventView2).toMatchObject(nextState);
  1369. });
  1370. it('using no provided table meta', function() {
  1371. // table meta may not be provided in the invalid query state;
  1372. // we will still want to be able to update columns
  1373. const eventView = new EventView(state);
  1374. const expected = {
  1375. ...state,
  1376. sorts: [{field: 'title', kind: 'desc'}],
  1377. fields: [{field: 'title'}, state.fields[1]],
  1378. };
  1379. const newColumn = {
  1380. kind: 'field',
  1381. field: 'title',
  1382. };
  1383. const eventView2 = eventView.withUpdatedColumn(0, newColumn, {});
  1384. expect(eventView2).toMatchObject(expected);
  1385. const eventView3 = eventView.withUpdatedColumn(0, newColumn);
  1386. expect(eventView3).toMatchObject(expected);
  1387. const eventView4 = eventView.withUpdatedColumn(0, newColumn, null);
  1388. expect(eventView4).toMatchObject(expected);
  1389. });
  1390. });
  1391. describe('update a column to a non-sortable column', function() {
  1392. it('default to a sortable column', function() {
  1393. const modifiedState = {
  1394. ...state,
  1395. fields: [{field: 'count()'}, {field: 'title'}],
  1396. };
  1397. const eventView = new EventView(modifiedState);
  1398. // this column is expected to be non-sortable
  1399. const newColumn = {
  1400. kind: 'field',
  1401. field: 'project.id',
  1402. };
  1403. const eventView2 = eventView.withUpdatedColumn(0, newColumn, meta);
  1404. expect(eventView2 !== eventView).toBeTruthy();
  1405. expect(eventView).toMatchObject(modifiedState);
  1406. const nextState = {
  1407. ...state,
  1408. sorts: [{field: 'title', kind: 'desc'}],
  1409. fields: [{field: 'project.id'}, {field: 'title'}],
  1410. };
  1411. expect(eventView2).toMatchObject(nextState);
  1412. });
  1413. it('has no sort if there are no sortable columns', function() {
  1414. const modifiedState = {
  1415. ...state,
  1416. fields: [{field: 'count()'}],
  1417. };
  1418. const eventView = new EventView(modifiedState);
  1419. // this column is expected to be non-sortable
  1420. const newColumn = {
  1421. kind: 'field',
  1422. field: 'project.id',
  1423. };
  1424. const eventView2 = eventView.withUpdatedColumn(0, newColumn, meta);
  1425. expect(eventView2 !== eventView).toBeTruthy();
  1426. expect(eventView).toMatchObject(modifiedState);
  1427. const nextState = {
  1428. ...state,
  1429. sorts: [],
  1430. fields: [{field: 'project.id'}],
  1431. };
  1432. expect(eventView2).toMatchObject(nextState);
  1433. });
  1434. });
  1435. });
  1436. describe('EventView.withDeletedColumn()', function() {
  1437. const state = {
  1438. id: '1234',
  1439. name: 'best query',
  1440. fields: [{field: 'count()'}, {field: 'project.id'}],
  1441. sorts: generateSorts(['count']),
  1442. query: 'event.type:error',
  1443. project: [42],
  1444. start: '2019-10-01T00:00:00',
  1445. end: '2019-10-02T00:00:00',
  1446. statsPeriod: '14d',
  1447. environment: ['staging'],
  1448. };
  1449. const meta = {
  1450. count: 'integer',
  1451. title: 'string',
  1452. };
  1453. it('returns itself when attempting to delete the last remaining column', function() {
  1454. const modifiedState = {
  1455. ...state,
  1456. fields: [{field: 'count()'}],
  1457. };
  1458. const eventView = new EventView(modifiedState);
  1459. const eventView2 = eventView.withDeletedColumn(0, meta);
  1460. expect(eventView2 === eventView).toBeTruthy();
  1461. expect(eventView).toMatchObject(modifiedState);
  1462. });
  1463. describe('deletes column, and use any remaining sortable column', function() {
  1464. it('using no provided table meta', function() {
  1465. // table meta may not be provided in the invalid query state;
  1466. // we will still want to be able to delete columns
  1467. const state2 = {
  1468. ...state,
  1469. fields: [{field: 'title'}, {field: 'timestamp'}, {field: 'count()'}],
  1470. sorts: generateSorts(['timestamp']),
  1471. };
  1472. const eventView = new EventView(state2);
  1473. const expected = {
  1474. ...state,
  1475. sorts: generateSorts(['title']),
  1476. fields: [{field: 'title'}, {field: 'count()'}],
  1477. };
  1478. const eventView2 = eventView.withDeletedColumn(1, {});
  1479. expect(eventView2).toMatchObject(expected);
  1480. const eventView3 = eventView.withDeletedColumn(1);
  1481. expect(eventView3).toMatchObject(expected);
  1482. const eventView4 = eventView.withDeletedColumn(1, null);
  1483. expect(eventView4).toMatchObject(expected);
  1484. });
  1485. it('has no remaining sortable column', function() {
  1486. const eventView = new EventView(state);
  1487. const eventView2 = eventView.withDeletedColumn(0, meta);
  1488. expect(eventView2 !== eventView).toBeTruthy();
  1489. expect(eventView).toMatchObject(state);
  1490. const nextState = {
  1491. ...state,
  1492. // we expect sorts to be empty since project.id is non-sortable
  1493. sorts: [],
  1494. fields: [state.fields[1]],
  1495. };
  1496. expect(eventView2).toMatchObject(nextState);
  1497. });
  1498. it('has a remaining sortable column', function() {
  1499. const modifiedState = {
  1500. ...state,
  1501. fields: [{field: 'count()'}, {field: 'project.id'}, {field: 'title'}],
  1502. };
  1503. const eventView = new EventView(modifiedState);
  1504. const eventView2 = eventView.withDeletedColumn(0, meta);
  1505. expect(eventView2 !== eventView).toBeTruthy();
  1506. expect(eventView).toMatchObject(modifiedState);
  1507. const nextState = {
  1508. ...state,
  1509. sorts: [{field: 'title', kind: 'desc'}],
  1510. fields: [{field: 'project.id'}, {field: 'title'}],
  1511. };
  1512. expect(eventView2).toMatchObject(nextState);
  1513. });
  1514. it('sorted column occurs at least twice', function() {
  1515. const modifiedState = {
  1516. ...state,
  1517. fields: [...state.fields, state.fields[0]],
  1518. };
  1519. const eventView = new EventView(modifiedState);
  1520. const eventView2 = eventView.withDeletedColumn(0, meta);
  1521. expect(eventView2 !== eventView).toBeTruthy();
  1522. expect(eventView).toMatchObject(modifiedState);
  1523. const nextState = {
  1524. ...state,
  1525. fields: [state.fields[1], state.fields[0]],
  1526. };
  1527. expect(eventView2).toMatchObject(nextState);
  1528. });
  1529. it('ensures there is at one auto-width column on deletion', function() {
  1530. const modifiedState = {
  1531. ...state,
  1532. fields: [
  1533. {field: 'id', width: 75},
  1534. {field: 'title', width: 100},
  1535. {field: 'project', width: 80},
  1536. {field: 'environment', width: 99},
  1537. ],
  1538. };
  1539. const eventView = new EventView(modifiedState);
  1540. let updated = eventView.withDeletedColumn(0, meta);
  1541. let updatedFields = [
  1542. {field: 'title', width: -1},
  1543. {field: 'project', width: 80},
  1544. {field: 'environment', width: 99},
  1545. ];
  1546. expect(updated.fields).toEqual(updatedFields);
  1547. updated = updated.withDeletedColumn(0, meta);
  1548. updatedFields = [
  1549. {field: 'project', width: -1},
  1550. {field: 'environment', width: 99},
  1551. ];
  1552. expect(updated.fields).toEqual(updatedFields);
  1553. });
  1554. });
  1555. });
  1556. describe('EventView.getSorts()', function() {
  1557. it('returns fields', function() {
  1558. const eventView = new EventView({
  1559. fields: [{field: 'count()'}, {field: 'project.id'}],
  1560. sorts: generateSorts(['count']),
  1561. project: [],
  1562. });
  1563. expect(eventView.getSorts()).toEqual([
  1564. {
  1565. key: 'count',
  1566. order: 'desc',
  1567. },
  1568. ]);
  1569. });
  1570. });
  1571. describe('EventView.getQuery()', function() {
  1572. it('with query', function() {
  1573. const eventView = new EventView({
  1574. fields: [],
  1575. sorts: [],
  1576. project: [],
  1577. query: 'event.type:error',
  1578. });
  1579. expect(eventView.getQuery()).toEqual('event.type:error');
  1580. expect(eventView.getQuery(null)).toEqual('event.type:error');
  1581. expect(eventView.getQuery('hello')).toEqual('event.type:error hello');
  1582. expect(eventView.getQuery(['event.type:error', 'hello'])).toEqual(
  1583. 'event.type:error hello'
  1584. );
  1585. });
  1586. it('without query', function() {
  1587. const eventView = new EventView({
  1588. fields: [],
  1589. sorts: [],
  1590. project: [],
  1591. });
  1592. expect(eventView.getQuery()).toEqual('');
  1593. expect(eventView.getQuery(null)).toEqual('');
  1594. expect(eventView.getQuery('hello')).toEqual('hello');
  1595. expect(eventView.getQuery(['event.type:error', 'hello'])).toEqual(
  1596. 'event.type:error hello'
  1597. );
  1598. });
  1599. });
  1600. describe('EventView.sortForField()', function() {
  1601. const state = {
  1602. id: '1234',
  1603. name: 'best query',
  1604. fields: [{field: 'count()'}, {field: 'project.id'}],
  1605. sorts: generateSorts(['count']),
  1606. query: 'event.type:error',
  1607. project: [42],
  1608. start: '2019-10-01T00:00:00',
  1609. end: '2019-10-02T00:00:00',
  1610. statsPeriod: '14d',
  1611. environment: ['staging'],
  1612. };
  1613. const eventView = new EventView(state);
  1614. const meta = {count: 'integer'};
  1615. it('returns the sort when selected field is sorted', function() {
  1616. const field = {
  1617. field: 'count()',
  1618. };
  1619. const actual = eventView.sortForField(field, meta);
  1620. expect(actual).toEqual({
  1621. field: 'count',
  1622. kind: 'desc',
  1623. });
  1624. });
  1625. it('returns undefined when selected field is not sorted', function() {
  1626. const field = {
  1627. field: 'project.id',
  1628. };
  1629. expect(eventView.sortForField(field, meta)).toBeUndefined();
  1630. });
  1631. it('returns undefined when no meta is provided', function() {
  1632. const field = {
  1633. field: 'project.id',
  1634. };
  1635. expect(eventView.sortForField(field, undefined)).toBeUndefined();
  1636. });
  1637. });
  1638. describe('EventView.sortOnField()', function() {
  1639. const state = {
  1640. id: '1234',
  1641. name: 'best query',
  1642. fields: [{field: 'count()'}, {field: 'project.id'}],
  1643. sorts: generateSorts(['count']),
  1644. query: 'event.type:error',
  1645. project: [42],
  1646. start: '2019-10-01T00:00:00',
  1647. end: '2019-10-02T00:00:00',
  1648. statsPeriod: '14d',
  1649. environment: ['staging'],
  1650. };
  1651. const meta = {count: 'integer', title: 'string'};
  1652. it('returns itself when attempting to sort on un-sortable field', function() {
  1653. const eventView = new EventView(state);
  1654. expect(eventView).toMatchObject(state);
  1655. const field = state.fields[1];
  1656. const eventView2 = eventView.sortOnField(field, meta);
  1657. expect(eventView2 === eventView).toBe(true);
  1658. });
  1659. it('reverses the sorted field', function() {
  1660. const eventView = new EventView(state);
  1661. expect(eventView).toMatchObject(state);
  1662. const field = state.fields[0];
  1663. const eventView2 = eventView.sortOnField(field, meta);
  1664. expect(eventView2 !== eventView).toBe(true);
  1665. const nextState = {
  1666. ...state,
  1667. sorts: [{field: 'count', kind: 'asc'}],
  1668. };
  1669. expect(eventView2).toMatchObject(nextState);
  1670. });
  1671. it('enforce sort order on sorted field', function() {
  1672. const eventView = new EventView(state);
  1673. expect(eventView).toMatchObject(state);
  1674. const field = state.fields[0];
  1675. const eventView2 = eventView.sortOnField(field, meta, 'asc');
  1676. expect(eventView2).toMatchObject({
  1677. ...state,
  1678. sorts: [{field: 'count', kind: 'asc'}],
  1679. });
  1680. const eventView3 = eventView.sortOnField(field, meta, 'desc');
  1681. expect(eventView3).toMatchObject({
  1682. ...state,
  1683. sorts: [{field: 'count', kind: 'desc'}],
  1684. });
  1685. });
  1686. it('sort on new field', function() {
  1687. const modifiedState = {
  1688. ...state,
  1689. fields: [...state.fields, {field: 'title'}],
  1690. };
  1691. const eventView = new EventView(modifiedState);
  1692. expect(eventView).toMatchObject(modifiedState);
  1693. const field = modifiedState.fields[2];
  1694. const eventView2 = eventView.sortOnField(field, meta);
  1695. expect(eventView2 !== eventView).toBe(true);
  1696. const nextState = {
  1697. ...modifiedState,
  1698. sorts: [{field: 'title', kind: 'desc'}],
  1699. };
  1700. expect(eventView2).toMatchObject(nextState);
  1701. // enforce asc sort order
  1702. const eventView3 = eventView.sortOnField(field, meta, 'asc');
  1703. expect(eventView3).toMatchObject({
  1704. ...modifiedState,
  1705. sorts: [{field: 'title', kind: 'asc'}],
  1706. });
  1707. // enforce desc sort order
  1708. const eventView4 = eventView.sortOnField(field, meta, 'desc');
  1709. expect(eventView4).toMatchObject({
  1710. ...modifiedState,
  1711. sorts: [{field: 'title', kind: 'desc'}],
  1712. });
  1713. });
  1714. });
  1715. describe('EventView.withSorts()', function() {
  1716. it('returns a clone', function() {
  1717. const eventView = new EventView({
  1718. fields: [{field: 'event.type'}],
  1719. });
  1720. const updated = eventView.withSorts([{kind: 'desc', field: 'event.type'}]);
  1721. expect(updated.sorts).not.toEqual(eventView.sorts);
  1722. });
  1723. it('only accepts sorting on fields in the view', function() {
  1724. const eventView = new EventView({
  1725. fields: [{field: 'event.type'}],
  1726. });
  1727. const updated = eventView.withSorts([
  1728. {kind: 'desc', field: 'event.type'},
  1729. {kind: 'asc', field: 'unknown'},
  1730. ]);
  1731. expect(updated.sorts).toEqual([{kind: 'desc', field: 'event.type'}]);
  1732. });
  1733. it('accepts aggregate field sorts', function() {
  1734. const eventView = new EventView({
  1735. fields: [{field: 'p50()'}],
  1736. });
  1737. const updated = eventView.withSorts([
  1738. {kind: 'desc', field: 'p50'},
  1739. {kind: 'asc', field: 'unknown'},
  1740. ]);
  1741. expect(updated.sorts).toEqual([{kind: 'desc', field: 'p50'}]);
  1742. });
  1743. });
  1744. describe('EventView.isEqualTo()', function() {
  1745. it('should be true when equal', function() {
  1746. const state = {
  1747. id: '1234',
  1748. name: 'best query',
  1749. fields: [{field: 'count()'}, {field: 'project.id'}],
  1750. sorts: generateSorts(['count']),
  1751. query: 'event.type:error',
  1752. project: [42],
  1753. start: '2019-10-01T00:00:00',
  1754. end: '2019-10-02T00:00:00',
  1755. statsPeriod: '14d',
  1756. environment: ['staging'],
  1757. yAxis: 'fam',
  1758. display: 'releases',
  1759. };
  1760. const eventView = new EventView(state);
  1761. const eventView2 = new EventView(state);
  1762. expect(eventView2 !== eventView).toBeTruthy();
  1763. expect(eventView).toMatchObject(state);
  1764. expect(eventView2).toMatchObject(state);
  1765. expect(eventView.isEqualTo(eventView2)).toBe(true);
  1766. // commutativity property holds
  1767. expect(eventView2.isEqualTo(eventView)).toBe(true);
  1768. });
  1769. it('should be true when datetime are equal but differ in format', function() {
  1770. const state = {
  1771. id: '1234',
  1772. name: 'best query',
  1773. fields: [{field: 'count()'}, {field: 'project.id'}],
  1774. sorts: generateSorts(['count']),
  1775. query: 'event.type:error',
  1776. project: [42],
  1777. start: '2019-10-20T21:02:51+0000',
  1778. end: '2019-10-23T19:27:04+0000',
  1779. environment: ['staging'],
  1780. };
  1781. const eventView = new EventView(state);
  1782. const eventView2 = new EventView({
  1783. ...state,
  1784. start: '2019-10-20T21:02:51Z',
  1785. end: '2019-10-23T19:27:04Z',
  1786. });
  1787. expect(eventView.isEqualTo(eventView2)).toBe(true);
  1788. });
  1789. it('should be false when not equal', function() {
  1790. const state = {
  1791. id: '1234',
  1792. name: 'best query',
  1793. fields: [{field: 'count()'}, {field: 'project.id'}],
  1794. sorts: generateSorts(['count']),
  1795. query: 'event.type:error',
  1796. project: [42],
  1797. start: '2019-10-01T00:00:00',
  1798. end: '2019-10-02T00:00:00',
  1799. statsPeriod: '14d',
  1800. environment: ['staging'],
  1801. yAxis: 'fam',
  1802. display: 'releases',
  1803. };
  1804. const differences = {
  1805. id: '12',
  1806. name: 'new query',
  1807. fields: [{field: 'project.id'}, {field: 'count()'}],
  1808. sorts: [{field: 'count', kind: 'asc'}],
  1809. query: 'event.type:transaction',
  1810. project: [24],
  1811. start: '2019-09-01T00:00:00',
  1812. end: '2020-09-01T00:00:00',
  1813. statsPeriod: '24d',
  1814. environment: [],
  1815. yAxis: 'ok boomer',
  1816. display: 'previous',
  1817. };
  1818. const eventView = new EventView(state);
  1819. for (const key in differences) {
  1820. const eventView2 = new EventView({...state, [key]: differences[key]});
  1821. expect(eventView.isEqualTo(eventView2)).toBe(false);
  1822. }
  1823. });
  1824. });
  1825. describe('EventView.getResultsViewUrlTarget()', function() {
  1826. const state = {
  1827. id: '1234',
  1828. name: 'best query',
  1829. fields: [{field: 'count()'}, {field: 'project.id'}],
  1830. sorts: generateSorts(['count']),
  1831. query: 'event.type:error',
  1832. project: [42],
  1833. start: '2019-10-01T00:00:00',
  1834. end: '2019-10-02T00:00:00',
  1835. statsPeriod: '14d',
  1836. environment: ['staging'],
  1837. display: 'previous',
  1838. };
  1839. const organization = TestStubs.Organization();
  1840. it('generates a URL', function() {
  1841. const view = new EventView(state);
  1842. const result = view.getResultsViewUrlTarget(organization.slug);
  1843. expect(result.pathname).toEqual('/organizations/org-slug/discover/results/');
  1844. expect(result.query.query).toEqual(state.query);
  1845. expect(result.query.project).toEqual(state.project);
  1846. expect(result.query.display).toEqual(state.display);
  1847. });
  1848. });
  1849. describe('EventView.getGlobalSelection()', function() {
  1850. it('return default global selection', function() {
  1851. const eventView = new EventView({});
  1852. expect(eventView.getGlobalSelection()).toMatchObject({
  1853. projects: [],
  1854. environments: [],
  1855. datetime: {
  1856. start: null,
  1857. end: null,
  1858. period: '',
  1859. // event views currently do not support the utc option,
  1860. // see comment in EventView.getGlobalSelection
  1861. utc: true,
  1862. },
  1863. });
  1864. });
  1865. it('returns global selection query', function() {
  1866. const state2 = {
  1867. project: [42],
  1868. start: 'start',
  1869. end: 'end',
  1870. statsPeriod: '42d',
  1871. environment: ['prod'],
  1872. };
  1873. const eventView = new EventView(state2);
  1874. expect(eventView.getGlobalSelection()).toMatchObject({
  1875. projects: state2.project,
  1876. environments: state2.environment,
  1877. datetime: {
  1878. start: state2.start,
  1879. end: state2.end,
  1880. period: state2.statsPeriod,
  1881. // event views currently do not support the utc option,
  1882. // see comment in EventView.getGlobalSelection
  1883. utc: true,
  1884. },
  1885. });
  1886. });
  1887. });
  1888. describe('EventView.getGlobalSelectionQuery()', function() {
  1889. it('return default global selection query', function() {
  1890. const eventView = new EventView({});
  1891. expect(eventView.getGlobalSelectionQuery()).toMatchObject({
  1892. project: [],
  1893. start: undefined,
  1894. end: undefined,
  1895. statsPeriod: undefined,
  1896. environment: [],
  1897. // event views currently do not support the utc option,
  1898. // see comment in EventView.getGlobalSelection
  1899. utc: 'true',
  1900. });
  1901. });
  1902. it('returns global selection query', function() {
  1903. const state2 = {
  1904. project: [42],
  1905. start: 'start',
  1906. end: 'end',
  1907. statsPeriod: '42d',
  1908. environment: ['prod'],
  1909. };
  1910. const eventView = new EventView(state2);
  1911. expect(eventView.getGlobalSelectionQuery()).toMatchObject({
  1912. ...state2,
  1913. // when generating the query, it converts numbers to strings
  1914. project: ['42'],
  1915. // event views currently do not support the utc option,
  1916. // see comment in EventView.getGlobalSelection
  1917. utc: 'true',
  1918. });
  1919. });
  1920. });
  1921. describe('EventView.generateBlankQueryStringObject()', function() {
  1922. it('should return blank values', function() {
  1923. const eventView = new EventView({});
  1924. expect(eventView.generateBlankQueryStringObject()).toEqual({
  1925. id: undefined,
  1926. name: undefined,
  1927. fields: undefined,
  1928. sorts: undefined,
  1929. query: undefined,
  1930. project: undefined,
  1931. start: undefined,
  1932. end: undefined,
  1933. statsPeriod: undefined,
  1934. environment: undefined,
  1935. yAxis: undefined,
  1936. cursor: undefined,
  1937. });
  1938. });
  1939. });
  1940. describe('EventView.getYAxisOptions()', function() {
  1941. const state = {
  1942. fields: [],
  1943. sorts: [],
  1944. query: '',
  1945. project: [],
  1946. statsPeriod: '42d',
  1947. environment: [],
  1948. };
  1949. function generateYaxis(value) {
  1950. return {
  1951. value,
  1952. label: value,
  1953. };
  1954. }
  1955. it('should return default options', function() {
  1956. const thisEventView = new EventView(state);
  1957. expect(thisEventView.getYAxisOptions()).toEqual(CHART_AXIS_OPTIONS);
  1958. });
  1959. it('should add aggregate fields as options', function() {
  1960. let thisEventView = new EventView({
  1961. ...state,
  1962. fields: generateFields(['ignored-field', 'count_unique(issue)']),
  1963. });
  1964. expect(thisEventView.getYAxisOptions()).toEqual([
  1965. generateYaxis('count_unique(issue)'),
  1966. ...CHART_AXIS_OPTIONS,
  1967. ]);
  1968. // should de-duplicate entries
  1969. thisEventView = new EventView({
  1970. ...state,
  1971. fields: generateFields(['ignored-field', 'count()']),
  1972. });
  1973. expect(thisEventView.getYAxisOptions()).toEqual([...CHART_AXIS_OPTIONS]);
  1974. });
  1975. it('should exclude yAxis options that are not useful', function() {
  1976. const thisEventView = new EventView({
  1977. ...state,
  1978. fields: generateFields([
  1979. 'ignored-field',
  1980. 'count_unique(issue)',
  1981. 'last_seen()',
  1982. 'latest_event()',
  1983. ]),
  1984. });
  1985. expect(thisEventView.getYAxisOptions()).toEqual([
  1986. generateYaxis('count_unique(issue)'),
  1987. ...CHART_AXIS_OPTIONS,
  1988. ]);
  1989. });
  1990. });
  1991. describe('EventView.getYAxis()', function() {
  1992. const state = {
  1993. fields: [],
  1994. sorts: [],
  1995. query: '',
  1996. project: [],
  1997. statsPeriod: '42d',
  1998. environment: [],
  1999. };
  2000. it('should return first default yAxis', function() {
  2001. const thisEventView = new EventView(state);
  2002. expect(thisEventView.getYAxis()).toEqual('count()');
  2003. });
  2004. it('should return valid yAxis', function() {
  2005. const thisEventView = new EventView({
  2006. ...state,
  2007. fields: generateFields([
  2008. 'ignored-field',
  2009. 'count_unique(user)',
  2010. 'last_seen',
  2011. 'latest_event',
  2012. ]),
  2013. yAxis: 'count_unique(user)',
  2014. });
  2015. expect(thisEventView.getYAxis()).toEqual('count_unique(user)');
  2016. });
  2017. it('should ignore invalid yAxis', function() {
  2018. const invalid = [
  2019. 'last_seen',
  2020. 'latest_event',
  2021. 'count_unique(issue)', // this is not one of the selected fields
  2022. ];
  2023. for (const option of invalid) {
  2024. const thisEventView = new EventView({
  2025. ...state,
  2026. fields: generateFields(['ignored-field', 'last_seen', 'latest_event']),
  2027. yAxis: option,
  2028. });
  2029. // yAxis defaults to the first entry of the default yAxis options
  2030. expect(thisEventView.getYAxis()).toEqual('count()');
  2031. }
  2032. });
  2033. });
  2034. describe('EventView.getDisplayOptions()', function() {
  2035. const state = {
  2036. fields: [],
  2037. sorts: [],
  2038. query: '',
  2039. project: [],
  2040. statsPeriod: '42d',
  2041. environment: [],
  2042. };
  2043. it('should return default options', function() {
  2044. const eventView = new EventView({
  2045. ...state,
  2046. // there needs to exist an aggregate or TOP 5 modes will be disabled
  2047. fields: [{field: 'count()'}],
  2048. });
  2049. expect(eventView.getDisplayOptions()).toEqual(DISPLAY_MODE_OPTIONS);
  2050. });
  2051. it('should disable previous when start/end are used.', function() {
  2052. const eventView = new EventView({
  2053. ...state,
  2054. end: '2020-04-13T12:13:14',
  2055. start: '2020-04-01T12:13:14',
  2056. });
  2057. const options = eventView.getDisplayOptions();
  2058. expect(options[1].value).toEqual('previous');
  2059. expect(options[1].disabled).toBeTruthy();
  2060. });
  2061. it('should disable top 5 period/daily if no aggregates present', function() {
  2062. const eventView = new EventView({
  2063. ...state,
  2064. });
  2065. const options = eventView.getDisplayOptions();
  2066. expect(options[2].value).toEqual('top5');
  2067. expect(options[2].disabled).toBeTruthy();
  2068. expect(options[4].value).toEqual('dailytop5');
  2069. expect(options[4].disabled).toBeTruthy();
  2070. });
  2071. });
  2072. describe('EventView.getDisplayMode()', function() {
  2073. const state = {
  2074. fields: [],
  2075. sorts: [],
  2076. query: '',
  2077. project: [],
  2078. statsPeriod: '42d',
  2079. environment: [],
  2080. };
  2081. it('should have default', function() {
  2082. const eventView = new EventView({
  2083. ...state,
  2084. });
  2085. const displayMode = eventView.getDisplayMode();
  2086. expect(displayMode).toEqual(DisplayModes.DEFAULT);
  2087. });
  2088. it('should return current mode when not disabled', function() {
  2089. const eventView = new EventView({
  2090. ...state,
  2091. display: DisplayModes.DAILY,
  2092. });
  2093. const displayMode = eventView.getDisplayMode();
  2094. expect(displayMode).toEqual(DisplayModes.DAILY);
  2095. });
  2096. it('should return default mode when disabled', function() {
  2097. const eventView = new EventView({
  2098. ...state,
  2099. // the existence of start and end will disable the PREVIOUS mode
  2100. end: '2020-04-13T12:13:14',
  2101. start: '2020-04-01T12:13:14',
  2102. display: DisplayModes.PREVIOUS,
  2103. });
  2104. const displayMode = eventView.getDisplayMode();
  2105. expect(displayMode).toEqual(DisplayModes.DEFAULT);
  2106. });
  2107. it('top 5 should fallback to default when disabled', function() {
  2108. const eventView = new EventView({
  2109. ...state,
  2110. // the lack of an aggregate will disable the TOP5 mode
  2111. display: DisplayModes.TOP5,
  2112. });
  2113. const displayMode = eventView.getDisplayMode();
  2114. expect(displayMode).toEqual(DisplayModes.DEFAULT);
  2115. });
  2116. it('top 5 daily should fallback to daily when disabled', function() {
  2117. const eventView = new EventView({
  2118. ...state,
  2119. // the lack of an aggregate will disable the DAILYTOP5 mode
  2120. display: DisplayModes.DAILYTOP5,
  2121. });
  2122. const displayMode = eventView.getDisplayMode();
  2123. expect(displayMode).toEqual(DisplayModes.DAILY);
  2124. });
  2125. it('daily mode should fall back to default when disabled', function() {
  2126. const eventView = new EventView({
  2127. ...state,
  2128. // the period being less than 24h will disable the DAILY mode
  2129. statsPeriod: '1h',
  2130. display: DisplayModes.DAILY,
  2131. });
  2132. const displayMode = eventView.getDisplayMode();
  2133. expect(displayMode).toEqual(DisplayModes.DEFAULT);
  2134. });
  2135. it('top 5 daily mode should fall back to default when daily is disabled', function() {
  2136. const eventView = new EventView({
  2137. ...state,
  2138. // the period being less than 24h will disable the DAILY mode
  2139. start: '2020-04-01T12:13:14',
  2140. end: '2020-04-02T12:10:14',
  2141. display: DisplayModes.TOP5DAILY,
  2142. });
  2143. const displayMode = eventView.getDisplayMode();
  2144. expect(displayMode).toEqual(DisplayModes.DEFAULT);
  2145. });
  2146. });
  2147. describe('EventView.getAggregateFields()', function() {
  2148. const state = {
  2149. fields: [
  2150. {field: 'title'},
  2151. {field: 'count()'},
  2152. {field: 'count_unique(user)'},
  2153. {field: 'apdex(300)'},
  2154. {field: 'transaction'},
  2155. ],
  2156. sorts: [],
  2157. query: '',
  2158. project: [],
  2159. statsPeriod: '42d',
  2160. environment: [],
  2161. };
  2162. it('getAggregateFields() returns only aggregates', function() {
  2163. const eventView = new EventView(state);
  2164. const expected = [
  2165. {field: 'count()'},
  2166. {field: 'count_unique(user)'},
  2167. {field: 'apdex(300)'},
  2168. ];
  2169. expect(eventView.getAggregateFields()).toEqual(expected);
  2170. });
  2171. });
  2172. describe('EventView.hasAggregateField', function() {
  2173. it('ensures an eventview has an aggregate field', function() {
  2174. let eventView = new EventView({
  2175. fields: [{field: 'foobar'}],
  2176. sorts: [],
  2177. query: '',
  2178. project: [],
  2179. environment: [],
  2180. });
  2181. expect(eventView.hasAggregateField()).toBe(false);
  2182. eventView = new EventView({
  2183. fields: [{field: 'count(foo.bar.is-Enterprise_42)'}],
  2184. sorts: [],
  2185. query: '',
  2186. project: [],
  2187. environment: [],
  2188. });
  2189. expect(eventView.hasAggregateField()).toBe(true);
  2190. });
  2191. });
  2192. describe('isAPIPayloadSimilar', function() {
  2193. const state = {
  2194. id: '1234',
  2195. name: 'best query',
  2196. fields: [{field: 'count()'}, {field: 'project.id'}],
  2197. sorts: generateSorts(['count']),
  2198. query: 'event.type:error',
  2199. project: [42],
  2200. start: '2019-10-01T00:00:00',
  2201. end: '2019-10-02T00:00:00',
  2202. statsPeriod: '14d',
  2203. environment: ['staging'],
  2204. };
  2205. const meta = {
  2206. count: 'integer',
  2207. title: 'string',
  2208. };
  2209. describe('getEventsAPIPayload', function() {
  2210. it('is not similar when relevant query string keys are present in the Location object', function() {
  2211. const thisEventView = new EventView(state);
  2212. const location = {
  2213. query: {
  2214. project: 'project',
  2215. environment: 'environment',
  2216. start: 'start',
  2217. end: 'end',
  2218. utc: 'utc',
  2219. statsPeriod: 'statsPeriod',
  2220. cursor: 'cursor',
  2221. },
  2222. };
  2223. const thisAPIPayload = thisEventView.getEventsAPIPayload(location);
  2224. const otherLocation = {};
  2225. const otherAPIPayload = thisEventView.getEventsAPIPayload(otherLocation);
  2226. const results = isAPIPayloadSimilar(thisAPIPayload, otherAPIPayload);
  2227. expect(results).toBe(false);
  2228. });
  2229. it('is similar when irrelevant query string keys are present in the Location object', function() {
  2230. const thisEventView = new EventView(state);
  2231. const location = {
  2232. query: {
  2233. bestCountry: 'canada',
  2234. },
  2235. };
  2236. const thisAPIPayload = thisEventView.getEventsAPIPayload(location);
  2237. const otherLocation = {};
  2238. const otherAPIPayload = thisEventView.getEventsAPIPayload(otherLocation);
  2239. const results = isAPIPayloadSimilar(thisAPIPayload, otherAPIPayload);
  2240. expect(results).toBe(true);
  2241. });
  2242. it('is not similar on sort key sorted in opposite directions', function() {
  2243. const thisEventView = new EventView(state);
  2244. const location = {};
  2245. const thisAPIPayload = thisEventView.getEventsAPIPayload(location);
  2246. const otherEventView = thisEventView.sortOnField({field: 'count()'}, meta);
  2247. const otherLocation = {};
  2248. const otherAPIPayload = otherEventView.getEventsAPIPayload(otherLocation);
  2249. const results = isAPIPayloadSimilar(thisAPIPayload, otherAPIPayload);
  2250. expect(results).toBe(false);
  2251. });
  2252. it('is not similar when a new column is added', function() {
  2253. const thisEventView = new EventView(state);
  2254. const location = {};
  2255. const thisAPIPayload = thisEventView.getEventsAPIPayload(location);
  2256. const otherEventView = thisEventView.clone();
  2257. otherEventView.fields.push({field: 'title', width: COL_WIDTH_UNDEFINED});
  2258. const otherLocation = {};
  2259. const otherAPIPayload = otherEventView.getEventsAPIPayload(otherLocation);
  2260. const results = isAPIPayloadSimilar(thisAPIPayload, otherAPIPayload);
  2261. expect(results).toBe(false);
  2262. });
  2263. it('is similar when a column is updated with no changes', function() {
  2264. const thisEventView = new EventView(state);
  2265. const location = {};
  2266. const thisAPIPayload = thisEventView.getEventsAPIPayload(location);
  2267. const newColumn = {
  2268. kind: 'function',
  2269. function: ['count', ''],
  2270. };
  2271. const otherEventView = thisEventView.withUpdatedColumn(0, newColumn, meta);
  2272. const otherLocation = {};
  2273. const otherAPIPayload = otherEventView.getEventsAPIPayload(otherLocation);
  2274. const results = isAPIPayloadSimilar(thisAPIPayload, otherAPIPayload);
  2275. expect(results).toBe(true);
  2276. });
  2277. it('is not similar when a column is updated with a replaced field', function() {
  2278. const thisEventView = new EventView(state);
  2279. const location = {};
  2280. const thisAPIPayload = thisEventView.getEventsAPIPayload(location);
  2281. const newColumn = {
  2282. kind: 'field',
  2283. field: 'title',
  2284. };
  2285. const otherEventView = thisEventView.withUpdatedColumn(0, newColumn, meta);
  2286. const otherLocation = {};
  2287. const otherAPIPayload = otherEventView.getEventsAPIPayload(otherLocation);
  2288. const results = isAPIPayloadSimilar(thisAPIPayload, otherAPIPayload);
  2289. expect(results).toBe(false);
  2290. });
  2291. it('is not similar when a column is updated with a replaced aggregation', function() {
  2292. const thisEventView = new EventView(state);
  2293. const location = {};
  2294. const thisAPIPayload = thisEventView.getEventsAPIPayload(location);
  2295. const newColumn = {
  2296. kind: 'function',
  2297. function: ['avg', ''],
  2298. };
  2299. const otherEventView = thisEventView.withUpdatedColumn(0, newColumn, meta);
  2300. const otherLocation = {};
  2301. const otherAPIPayload = otherEventView.getEventsAPIPayload(otherLocation);
  2302. const results = isAPIPayloadSimilar(thisAPIPayload, otherAPIPayload);
  2303. expect(results).toBe(false);
  2304. });
  2305. it('is similar when a column is renamed', function() {
  2306. const thisEventView = new EventView(state);
  2307. const location = {};
  2308. const thisAPIPayload = thisEventView.getEventsAPIPayload(location);
  2309. const newColumn = {
  2310. kind: 'function',
  2311. function: ['count', ''],
  2312. };
  2313. const otherEventView = thisEventView.withUpdatedColumn(0, newColumn, meta);
  2314. const otherLocation = {};
  2315. const otherAPIPayload = otherEventView.getEventsAPIPayload(otherLocation);
  2316. const results = isAPIPayloadSimilar(thisAPIPayload, otherAPIPayload);
  2317. expect(results).toBe(true);
  2318. });
  2319. it('is not similar when a column is deleted', function() {
  2320. const thisEventView = new EventView(state);
  2321. const location = {};
  2322. const thisAPIPayload = thisEventView.getEventsAPIPayload(location);
  2323. const otherEventView = thisEventView.withDeletedColumn(0, meta);
  2324. const otherLocation = {};
  2325. const otherAPIPayload = otherEventView.getEventsAPIPayload(otherLocation);
  2326. const results = isAPIPayloadSimilar(thisAPIPayload, otherAPIPayload);
  2327. expect(results).toBe(false);
  2328. });
  2329. });
  2330. describe('getFacetsAPIPayload', function() {
  2331. it('only includes relevant parameters', function() {
  2332. const thisEventView = new EventView(state);
  2333. const location = {};
  2334. const results = thisEventView.getFacetsAPIPayload(location);
  2335. const expected = {
  2336. query: state.query,
  2337. project: ['42'],
  2338. statsPeriod: state.statsPeriod,
  2339. environment: state.environment,
  2340. };
  2341. expect(results).toEqual(expected);
  2342. });
  2343. it('is similar on sort key sorted in opposite directions', function() {
  2344. const thisEventView = new EventView(state);
  2345. const location = {};
  2346. const thisAPIPayload = thisEventView.getFacetsAPIPayload(location);
  2347. const newColumn = {
  2348. kind: 'function',
  2349. function: ['count', ''],
  2350. };
  2351. const otherEventView = thisEventView.withUpdatedColumn(0, newColumn, meta);
  2352. const otherLocation = {};
  2353. const otherAPIPayload = otherEventView.getFacetsAPIPayload(otherLocation);
  2354. const results = isAPIPayloadSimilar(thisAPIPayload, otherAPIPayload);
  2355. expect(results).toBe(true);
  2356. });
  2357. it('is similar when a columns are different', function() {
  2358. const thisEventView = new EventView(state);
  2359. const location = {};
  2360. const thisAPIPayload = thisEventView.getFacetsAPIPayload(location);
  2361. const otherEventView = thisEventView.clone();
  2362. otherEventView.fields.push({field: 'title', width: COL_WIDTH_UNDEFINED});
  2363. const otherLocation = {};
  2364. const otherAPIPayload = otherEventView.getFacetsAPIPayload(otherLocation);
  2365. const results = isAPIPayloadSimilar(thisAPIPayload, otherAPIPayload);
  2366. expect(results).toBe(true);
  2367. });
  2368. });
  2369. });
  2370. describe('pickRelevantLocationQueryStrings', function() {
  2371. it('picks relevant query strings', function() {
  2372. const location = {
  2373. query: {
  2374. project: 'project',
  2375. environment: 'environment',
  2376. start: 'start',
  2377. end: 'end',
  2378. utc: 'utc',
  2379. statsPeriod: 'statsPeriod',
  2380. cursor: 'cursor',
  2381. // irrelevant query strings
  2382. bestCountry: 'canada',
  2383. },
  2384. };
  2385. const actual = pickRelevantLocationQueryStrings(location);
  2386. const expected = {
  2387. start: 'start',
  2388. end: 'end',
  2389. utc: 'utc',
  2390. statsPeriod: 'statsPeriod',
  2391. cursor: 'cursor',
  2392. };
  2393. expect(actual).toEqual(expected);
  2394. });
  2395. });