-
Notifications
You must be signed in to change notification settings - Fork 545
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
MQE time related functions #10486
Open
lamida
wants to merge
26
commits into
main
Choose a base branch
from
lamida/mqe-time-functions
base: main
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.
+371
−97
Open
MQE time related functions #10486
Changes from all commits
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
ecff315
Implement time functions
lamida 7389d10
Add more times function
lamida a71e555
Try restructuring the functions
lamida b9b6d83
Implement time related functions
lamida 59b2039
Remove unused field
lamida 001b929
Remove commented import
lamida ac31177
Add comment for readability
lamida 91989e2
Missed to uncomment test
lamida 9de48b5
Add missing license header
lamida 5ebc1ff
Refactor the time function name
lamida eeafd62
Missed function to uncomment
lamida 0e1f3a7
Split the file between times function and time operator
lamida 9b4361e
Push new times function
lamida 55e1998
Update pkg/streamingpromql/functions.go
lamida b0d0a7d
Move ok declaration inside if
lamida 2aa5ed6
Remove unneeded block
lamida 05bd286
Return histogram slice to pool in defer
lamida a75f93b
Fix unused parameter
lamida aa4d1f2
Add range query test for times function
lamida 2185f84
Add more time tests
lamida 83024da
Update pkg/streamingpromql/testdata/ours/functions.test
lamida 6e42b66
Update pkg/streamingpromql/testdata/ours/functions.test
lamida 2510116
Update pkg/streamingpromql/testdata/ours/functions.test
lamida 16843e7
Inline TimeFunctionOperatorFactory
lamida dbbba00
Fix the test
lamida 40cb57a
Return histogram to the poll
lamida 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 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 |
---|---|---|
@@ -0,0 +1,57 @@ | ||
// SPDX-License-Identifier: AGPL-3.0-only | ||
|
||
package functions | ||
|
||
import ( | ||
"time" | ||
|
||
"github.com/grafana/mimir/pkg/streamingpromql/limiting" | ||
"github.com/grafana/mimir/pkg/streamingpromql/types" | ||
) | ||
|
||
var DaysInMonth = timeWrapperFunc(func(t time.Time) float64 { | ||
return float64(32 - time.Date(t.Year(), t.Month(), 32, 0, 0, 0, 0, time.UTC).Day()) | ||
}) | ||
|
||
var DayOfMonth = timeWrapperFunc(func(t time.Time) float64 { | ||
return float64(t.Day()) | ||
}) | ||
|
||
var DayOfWeek = timeWrapperFunc(func(t time.Time) float64 { | ||
return float64(t.Weekday()) | ||
}) | ||
|
||
var DayOfYear = timeWrapperFunc(func(t time.Time) float64 { | ||
return float64(t.YearDay()) | ||
}) | ||
|
||
var Hour = timeWrapperFunc(func(t time.Time) float64 { | ||
return float64(t.Hour()) | ||
}) | ||
|
||
var Minute = timeWrapperFunc(func(t time.Time) float64 { | ||
return float64(t.Minute()) | ||
}) | ||
|
||
var Month = timeWrapperFunc(func(t time.Time) float64 { | ||
return float64(t.Month()) | ||
}) | ||
|
||
var Year = timeWrapperFunc(func(t time.Time) float64 { | ||
return float64(t.Year()) | ||
}) | ||
|
||
func timeWrapperFunc(f func(t time.Time) float64) InstantVectorSeriesFunction { | ||
return func(seriesData types.InstantVectorSeriesData, _ []types.ScalarData, _ types.QueryTimeRange, memoryConsumptionTracker *limiting.MemoryConsumptionTracker) (types.InstantVectorSeriesData, error) { | ||
|
||
// we don't do time based function on histograms | ||
types.HPointSlicePool.Put(seriesData.Histograms, memoryConsumptionTracker) | ||
charleskorn marked this conversation as resolved.
Show resolved
Hide resolved
|
||
seriesData.Histograms = nil | ||
charleskorn marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
for i := range seriesData.Floats { | ||
t := time.Unix(int64(seriesData.Floats[i].F), 0).UTC() | ||
seriesData.Floats[i].F = f(t) | ||
} | ||
return seriesData, nil | ||
} | ||
} |
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 | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,58 @@ | ||||||
// SPDX-License-Identifier: AGPL-3.0-only | ||||||
|
||||||
package operators | ||||||
|
||||||
import ( | ||||||
"context" | ||||||
|
||||||
"github.com/prometheus/prometheus/promql/parser/posrange" | ||||||
|
||||||
"github.com/grafana/mimir/pkg/streamingpromql/limiting" | ||||||
"github.com/grafana/mimir/pkg/streamingpromql/types" | ||||||
) | ||||||
|
||||||
type Time struct { | ||||||
TimeRange types.QueryTimeRange | ||||||
MemoryConsumptionTracker *limiting.MemoryConsumptionTracker | ||||||
expressionPosition posrange.PositionRange | ||||||
} | ||||||
|
||||||
var _ types.ScalarOperator = &Time{} | ||||||
|
||||||
func NewTime( | ||||||
timeRange types.QueryTimeRange, | ||||||
memoryConsumptionTracker *limiting.MemoryConsumptionTracker, | ||||||
expressionPosition posrange.PositionRange, | ||||||
) *Time { | ||||||
return &Time{ | ||||||
TimeRange: timeRange, | ||||||
MemoryConsumptionTracker: memoryConsumptionTracker, | ||||||
expressionPosition: expressionPosition, | ||||||
} | ||||||
} | ||||||
|
||||||
func (s *Time) GetValues(_ context.Context) (types.ScalarData, error) { | ||||||
samples, err := types.FPointSlicePool.Get(s.TimeRange.StepCount, s.MemoryConsumptionTracker) | ||||||
|
||||||
if err != nil { | ||||||
return types.ScalarData{}, err | ||||||
} | ||||||
|
||||||
samples = samples[:s.TimeRange.StepCount] | ||||||
|
||||||
for step := 0; step < s.TimeRange.StepCount; step++ { | ||||||
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. [nit] This could be simplified to:
Suggested change
|
||||||
t := s.TimeRange.StartT + int64(step)*s.TimeRange.IntervalMilliseconds | ||||||
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. [nit] This could be simplified to:
Suggested change
|
||||||
samples[step].T = t | ||||||
samples[step].F = float64(t) / 1000 | ||||||
} | ||||||
|
||||||
return types.ScalarData{Samples: samples}, nil | ||||||
} | ||||||
|
||||||
func (s *Time) ExpressionPosition() posrange.PositionRange { | ||||||
return s.expressionPosition | ||||||
} | ||||||
|
||||||
func (s *Time) Close() { | ||||||
// Nothing to do. | ||||||
} |
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.
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.
[nit] This could be simplified to a structure like: