-
-
Notifications
You must be signed in to change notification settings - Fork 4.3k
fix(discover2): Various fixes #15315
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
02c7f86
add guard when table metadata is un-available
dashed 278e24e
pass normalized date selection to api payload
dashed cf1fd70
normalize datetime selection for the events chart
dashed c7753de
i force pushed and lost mark's commits :(
dashed 9dde0d7
tests/js/spec/views/eventsV2/eventView.spec.jsx
dashed 35b0de4
tests
dashed ce35d10
fix
dashed 5993d91
oops
dashed 8f7b093
validate statsPeriod, start, and end
dashed f164635
more fix
dashed f93d2ee
getParams test
dashed 1841183
update tests/js/spec/components/organizations/globalSelectionHeader.s…
dashed 5f7999e
add normalizeDateTimeString
dashed 92f0922
update tests
dashed 55082e5
update tests
dashed 2b8408c
fix
dashed 8fbf1f9
normalize datetime at saved query instead
dashed 56b4994
update tests
dashed 954c129
fix tests
dashed a24056e
move getParams
dashed File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
142 changes: 142 additions & 0 deletions
142
src/sentry/static/sentry/app/components/organizations/globalSelectionHeader/getParams.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
import {DEFAULT_STATS_PERIOD} from 'app/constants'; | ||
import {defined} from 'app/utils'; | ||
import moment from 'moment'; | ||
|
||
const STATS_PERIOD_PATTERN = '^\\d+[hdmsw]?$'; | ||
|
||
function validStatsPeriod(input: string) { | ||
return !!input.match(STATS_PERIOD_PATTERN); | ||
} | ||
|
||
const getStatsPeriodValue = ( | ||
maybe: string | string[] | undefined | null | ||
): string | undefined => { | ||
if (Array.isArray(maybe)) { | ||
if (maybe.length <= 0) { | ||
return undefined; | ||
} | ||
|
||
return maybe.find(validStatsPeriod); | ||
} | ||
|
||
if (typeof maybe === 'string' && validStatsPeriod(maybe)) { | ||
return maybe; | ||
} | ||
|
||
return undefined; | ||
}; | ||
|
||
// We normalize potential datetime strings into the form that would be valid | ||
// if it were to be parsed by datetime.strptime using the format %Y-%m-%dT%H:%M:%S.%f | ||
// This format was transformed to the form that moment.js understands using | ||
// https://gist.github.com/asafge/0b13c5066d06ae9a4446 | ||
const normalizeDateTimeString = ( | ||
input: string | undefined | null | ||
): string | undefined => { | ||
if (!input) { | ||
return undefined; | ||
} | ||
|
||
const parsed = moment.utc(input); | ||
|
||
if (!parsed.isValid()) { | ||
return undefined; | ||
} | ||
|
||
return parsed.format('YYYY-MM-DDTHH:mm:ss.SSS'); | ||
}; | ||
|
||
const getDateTimeString = ( | ||
maybe: string | string[] | undefined | null | ||
): string | undefined => { | ||
if (Array.isArray(maybe)) { | ||
if (maybe.length <= 0) { | ||
return undefined; | ||
} | ||
|
||
const result = maybe.find(needle => { | ||
return moment.utc(needle).isValid(); | ||
}); | ||
|
||
return normalizeDateTimeString(result); | ||
} | ||
|
||
return normalizeDateTimeString(maybe); | ||
}; | ||
|
||
const parseUtcValue = (utc: any) => { | ||
if (typeof utc !== 'undefined') { | ||
return utc === true || utc === 'true' ? 'true' : 'false'; | ||
} | ||
return undefined; | ||
}; | ||
|
||
const getUtcValue = (maybe: string | string[] | undefined | null): string | undefined => { | ||
if (Array.isArray(maybe)) { | ||
if (maybe.length <= 0) { | ||
return undefined; | ||
} | ||
|
||
return maybe.find(needle => { | ||
return !!parseUtcValue(needle); | ||
}); | ||
} | ||
|
||
maybe = parseUtcValue(maybe); | ||
|
||
if (typeof maybe === 'string') { | ||
return maybe; | ||
} | ||
|
||
return undefined; | ||
}; | ||
|
||
interface Params { | ||
start?: string | string[] | undefined | null; | ||
end?: string | string[] | undefined | null; | ||
period?: string | string[] | undefined | null; | ||
statsPeriod?: string | string[] | undefined | null; | ||
utc?: string | string[] | undefined | null; | ||
[others: string]: string | string[] | undefined | null; | ||
} | ||
|
||
// Filters out params with null values and returns a default | ||
// `statsPeriod` when necessary. | ||
// | ||
// Accepts `period` and `statsPeriod` but will only return `statsPeriod` | ||
// | ||
// TODO(billy): Make period parameter name consistent | ||
export function getParams(params: Params): {[key: string]: string | string[]} { | ||
const {start, end, period, statsPeriod, utc, ...otherParams} = params; | ||
|
||
// `statsPeriod` takes precendence for now | ||
let coercedPeriod = getStatsPeriodValue(statsPeriod) || getStatsPeriodValue(period); | ||
|
||
const dateTimeStart = getDateTimeString(start); | ||
const dateTimeEnd = getDateTimeString(end); | ||
|
||
if (!(dateTimeStart && dateTimeEnd)) { | ||
if (!coercedPeriod) { | ||
coercedPeriod = DEFAULT_STATS_PERIOD; | ||
} | ||
} | ||
|
||
// Filter null values | ||
return Object.entries({ | ||
statsPeriod: coercedPeriod, | ||
start: coercedPeriod ? null : dateTimeStart, | ||
end: coercedPeriod ? null : dateTimeEnd, | ||
// coerce utc into a string (it can be both: a string representation from router, | ||
// or a boolean from time range picker) | ||
utc: getUtcValue(utc), | ||
...otherParams, | ||
}) | ||
.filter(([_key, value]) => defined(value)) | ||
.reduce( | ||
(acc, [key, value]) => ({ | ||
...acc, | ||
[key]: value, | ||
}), | ||
{} | ||
); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 0 additions & 55 deletions
55
src/sentry/static/sentry/app/views/events/utils/getParams.tsx
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm seeing some issues stemming from the global selection header propagating down to discover2.
Invalid datetime selections can easily throw an error and break the app with no actual recovery flow.
I feel it's better to parse and validate the datetime selection early here; rather than doing it at the API payload generation everywhere.