-
-
Notifications
You must be signed in to change notification settings - Fork 76
Dashboard static range issues #210
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
base: master
Are you sure you want to change the base?
Changes from all commits
020a711
e1cb845
a7350ac
8aa733e
53dfc56
f30a71f
e54bd84
daac58e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -46,7 +46,7 @@ func Validate(value string) error { | |
func ParseTime(now time.Time, value string, startOf bool, weekday time.Weekday) (time.Time, error) { | ||
parse, err := time.Parse(time.RFC3339, value) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
First of I think it would be okay if we only support dates and not date times when specifying the range on the dashboard page. I think this is enough and it removes the need from the user to specify a time. When the "end" range then uses endOfDay of the given date. I imaging that the impl of the RelativeDateTimeSelector can look like this. If the parsing was unsuccessful, we just forward the invalid value to setValue, if successful, parseRelativeTime returns a normalized time which is either the raw relative time (e.g. As input we always get the formatted RFC3339 date, so we parse the value input of the component as RFC3339. const parsed = parseRelativeTime(value, type, moment(), 'YYYY-MM-DDTHH:mm:ssZ');
return (
<TextField
fullWidth
style={style}
// .localized is similar to .normalized but the opposite.
// if it's a absolute date, do date.format('l), otherwise return the raw relative date expression
value={parsed.localized}
disabled={disabled}
InputProps={{disableUnderline}}
onChange={(e) => {
const newValue = e.target.value;
const result = parseRelativeTime(value, type, moment(), 'l');
setValue(result.success ? result.normalized : newValue, result.success);
}}
error={!parsed.success}
helperText={
small ? undefined : !parsed.success ? (
<Typography color={'secondary'} variant={'caption'}>
{parsed.error}
</Typography>
) : (
<Typography variant={'caption'}>{parsed.preview.format('llll')}</Typography>
)
}
label={label}
/>
); There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So, basically merge the functionality of normalize/friendly methods I made into parseRelativeTime and get rid of them? + allowing omitting time and entering only date There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes |
||
if err == nil { | ||
return parse, nil | ||
return model.Time(parse).OmitTimeZone(), nil | ||
} | ||
|
||
return timemath.Parse(now, value, startOf, weekday) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,10 @@ | ||
import moment from 'moment'; | ||
import moment from 'moment-timezone'; | ||
|
||
interface Success { | ||
success: true; | ||
value: moment.Moment; | ||
preview: moment.Moment; | ||
localized: string; | ||
normalized: string; | ||
} | ||
|
||
interface Failure { | ||
|
@@ -32,9 +34,35 @@ enum Unit { | |
Second = 's', | ||
} | ||
|
||
// tslint:disable-next-line:cyclomatic-complexity mccabe-complexity | ||
export const parseRelativeTime = (value: string, divide: 'endOf' | 'startOf', nowDate = moment()): Success | Failure => { | ||
if (isValidDate(value)) { | ||
return success(asDate(value)); | ||
for (const format of ['YYYY-MM-DD HH:mm', 'YYYY-MM-DD', 'YYYY-MM-DD[T]HH:mm:ssZ']) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this should only support FYI: I'll be unable to properly review this until end of next week. |
||
if (isValidDate(value, format)) { | ||
const parsed = asDate(value, format); | ||
if (divide === 'endOf' && format === 'YYYY-MM-DD') { | ||
parsed.endOf('day'); | ||
} | ||
|
||
if (format === 'YYYY-MM-DD[T]HH:mm:ssZ') { | ||
const localDate = parsed.clone().local(); | ||
if ( | ||
(divide === 'startOf' && parsed.isSame(localDate.startOf('day'), 'second')) || | ||
(divide === 'endOf' && parsed.isSame(localDate.endOf('day'), 'second')) | ||
) { | ||
value = asDate(value).format('YYYY-MM-DD'); | ||
} else { | ||
value = asDate(value).format('YYYY-MM-DD HH:mm'); | ||
} | ||
} | ||
return success( | ||
parsed, | ||
value, | ||
parsed | ||
.clone() | ||
.utc() | ||
.format() | ||
); | ||
} | ||
} | ||
|
||
if (value.substr(0, 3) === 'now') { | ||
|
@@ -105,18 +133,18 @@ export const parseRelativeTime = (value: string, divide: 'endOf' | 'startOf', no | |
if (expectNext === Type.Value) { | ||
return failure('Expected number at the end but got nothing'); | ||
} | ||
return success(time); | ||
return success(time, value, value); | ||
} | ||
|
||
if (value.indexOf('now') !== -1) { | ||
return failure("'now' must be at the start"); | ||
} | ||
|
||
return failure("Expected valid date or 'now' at index 0"); | ||
return failure("Expected valid date (e.g. 2020-01-01 16:30) or 'now' at index 0"); | ||
}; | ||
|
||
export const success = (value: moment.Moment): Success => { | ||
return {success: true, value}; | ||
export const success = (value: moment.Moment, localized: string, normalized: string): Success => { | ||
return {success: true, preview: value, normalized, localized}; | ||
}; | ||
export const failure = (error: string): Failure => { | ||
return {success: false, error}; | ||
|
@@ -133,7 +161,7 @@ export const isValidDate = (value: string, format?: string) => { | |
return asDate(value, format).isValid(); | ||
}; | ||
|
||
export const asDate = (value: string, format = 'YYYY-MM-DD HH:mm') => { | ||
export const asDate = (value: string, format = 'YYYY-MM-DD[T]HH:mm:ssZ') => { | ||
return moment(value, format, true); | ||
}; | ||
export const isSameDate = (from: moment.Moment, to?: moment.Moment): boolean => { | ||
|
Uh oh!
There was an error while loading. Please reload this page.