-
Notifications
You must be signed in to change notification settings - Fork 274
feat(time-format): improve support for formatting with granularity in mind #509
Conversation
This pull request is being automatically deployed with Vercel (learn more). 🔍 Inspect: https://vercel.com/superset/superset-ui/q2ddcb6he |
Codecov Report
@@ Coverage Diff @@
## master #509 +/- ##
==========================================
+ Coverage 22.63% 23.32% +0.68%
==========================================
Files 281 286 +5
Lines 6692 6752 +60
Branches 648 672 +24
==========================================
+ Hits 1515 1575 +60
Misses 5138 5138
Partials 39 39
Continue to review full report at Codecov.
|
@@ -1,5 +1,5 @@ | |||
import { utcFormat, timeFormat } from 'd3-time-format'; | |||
import { utcUtils, localTimeUtils } from '../utils'; | |||
import { utcUtils, localTimeUtils } from '../utils/d3Time'; |
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.
move and rename file
/** | ||
* search for `builtin_time_grains` in incubator-superset/superset/db_engine_specs/base.py | ||
*/ | ||
export const TimeGranularity = { |
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.
Make this also a constant to avoid having to type the awkward granularity string for WEEK_ENDING_xxx
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.
In theory, this is definitely a nice feature to have, but I'm worried whether it will lead to potential confusions if we do date truncating on both backend and frontend----users might see inconsistent datetime in charts and raw data.
What is the use case for the new API?
packages/superset-ui-time-format/src/utils/createTimeRangeFromGranularity.ts
Outdated
Show resolved
Hide resolved
} | ||
|
||
// Year | ||
return deductOneMs(createDate({ year: year + 1, useLocalTime })); |
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.
How you thought of utilize d3-time
intervals? https://github.com/d3/d3-time#interval_round
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.
That could work. Will look into it.
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.
Tried it. The floor
and ceil
functions work if the value is in the middle of the range but not so useful for the case we are handlings that the inputs are already at the beginning of the range. Will have to add offset before calling ceil
. It also ceils to YYYY-mm-dd 00:00:00.000
instead of YYYY-mm-dd 23.59.59.999
that I would like.
Could you elaborate on the truncation? I am not sure I understand what you mean. |
|
For example, when a data point is returned from database, it seems like a single point in time.
However, it is rarely a single point in time. If this query has This is what the actual data point is.
so Now we know the time range is
|
I mean It calculates an Maybe I was just overthinking. |
The granularity
This is already the current behavior I think. This feature allows it to be more correct if the granularity is passed to the formatter. It is an opt-in feature and should not affect charts (except |
2c064d1
to
08ac1b5
Compare
🏆 Enhancements
What are the changes?
Extends core time formatting functions to include granularity support
getTimeFormatter(formatId)
togetTimeFormatter(formatId?: string, granularity?: TimeGranularity)
formatTime(formatId, value)
toformatTime(formatId: string | undefined, value: Date | null | undefined, granularity?: TimeGranularity)
New APIs
getTimeRangeFormatter(formatId?: string)
formatTimeRange(formatId: string | undefined, range: (Date | null | undefined)[])
Others
Convert the
granularity
string into constants.Explanation of technical changes
When a
time
includesgranularity
, it means the giventime
is not a single point in time, but a time range. The changes in this PR includes logic for reconstructing the time range fromtime
andgranularity
. The current logic assumes that the input time will be a valid output from database based on thegranularity
. For example, if thegranularity
isP1M
(monthly), the database will always return the first day of the month.Formatting a time point with granularity then follows these steps:
time
andgranularity
into a time range.TimeRangeFormatter
, along withformatId
(~d3 format string) or if no format is specified, look up default format string from a lookup table based ongranularity
(TimeFormatsForGranularity
)TimeRangeFormatter
at the moment is quite naive. Just try to apply same formatting to thestart
andend
of the time range. If bothstart
andend
have the same output then return juststart
, otherwise return${start} - ${end}
.See more concrete examples in the unit tests.
Future plans
TimeRangeFormatter
can also have its own registry in case developer wants to override how certain format works for range.TimeFormatsForGranularity
, which each application may have different opinion about default settings.