-
-
Notifications
You must be signed in to change notification settings - Fork 4.6k
Expand file tree
/
Copy pathtransactionSearchQueryBuilder.tsx
More file actions
156 lines (142 loc) · 5.08 KB
/
transactionSearchQueryBuilder.tsx
File metadata and controls
156 lines (142 loc) · 5.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import {useCallback, useEffect, useMemo} from 'react';
import {fetchTagValues, loadOrganizationTags} from 'sentry/actionCreators/tags';
import {
STATIC_FIELD_TAGS_WITHOUT_ERROR_FIELDS,
STATIC_SEMVER_TAGS,
STATIC_SPAN_TAGS,
} from 'sentry/components/events/searchBarFieldConstants';
import {normalizeDateTimeParams} from 'sentry/components/pageFilters/parse';
import usePageFilters from 'sentry/components/pageFilters/usePageFilters';
import {SearchQueryBuilder} from 'sentry/components/searchQueryBuilder';
import type {GetTagValues} from 'sentry/components/searchQueryBuilder';
import type {CallbackSearchState} from 'sentry/components/searchQueryBuilder/types';
import {t} from 'sentry/locale';
import type {PageFilters} from 'sentry/types/core';
import {SavedSearchType, type TagCollection} from 'sentry/types/group';
import {defined} from 'sentry/utils';
import {
ALL_INSIGHTS_FILTER_KEY_SECTIONS,
isAggregateField,
isMeasurement,
} from 'sentry/utils/discover/fields';
import {DEVICE_CLASS_TAG_VALUES, FieldKind, isDeviceClass} from 'sentry/utils/fields';
import {getMeasurements} from 'sentry/utils/measurements/measurements';
import {getHasTag} from 'sentry/utils/tag';
import useApi from 'sentry/utils/useApi';
import useOrganization from 'sentry/utils/useOrganization';
import useTags from 'sentry/utils/useTags';
interface TransactionSearchQueryBuilderProps {
initialQuery: string;
searchSource: string;
datetime?: PageFilters['datetime'];
disableLoadingTags?: boolean;
disallowFreeText?: boolean;
filterKeyMenuWidth?: number;
onSearch?: (query: string, state: CallbackSearchState) => void;
placeholder?: string;
projects?: PageFilters['projects'] | readonly number[];
trailingItems?: React.ReactNode;
}
export function TransactionSearchQueryBuilder({
initialQuery,
searchSource,
datetime,
onSearch,
placeholder,
projects,
disallowFreeText = true,
disableLoadingTags,
filterKeyMenuWidth,
trailingItems,
}: TransactionSearchQueryBuilderProps) {
const api = useApi();
const organization = useOrganization();
const {selection} = usePageFilters();
const tags = useTags();
const placeholderText = useMemo(() => {
return placeholder ?? t('Search for events, users, tags, and more');
}, [placeholder]);
useEffect(() => {
if (!disableLoadingTags) {
loadOrganizationTags(api, organization.slug, selection);
}
}, [api, organization.slug, selection, disableLoadingTags]);
const filterTags = useMemo(() => {
const measurements = getMeasurements();
const combinedTags: TagCollection = {
...STATIC_SPAN_TAGS,
...STATIC_FIELD_TAGS_WITHOUT_ERROR_FIELDS,
...STATIC_SEMVER_TAGS,
...measurements,
...tags,
};
if (organization.features.includes('performance-transaction-summary-eap')) {
combinedTags['request.method'] = {
key: 'request.method',
name: 'request.method',
kind: FieldKind.FIELD,
};
}
combinedTags.has = getHasTag(combinedTags);
return combinedTags;
}, [organization.features, tags]);
const filterKeySections = useMemo(
() => [
...ALL_INSIGHTS_FILTER_KEY_SECTIONS,
{
value: 'custom_fields',
label: 'Custom Tags',
children: Object.keys(tags),
},
],
[tags]
);
// This is adapted from the `getEventFieldValues` function in `events/searchBar.tsx`
const getTransactionFilterTagValues = useCallback<GetTagValues>(
async (tag, queryString) => {
if (isAggregateField(tag.key) || isMeasurement(tag.key)) {
// We can't really auto suggest values for aggregate fields
// or measurements, so we simply don't
return Promise.resolve([]);
}
//
// device.class is stored as "numbers" in snuba, but we want to suggest high, medium,
// and low search filter values because discover maps device.class to these values.
if (isDeviceClass(tag.key)) {
return Promise.resolve(DEVICE_CLASS_TAG_VALUES);
}
try {
const results = await fetchTagValues({
api,
orgSlug: organization.slug,
tagKey: tag.key,
search: queryString,
projectIds: projects?.map(String) ?? selection.projects?.map(String),
includeTransactions: true,
sort: '-count',
endpointParams: normalizeDateTimeParams(datetime ?? selection.datetime),
});
return results.filter(({name}) => defined(name)).map(({name}) => name);
} catch (e) {
throw new Error(`Unable to fetch event field values: ${e}`);
}
},
[api, organization, datetime, projects, selection.datetime, selection.projects]
);
return (
<SearchQueryBuilder
placeholder={placeholderText}
filterKeys={filterTags}
initialQuery={initialQuery}
onSearch={onSearch}
searchSource={searchSource}
filterKeySections={filterKeySections}
getTagValues={getTransactionFilterTagValues}
disallowFreeText={disallowFreeText}
disallowUnsupportedFilters
recentSearches={SavedSearchType.EVENT}
filterKeyMenuWidth={filterKeyMenuWidth}
trailingItems={trailingItems}
/>
);
}