-
Notifications
You must be signed in to change notification settings - Fork 1.9k
feat(cubejs-client-core): add custom granularities support to drilldown() #10094
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
Open
paco-valdez
wants to merge
11
commits into
cube-js:master
Choose a base branch
from
paco-valdez:drill-down-custom-granularities
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+302
−22
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
77528ec
Fix drilldown() for custom granularities
paco-valdez b36ba74
Merge branch 'master' into drill-down-custom-granularities
paco-valdez ab04e00
fix dayRange func
paco-valdez 3b93ada
Merge branch 'drill-down-custom-granularities' of github.com:paco-val…
paco-valdez 2f0d2da
use dayRange instead of new func
paco-valdez 2d68100
remove new func
paco-valdez e65f81d
Merge branch 'master' into drill-down-custom-granularities
paco-valdez a5f6729
fix lint
paco-valdez 2c1bfb1
'Add case for non-aligned origin'
paco-valdez 871a974
Merge branch 'master' into drill-down-custom-granularities
paco-valdez cb5b9f1
Merge branch 'master' into drill-down-custom-granularities
paco-valdez 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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -79,25 +79,6 @@ export const isPredefinedGranularity = (granularity: TimeDimensionGranularity): | |
| export const DateRegex = /^\d\d\d\d-\d\d-\d\d$/; | ||
| export const LocalDateRegex = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}.\d{3}Z?$/; | ||
|
|
||
| export const dayRange = (from: any, to: any): DayRange => ({ | ||
| by: (value: any) => { | ||
| const results = []; | ||
|
|
||
| let start = internalDayjs(from); | ||
| const end = internalDayjs(to); | ||
|
|
||
| while (start.startOf(value).isBefore(end) || start.isSame(end)) { | ||
| results.push(start); | ||
| start = start.add(1, value); | ||
| } | ||
|
|
||
| return results; | ||
| }, | ||
| snapTo: (value: any): DayRange => dayRange(internalDayjs(from).startOf(value), internalDayjs(to).endOf(value)), | ||
| start: internalDayjs(from), | ||
| end: internalDayjs(to), | ||
| }); | ||
|
|
||
| /** | ||
| * Parse PostgreSQL-like interval string into object | ||
| * E.g. '2 years 15 months 100 weeks 99 hours 15 seconds' | ||
|
|
@@ -202,6 +183,65 @@ function alignToOrigin(startDate: dayjs.Dayjs, interval: ParsedInterval, origin: | |
| return alignedDate; | ||
| } | ||
|
|
||
| export const dayRange = (from: any, to: any, annotations?: Record<string, { granularity?: Granularity }>): DayRange => ({ | ||
| by: (value: any) => { | ||
| const results = []; | ||
|
|
||
| let start = internalDayjs(from); | ||
| const end = internalDayjs(to); | ||
|
|
||
| while (start.startOf(value).isBefore(end) || start.isSame(end)) { | ||
| results.push(start); | ||
| start = start.add(1, value); | ||
| } | ||
|
|
||
| return results; | ||
| }, | ||
| snapTo: (value: any): DayRange => { | ||
| // Check if this is a custom granularity | ||
| if (!isPredefinedGranularity(value) && annotations) { | ||
| // Try to find the custom granularity metadata | ||
| // The annotation key might be in format "Cube.dimension.granularity" | ||
| // So we need to search through all annotations | ||
| let customGranularity: Granularity | undefined; | ||
|
|
||
| for (const key of Object.keys(annotations)) { | ||
| if (key.endsWith(`.${value}`) && annotations[key].granularity) { | ||
| customGranularity = annotations[key].granularity; | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| if (customGranularity?.interval) { | ||
| // For custom granularities, calculate the range for the bucket | ||
| const intervalParsed = parseSqlInterval(customGranularity.interval); | ||
| let intervalStart = internalDayjs(from); | ||
|
|
||
| // If custom granularity has an origin, align to it | ||
| if (customGranularity.origin) { | ||
| let origin = internalDayjs(customGranularity.origin); | ||
| if (customGranularity.offset) { | ||
|
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. offset and origin are mutually exclusive |
||
| origin = addInterval(origin, parseSqlInterval(customGranularity.offset)); | ||
| } | ||
|
|
||
| // Align the value to the origin to find the actual bucket start | ||
| intervalStart = alignToOrigin(intervalStart, intervalParsed, origin); | ||
| } | ||
|
|
||
| // End is start + interval - 1 millisecond (to stay within the bucket) | ||
| const intervalEnd = addInterval(intervalStart, intervalParsed).subtract(1, 'millisecond'); | ||
|
|
||
| return dayRange(intervalStart, intervalEnd, annotations); | ||
| } | ||
| } | ||
|
|
||
| // Default behavior for predefined granularities | ||
| return dayRange(internalDayjs(from).startOf(value), internalDayjs(to).endOf(value), annotations); | ||
| }, | ||
| start: internalDayjs(from), | ||
| end: internalDayjs(to), | ||
| }); | ||
|
|
||
| /** | ||
| * Returns the time series points for the custom interval | ||
| * TODO: It's almost a copy/paste of timeSeriesFromCustomInterval from | ||
|
|
||
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.
How would this work for custom intervals like
3 daysor2 days 3 hours 4 minutes?