-
-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
Copy pathindex.tsx
103 lines (86 loc) · 2.85 KB
/
index.tsx
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
import React from 'react';
import {
ParseResult,
parseSearch,
Token,
TokenResult,
} from 'sentry/components/searchSyntax/parser';
import {AggregationKey, FieldKey, getFieldDefinition} from 'sentry/utils/fields';
import {
ON_DEMAND_METRICS_UNSUPPORTED_TAGS,
STANDARD_SEARCH_FIELD_KEYS,
} from 'sentry/utils/onDemandMetrics/constants';
function isStandardSearchFilterKey(key: string): boolean {
return STANDARD_SEARCH_FIELD_KEYS.has(key as FieldKey);
}
function isOnDemandSupportedFilterKey(key: string): boolean {
return !ON_DEMAND_METRICS_UNSUPPORTED_TAGS.has(key as FieldKey);
}
function isCustomTag(key: string): boolean {
return !getFieldDefinition(key);
}
export function createOnDemandFilterWarning(warning: React.ReactNode) {
return (key: string) => {
const fieldKey = key as FieldKey;
if (isCustomTag(fieldKey)) {
return warning;
}
if (!isStandardSearchFilterKey(fieldKey) && isOnDemandSupportedFilterKey(fieldKey)) {
return warning;
}
return null;
};
}
export function isOnDemandAggregate(aggregate: string): boolean {
return aggregate.includes(AggregationKey.APDEX);
}
export function isOnDemandQueryString(query: string): boolean {
const searchFilterKeys = getSearchFilterKeys(query);
const isStandardSearch = searchFilterKeys.every(isStandardSearchFilterKey);
const isOnDemandSupportedSearch = searchFilterKeys.some(isOnDemandSupportedFilterKey);
const hasCustomTags = searchFilterKeys.some(isCustomTag);
return !isStandardSearch && (isOnDemandSupportedSearch || hasCustomTags);
}
export function isOnDemandSearchKey(searchKey: string): boolean {
return (
!isStandardSearchFilterKey(searchKey) &&
(isOnDemandSupportedFilterKey(searchKey) || isCustomTag(searchKey))
);
}
type SearchFilter = {key: string; operator: string; value: string};
function getSearchFilterKeys(query: string): string[] {
try {
return getSearchFilters(query).map(({key}) => key);
} catch (e) {
return [];
}
}
export function getSearchFilters(query: string): SearchFilter[] {
try {
const tokens = parseSearch(query);
if (!tokens) {
return [];
}
return getSearchFiltersFromTokens(tokens);
} catch (e) {
return [];
}
}
function getSearchFiltersFromTokens(tokens: ParseResult): SearchFilter[] {
return tokens.flatMap(getTokenKeyValuePair).filter(Boolean) as SearchFilter[];
}
function getTokenKeyValuePair(
token: TokenResult<Token>
): SearchFilter[] | SearchFilter | null {
if (token.type === Token.LOGIC_GROUP) {
return getSearchFiltersFromTokens(token.inner);
}
if (token.type === Token.FILTER) {
return {key: token.key.text, operator: token.operator, value: token.value.text};
}
return null;
}
export function getOnDemandKeys(query: string): string[] {
const searchFilterKeys = getSearchFilterKeys(query);
return searchFilterKeys.filter(isOnDemandSearchKey);
}