123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455 |
- import {Location, Query} from 'history';
- import cloneDeep from 'lodash/cloneDeep';
- import isEqual from 'lodash/isEqual';
- import isString from 'lodash/isString';
- import omit from 'lodash/omit';
- import pick from 'lodash/pick';
- import uniqBy from 'lodash/uniqBy';
- import moment from 'moment';
- import {EventQuery} from 'sentry/actionCreators/events';
- import {COL_WIDTH_UNDEFINED} from 'sentry/components/gridEditable';
- import {normalizeDateTimeParams} from 'sentry/components/organizations/pageFilters/parse';
- import {DEFAULT_PER_PAGE} from 'sentry/constants';
- import {URL_PARAM} from 'sentry/constants/pageFilters';
- import {t} from 'sentry/locale';
- import {NewQuery, PageFilters, SavedQuery, SelectValue, User} from 'sentry/types';
- import {
- aggregateOutputType,
- Column,
- ColumnType,
- Field,
- generateFieldAsString,
- getAggregateAlias,
- getEquation,
- isAggregateEquation,
- isAggregateField,
- isEquation,
- isLegalYAxisType,
- Sort,
- } from 'sentry/utils/discover/fields';
- import {
- CHART_AXIS_OPTIONS,
- DISPLAY_MODE_FALLBACK_OPTIONS,
- DISPLAY_MODE_OPTIONS,
- DisplayModes,
- TOP_N,
- } from 'sentry/utils/discover/types';
- import {decodeList, decodeScalar} from 'sentry/utils/queryString';
- import {
- FieldValueKind,
- TableColumn,
- TableColumnSort,
- } from 'sentry/views/eventsV2/table/types';
- import {decodeColumnOrder} from 'sentry/views/eventsV2/utils';
- import {SpanOperationBreakdownFilter} from 'sentry/views/performance/transactionSummary/filter';
- import {EventsDisplayFilterName} from 'sentry/views/performance/transactionSummary/transactionEvents/utils';
- import {statsPeriodToDays} from '../dates';
- import {WebVital} from '../fields';
- import {MutableSearch} from '../tokenizeSearch';
- import {getSortField} from './fieldRenderers';
- // Metadata mapping for discover results.
- export type MetaType = Record<string, ColumnType> & {
- isMetricsData?: boolean;
- tips?: {columns: string; query: string};
- units?: Record<string, string>;
- };
- export type EventsMetaType = {fields: Record<string, ColumnType>} & {
- isMetricsData?: boolean;
- };
- // Data in discover results.
- export type EventData = Record<string, any>;
- export type LocationQuery = {
- cursor?: string | string[] | null;
- end?: string | string[] | null;
- start?: string | string[] | null;
- statsPeriod?: string | string[] | null;
- utc?: string | string[] | null;
- };
- const DATETIME_QUERY_STRING_KEYS = ['start', 'end', 'utc', 'statsPeriod'] as const;
- const EXTERNAL_QUERY_STRING_KEYS: Readonly<Array<keyof LocationQuery>> = [
- ...DATETIME_QUERY_STRING_KEYS,
- 'cursor',
- ];
- const setSortOrder = (sort: Sort, kind: 'desc' | 'asc'): Sort => ({
- kind,
- field: sort.field,
- });
- const reverseSort = (sort: Sort): Sort => ({
- kind: sort.kind === 'desc' ? 'asc' : 'desc',
- field: sort.field,
- });
- const isSortEqualToField = (
- sort: Sort,
- field: Field,
- tableMeta: MetaType | undefined
- ): boolean => {
- const sortKey = getSortKeyFromField(field, tableMeta);
- return sort.field === sortKey;
- };
- const fieldToSort = (
- field: Field,
- tableMeta: MetaType | undefined,
- kind?: 'desc' | 'asc',
- useFunctionFormat?: boolean
- ): Sort | undefined => {
- const sortKey = getSortKeyFromField(field, tableMeta, useFunctionFormat);
- if (!sortKey) {
- return void 0;
- }
- return {
- kind: kind || 'desc',
- field: sortKey,
- };
- };
- function getSortKeyFromField(
- field: Field,
- tableMeta?: MetaType,
- useFunctionFormat?: boolean
- ): string | null {
- const fieldString = useFunctionFormat ? field.field : getAggregateAlias(field.field);
- return getSortField(fieldString, tableMeta);
- }
- export function isFieldSortable(field: Field, tableMeta?: MetaType): boolean {
- return !!getSortKeyFromField(field, tableMeta);
- }
- const decodeFields = (location: Location): Array<Field> => {
- const {query} = location;
- if (!query || !query.field) {
- return [];
- }
- const fields = decodeList(query.field);
- const widths = decodeList(query.widths);
- const parsed: Field[] = [];
- fields.forEach((field, i) => {
- const w = Number(widths[i]);
- const width = !isNaN(w) ? w : COL_WIDTH_UNDEFINED;
- parsed.push({field, width});
- });
- return parsed;
- };
- const parseSort = (sort: string): Sort => {
- sort = sort.trim();
- if (sort.startsWith('-')) {
- return {
- kind: 'desc',
- field: sort.substring(1),
- };
- }
- return {
- kind: 'asc',
- field: sort,
- };
- };
- export const fromSorts = (sorts: string | string[] | undefined): Array<Sort> => {
- if (sorts === undefined) {
- return [];
- }
- sorts = isString(sorts) ? [sorts] : sorts;
- // NOTE: sets are iterated in insertion order
- const uniqueSorts = [...new Set(sorts)];
- return uniqueSorts.reduce((acc: Array<Sort>, sort: string) => {
- acc.push(parseSort(sort));
- return acc;
- }, []);
- };
- const decodeSorts = (location: Location): Array<Sort> => {
- const {query} = location;
- if (!query || !query.sort) {
- return [];
- }
- const sorts = decodeList(query.sort);
- return fromSorts(sorts);
- };
- export const encodeSort = (sort: Sort): string => {
- switch (sort.kind) {
- case 'desc': {
- return `-${sort.field}`;
- }
- case 'asc': {
- return String(sort.field);
- }
- default: {
- throw new Error('Unexpected sort type');
- }
- }
- };
- const encodeSorts = (sorts: Readonly<Array<Sort>>): Array<string> =>
- sorts.map(encodeSort);
- const collectQueryStringByKey = (query: Query, key: string): Array<string> => {
- const needle = query[key];
- const collection = decodeList(needle);
- return collection.reduce((acc: Array<string>, item: string) => {
- item = item.trim();
- if (item.length > 0) {
- acc.push(item);
- }
- return acc;
- }, []);
- };
- const decodeQuery = (location: Location): string => {
- if (!location.query || !location.query.query) {
- return '';
- }
- const queryParameter = location.query.query;
- return decodeScalar(queryParameter, '').trim();
- };
- const decodeTeam = (value: string): 'myteams' | number => {
- if (value === 'myteams') {
- return value;
- }
- return parseInt(value, 10);
- };
- const decodeTeams = (location: Location): ('myteams' | number)[] => {
- if (!location.query || !location.query.team) {
- return [];
- }
- const value = location.query.team;
- return (Array.isArray(value) ? value.map(decodeTeam) : [decodeTeam(value)]).filter(
- team => team === 'myteams' || !isNaN(team)
- );
- };
- const decodeProjects = (location: Location): number[] => {
- if (!location.query || !location.query.project) {
- return [];
- }
- const value = location.query.project;
- return Array.isArray(value) ? value.map(i => parseInt(i, 10)) : [parseInt(value, 10)];
- };
- const queryStringFromSavedQuery = (saved: NewQuery | SavedQuery): string => {
- if (saved.query) {
- return saved.query || '';
- }
- return '';
- };
- function validateTableMeta(tableMeta: MetaType | undefined): MetaType | undefined {
- return tableMeta && Object.keys(tableMeta).length > 0 ? tableMeta : undefined;
- }
- class EventView {
- id: string | undefined;
- name: string | undefined;
- fields: Readonly<Field[]>;
- sorts: Readonly<Sort[]>;
- query: string;
- team: Readonly<('myteams' | number)[]>;
- project: Readonly<number[]>;
- start: string | undefined;
- end: string | undefined;
- statsPeriod: string | undefined;
- utc?: string | boolean | undefined;
- environment: Readonly<string[]>;
- yAxis: string | undefined;
- display: string | undefined;
- topEvents: string | undefined;
- interval: string | undefined;
- expired?: boolean;
- createdBy: User | undefined;
- 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.
- constructor(props: {
- additionalConditions: MutableSearch;
- createdBy: User | undefined;
- display: string | undefined;
- end: string | undefined;
- environment: Readonly<string[]>;
- fields: Readonly<Field[]>;
- id: string | undefined;
- name: string | undefined;
- project: Readonly<number[]>;
- query: string;
- sorts: Readonly<Sort[]>;
- start: string | undefined;
- statsPeriod: string | undefined;
- team: Readonly<('myteams' | number)[]>;
- topEvents: string | undefined;
- yAxis: string | undefined;
- expired?: boolean;
- interval?: string;
- utc?: string | boolean | undefined;
- }) {
- const fields: Field[] = Array.isArray(props.fields) ? props.fields : [];
- let sorts: Sort[] = Array.isArray(props.sorts) ? props.sorts : [];
- const team = Array.isArray(props.team) ? props.team : [];
- const project = Array.isArray(props.project) ? props.project : [];
- const environment = Array.isArray(props.environment) ? props.environment : [];
- // only include sort keys that are included in the fields
- let equations = 0;
- const sortKeys = fields
- .map(field => {
- if (field.field && isEquation(field.field)) {
- const sortKey = getSortKeyFromField(
- {field: `equation[${equations}]`},
- undefined
- );
- equations += 1;
- return sortKey;
- }
- return getSortKeyFromField(field, undefined);
- })
- .filter((sortKey): sortKey is string => !!sortKey);
- const sort = sorts.find(currentSort => sortKeys.includes(currentSort.field));
- sorts = sort ? [sort] : [];
- const id = props.id !== null && props.id !== void 0 ? String(props.id) : void 0;
- this.id = id;
- this.name = props.name;
- this.fields = fields;
- this.sorts = sorts;
- this.query = typeof props.query === 'string' ? props.query : '';
- this.team = team;
- this.project = project;
- this.start = props.start;
- this.end = props.end;
- this.statsPeriod = props.statsPeriod;
- this.utc = props.utc;
- this.environment = environment;
- this.yAxis = props.yAxis;
- this.display = props.display;
- this.topEvents = props.topEvents;
- this.interval = props.interval;
- this.createdBy = props.createdBy;
- this.expired = props.expired;
- this.additionalConditions = props.additionalConditions
- ? props.additionalConditions.copy()
- : new MutableSearch([]);
- }
- static fromLocation(location: Location): EventView {
- const {start, end, statsPeriod} = normalizeDateTimeParams(location.query);
- return new EventView({
- id: decodeScalar(location.query.id),
- name: decodeScalar(location.query.name),
- fields: decodeFields(location),
- sorts: decodeSorts(location),
- query: decodeQuery(location),
- team: decodeTeams(location),
- project: decodeProjects(location),
- start: decodeScalar(start),
- end: decodeScalar(end),
- statsPeriod: decodeScalar(statsPeriod),
- environment: collectQueryStringByKey(location.query, 'environment'),
- yAxis: decodeScalar(location.query.yAxis),
- display: decodeScalar(location.query.display),
- topEvents: decodeScalar(location.query.topEvents),
- interval: decodeScalar(location.query.interval),
- createdBy: undefined,
- additionalConditions: new MutableSearch([]),
- });
- }
- static fromNewQueryWithLocation(newQuery: NewQuery, location: Location): EventView {
- const query = location.query;
- // apply global selection header values from location whenever possible
- const environment: string[] =
- Array.isArray(newQuery.environment) && newQuery.environment.length > 0
- ? newQuery.environment
- : collectQueryStringByKey(query, 'environment');
- const project: number[] =
- Array.isArray(newQuery.projects) && newQuery.projects.length > 0
- ? newQuery.projects
- : decodeProjects(location);
- const saved: NewQuery = {
- ...newQuery,
- environment,
- projects: project,
- // datetime selection
- start: newQuery.start || decodeScalar(query.start),
- end: newQuery.end || decodeScalar(query.end),
- range: newQuery.range || decodeScalar(query.statsPeriod),
- };
- return EventView.fromSavedQuery(saved);
- }
- static getFields(saved: NewQuery | SavedQuery) {
- return saved.fields.map((field, i) => {
- const width =
- saved.widths && saved.widths[i] ? Number(saved.widths[i]) : COL_WIDTH_UNDEFINED;
- return {field, width};
- });
- }
- static fromSavedQuery(saved: NewQuery | SavedQuery): EventView {
- const fields = EventView.getFields(saved);
- // normalize datetime selection
- const {start, end, statsPeriod, utc} = normalizeDateTimeParams({
- start: saved.start,
- end: saved.end,
- statsPeriod: saved.range,
- utc: saved.utc,
- });
- return new EventView({
- id: saved.id,
- name: saved.name,
- fields,
- query: queryStringFromSavedQuery(saved),
- team: saved.teams ?? [],
- project: saved.projects,
- start: decodeScalar(start),
- end: decodeScalar(end),
- statsPeriod: decodeScalar(statsPeriod),
- utc,
- sorts: fromSorts(saved.orderby),
- environment: collectQueryStringByKey(
- {
- environment: saved.environment as string[],
- },
- 'environment'
- ),
- // Workaround to only use the first yAxis since eventView yAxis doesn't accept string[]
- yAxis: Array.isArray(saved.yAxis) ? saved.yAxis[0] : saved.yAxis,
- display: saved.display,
- topEvents: saved.topEvents ? saved.topEvents.toString() : undefined,
- interval: saved.interval,
- createdBy: saved.createdBy,
- expired: saved.expired,
- additionalConditions: new MutableSearch([]),
- });
- }
- static fromSavedQueryOrLocation(
- saved: SavedQuery | undefined,
- location: Location
- ): EventView {
- let fields = decodeFields(location);
- const {start, end, statsPeriod, utc} = normalizeDateTimeParams(location.query);
- const id = decodeScalar(location.query.id);
- const teams = decodeTeams(location);
- const projects = decodeProjects(location);
- const sorts = decodeSorts(location);
- const environments = collectQueryStringByKey(location.query, 'environment');
- if (saved) {
- if (fields.length === 0) {
- fields = EventView.getFields(saved);
- }
- return new EventView({
- id: id || saved.id,
- name: decodeScalar(location.query.name) || saved.name,
- fields,
- query:
- 'query' in location.query
- ? decodeQuery(location)
- : queryStringFromSavedQuery(saved),
- sorts: sorts.length === 0 ? fromSorts(saved.orderby) : sorts,
- yAxis:
- decodeScalar(location.query.yAxis) ||
- // Workaround to only use the first yAxis since eventView yAxis doesn't accept string[]
- (Array.isArray(saved.yAxis) ? saved.yAxis[0] : saved.yAxis),
- display: decodeScalar(location.query.display) || saved.display,
- topEvents: (
- decodeScalar(location.query.topEvents) ||
- saved.topEvents ||
- TOP_N
- ).toString(),
- interval: decodeScalar(location.query.interval) || saved.interval,
- createdBy: saved.createdBy,
- expired: saved.expired,
- additionalConditions: new MutableSearch([]),
- // Always read team from location since they can be set by other parts
- // of the UI
- team: teams,
- // Always read project and environment from location since they can
- // be set by the GlobalSelectionHeaders.
- project: projects,
- environment: environments,
- start: decodeScalar(start),
- end: decodeScalar(end),
- statsPeriod: decodeScalar(statsPeriod),
- utc,
- });
- }
- return EventView.fromLocation(location);
- }
- isEqualTo(other: EventView): boolean {
- const defaults = {
- id: undefined,
- name: undefined,
- query: undefined,
- statsPeriod: undefined,
- fields: undefined,
- sorts: undefined,
- project: undefined,
- environment: undefined,
- interval: undefined,
- yAxis: 'count()',
- display: DisplayModes.DEFAULT,
- topEvents: '5',
- };
- const keys = Object.keys(defaults);
- for (const key of keys) {
- const currentValue = this[key] ?? defaults[key];
- const otherValue = other[key] ?? defaults[key];
- if (!isEqual(currentValue, otherValue)) {
- return false;
- }
- }
- // compare datetime selections using moment
- const dateTimeKeys = ['start', 'end'];
- for (const key of dateTimeKeys) {
- const currentValue = this[key];
- const otherValue = other[key];
- if (currentValue && otherValue) {
- const currentDateTime = moment.utc(currentValue);
- const otherDateTime = moment.utc(otherValue);
- if (!currentDateTime.isSame(otherDateTime)) {
- return false;
- }
- }
- }
- return true;
- }
- toNewQuery(): NewQuery {
- const orderby = this.sorts.length > 0 ? encodeSorts(this.sorts)[0] : undefined;
- const newQuery: NewQuery = {
- version: 2,
- id: this.id,
- name: this.name || '',
- fields: this.getFields(),
- widths: this.getWidths().map(w => String(w)),
- orderby,
- query: this.query || '',
- projects: this.project,
- start: this.start,
- end: this.end,
- range: this.statsPeriod,
- environment: this.environment,
- yAxis: this.yAxis ? [this.yAxis] : undefined,
- display: this.display,
- topEvents: this.topEvents,
- interval: this.interval,
- };
- if (!newQuery.query) {
- // if query is an empty string, then it cannot be saved, so we omit it
- // from the payload
- delete newQuery.query;
- }
- return newQuery;
- }
- getPageFilters(): PageFilters {
- return {
- projects: this.project as number[],
- environments: this.environment as string[],
- datetime: {
- start: this.start ?? null,
- end: this.end ?? null,
- period: this.statsPeriod ?? null,
- // TODO(tony) Add support for the Use UTC option from the global
- // headers, currently, that option is not supported and all times are
- // assumed to be UTC
- utc: true,
- },
- };
- }
- getPageFiltersQuery(): Query {
- const {
- environments: environment,
- projects,
- datetime: {start, end, period, utc},
- } = this.getPageFilters();
- return {
- project: projects.map(proj => proj.toString()),
- environment,
- utc: utc ? 'true' : 'false',
- // since these values are from `getGlobalSelection`
- // we know they have type `string | null`
- start: (start ?? undefined) as string | undefined,
- end: (end ?? undefined) as string | undefined,
- // we can't use the ?? operator here as we want to
- // convert the empty string to undefined
- statsPeriod: period ? period : undefined,
- };
- }
- generateBlankQueryStringObject(): Query {
- const output = {
- id: undefined,
- name: undefined,
- field: undefined,
- widths: undefined,
- sort: undefined,
- tag: undefined,
- query: undefined,
- yAxis: undefined,
- display: undefined,
- topEvents: undefined,
- interval: undefined,
- };
- for (const field of EXTERNAL_QUERY_STRING_KEYS) {
- output[field] = undefined;
- }
- return output;
- }
- generateQueryStringObject(): Query {
- const output = {
- id: this.id,
- name: this.name,
- field: this.getFields(),
- widths: this.getWidths(),
- sort: encodeSorts(this.sorts),
- environment: this.environment,
- project: this.project,
- query: this.query,
- yAxis: this.yAxis || this.getYAxis(),
- display: this.display,
- topEvents: this.topEvents,
- interval: this.interval,
- };
- for (const field of EXTERNAL_QUERY_STRING_KEYS) {
- if (this[field] && this[field].length) {
- output[field] = this[field];
- }
- }
- return cloneDeep(output as any);
- }
- isValid(): boolean {
- return this.fields.length > 0;
- }
- getWidths(): number[] {
- const result = this.fields.map(field =>
- field.width ? field.width : COL_WIDTH_UNDEFINED
- );
- while (result.length > 0) {
- const width = result[result.length - 1];
- if (width === COL_WIDTH_UNDEFINED) {
- result.pop();
- continue;
- }
- break;
- }
- return result;
- }
- getFields(): string[] {
- return this.fields.map(field => field.field);
- }
- getEquations(): string[] {
- return this.fields
- .filter(field => isEquation(field.field))
- .map(field => getEquation(field.field));
- }
- getAggregateFields(): Field[] {
- return this.fields.filter(
- field => isAggregateField(field.field) || isAggregateEquation(field.field)
- );
- }
- hasAggregateField() {
- return this.fields.some(field => isAggregateField(field.field));
- }
- hasIdField() {
- return this.fields.some(field => field.field === 'id');
- }
- numOfColumns(): number {
- return this.fields.length;
- }
- getColumns(useFullEquationAsKey?: boolean): TableColumn<React.ReactText>[] {
- return decodeColumnOrder(this.fields, useFullEquationAsKey);
- }
- getDays(): number {
- const statsPeriod = decodeScalar(this.statsPeriod);
- return statsPeriodToDays(statsPeriod, this.start, this.end);
- }
- clone(): EventView {
- // NOTE: We rely on usage of Readonly from TypeScript to ensure we do not mutate
- // the attributes of EventView directly. This enables us to quickly
- // clone new instances of EventView.
- return new EventView({
- id: this.id,
- name: this.name,
- fields: this.fields,
- sorts: this.sorts,
- query: this.query,
- team: this.team,
- project: this.project,
- start: this.start,
- end: this.end,
- statsPeriod: this.statsPeriod,
- environment: this.environment,
- yAxis: this.yAxis,
- display: this.display,
- topEvents: this.topEvents,
- interval: this.interval,
- expired: this.expired,
- createdBy: this.createdBy,
- additionalConditions: this.additionalConditions.copy(),
- });
- }
- withSorts(sorts: Sort[]): EventView {
- const newEventView = this.clone();
- const fields = newEventView.fields.map(field => getAggregateAlias(field.field));
- newEventView.sorts = sorts.filter(sort => fields.includes(sort.field));
- return newEventView;
- }
- withColumns(columns: Column[]): EventView {
- const newEventView = this.clone();
- const fields: Field[] = columns
- .filter(
- col =>
- ((col.kind === 'field' || col.kind === FieldValueKind.EQUATION) && col.field) ||
- (col.kind === 'function' && col.function[0])
- )
- .map(col => generateFieldAsString(col))
- .map((field, i) => {
- // newly added field
- if (!newEventView.fields[i]) {
- return {field, width: COL_WIDTH_UNDEFINED};
- }
- // Existing columns that were not re ordered should retain
- // their old widths.
- const existing = newEventView.fields[i];
- const width =
- existing.field === field && existing.width !== undefined
- ? existing.width
- : COL_WIDTH_UNDEFINED;
- return {field, width};
- });
- newEventView.fields = fields;
- // Update sorts as sorted fields may have been removed.
- if (newEventView.sorts) {
- // Filter the sort fields down to those that are still selected.
- const sortKeys = fields.map(field => fieldToSort(field, undefined)?.field);
- const newSort = newEventView.sorts.filter(
- sort => sort && sortKeys.includes(sort.field)
- );
- // If the sort field was removed, try and find a new sortable column.
- if (newSort.length === 0) {
- const sortField = fields.find(field => isFieldSortable(field, undefined));
- if (sortField) {
- newSort.push({field: sortField.field, kind: 'desc'});
- }
- }
- newEventView.sorts = newSort;
- }
- newEventView.yAxis = newEventView.getYAxis();
- return newEventView;
- }
- withNewColumn(newColumn: Column): EventView {
- const fieldAsString = generateFieldAsString(newColumn);
- const newField: Field = {
- field: fieldAsString,
- width: COL_WIDTH_UNDEFINED,
- };
- const newEventView = this.clone();
- newEventView.fields = [...newEventView.fields, newField];
- return newEventView;
- }
- withResizedColumn(columnIndex: number, newWidth: number) {
- const field = this.fields[columnIndex];
- const newEventView = this.clone();
- if (!field) {
- return newEventView;
- }
- const updateWidth = field.width !== newWidth;
- if (updateWidth) {
- const fields = [...newEventView.fields];
- fields[columnIndex] = {
- ...field,
- width: newWidth,
- };
- newEventView.fields = fields;
- }
- return newEventView;
- }
- withUpdatedColumn(
- columnIndex: number,
- updatedColumn: Column,
- tableMeta: MetaType | undefined
- ): EventView {
- const columnToBeUpdated = this.fields[columnIndex];
- const fieldAsString = generateFieldAsString(updatedColumn);
- const updateField = columnToBeUpdated.field !== fieldAsString;
- if (!updateField) {
- return this;
- }
- // ensure tableMeta is non-empty
- tableMeta = validateTableMeta(tableMeta);
- const newEventView = this.clone();
- const updatedField: Field = {
- field: fieldAsString,
- width: COL_WIDTH_UNDEFINED,
- };
- const fields = [...newEventView.fields];
- fields[columnIndex] = updatedField;
- newEventView.fields = fields;
- // if the updated column is one of the sorted columns, we may need to remove
- // it from the list of sorts
- const needleSortIndex = this.sorts.findIndex(sort =>
- isSortEqualToField(sort, columnToBeUpdated, tableMeta)
- );
- if (needleSortIndex >= 0) {
- const needleSort = this.sorts[needleSortIndex];
- const numOfColumns = this.fields.reduce((sum, currentField) => {
- if (isSortEqualToField(needleSort, currentField, tableMeta)) {
- return sum + 1;
- }
- return sum;
- }, 0);
- // do not bother deleting the sort key if there are more than one columns
- // of it in the table.
- if (numOfColumns <= 1) {
- if (isFieldSortable(updatedField, tableMeta)) {
- // use the current updated field as the sort key
- const sort = fieldToSort(updatedField, tableMeta)!;
- // preserve the sort kind
- sort.kind = needleSort.kind;
- const sorts = [...newEventView.sorts];
- sorts[needleSortIndex] = sort;
- newEventView.sorts = sorts;
- } else {
- const sorts = [...newEventView.sorts];
- sorts.splice(needleSortIndex, 1);
- newEventView.sorts = [...new Set(sorts)];
- }
- }
- if (newEventView.sorts.length <= 0 && newEventView.fields.length > 0) {
- // establish a default sort by finding the first sortable field
- if (isFieldSortable(updatedField, tableMeta)) {
- // use the current updated field as the sort key
- const sort = fieldToSort(updatedField, tableMeta)!;
- // preserve the sort kind
- sort.kind = needleSort.kind;
- newEventView.sorts = [sort];
- } else {
- const sortableFieldIndex = newEventView.fields.findIndex(currentField =>
- isFieldSortable(currentField, tableMeta)
- );
- if (sortableFieldIndex >= 0) {
- const fieldToBeSorted = newEventView.fields[sortableFieldIndex];
- const sort = fieldToSort(fieldToBeSorted, tableMeta)!;
- newEventView.sorts = [sort];
- }
- }
- }
- }
- newEventView.yAxis = newEventView.getYAxis();
- return newEventView;
- }
- withDeletedColumn(columnIndex: number, tableMeta: MetaType | undefined): EventView {
- // Disallow removal of the orphan column, and check for out-of-bounds
- if (this.fields.length <= 1 || this.fields.length <= columnIndex || columnIndex < 0) {
- return this;
- }
- // ensure tableMeta is non-empty
- tableMeta = validateTableMeta(tableMeta);
- // delete the column
- const newEventView = this.clone();
- const fields = [...newEventView.fields];
- fields.splice(columnIndex, 1);
- newEventView.fields = fields;
- // Ensure there is at least one auto width column
- // To ensure a well formed table results.
- const hasAutoIndex = fields.find(field => field.width === COL_WIDTH_UNDEFINED);
- if (!hasAutoIndex) {
- newEventView.fields[0].width = COL_WIDTH_UNDEFINED;
- }
- // if the deleted column is one of the sorted columns, we need to remove
- // it from the list of sorts
- const columnToBeDeleted = this.fields[columnIndex];
- const needleSortIndex = this.sorts.findIndex(sort =>
- isSortEqualToField(sort, columnToBeDeleted, tableMeta)
- );
- if (needleSortIndex >= 0) {
- const needleSort = this.sorts[needleSortIndex];
- const numOfColumns = this.fields.reduce((sum, field) => {
- if (isSortEqualToField(needleSort, field, tableMeta)) {
- return sum + 1;
- }
- return sum;
- }, 0);
- // do not bother deleting the sort key if there are more than one columns
- // of it in the table.
- if (numOfColumns <= 1) {
- const sorts = [...newEventView.sorts];
- sorts.splice(needleSortIndex, 1);
- newEventView.sorts = [...new Set(sorts)];
- if (newEventView.sorts.length <= 0 && newEventView.fields.length > 0) {
- // establish a default sort by finding the first sortable field
- const sortableFieldIndex = newEventView.fields.findIndex(field =>
- isFieldSortable(field, tableMeta)
- );
- if (sortableFieldIndex >= 0) {
- const fieldToBeSorted = newEventView.fields[sortableFieldIndex];
- const sort = fieldToSort(fieldToBeSorted, tableMeta)!;
- newEventView.sorts = [sort];
- }
- }
- }
- }
- newEventView.yAxis = newEventView.getYAxis();
- return newEventView;
- }
- withTeams(teams: ('myteams' | number)[]): EventView {
- const newEventView = this.clone();
- newEventView.team = teams;
- return newEventView;
- }
- getSorts(): TableColumnSort<React.ReactText>[] {
- return this.sorts.map(
- sort =>
- ({
- key: sort.field,
- order: sort.kind,
- } as TableColumnSort<string>)
- );
- }
- // returns query input for the search
- getQuery(inputQuery: string | string[] | null | undefined): string {
- const queryParts: string[] = [];
- if (this.query) {
- if (this.additionalConditions) {
- queryParts.push(this.getQueryWithAdditionalConditions());
- } else {
- queryParts.push(this.query);
- }
- }
- if (inputQuery) {
- // there may be duplicate query in the query string
- // e.g. query=hello&query=world
- if (Array.isArray(inputQuery)) {
- inputQuery.forEach(query => {
- if (typeof query === 'string' && !queryParts.includes(query)) {
- queryParts.push(query);
- }
- });
- }
- if (typeof inputQuery === 'string' && !queryParts.includes(inputQuery)) {
- queryParts.push(inputQuery);
- }
- }
- return queryParts.join(' ');
- }
- getFacetsAPIPayload(
- location: Location
- ): Exclude<EventQuery & LocationQuery, 'sort' | 'cursor'> {
- const payload = this.getEventsAPIPayload(location);
- const remove = [
- 'id',
- 'name',
- 'per_page',
- 'sort',
- 'cursor',
- 'field',
- 'equation',
- 'interval',
- ];
- for (const key of remove) {
- delete payload[key];
- }
- return payload;
- }
- normalizeDateSelection(location: Location) {
- const query = (location && location.query) || {};
- // pick only the query strings that we care about
- const picked = pickRelevantLocationQueryStrings(location);
- const hasDateSelection = this.statsPeriod || (this.start && this.end);
- // an eventview's date selection has higher precedence than the date selection in the query string
- const dateSelection = hasDateSelection
- ? {
- start: this.start,
- end: this.end,
- statsPeriod: this.statsPeriod,
- }
- : {
- start: picked.start,
- end: picked.end,
- period: decodeScalar(query.period),
- statsPeriod: picked.statsPeriod,
- };
- // normalize datetime selection
- return normalizeDateTimeParams({
- ...dateSelection,
- utc: decodeScalar(query.utc),
- });
- }
- // Takes an EventView instance and converts it into the format required for the events API
- getEventsAPIPayload(
- location: Location,
- forceAppendRawQueryString?: string
- ): EventQuery & LocationQuery {
- // pick only the query strings that we care about
- const picked = pickRelevantLocationQueryStrings(location);
- // normalize datetime selection
- const normalizedTimeWindowParams = this.normalizeDateSelection(location);
- const sort =
- this.sorts.length <= 0
- ? undefined
- : this.sorts.length > 1
- ? encodeSorts(this.sorts)
- : encodeSort(this.sorts[0]);
- const fields = this.getFields();
- const team = this.team.map(proj => String(proj));
- const project = this.project.map(proj => String(proj));
- const environment = this.environment as string[];
- let queryString = this.getQueryWithAdditionalConditions();
- if (forceAppendRawQueryString) {
- queryString += ' ' + forceAppendRawQueryString;
- }
- // generate event query
- const eventQuery = Object.assign(
- omit(picked, DATETIME_QUERY_STRING_KEYS),
- normalizedTimeWindowParams,
- {
- team,
- project,
- environment,
- field: [...new Set(fields)],
- sort,
- per_page: DEFAULT_PER_PAGE,
- query: queryString,
- }
- ) as EventQuery & LocationQuery;
- if (eventQuery.team && !eventQuery.team.length) {
- delete eventQuery.team;
- }
- if (!eventQuery.sort) {
- delete eventQuery.sort;
- }
- return eventQuery;
- }
- getResultsViewUrlTarget(slug: string): {pathname: string; query: Query} {
- return {
- pathname: `/organizations/${slug}/discover/results/`,
- query: this.generateQueryStringObject(),
- };
- }
- getResultsViewShortUrlTarget(slug: string): {pathname: string; query: Query} {
- const output = {id: this.id};
- for (const field of [...Object.values(URL_PARAM), 'cursor']) {
- if (this[field] && this[field].length) {
- output[field] = this[field];
- }
- }
- return {
- pathname: `/organizations/${slug}/discover/results/`,
- query: cloneDeep(output as any),
- };
- }
- getPerformanceTransactionEventsViewUrlTarget(
- slug: string,
- options: {
- breakdown?: SpanOperationBreakdownFilter;
- showTransactions?: EventsDisplayFilterName;
- webVital?: WebVital;
- }
- ): {pathname: string; query: Query} {
- const {showTransactions, breakdown, webVital} = options;
- const output = {
- sort: encodeSorts(this.sorts),
- project: this.project,
- query: this.query,
- transaction: this.name,
- showTransactions,
- breakdown,
- webVital,
- };
- for (const field of EXTERNAL_QUERY_STRING_KEYS) {
- if (this[field] && this[field].length) {
- output[field] = this[field];
- }
- }
- const query = cloneDeep(output as any);
- return {
- pathname: `/organizations/${slug}/performance/summary/events/`,
- query,
- };
- }
- sortForField(field: Field, tableMeta: MetaType | undefined): Sort | undefined {
- if (!tableMeta) {
- return undefined;
- }
- return this.sorts.find(sort => isSortEqualToField(sort, field, tableMeta));
- }
- sortOnField(
- field: Field,
- tableMeta: MetaType,
- kind?: 'desc' | 'asc',
- useFunctionFormat?: boolean
- ): EventView {
- // check if field can be sorted
- if (!isFieldSortable(field, tableMeta)) {
- return this;
- }
- const needleIndex = this.sorts.findIndex(sort =>
- isSortEqualToField(sort, field, tableMeta)
- );
- if (needleIndex >= 0) {
- const newEventView = this.clone();
- const currentSort = this.sorts[needleIndex];
- const sorts = [...newEventView.sorts];
- sorts[needleIndex] = kind
- ? setSortOrder(
- {...currentSort, ...(useFunctionFormat ? {field: field.field} : {})},
- kind
- )
- : reverseSort({
- ...currentSort,
- ...(useFunctionFormat ? {field: field.field} : {}),
- });
- newEventView.sorts = sorts;
- return newEventView;
- }
- // field is currently not sorted; so, we sort on it
- const newEventView = this.clone();
- // invariant: this is not falsey, since sortKey exists
- const sort = fieldToSort(field, tableMeta, kind, useFunctionFormat)!;
- newEventView.sorts = [sort];
- return newEventView;
- }
- getYAxisOptions(): SelectValue<string>[] {
- // Make option set and add the default options in.
- return uniqBy(
- this.getAggregateFields()
- // Only include aggregates that make sense to be graphable (eg. not string or date)
- .filter(
- (field: Field) =>
- isLegalYAxisType(aggregateOutputType(field.field)) ||
- isAggregateEquation(field.field)
- )
- .map((field: Field) => ({
- label: isEquation(field.field) ? getEquation(field.field) : field.field,
- value: field.field,
- }))
- .concat(CHART_AXIS_OPTIONS),
- 'value'
- );
- }
- getYAxis(): string {
- const yAxisOptions = this.getYAxisOptions();
- const yAxis = this.yAxis;
- const defaultOption = yAxisOptions[0].value;
- if (!yAxis) {
- return defaultOption;
- }
- // ensure current selected yAxis is one of the items in yAxisOptions
- const result = yAxisOptions.findIndex(
- (option: SelectValue<string>) => option.value === yAxis
- );
- if (result >= 0) {
- return yAxis;
- }
- return defaultOption;
- }
- getDisplayOptions(): SelectValue<string>[] {
- return DISPLAY_MODE_OPTIONS.map(item => {
- if (item.value === DisplayModes.PREVIOUS) {
- if (this.start || this.end) {
- return {...item, disabled: true};
- }
- }
- if (item.value === DisplayModes.TOP5 || item.value === DisplayModes.DAILYTOP5) {
- if (this.getAggregateFields().length === 0) {
- return {
- ...item,
- disabled: true,
- tooltip: t('Add a function that groups events to use this view.'),
- };
- }
- }
- if (item.value === DisplayModes.DAILY || item.value === DisplayModes.DAILYTOP5) {
- if (this.getDays() < 1) {
- return {
- ...item,
- disabled: true,
- tooltip: t('Change the date rage to at least 1 day to use this view.'),
- };
- }
- }
- return item;
- });
- }
- getDisplayMode() {
- const mode = this.display ?? DisplayModes.DEFAULT;
- const displayOptions = this.getDisplayOptions();
- let display = (Object.values(DisplayModes) as string[]).includes(mode)
- ? mode
- : DisplayModes.DEFAULT;
- const cond = option => option.value === display;
- // Just in case we define a fallback chain that results in an infinite loop.
- // The number 5 isn't anything special, its just larger than the longest fallback
- // chain that exists and isn't too big.
- for (let i = 0; i < 5; i++) {
- const selectedOption = displayOptions.find(cond);
- if (selectedOption && !selectedOption.disabled) {
- return display;
- }
- display = DISPLAY_MODE_FALLBACK_OPTIONS[display];
- }
- // after trying to find an enabled display mode and failing to find one,
- // we just use the default display mode
- return DisplayModes.DEFAULT;
- }
- getQueryWithAdditionalConditions() {
- const {query} = this;
- if (this.additionalConditions.isEmpty()) {
- return query;
- }
- const conditions = new MutableSearch(query);
- Object.entries(this.additionalConditions.filters).forEach(([tag, tagValues]) => {
- const existingTagValues = conditions.getFilterValues(tag);
- const newTagValues = tagValues.filter(
- tagValue => !existingTagValues.includes(tagValue)
- );
- if (newTagValues.length) {
- conditions.addFilterValues(tag, newTagValues);
- }
- });
- return conditions.formatString();
- }
- }
- export type ImmutableEventView = Readonly<Omit<EventView, 'additionalConditions'>>;
- const isFieldsSimilar = (
- currentValue: Array<string>,
- otherValue: Array<string>
- ): boolean => {
- // For equation's their order matters because we alias them based on index
- const currentEquations = currentValue.filter(isEquation);
- const otherEquations = otherValue.filter(isEquation);
- // Field orders don't matter, so using a set for comparison
- const currentFields = new Set(currentValue.filter(value => !isEquation(value)));
- const otherFields = new Set(otherValue.filter(value => !isEquation(value)));
- if (!isEqual(currentEquations, otherEquations)) {
- return false;
- }
- if (!isEqual(currentFields, otherFields)) {
- return false;
- }
- return true;
- };
- export const isAPIPayloadSimilar = (
- current: EventQuery & LocationQuery,
- other: EventQuery & LocationQuery
- ): boolean => {
- const currentKeys = new Set(Object.keys(current));
- const otherKeys = new Set(Object.keys(other));
- if (!isEqual(currentKeys, otherKeys)) {
- return false;
- }
- for (const key of currentKeys) {
- const currentValue = current[key];
- const otherValue = other[key];
- if (key === 'field') {
- if (!isFieldsSimilar(currentValue, otherValue)) {
- return false;
- }
- } else {
- const currentTarget = Array.isArray(currentValue)
- ? new Set(currentValue)
- : currentValue;
- const otherTarget = Array.isArray(otherValue) ? new Set(otherValue) : otherValue;
- if (!isEqual(currentTarget, otherTarget)) {
- return false;
- }
- }
- }
- return true;
- };
- export function pickRelevantLocationQueryStrings(location: Location) {
- const query = location.query || {};
- const picked = pick(query || {}, EXTERNAL_QUERY_STRING_KEYS);
- return picked;
- }
- export default EventView;
|