-
-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
Copy pathutils.tsx
340 lines (297 loc) · 9.98 KB
/
utils.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
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
import {Fragment} from 'react';
import moment from 'moment-timezone';
import {DateTime} from 'sentry/components/dateTime';
import autoCompleteFilter from 'sentry/components/dropdownAutoComplete/autoCompleteFilter';
import type {ItemsBeforeFilter} from 'sentry/components/dropdownAutoComplete/types';
import {DEFAULT_RELATIVE_PERIODS} from 'sentry/constants';
import {t, tn} from 'sentry/locale';
import type {DateString} from 'sentry/types/core';
import {
DEFAULT_DAY_END_TIME,
DEFAULT_DAY_START_TIME,
getFormattedDate,
} from 'sentry/utils/dates';
import TimeRangeItemLabel from './timeRangeItemLabel';
type PeriodUnit = 's' | 'm' | 'h' | 'd' | 'w';
type RelativePeriodUnit = Exclude<PeriodUnit, 's'>;
export type RelativeUnitsMapping = {
[Unit: string]: {
convertToDaysMultiplier: number;
label: (num: number) => string;
momentUnit: moment.unitOfTime.DurationConstructor;
searchKey: string;
};
};
const DATE_TIME_FORMAT = 'YYYY-MM-DDTHH:mm:ss';
const STATS_PERIOD_REGEX = /^(\d+)([mhdw]{1})$/;
const SUPPORTED_RELATIVE_PERIOD_UNITS: RelativeUnitsMapping = {
m: {
label: (num: number) => tn('Last minute', 'Last %s minutes', num),
searchKey: t('minutes'),
momentUnit: 'minutes',
convertToDaysMultiplier: 1 / (60 * 24),
},
h: {
label: (num: number) => tn('Last hour', 'Last %s hours', num),
searchKey: t('hours'),
momentUnit: 'hours',
convertToDaysMultiplier: 1 / 24,
},
d: {
label: (num: number) => tn('Last day', 'Last %s days', num),
searchKey: t('days'),
momentUnit: 'days',
convertToDaysMultiplier: 1,
},
w: {
label: (num: number) => tn('Last week', 'Last %s weeks', num),
searchKey: t('weeks'),
momentUnit: 'weeks',
convertToDaysMultiplier: 7,
},
};
const SUPPORTED_RELATIVE_UNITS_LIST = Object.keys(
SUPPORTED_RELATIVE_PERIOD_UNITS
) as RelativePeriodUnit[];
const parseStatsPeriodString = (statsPeriodString: string) => {
const result = STATS_PERIOD_REGEX.exec(statsPeriodString);
if (result === null) {
throw new Error('Invalid stats period');
}
const value = parseInt(result[1], 10);
const unit = result[2] as RelativePeriodUnit;
return {
value,
unit,
};
};
/**
* Converts a relative stats period, e.g. `1h` to an object containing a start
* and end date, with the end date as the current time and the start date as the
* time that is the current time less the statsPeriod.
*
* @param statsPeriod Relative stats period
* @param outputFormat Format of outputted start/end date
* @return Object containing start and end date as YYYY-MM-DDTHH:mm:ss
*
*/
export function parseStatsPeriod(
statsPeriod: string,
outputFormat: string | null = DATE_TIME_FORMAT
): {end: string; start: string} {
const {value, unit} = parseStatsPeriodString(statsPeriod);
const momentUnit = SUPPORTED_RELATIVE_PERIOD_UNITS[unit].momentUnit;
const format = outputFormat === null ? undefined : outputFormat;
return {
start: moment().subtract(value, momentUnit).format(format),
end: moment().format(format),
};
}
/**
* Given a relative stats period, e.g. `1h`, return a pretty string if it
* is a default stats period. Otherwise if it's a valid period (can be any number
* followed by a single character s|m|h|d) display "Other" or "Invalid period" if invalid
*
* @param relative Relative stats period
* @return either one of the default "Last x days" string, "Other" if period is valid on the backend, or "Invalid period" otherwise
*/
export function getRelativeSummary(
relative: string,
relativeOptions?: Record<string, React.ReactNode>
): string {
try {
const defaultRelativePeriodString =
relativeOptions?.[relative] ?? DEFAULT_RELATIVE_PERIODS[relative];
if (defaultRelativePeriodString) {
return defaultRelativePeriodString;
}
const {value, unit} = parseStatsPeriodString(relative);
return SUPPORTED_RELATIVE_PERIOD_UNITS[unit].label(value);
} catch {
return 'Invalid period';
}
}
/**
* Returns an absolute time range summary given the start and end timestamps. If the
* start/end time coincides with the default day start/end time, then the returned
* summary will include the date only (e.g. "Jan 1–Jan 2"). Otherwise, both the date and
* time will be shown (e.g. "Jan 1, 1:00 AM–Jan 2, 11:00PM").
*/
export function getAbsoluteSummary(
start: DateString,
end: DateString,
utc?: boolean | null
) {
const startTimeFormatted = getFormattedDate(start, 'HH:mm:ss', {local: true});
const endTimeFormatted = getFormattedDate(end, 'HH:mm:ss', {local: true});
const showDateOnly =
startTimeFormatted === DEFAULT_DAY_START_TIME &&
endTimeFormatted === DEFAULT_DAY_END_TIME;
return (
<Fragment>
<DateTime date={start} dateOnly={showDateOnly} utc={!!utc} />
{'–'}
<DateTime date={end} dateOnly={showDateOnly} utc={!!utc} />
</Fragment>
);
}
export function makeItem(
amount: number,
unit: string,
label: (num: number) => string,
index: number
) {
return {
value: `${amount}${unit}`,
['data-test-id']: `${amount}${unit}`,
label: <TimeRangeItemLabel>{label(amount)}</TimeRangeItemLabel>,
searchKey: `${amount}${unit}`,
index,
};
}
function timePeriodIsWithinLimit<T extends RelativeUnitsMapping>({
amount,
unit,
maxDays,
supportedPeriods,
}: {
amount: number;
supportedPeriods: T;
unit: keyof T & string;
maxDays?: number;
}) {
if (!maxDays) {
return true;
}
const daysMultiplier = supportedPeriods[unit].convertToDaysMultiplier;
const numberOfDays = amount * daysMultiplier;
return numberOfDays <= maxDays;
}
/**
* A custom autocomplete implementation for <TimeRangeSelector />
* This function generates relative time ranges based on the user's input (not limited to those present in the initial set).
*
* When the user begins their input with a number, we provide all unit options for them to choose from:
* "5" => ["Last 5 seconds", "Last 5 minutes", "Last 5 hours", "Last 5 days", "Last 5 weeks"]
*
* When the user adds text after the number, we filter those options to the matching unit:
* "5d" => ["Last 5 days"]
* "5 days" => ["Last 5 days"]
*
* If the input does not begin with a number, we do a simple filter of the preset options.
*/
export const _timeRangeAutoCompleteFilter = function <T extends RelativeUnitsMapping>(
items: ItemsBeforeFilter | null,
filterValue: string,
{
supportedPeriods,
supportedUnits,
maxDays,
maxDateRange,
}: {
supportedPeriods: T;
supportedUnits: Array<keyof T & string>;
maxDateRange?: number;
maxDays?: number;
}
): ReturnType<typeof autoCompleteFilter> {
if (!items) {
return [];
}
const match = filterValue.match(/(?<digits>\d+)\s*(?<string>\w*)/);
const userSuppliedAmount = Number(match?.groups?.digits);
const userSuppliedUnits = (match?.groups?.string ?? '').trim().toLowerCase();
const userSuppliedAmountIsValid = !isNaN(userSuppliedAmount) && userSuppliedAmount > 0;
// If there is a number w/o units, show all unit options within limit
if (userSuppliedAmountIsValid && !userSuppliedUnits) {
return supportedUnits
.filter(unit =>
timePeriodIsWithinLimit({
amount: userSuppliedAmount,
unit,
maxDays: maxDateRange ?? maxDays,
supportedPeriods,
})
)
.map((unit, index) =>
makeItem(userSuppliedAmount, unit, supportedPeriods[unit].label, index)
);
}
// If there is a number followed by units, show the matching number/unit option
if (userSuppliedAmountIsValid && userSuppliedUnits) {
const matchingUnit = supportedUnits.find(unit => {
if (userSuppliedUnits.length === 1) {
return unit === userSuppliedUnits;
}
return supportedPeriods[unit].searchKey.startsWith(userSuppliedUnits);
});
if (
matchingUnit &&
timePeriodIsWithinLimit({
amount: userSuppliedAmount,
unit: matchingUnit,
maxDays,
supportedPeriods,
})
) {
return [
makeItem(
userSuppliedAmount,
matchingUnit,
supportedPeriods[matchingUnit].label,
0
),
];
}
}
// Otherwise, do a normal filter search
return autoCompleteFilter(items, filterValue);
};
export const timeRangeAutoCompleteFilter = function (
items: ItemsBeforeFilter | null,
filterValue: string,
options: {
maxDateRange?: number;
maxDays?: number;
supportedPeriods?: RelativeUnitsMapping;
supportedUnits?: RelativePeriodUnit[];
}
): ReturnType<typeof autoCompleteFilter> {
return _timeRangeAutoCompleteFilter(items, filterValue, {
supportedPeriods: SUPPORTED_RELATIVE_PERIOD_UNITS,
supportedUnits: SUPPORTED_RELATIVE_UNITS_LIST,
...options,
});
};
/**
* Returns an object whose key is the arbitrary period string and whose value is a
* human-readable label for that period. E.g. '2h' returns {'2h': 'Last 2 hours'}.
*/
export function getArbitraryRelativePeriod(arbitraryPeriod?: string | null) {
// If arbitraryPeriod is invalid
if (!arbitraryPeriod || !STATS_PERIOD_REGEX.exec(arbitraryPeriod)) {
return {};
}
// Get the custom period label ("8D" --> "8 Days")
const {value, unit} = parseStatsPeriodString(arbitraryPeriod);
return {[arbitraryPeriod]: SUPPORTED_RELATIVE_PERIOD_UNITS[unit].label(value)};
}
/**
* Returns an object with sorted relative time periods, where the period with the most
* recent start time comes first (e.g. 1H — 2H - 1D — 7D…)
*/
export function getSortedRelativePeriods(
relativePeriods: Record<string, React.ReactNode>
) {
const entries = Object.entries(relativePeriods);
const validPeriods = entries.filter(([period]) => !!STATS_PERIOD_REGEX.exec(period));
const invalidPeriods = entries.filter(([period]) => !STATS_PERIOD_REGEX.exec(period));
const sortedValidPeriods = validPeriods.sort((a, b) => {
const [periodA] = a;
const [periodB] = b;
return moment(parseStatsPeriod(periodB).start).diff(
moment(parseStatsPeriod(periodA).start)
);
});
return Object.fromEntries(invalidPeriods.concat(sortedValidPeriods));
}