eventView.tsx 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452
  1. import {Location, Query} from 'history';
  2. import cloneDeep from 'lodash/cloneDeep';
  3. import isEqual from 'lodash/isEqual';
  4. import isString from 'lodash/isString';
  5. import omit from 'lodash/omit';
  6. import pick from 'lodash/pick';
  7. import uniqBy from 'lodash/uniqBy';
  8. import moment from 'moment';
  9. import {EventQuery} from 'sentry/actionCreators/events';
  10. import {COL_WIDTH_UNDEFINED} from 'sentry/components/gridEditable';
  11. import {normalizeDateTimeParams} from 'sentry/components/organizations/pageFilters/parse';
  12. import {DEFAULT_PER_PAGE} from 'sentry/constants';
  13. import {URL_PARAM} from 'sentry/constants/pageFilters';
  14. import {t} from 'sentry/locale';
  15. import {NewQuery, PageFilters, SavedQuery, SelectValue, User} from 'sentry/types';
  16. import {
  17. aggregateOutputType,
  18. Column,
  19. ColumnType,
  20. Field,
  21. generateFieldAsString,
  22. getAggregateAlias,
  23. getEquation,
  24. isAggregateEquation,
  25. isAggregateField,
  26. isEquation,
  27. isLegalYAxisType,
  28. Sort,
  29. } from 'sentry/utils/discover/fields';
  30. import {
  31. CHART_AXIS_OPTIONS,
  32. DISPLAY_MODE_FALLBACK_OPTIONS,
  33. DISPLAY_MODE_OPTIONS,
  34. DisplayModes,
  35. TOP_N,
  36. } from 'sentry/utils/discover/types';
  37. import {decodeList, decodeScalar} from 'sentry/utils/queryString';
  38. import {
  39. FieldValueKind,
  40. TableColumn,
  41. TableColumnSort,
  42. } from 'sentry/views/eventsV2/table/types';
  43. import {decodeColumnOrder} from 'sentry/views/eventsV2/utils';
  44. import {SpanOperationBreakdownFilter} from 'sentry/views/performance/transactionSummary/filter';
  45. import {EventsDisplayFilterName} from 'sentry/views/performance/transactionSummary/transactionEvents/utils';
  46. import {statsPeriodToDays} from '../dates';
  47. import {WebVital} from '../fields';
  48. import {MutableSearch} from '../tokenizeSearch';
  49. import {getSortField} from './fieldRenderers';
  50. // Metadata mapping for discover results.
  51. export type MetaType = Record<string, ColumnType> & {
  52. isMetricsData?: boolean;
  53. tips?: {columns: string; query: string};
  54. units?: Record<string, string>;
  55. };
  56. export type EventsMetaType = {fields: Record<string, ColumnType>} & {
  57. isMetricsData?: boolean;
  58. };
  59. // Data in discover results.
  60. export type EventData = Record<string, any>;
  61. export type LocationQuery = {
  62. cursor?: string | string[] | null;
  63. end?: string | string[] | null;
  64. start?: string | string[] | null;
  65. statsPeriod?: string | string[] | null;
  66. utc?: string | string[] | null;
  67. };
  68. const DATETIME_QUERY_STRING_KEYS = ['start', 'end', 'utc', 'statsPeriod'] as const;
  69. const EXTERNAL_QUERY_STRING_KEYS: Readonly<Array<keyof LocationQuery>> = [
  70. ...DATETIME_QUERY_STRING_KEYS,
  71. 'cursor',
  72. ];
  73. const setSortOrder = (sort: Sort, kind: 'desc' | 'asc'): Sort => ({
  74. kind,
  75. field: sort.field,
  76. });
  77. const reverseSort = (sort: Sort): Sort => ({
  78. kind: sort.kind === 'desc' ? 'asc' : 'desc',
  79. field: sort.field,
  80. });
  81. const isSortEqualToField = (
  82. sort: Sort,
  83. field: Field,
  84. tableMeta: MetaType | undefined
  85. ): boolean => {
  86. const sortKey = getSortKeyFromField(field, tableMeta);
  87. return sort.field === sortKey;
  88. };
  89. const fieldToSort = (
  90. field: Field,
  91. tableMeta: MetaType | undefined,
  92. kind?: 'desc' | 'asc',
  93. useFunctionFormat?: boolean
  94. ): Sort | undefined => {
  95. const sortKey = getSortKeyFromField(field, tableMeta, useFunctionFormat);
  96. if (!sortKey) {
  97. return void 0;
  98. }
  99. return {
  100. kind: kind || 'desc',
  101. field: sortKey,
  102. };
  103. };
  104. function getSortKeyFromField(
  105. field: Field,
  106. tableMeta?: MetaType,
  107. useFunctionFormat?: boolean
  108. ): string | null {
  109. const fieldString = useFunctionFormat ? field.field : getAggregateAlias(field.field);
  110. return getSortField(fieldString, tableMeta);
  111. }
  112. export function isFieldSortable(field: Field, tableMeta?: MetaType): boolean {
  113. return !!getSortKeyFromField(field, tableMeta);
  114. }
  115. const decodeFields = (location: Location): Array<Field> => {
  116. const {query} = location;
  117. if (!query || !query.field) {
  118. return [];
  119. }
  120. const fields = decodeList(query.field);
  121. const widths = decodeList(query.widths);
  122. const parsed: Field[] = [];
  123. fields.forEach((field, i) => {
  124. const w = Number(widths[i]);
  125. const width = !isNaN(w) ? w : COL_WIDTH_UNDEFINED;
  126. parsed.push({field, width});
  127. });
  128. return parsed;
  129. };
  130. const parseSort = (sort: string): Sort => {
  131. sort = sort.trim();
  132. if (sort.startsWith('-')) {
  133. return {
  134. kind: 'desc',
  135. field: sort.substring(1),
  136. };
  137. }
  138. return {
  139. kind: 'asc',
  140. field: sort,
  141. };
  142. };
  143. export const fromSorts = (sorts: string | string[] | undefined): Array<Sort> => {
  144. if (sorts === undefined) {
  145. return [];
  146. }
  147. sorts = isString(sorts) ? [sorts] : sorts;
  148. // NOTE: sets are iterated in insertion order
  149. const uniqueSorts = [...new Set(sorts)];
  150. return uniqueSorts.reduce((acc: Array<Sort>, sort: string) => {
  151. acc.push(parseSort(sort));
  152. return acc;
  153. }, []);
  154. };
  155. const decodeSorts = (location: Location): Array<Sort> => {
  156. const {query} = location;
  157. if (!query || !query.sort) {
  158. return [];
  159. }
  160. const sorts = decodeList(query.sort);
  161. return fromSorts(sorts);
  162. };
  163. export const encodeSort = (sort: Sort): string => {
  164. switch (sort.kind) {
  165. case 'desc': {
  166. return `-${sort.field}`;
  167. }
  168. case 'asc': {
  169. return String(sort.field);
  170. }
  171. default: {
  172. throw new Error('Unexpected sort type');
  173. }
  174. }
  175. };
  176. const encodeSorts = (sorts: Readonly<Array<Sort>>): Array<string> =>
  177. sorts.map(encodeSort);
  178. const collectQueryStringByKey = (query: Query, key: string): Array<string> => {
  179. const needle = query[key];
  180. const collection = decodeList(needle);
  181. return collection.reduce((acc: Array<string>, item: string) => {
  182. item = item.trim();
  183. if (item.length > 0) {
  184. acc.push(item);
  185. }
  186. return acc;
  187. }, []);
  188. };
  189. const decodeQuery = (location: Location): string => {
  190. if (!location.query || !location.query.query) {
  191. return '';
  192. }
  193. const queryParameter = location.query.query;
  194. return decodeScalar(queryParameter, '').trim();
  195. };
  196. const decodeTeam = (value: string): 'myteams' | number => {
  197. if (value === 'myteams') {
  198. return value;
  199. }
  200. return parseInt(value, 10);
  201. };
  202. const decodeTeams = (location: Location): ('myteams' | number)[] => {
  203. if (!location.query || !location.query.team) {
  204. return [];
  205. }
  206. const value = location.query.team;
  207. return (Array.isArray(value) ? value.map(decodeTeam) : [decodeTeam(value)]).filter(
  208. team => team === 'myteams' || !isNaN(team)
  209. );
  210. };
  211. const decodeProjects = (location: Location): number[] => {
  212. if (!location.query || !location.query.project) {
  213. return [];
  214. }
  215. const value = location.query.project;
  216. return Array.isArray(value) ? value.map(i => parseInt(i, 10)) : [parseInt(value, 10)];
  217. };
  218. const queryStringFromSavedQuery = (saved: NewQuery | SavedQuery): string => {
  219. if (saved.query) {
  220. return saved.query || '';
  221. }
  222. return '';
  223. };
  224. function validateTableMeta(tableMeta: MetaType | undefined): MetaType | undefined {
  225. return tableMeta && Object.keys(tableMeta).length > 0 ? tableMeta : undefined;
  226. }
  227. class EventView {
  228. id: string | undefined;
  229. name: string | undefined;
  230. fields: Readonly<Field[]>;
  231. sorts: Readonly<Sort[]>;
  232. query: string;
  233. team: Readonly<('myteams' | number)[]>;
  234. project: Readonly<number[]>;
  235. start: string | undefined;
  236. end: string | undefined;
  237. statsPeriod: string | undefined;
  238. utc?: string | boolean | undefined;
  239. environment: Readonly<string[]>;
  240. yAxis: string | undefined;
  241. display: string | undefined;
  242. topEvents: string | undefined;
  243. interval: string | undefined;
  244. expired?: boolean;
  245. createdBy: User | undefined;
  246. additionalConditions: MutableSearch; // This allows views to always add additional conditions to the query to get specific data. It should not show up in the UI unless explicitly called.
  247. constructor(props: {
  248. additionalConditions: MutableSearch;
  249. createdBy: User | undefined;
  250. display: string | undefined;
  251. end: string | undefined;
  252. environment: Readonly<string[]>;
  253. fields: Readonly<Field[]>;
  254. id: string | undefined;
  255. name: string | undefined;
  256. project: Readonly<number[]>;
  257. query: string;
  258. sorts: Readonly<Sort[]>;
  259. start: string | undefined;
  260. statsPeriod: string | undefined;
  261. team: Readonly<('myteams' | number)[]>;
  262. topEvents: string | undefined;
  263. yAxis: string | undefined;
  264. expired?: boolean;
  265. interval?: string;
  266. utc?: string | boolean | undefined;
  267. }) {
  268. const fields: Field[] = Array.isArray(props.fields) ? props.fields : [];
  269. let sorts: Sort[] = Array.isArray(props.sorts) ? props.sorts : [];
  270. const team = Array.isArray(props.team) ? props.team : [];
  271. const project = Array.isArray(props.project) ? props.project : [];
  272. const environment = Array.isArray(props.environment) ? props.environment : [];
  273. // only include sort keys that are included in the fields
  274. let equations = 0;
  275. const sortKeys = fields
  276. .map(field => {
  277. if (field.field && isEquation(field.field)) {
  278. const sortKey = getSortKeyFromField(
  279. {field: `equation[${equations}]`},
  280. undefined
  281. );
  282. equations += 1;
  283. return sortKey;
  284. }
  285. return getSortKeyFromField(field, undefined);
  286. })
  287. .filter((sortKey): sortKey is string => !!sortKey);
  288. const sort = sorts.find(currentSort => sortKeys.includes(currentSort.field));
  289. sorts = sort ? [sort] : [];
  290. const id = props.id !== null && props.id !== void 0 ? String(props.id) : void 0;
  291. this.id = id;
  292. this.name = props.name;
  293. this.fields = fields;
  294. this.sorts = sorts;
  295. this.query = typeof props.query === 'string' ? props.query : '';
  296. this.team = team;
  297. this.project = project;
  298. this.start = props.start;
  299. this.end = props.end;
  300. this.statsPeriod = props.statsPeriod;
  301. this.utc = props.utc;
  302. this.environment = environment;
  303. this.yAxis = props.yAxis;
  304. this.display = props.display;
  305. this.topEvents = props.topEvents;
  306. this.interval = props.interval;
  307. this.createdBy = props.createdBy;
  308. this.expired = props.expired;
  309. this.additionalConditions = props.additionalConditions
  310. ? props.additionalConditions.copy()
  311. : new MutableSearch([]);
  312. }
  313. static fromLocation(location: Location): EventView {
  314. const {start, end, statsPeriod} = normalizeDateTimeParams(location.query);
  315. return new EventView({
  316. id: decodeScalar(location.query.id),
  317. name: decodeScalar(location.query.name),
  318. fields: decodeFields(location),
  319. sorts: decodeSorts(location),
  320. query: decodeQuery(location),
  321. team: decodeTeams(location),
  322. project: decodeProjects(location),
  323. start: decodeScalar(start),
  324. end: decodeScalar(end),
  325. statsPeriod: decodeScalar(statsPeriod),
  326. environment: collectQueryStringByKey(location.query, 'environment'),
  327. yAxis: decodeScalar(location.query.yAxis),
  328. display: decodeScalar(location.query.display),
  329. topEvents: decodeScalar(location.query.topEvents),
  330. interval: decodeScalar(location.query.interval),
  331. createdBy: undefined,
  332. additionalConditions: new MutableSearch([]),
  333. });
  334. }
  335. static fromNewQueryWithLocation(newQuery: NewQuery, location: Location): EventView {
  336. const query = location.query;
  337. // apply global selection header values from location whenever possible
  338. const environment: string[] =
  339. Array.isArray(newQuery.environment) && newQuery.environment.length > 0
  340. ? newQuery.environment
  341. : collectQueryStringByKey(query, 'environment');
  342. const project: number[] =
  343. Array.isArray(newQuery.projects) && newQuery.projects.length > 0
  344. ? newQuery.projects
  345. : decodeProjects(location);
  346. const saved: NewQuery = {
  347. ...newQuery,
  348. environment,
  349. projects: project,
  350. // datetime selection
  351. start: newQuery.start || decodeScalar(query.start),
  352. end: newQuery.end || decodeScalar(query.end),
  353. range: newQuery.range || decodeScalar(query.statsPeriod),
  354. };
  355. return EventView.fromSavedQuery(saved);
  356. }
  357. static getFields(saved: NewQuery | SavedQuery) {
  358. return saved.fields.map((field, i) => {
  359. const width =
  360. saved.widths && saved.widths[i] ? Number(saved.widths[i]) : COL_WIDTH_UNDEFINED;
  361. return {field, width};
  362. });
  363. }
  364. static fromSavedQuery(saved: NewQuery | SavedQuery): EventView {
  365. const fields = EventView.getFields(saved);
  366. // normalize datetime selection
  367. const {start, end, statsPeriod, utc} = normalizeDateTimeParams({
  368. start: saved.start,
  369. end: saved.end,
  370. statsPeriod: saved.range,
  371. utc: saved.utc,
  372. });
  373. return new EventView({
  374. id: saved.id,
  375. name: saved.name,
  376. fields,
  377. query: queryStringFromSavedQuery(saved),
  378. team: saved.teams ?? [],
  379. project: saved.projects,
  380. start: decodeScalar(start),
  381. end: decodeScalar(end),
  382. statsPeriod: decodeScalar(statsPeriod),
  383. utc,
  384. sorts: fromSorts(saved.orderby),
  385. environment: collectQueryStringByKey(
  386. {
  387. environment: saved.environment as string[],
  388. },
  389. 'environment'
  390. ),
  391. // Workaround to only use the first yAxis since eventView yAxis doesn't accept string[]
  392. yAxis: Array.isArray(saved.yAxis) ? saved.yAxis[0] : saved.yAxis,
  393. display: saved.display,
  394. topEvents: saved.topEvents ? saved.topEvents.toString() : undefined,
  395. createdBy: saved.createdBy,
  396. expired: saved.expired,
  397. additionalConditions: new MutableSearch([]),
  398. });
  399. }
  400. static fromSavedQueryOrLocation(
  401. saved: SavedQuery | undefined,
  402. location: Location
  403. ): EventView {
  404. let fields = decodeFields(location);
  405. const {start, end, statsPeriod, utc} = normalizeDateTimeParams(location.query);
  406. const id = decodeScalar(location.query.id);
  407. const teams = decodeTeams(location);
  408. const projects = decodeProjects(location);
  409. const sorts = decodeSorts(location);
  410. const environments = collectQueryStringByKey(location.query, 'environment');
  411. if (saved) {
  412. if (fields.length === 0) {
  413. fields = EventView.getFields(saved);
  414. }
  415. return new EventView({
  416. id: id || saved.id,
  417. name: decodeScalar(location.query.name) || saved.name,
  418. fields,
  419. query:
  420. 'query' in location.query
  421. ? decodeQuery(location)
  422. : queryStringFromSavedQuery(saved),
  423. sorts: sorts.length === 0 ? fromSorts(saved.orderby) : sorts,
  424. yAxis:
  425. decodeScalar(location.query.yAxis) ||
  426. // Workaround to only use the first yAxis since eventView yAxis doesn't accept string[]
  427. (Array.isArray(saved.yAxis) ? saved.yAxis[0] : saved.yAxis),
  428. display: decodeScalar(location.query.display) || saved.display,
  429. topEvents: (
  430. decodeScalar(location.query.topEvents) ||
  431. saved.topEvents ||
  432. TOP_N
  433. ).toString(),
  434. interval: decodeScalar(location.query.interval),
  435. createdBy: saved.createdBy,
  436. expired: saved.expired,
  437. additionalConditions: new MutableSearch([]),
  438. // Always read team from location since they can be set by other parts
  439. // of the UI
  440. team: teams,
  441. // Always read project and environment from location since they can
  442. // be set by the GlobalSelectionHeaders.
  443. project: projects,
  444. environment: environments,
  445. start: decodeScalar(start),
  446. end: decodeScalar(end),
  447. statsPeriod: decodeScalar(statsPeriod),
  448. utc,
  449. });
  450. }
  451. return EventView.fromLocation(location);
  452. }
  453. isEqualTo(other: EventView): boolean {
  454. const defaults = {
  455. id: undefined,
  456. name: undefined,
  457. query: undefined,
  458. statsPeriod: undefined,
  459. fields: undefined,
  460. sorts: undefined,
  461. project: undefined,
  462. environment: undefined,
  463. yAxis: 'count()',
  464. display: DisplayModes.DEFAULT,
  465. topEvents: '5',
  466. };
  467. const keys = Object.keys(defaults);
  468. for (const key of keys) {
  469. const currentValue = this[key] ?? defaults[key];
  470. const otherValue = other[key] ?? defaults[key];
  471. if (!isEqual(currentValue, otherValue)) {
  472. return false;
  473. }
  474. }
  475. // compare datetime selections using moment
  476. const dateTimeKeys = ['start', 'end'];
  477. for (const key of dateTimeKeys) {
  478. const currentValue = this[key];
  479. const otherValue = other[key];
  480. if (currentValue && otherValue) {
  481. const currentDateTime = moment.utc(currentValue);
  482. const otherDateTime = moment.utc(otherValue);
  483. if (!currentDateTime.isSame(otherDateTime)) {
  484. return false;
  485. }
  486. }
  487. }
  488. return true;
  489. }
  490. toNewQuery(): NewQuery {
  491. const orderby = this.sorts.length > 0 ? encodeSorts(this.sorts)[0] : undefined;
  492. const newQuery: NewQuery = {
  493. version: 2,
  494. id: this.id,
  495. name: this.name || '',
  496. fields: this.getFields(),
  497. widths: this.getWidths().map(w => String(w)),
  498. orderby,
  499. query: this.query || '',
  500. projects: this.project,
  501. start: this.start,
  502. end: this.end,
  503. range: this.statsPeriod,
  504. environment: this.environment,
  505. yAxis: this.yAxis ? [this.yAxis] : undefined,
  506. display: this.display,
  507. topEvents: this.topEvents,
  508. };
  509. if (!newQuery.query) {
  510. // if query is an empty string, then it cannot be saved, so we omit it
  511. // from the payload
  512. delete newQuery.query;
  513. }
  514. return newQuery;
  515. }
  516. getPageFilters(): PageFilters {
  517. return {
  518. projects: this.project as number[],
  519. environments: this.environment as string[],
  520. datetime: {
  521. start: this.start ?? null,
  522. end: this.end ?? null,
  523. period: this.statsPeriod ?? null,
  524. // TODO(tony) Add support for the Use UTC option from the global
  525. // headers, currently, that option is not supported and all times are
  526. // assumed to be UTC
  527. utc: true,
  528. },
  529. };
  530. }
  531. getPageFiltersQuery(): Query {
  532. const {
  533. environments: environment,
  534. projects,
  535. datetime: {start, end, period, utc},
  536. } = this.getPageFilters();
  537. return {
  538. project: projects.map(proj => proj.toString()),
  539. environment,
  540. utc: utc ? 'true' : 'false',
  541. // since these values are from `getGlobalSelection`
  542. // we know they have type `string | null`
  543. start: (start ?? undefined) as string | undefined,
  544. end: (end ?? undefined) as string | undefined,
  545. // we can't use the ?? operator here as we want to
  546. // convert the empty string to undefined
  547. statsPeriod: period ? period : undefined,
  548. };
  549. }
  550. generateBlankQueryStringObject(): Query {
  551. const output = {
  552. id: undefined,
  553. name: undefined,
  554. field: undefined,
  555. widths: undefined,
  556. sort: undefined,
  557. tag: undefined,
  558. query: undefined,
  559. yAxis: undefined,
  560. display: undefined,
  561. topEvents: undefined,
  562. interval: undefined,
  563. };
  564. for (const field of EXTERNAL_QUERY_STRING_KEYS) {
  565. output[field] = undefined;
  566. }
  567. return output;
  568. }
  569. generateQueryStringObject(): Query {
  570. const output = {
  571. id: this.id,
  572. name: this.name,
  573. field: this.getFields(),
  574. widths: this.getWidths(),
  575. sort: encodeSorts(this.sorts),
  576. environment: this.environment,
  577. project: this.project,
  578. query: this.query,
  579. yAxis: this.yAxis || this.getYAxis(),
  580. display: this.display,
  581. topEvents: this.topEvents,
  582. interval: this.interval,
  583. };
  584. for (const field of EXTERNAL_QUERY_STRING_KEYS) {
  585. if (this[field] && this[field].length) {
  586. output[field] = this[field];
  587. }
  588. }
  589. return cloneDeep(output as any);
  590. }
  591. isValid(): boolean {
  592. return this.fields.length > 0;
  593. }
  594. getWidths(): number[] {
  595. const result = this.fields.map(field =>
  596. field.width ? field.width : COL_WIDTH_UNDEFINED
  597. );
  598. while (result.length > 0) {
  599. const width = result[result.length - 1];
  600. if (width === COL_WIDTH_UNDEFINED) {
  601. result.pop();
  602. continue;
  603. }
  604. break;
  605. }
  606. return result;
  607. }
  608. getFields(): string[] {
  609. return this.fields.map(field => field.field);
  610. }
  611. getEquations(): string[] {
  612. return this.fields
  613. .filter(field => isEquation(field.field))
  614. .map(field => getEquation(field.field));
  615. }
  616. getAggregateFields(): Field[] {
  617. return this.fields.filter(
  618. field => isAggregateField(field.field) || isAggregateEquation(field.field)
  619. );
  620. }
  621. hasAggregateField() {
  622. return this.fields.some(field => isAggregateField(field.field));
  623. }
  624. hasIdField() {
  625. return this.fields.some(field => field.field === 'id');
  626. }
  627. numOfColumns(): number {
  628. return this.fields.length;
  629. }
  630. getColumns(useFullEquationAsKey?: boolean): TableColumn<React.ReactText>[] {
  631. return decodeColumnOrder(this.fields, useFullEquationAsKey);
  632. }
  633. getDays(): number {
  634. const statsPeriod = decodeScalar(this.statsPeriod);
  635. return statsPeriodToDays(statsPeriod, this.start, this.end);
  636. }
  637. clone(): EventView {
  638. // NOTE: We rely on usage of Readonly from TypeScript to ensure we do not mutate
  639. // the attributes of EventView directly. This enables us to quickly
  640. // clone new instances of EventView.
  641. return new EventView({
  642. id: this.id,
  643. name: this.name,
  644. fields: this.fields,
  645. sorts: this.sorts,
  646. query: this.query,
  647. team: this.team,
  648. project: this.project,
  649. start: this.start,
  650. end: this.end,
  651. statsPeriod: this.statsPeriod,
  652. environment: this.environment,
  653. yAxis: this.yAxis,
  654. display: this.display,
  655. topEvents: this.topEvents,
  656. interval: this.interval,
  657. expired: this.expired,
  658. createdBy: this.createdBy,
  659. additionalConditions: this.additionalConditions.copy(),
  660. });
  661. }
  662. withSorts(sorts: Sort[]): EventView {
  663. const newEventView = this.clone();
  664. const fields = newEventView.fields.map(field => getAggregateAlias(field.field));
  665. newEventView.sorts = sorts.filter(sort => fields.includes(sort.field));
  666. return newEventView;
  667. }
  668. withColumns(columns: Column[]): EventView {
  669. const newEventView = this.clone();
  670. const fields: Field[] = columns
  671. .filter(
  672. col =>
  673. ((col.kind === 'field' || col.kind === FieldValueKind.EQUATION) && col.field) ||
  674. (col.kind === 'function' && col.function[0])
  675. )
  676. .map(col => generateFieldAsString(col))
  677. .map((field, i) => {
  678. // newly added field
  679. if (!newEventView.fields[i]) {
  680. return {field, width: COL_WIDTH_UNDEFINED};
  681. }
  682. // Existing columns that were not re ordered should retain
  683. // their old widths.
  684. const existing = newEventView.fields[i];
  685. const width =
  686. existing.field === field && existing.width !== undefined
  687. ? existing.width
  688. : COL_WIDTH_UNDEFINED;
  689. return {field, width};
  690. });
  691. newEventView.fields = fields;
  692. // Update sorts as sorted fields may have been removed.
  693. if (newEventView.sorts) {
  694. // Filter the sort fields down to those that are still selected.
  695. const sortKeys = fields.map(field => fieldToSort(field, undefined)?.field);
  696. const newSort = newEventView.sorts.filter(
  697. sort => sort && sortKeys.includes(sort.field)
  698. );
  699. // If the sort field was removed, try and find a new sortable column.
  700. if (newSort.length === 0) {
  701. const sortField = fields.find(field => isFieldSortable(field, undefined));
  702. if (sortField) {
  703. newSort.push({field: sortField.field, kind: 'desc'});
  704. }
  705. }
  706. newEventView.sorts = newSort;
  707. }
  708. newEventView.yAxis = newEventView.getYAxis();
  709. return newEventView;
  710. }
  711. withNewColumn(newColumn: Column): EventView {
  712. const fieldAsString = generateFieldAsString(newColumn);
  713. const newField: Field = {
  714. field: fieldAsString,
  715. width: COL_WIDTH_UNDEFINED,
  716. };
  717. const newEventView = this.clone();
  718. newEventView.fields = [...newEventView.fields, newField];
  719. return newEventView;
  720. }
  721. withResizedColumn(columnIndex: number, newWidth: number) {
  722. const field = this.fields[columnIndex];
  723. const newEventView = this.clone();
  724. if (!field) {
  725. return newEventView;
  726. }
  727. const updateWidth = field.width !== newWidth;
  728. if (updateWidth) {
  729. const fields = [...newEventView.fields];
  730. fields[columnIndex] = {
  731. ...field,
  732. width: newWidth,
  733. };
  734. newEventView.fields = fields;
  735. }
  736. return newEventView;
  737. }
  738. withUpdatedColumn(
  739. columnIndex: number,
  740. updatedColumn: Column,
  741. tableMeta: MetaType | undefined
  742. ): EventView {
  743. const columnToBeUpdated = this.fields[columnIndex];
  744. const fieldAsString = generateFieldAsString(updatedColumn);
  745. const updateField = columnToBeUpdated.field !== fieldAsString;
  746. if (!updateField) {
  747. return this;
  748. }
  749. // ensure tableMeta is non-empty
  750. tableMeta = validateTableMeta(tableMeta);
  751. const newEventView = this.clone();
  752. const updatedField: Field = {
  753. field: fieldAsString,
  754. width: COL_WIDTH_UNDEFINED,
  755. };
  756. const fields = [...newEventView.fields];
  757. fields[columnIndex] = updatedField;
  758. newEventView.fields = fields;
  759. // if the updated column is one of the sorted columns, we may need to remove
  760. // it from the list of sorts
  761. const needleSortIndex = this.sorts.findIndex(sort =>
  762. isSortEqualToField(sort, columnToBeUpdated, tableMeta)
  763. );
  764. if (needleSortIndex >= 0) {
  765. const needleSort = this.sorts[needleSortIndex];
  766. const numOfColumns = this.fields.reduce((sum, currentField) => {
  767. if (isSortEqualToField(needleSort, currentField, tableMeta)) {
  768. return sum + 1;
  769. }
  770. return sum;
  771. }, 0);
  772. // do not bother deleting the sort key if there are more than one columns
  773. // of it in the table.
  774. if (numOfColumns <= 1) {
  775. if (isFieldSortable(updatedField, tableMeta)) {
  776. // use the current updated field as the sort key
  777. const sort = fieldToSort(updatedField, tableMeta)!;
  778. // preserve the sort kind
  779. sort.kind = needleSort.kind;
  780. const sorts = [...newEventView.sorts];
  781. sorts[needleSortIndex] = sort;
  782. newEventView.sorts = sorts;
  783. } else {
  784. const sorts = [...newEventView.sorts];
  785. sorts.splice(needleSortIndex, 1);
  786. newEventView.sorts = [...new Set(sorts)];
  787. }
  788. }
  789. if (newEventView.sorts.length <= 0 && newEventView.fields.length > 0) {
  790. // establish a default sort by finding the first sortable field
  791. if (isFieldSortable(updatedField, tableMeta)) {
  792. // use the current updated field as the sort key
  793. const sort = fieldToSort(updatedField, tableMeta)!;
  794. // preserve the sort kind
  795. sort.kind = needleSort.kind;
  796. newEventView.sorts = [sort];
  797. } else {
  798. const sortableFieldIndex = newEventView.fields.findIndex(currentField =>
  799. isFieldSortable(currentField, tableMeta)
  800. );
  801. if (sortableFieldIndex >= 0) {
  802. const fieldToBeSorted = newEventView.fields[sortableFieldIndex];
  803. const sort = fieldToSort(fieldToBeSorted, tableMeta)!;
  804. newEventView.sorts = [sort];
  805. }
  806. }
  807. }
  808. }
  809. newEventView.yAxis = newEventView.getYAxis();
  810. return newEventView;
  811. }
  812. withDeletedColumn(columnIndex: number, tableMeta: MetaType | undefined): EventView {
  813. // Disallow removal of the orphan column, and check for out-of-bounds
  814. if (this.fields.length <= 1 || this.fields.length <= columnIndex || columnIndex < 0) {
  815. return this;
  816. }
  817. // ensure tableMeta is non-empty
  818. tableMeta = validateTableMeta(tableMeta);
  819. // delete the column
  820. const newEventView = this.clone();
  821. const fields = [...newEventView.fields];
  822. fields.splice(columnIndex, 1);
  823. newEventView.fields = fields;
  824. // Ensure there is at least one auto width column
  825. // To ensure a well formed table results.
  826. const hasAutoIndex = fields.find(field => field.width === COL_WIDTH_UNDEFINED);
  827. if (!hasAutoIndex) {
  828. newEventView.fields[0].width = COL_WIDTH_UNDEFINED;
  829. }
  830. // if the deleted column is one of the sorted columns, we need to remove
  831. // it from the list of sorts
  832. const columnToBeDeleted = this.fields[columnIndex];
  833. const needleSortIndex = this.sorts.findIndex(sort =>
  834. isSortEqualToField(sort, columnToBeDeleted, tableMeta)
  835. );
  836. if (needleSortIndex >= 0) {
  837. const needleSort = this.sorts[needleSortIndex];
  838. const numOfColumns = this.fields.reduce((sum, field) => {
  839. if (isSortEqualToField(needleSort, field, tableMeta)) {
  840. return sum + 1;
  841. }
  842. return sum;
  843. }, 0);
  844. // do not bother deleting the sort key if there are more than one columns
  845. // of it in the table.
  846. if (numOfColumns <= 1) {
  847. const sorts = [...newEventView.sorts];
  848. sorts.splice(needleSortIndex, 1);
  849. newEventView.sorts = [...new Set(sorts)];
  850. if (newEventView.sorts.length <= 0 && newEventView.fields.length > 0) {
  851. // establish a default sort by finding the first sortable field
  852. const sortableFieldIndex = newEventView.fields.findIndex(field =>
  853. isFieldSortable(field, tableMeta)
  854. );
  855. if (sortableFieldIndex >= 0) {
  856. const fieldToBeSorted = newEventView.fields[sortableFieldIndex];
  857. const sort = fieldToSort(fieldToBeSorted, tableMeta)!;
  858. newEventView.sorts = [sort];
  859. }
  860. }
  861. }
  862. }
  863. newEventView.yAxis = newEventView.getYAxis();
  864. return newEventView;
  865. }
  866. withTeams(teams: ('myteams' | number)[]): EventView {
  867. const newEventView = this.clone();
  868. newEventView.team = teams;
  869. return newEventView;
  870. }
  871. getSorts(): TableColumnSort<React.ReactText>[] {
  872. return this.sorts.map(
  873. sort =>
  874. ({
  875. key: sort.field,
  876. order: sort.kind,
  877. } as TableColumnSort<string>)
  878. );
  879. }
  880. // returns query input for the search
  881. getQuery(inputQuery: string | string[] | null | undefined): string {
  882. const queryParts: string[] = [];
  883. if (this.query) {
  884. if (this.additionalConditions) {
  885. queryParts.push(this.getQueryWithAdditionalConditions());
  886. } else {
  887. queryParts.push(this.query);
  888. }
  889. }
  890. if (inputQuery) {
  891. // there may be duplicate query in the query string
  892. // e.g. query=hello&query=world
  893. if (Array.isArray(inputQuery)) {
  894. inputQuery.forEach(query => {
  895. if (typeof query === 'string' && !queryParts.includes(query)) {
  896. queryParts.push(query);
  897. }
  898. });
  899. }
  900. if (typeof inputQuery === 'string' && !queryParts.includes(inputQuery)) {
  901. queryParts.push(inputQuery);
  902. }
  903. }
  904. return queryParts.join(' ');
  905. }
  906. getFacetsAPIPayload(
  907. location: Location
  908. ): Exclude<EventQuery & LocationQuery, 'sort' | 'cursor'> {
  909. const payload = this.getEventsAPIPayload(location);
  910. const remove = [
  911. 'id',
  912. 'name',
  913. 'per_page',
  914. 'sort',
  915. 'cursor',
  916. 'field',
  917. 'equation',
  918. 'interval',
  919. ];
  920. for (const key of remove) {
  921. delete payload[key];
  922. }
  923. return payload;
  924. }
  925. normalizeDateSelection(location: Location) {
  926. const query = (location && location.query) || {};
  927. // pick only the query strings that we care about
  928. const picked = pickRelevantLocationQueryStrings(location);
  929. const hasDateSelection = this.statsPeriod || (this.start && this.end);
  930. // an eventview's date selection has higher precedence than the date selection in the query string
  931. const dateSelection = hasDateSelection
  932. ? {
  933. start: this.start,
  934. end: this.end,
  935. statsPeriod: this.statsPeriod,
  936. }
  937. : {
  938. start: picked.start,
  939. end: picked.end,
  940. period: decodeScalar(query.period),
  941. statsPeriod: picked.statsPeriod,
  942. };
  943. // normalize datetime selection
  944. return normalizeDateTimeParams({
  945. ...dateSelection,
  946. utc: decodeScalar(query.utc),
  947. });
  948. }
  949. // Takes an EventView instance and converts it into the format required for the events API
  950. getEventsAPIPayload(
  951. location: Location,
  952. forceAppendRawQueryString?: string
  953. ): EventQuery & LocationQuery {
  954. // pick only the query strings that we care about
  955. const picked = pickRelevantLocationQueryStrings(location);
  956. // normalize datetime selection
  957. const normalizedTimeWindowParams = this.normalizeDateSelection(location);
  958. const sort =
  959. this.sorts.length <= 0
  960. ? undefined
  961. : this.sorts.length > 1
  962. ? encodeSorts(this.sorts)
  963. : encodeSort(this.sorts[0]);
  964. const fields = this.getFields();
  965. const team = this.team.map(proj => String(proj));
  966. const project = this.project.map(proj => String(proj));
  967. const environment = this.environment as string[];
  968. let queryString = this.getQueryWithAdditionalConditions();
  969. if (forceAppendRawQueryString) {
  970. queryString += ' ' + forceAppendRawQueryString;
  971. }
  972. // generate event query
  973. const eventQuery = Object.assign(
  974. omit(picked, DATETIME_QUERY_STRING_KEYS),
  975. normalizedTimeWindowParams,
  976. {
  977. team,
  978. project,
  979. environment,
  980. field: [...new Set(fields)],
  981. sort,
  982. per_page: DEFAULT_PER_PAGE,
  983. query: queryString,
  984. }
  985. ) as EventQuery & LocationQuery;
  986. if (eventQuery.team && !eventQuery.team.length) {
  987. delete eventQuery.team;
  988. }
  989. if (!eventQuery.sort) {
  990. delete eventQuery.sort;
  991. }
  992. return eventQuery;
  993. }
  994. getResultsViewUrlTarget(slug: string): {pathname: string; query: Query} {
  995. return {
  996. pathname: `/organizations/${slug}/discover/results/`,
  997. query: this.generateQueryStringObject(),
  998. };
  999. }
  1000. getResultsViewShortUrlTarget(slug: string): {pathname: string; query: Query} {
  1001. const output = {id: this.id};
  1002. for (const field of [...Object.values(URL_PARAM), 'cursor']) {
  1003. if (this[field] && this[field].length) {
  1004. output[field] = this[field];
  1005. }
  1006. }
  1007. return {
  1008. pathname: `/organizations/${slug}/discover/results/`,
  1009. query: cloneDeep(output as any),
  1010. };
  1011. }
  1012. getPerformanceTransactionEventsViewUrlTarget(
  1013. slug: string,
  1014. options: {
  1015. breakdown?: SpanOperationBreakdownFilter;
  1016. showTransactions?: EventsDisplayFilterName;
  1017. webVital?: WebVital;
  1018. }
  1019. ): {pathname: string; query: Query} {
  1020. const {showTransactions, breakdown, webVital} = options;
  1021. const output = {
  1022. sort: encodeSorts(this.sorts),
  1023. project: this.project,
  1024. query: this.query,
  1025. transaction: this.name,
  1026. showTransactions,
  1027. breakdown,
  1028. webVital,
  1029. };
  1030. for (const field of EXTERNAL_QUERY_STRING_KEYS) {
  1031. if (this[field] && this[field].length) {
  1032. output[field] = this[field];
  1033. }
  1034. }
  1035. const query = cloneDeep(output as any);
  1036. return {
  1037. pathname: `/organizations/${slug}/performance/summary/events/`,
  1038. query,
  1039. };
  1040. }
  1041. sortForField(field: Field, tableMeta: MetaType | undefined): Sort | undefined {
  1042. if (!tableMeta) {
  1043. return undefined;
  1044. }
  1045. return this.sorts.find(sort => isSortEqualToField(sort, field, tableMeta));
  1046. }
  1047. sortOnField(
  1048. field: Field,
  1049. tableMeta: MetaType,
  1050. kind?: 'desc' | 'asc',
  1051. useFunctionFormat?: boolean
  1052. ): EventView {
  1053. // check if field can be sorted
  1054. if (!isFieldSortable(field, tableMeta)) {
  1055. return this;
  1056. }
  1057. const needleIndex = this.sorts.findIndex(sort =>
  1058. isSortEqualToField(sort, field, tableMeta)
  1059. );
  1060. if (needleIndex >= 0) {
  1061. const newEventView = this.clone();
  1062. const currentSort = this.sorts[needleIndex];
  1063. const sorts = [...newEventView.sorts];
  1064. sorts[needleIndex] = kind
  1065. ? setSortOrder(
  1066. {...currentSort, ...(useFunctionFormat ? {field: field.field} : {})},
  1067. kind
  1068. )
  1069. : reverseSort({
  1070. ...currentSort,
  1071. ...(useFunctionFormat ? {field: field.field} : {}),
  1072. });
  1073. newEventView.sorts = sorts;
  1074. return newEventView;
  1075. }
  1076. // field is currently not sorted; so, we sort on it
  1077. const newEventView = this.clone();
  1078. // invariant: this is not falsey, since sortKey exists
  1079. const sort = fieldToSort(field, tableMeta, kind, useFunctionFormat)!;
  1080. newEventView.sorts = [sort];
  1081. return newEventView;
  1082. }
  1083. getYAxisOptions(): SelectValue<string>[] {
  1084. // Make option set and add the default options in.
  1085. return uniqBy(
  1086. this.getAggregateFields()
  1087. // Only include aggregates that make sense to be graphable (eg. not string or date)
  1088. .filter(
  1089. (field: Field) =>
  1090. isLegalYAxisType(aggregateOutputType(field.field)) ||
  1091. isAggregateEquation(field.field)
  1092. )
  1093. .map((field: Field) => ({
  1094. label: isEquation(field.field) ? getEquation(field.field) : field.field,
  1095. value: field.field,
  1096. }))
  1097. .concat(CHART_AXIS_OPTIONS),
  1098. 'value'
  1099. );
  1100. }
  1101. getYAxis(): string {
  1102. const yAxisOptions = this.getYAxisOptions();
  1103. const yAxis = this.yAxis;
  1104. const defaultOption = yAxisOptions[0].value;
  1105. if (!yAxis) {
  1106. return defaultOption;
  1107. }
  1108. // ensure current selected yAxis is one of the items in yAxisOptions
  1109. const result = yAxisOptions.findIndex(
  1110. (option: SelectValue<string>) => option.value === yAxis
  1111. );
  1112. if (result >= 0) {
  1113. return yAxis;
  1114. }
  1115. return defaultOption;
  1116. }
  1117. getDisplayOptions(): SelectValue<string>[] {
  1118. return DISPLAY_MODE_OPTIONS.map(item => {
  1119. if (item.value === DisplayModes.PREVIOUS) {
  1120. if (this.start || this.end) {
  1121. return {...item, disabled: true};
  1122. }
  1123. }
  1124. if (item.value === DisplayModes.TOP5 || item.value === DisplayModes.DAILYTOP5) {
  1125. if (this.getAggregateFields().length === 0) {
  1126. return {
  1127. ...item,
  1128. disabled: true,
  1129. tooltip: t('Add a function that groups events to use this view.'),
  1130. };
  1131. }
  1132. }
  1133. if (item.value === DisplayModes.DAILY || item.value === DisplayModes.DAILYTOP5) {
  1134. if (this.getDays() < 1) {
  1135. return {
  1136. ...item,
  1137. disabled: true,
  1138. tooltip: t('Change the date rage to at least 1 day to use this view.'),
  1139. };
  1140. }
  1141. }
  1142. return item;
  1143. });
  1144. }
  1145. getDisplayMode() {
  1146. const mode = this.display ?? DisplayModes.DEFAULT;
  1147. const displayOptions = this.getDisplayOptions();
  1148. let display = (Object.values(DisplayModes) as string[]).includes(mode)
  1149. ? mode
  1150. : DisplayModes.DEFAULT;
  1151. const cond = option => option.value === display;
  1152. // Just in case we define a fallback chain that results in an infinite loop.
  1153. // The number 5 isn't anything special, its just larger than the longest fallback
  1154. // chain that exists and isn't too big.
  1155. for (let i = 0; i < 5; i++) {
  1156. const selectedOption = displayOptions.find(cond);
  1157. if (selectedOption && !selectedOption.disabled) {
  1158. return display;
  1159. }
  1160. display = DISPLAY_MODE_FALLBACK_OPTIONS[display];
  1161. }
  1162. // after trying to find an enabled display mode and failing to find one,
  1163. // we just use the default display mode
  1164. return DisplayModes.DEFAULT;
  1165. }
  1166. getQueryWithAdditionalConditions() {
  1167. const {query} = this;
  1168. if (this.additionalConditions.isEmpty()) {
  1169. return query;
  1170. }
  1171. const conditions = new MutableSearch(query);
  1172. Object.entries(this.additionalConditions.filters).forEach(([tag, tagValues]) => {
  1173. const existingTagValues = conditions.getFilterValues(tag);
  1174. const newTagValues = tagValues.filter(
  1175. tagValue => !existingTagValues.includes(tagValue)
  1176. );
  1177. if (newTagValues.length) {
  1178. conditions.addFilterValues(tag, newTagValues);
  1179. }
  1180. });
  1181. return conditions.formatString();
  1182. }
  1183. }
  1184. export type ImmutableEventView = Readonly<Omit<EventView, 'additionalConditions'>>;
  1185. const isFieldsSimilar = (
  1186. currentValue: Array<string>,
  1187. otherValue: Array<string>
  1188. ): boolean => {
  1189. // For equation's their order matters because we alias them based on index
  1190. const currentEquations = currentValue.filter(isEquation);
  1191. const otherEquations = otherValue.filter(isEquation);
  1192. // Field orders don't matter, so using a set for comparison
  1193. const currentFields = new Set(currentValue.filter(value => !isEquation(value)));
  1194. const otherFields = new Set(otherValue.filter(value => !isEquation(value)));
  1195. if (!isEqual(currentEquations, otherEquations)) {
  1196. return false;
  1197. }
  1198. if (!isEqual(currentFields, otherFields)) {
  1199. return false;
  1200. }
  1201. return true;
  1202. };
  1203. export const isAPIPayloadSimilar = (
  1204. current: EventQuery & LocationQuery,
  1205. other: EventQuery & LocationQuery
  1206. ): boolean => {
  1207. const currentKeys = new Set(Object.keys(current));
  1208. const otherKeys = new Set(Object.keys(other));
  1209. if (!isEqual(currentKeys, otherKeys)) {
  1210. return false;
  1211. }
  1212. for (const key of currentKeys) {
  1213. const currentValue = current[key];
  1214. const otherValue = other[key];
  1215. if (key === 'field') {
  1216. if (!isFieldsSimilar(currentValue, otherValue)) {
  1217. return false;
  1218. }
  1219. } else {
  1220. const currentTarget = Array.isArray(currentValue)
  1221. ? new Set(currentValue)
  1222. : currentValue;
  1223. const otherTarget = Array.isArray(otherValue) ? new Set(otherValue) : otherValue;
  1224. if (!isEqual(currentTarget, otherTarget)) {
  1225. return false;
  1226. }
  1227. }
  1228. }
  1229. return true;
  1230. };
  1231. export function pickRelevantLocationQueryStrings(location: Location) {
  1232. const query = location.query || {};
  1233. const picked = pick(query || {}, EXTERNAL_QUERY_STRING_KEYS);
  1234. return picked;
  1235. }
  1236. export default EventView;