feat: add Elasticsearch/Grafana date-math syntax to time filter - #2322
Open
pike00 wants to merge 1 commit into
Open
feat: add Elasticsearch/Grafana date-math syntax to time filter#2322pike00 wants to merge 1 commit into
pike00 wants to merge 1 commit into
Conversation
Adds now-relative date expressions (now-1y, now/M, now-1y/y, /fy, /fQ) and an explicit snap concept for date-math. All legacy syntax preserved.
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
Adds Elasticsearch/Grafana-style date-math expressions (
now-1y,now/M,now-1y/y, etc.) to the Fava time filter, with an explicit snap concept for period boundaries.New syntax
d(days),w(weeks),M(months),y(years)/d,/w,/M,/y,/fy(fiscal year),/fQ(fiscal quarter)now-30d - now,now/M - now,now-1y/y - now/yWithout an explicit snap, the unit determines the period (e.g.
now-1M= last calendar month, equivalent tonow-1M/M).Changes
Backend (
src/fava/util/date.py)DATEMATH_RE— regex fornow[+/-N][unit][/snap]_snap_date()— snaps tod/w/M/y/fy/fQboundaries_period_end_from_snap()— computes exclusive end from a snapped start_eval_datemath()— evaluates a single date-math expression_parse_datemath()— handles single and range date-math, tried before legacy parserparse_date_resolved()— returns(begin, end, "YYYY-MM-DD to YYYY-MM-DD")for UI/API use2024,year-1,FY2024,2024 - 2025) preservedTests (
tests/test_util_date.py)Docs (
docs/time-filter-syntax.md)Backward compatibility
All existing syntax continues to work unchanged. Date-math is tried first, then falls through to the legacy parser.
Snap concept
A snap rounds a date down to the start of a period. Without a snap,
now-1ymeans "exactly 1 year ago" (a single point). With a snap,now-1y/ymeans "the start of the year that was 1 year ago" (the full calendar year). Without an explicit snap, the unit implies the snap —now-1Mis equivalent tonow-1M/M.