forked from apache/superset
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Handle "ambiguous durations" (apache#5785)
* WIP * Update interface * Fix truncate * Improve unit tests * Improve code * Use moment.js to parse ISO durations * Fix typo and React props (cherry picked from commit f740974) (cherry picked from commit 25ac26d)
- Loading branch information
1 parent
cac159e
commit 3274ae4
Showing
7 changed files
with
1,855 additions
and
178 deletions.
There are no files selected for viewing
This file contains 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 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 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 |
---|---|---|
@@ -1,38 +1,106 @@ | ||
import parseIsoDuration from 'parse-iso-duration'; | ||
import moment from 'moment'; | ||
|
||
|
||
// array with the minimum values of each part of a timestamp -- note that | ||
// months are zero-indexed in Javascript | ||
const truncatePartTo = [ | ||
1, // year | ||
0, // month | ||
1, // day | ||
0, // hour | ||
0, // minute | ||
0, // second | ||
0, // millisecond | ||
]; | ||
|
||
|
||
export function truncate(timestamp, step) { | ||
/* | ||
* Truncate timestamp down to duration resolution. | ||
*/ | ||
const lowerBound = moment(timestamp).subtract(step); | ||
const explodedTimestamp = timestamp.toArray(); | ||
const explodedLowerBound = lowerBound.toArray(); | ||
|
||
const firstDiffIndex = explodedTimestamp | ||
.map((part, i) => (explodedLowerBound[i] !== part)) | ||
.indexOf(true); | ||
const dateParts = explodedTimestamp.map((part, i) => { | ||
if (i === firstDiffIndex) { | ||
// truncate down to closest `truncatePartTo[i] + n * step` | ||
const difference = part - explodedLowerBound[i]; | ||
return part - ((part - truncatePartTo[i]) % difference); | ||
} else if (i < firstDiffIndex || firstDiffIndex === -1) { | ||
return part; | ||
} | ||
return truncatePartTo[i]; | ||
}); | ||
|
||
return moment(dateParts); | ||
} | ||
|
||
function getStepSeconds(step, start) { | ||
/* Return number of seconds in a step. | ||
* | ||
* The step might be ambigous, eg, "1 month" has a variable number of | ||
* seconds, which is why we need to know the start time. | ||
*/ | ||
const startMillliseconds = parseInt(moment(start).format('x'), 10); | ||
const endMilliseconds = parseInt(moment(start).add(step).format('x'), 10); | ||
return endMilliseconds - startMillliseconds; | ||
} | ||
|
||
export const getPlaySliderParams = function (timestamps, timeGrain) { | ||
let start = Math.min(...timestamps); | ||
let end = Math.max(...timestamps); | ||
const minTimestamp = moment(Math.min(...timestamps)); | ||
const maxTimestamp = moment(Math.max(...timestamps)); | ||
let step; | ||
let reference; | ||
|
||
if (timeGrain.indexOf('/') > 0) { | ||
if (timeGrain.indexOf('/') !== -1) { | ||
// Here, time grain is a time interval instead of a simple duration, either | ||
// `reference/duration` or `duration/reference`. We need to parse the | ||
// duration and make sure that start and end are in the right places. For | ||
// example, if `reference` is a Saturday and `duration` is 1 week (P1W) | ||
// then both start and end should be Saturdays. | ||
const parts = timeGrain.split('/', 2); | ||
let reference; | ||
if (parts[0].endsWith('Z')) { // ISO string | ||
reference = new Date(parts[0]).getTime(); | ||
step = parseIsoDuration(parts[1]); | ||
reference = moment(parts[0]); | ||
step = moment.duration(parts[1]); | ||
} else { | ||
reference = new Date(parts[1]).getTime(); | ||
step = parseIsoDuration(parts[0]); | ||
reference = moment(parts[1]); | ||
step = moment.duration(parts[0]); | ||
} | ||
start = reference + step * Math.floor((start - reference) / step); | ||
end = reference + step * (Math.floor((end - reference) / step) + 1); | ||
} else { | ||
// lock start and end to the closest steps | ||
step = parseIsoDuration(timeGrain); | ||
start -= start % step; | ||
end += step - end % step; | ||
step = moment.duration(timeGrain); | ||
reference = truncate(minTimestamp, step); | ||
} | ||
|
||
const values = timeGrain != null ? [start, start + step] : [start, end]; | ||
// find the largest `reference + n * step` smaller than the minimum timestamp | ||
const start = moment(reference); | ||
while (start < minTimestamp) { | ||
start.add(step); | ||
} | ||
while (start > minTimestamp) { | ||
start.subtract(step); | ||
} | ||
|
||
// find the smallest `reference + n * step` larger than the maximum timestamp | ||
const end = moment(reference); | ||
while (end > maxTimestamp) { | ||
end.subtract(step); | ||
} | ||
while (end < maxTimestamp) { | ||
end.add(step); | ||
} | ||
|
||
const values = timeGrain != null ? [start, moment(start).add(step)] : [start, end]; | ||
const disabled = timestamps.every(timestamp => timestamp === null); | ||
|
||
return { start, end, step, values, disabled }; | ||
return { | ||
start: parseInt(start.format('x'), 10), | ||
end: parseInt(end.format('x'), 10), | ||
getStep: getStepSeconds.bind(this, step), | ||
values: values.map(v => parseInt(v.format('x'), 10)), | ||
disabled, | ||
}; | ||
}; | ||
|
This file contains 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 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 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.