eventView.spec.tsx 110 KB

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