-
Notifications
You must be signed in to change notification settings - Fork 453
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
[query] Implemented the Graphite integralByInterval
function
#2596
Merged
robskillington
merged 32 commits into
m3db:master
from
teddywahle:graphite-integral-by-interval
Sep 21, 2020
Merged
Changes from 28 commits
Commits
Show all changes
32 commits
Select commit
Hold shift + click to select a range
4e901d7
Added the moving movingMin function
teddywahle f42685a
Merge branch 'master' into twahle-moving-min
teddywahle f0df4ce
worked on moving min
teddywahle bef6377
Merge branch 'twahle-moving-min' of https://github.com/teddywahle/m3 …
teddywahle 8ce6dc3
more work on the moving min func
teddywahle 30ef946
Merge branch 'master' into twahle-moving-min
teddywahle cf9288f
updated movingMedian
teddywahle 50b0ae9
Merge branch 'twahle-moving-min' of https://github.com/teddywahle/m3 …
teddywahle 1ebe629
Apply suggestions from code review
teddywahle ebaafce
testMovingFunction
teddywahle 2a0643c
wrote test movingSum function
teddywahle 53222b9
Merge branch 'master' into twahle-moving-min
teddywahle f45d288
Merge branch 'master' into master
teddywahle 12a47a7
Apply suggestions from code review
teddywahle 67b5378
Apply suggestions from code review
teddywahle fabdacb
finished basic logic, just need to write tests
teddywahle 58d7914
Merge branch 'master' into graphite-integral-by-interval
teddywahle 70fc626
completed testing for integralByInteral
teddywahle 9083619
Merge branch 'master' into graphite-integral-by-interval
teddywahle 26f1190
Merge branch 'master' into graphite-integral-by-interval
teddywahle 0f425b9
Update src/query/graphite/native/builtin_functions.go
teddywahle 78a46a3
Merge branch 'master' into graphite-integral-by-interval
teddywahle deb483e
made variables more Go-like
teddywahle 9bde29f
Merge branch 'master' into graphite-integral-by-interval
teddywahle a912b30
Update src/query/graphite/native/builtin_functions.go
teddywahle 03ad7d5
Apply suggestions from code review
teddywahle 970ed5c
Ran go fmt
teddywahle b0155f4
Added proper test case from graphite-web source code
teddywahle 6f85d3c
Apply suggestions from code review
teddywahle 678690a
Apply suggestions from code review
teddywahle 6cabbd4
fixed formatting
teddywahle 54d6307
Merge branch 'master' into graphite-integral-by-interval
teddywahle 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -271,7 +271,7 @@ func delay( | |
func delayValuesHelper(ctx *common.Context, series *ts.Series, steps int) ts.Values { | ||
output := ts.NewValues(ctx, series.MillisPerStep(), series.Len()) | ||
for i := steps; i < series.Len(); i++ { | ||
output.SetValueAt(i, series.ValueAt(i - steps)) | ||
output.SetValueAt(i, series.ValueAt(i-steps)) | ||
} | ||
return output | ||
} | ||
|
@@ -281,7 +281,7 @@ func delayValuesHelper(ctx *common.Context, series *ts.Series, steps int) ts.Val | |
// Useful for filtering out a part of a series of data from a wider range of data. | ||
func timeSlice(ctx *common.Context, inputPath singlePathSpec, start string, end string) (ts.SeriesList, error) { | ||
var ( | ||
now = time.Now() | ||
now = time.Now() | ||
tzOffsetForAbsoluteTime time.Duration | ||
) | ||
startTime, err := graphite.ParseTime(start, now, tzOffsetForAbsoluteTime) | ||
|
@@ -633,8 +633,8 @@ func lowestCurrent(_ *common.Context, input singlePathSpec, n int) (ts.SeriesLis | |
type windowSizeFunc func(stepSize int) int | ||
|
||
type windowSizeParsed struct { | ||
deltaValue time.Duration | ||
stringValue string | ||
deltaValue time.Duration | ||
stringValue string | ||
windowSizeFunc windowSizeFunc | ||
} | ||
|
||
|
@@ -841,7 +841,7 @@ func exponentialMovingAverage(ctx *common.Context, input singlePathSpec, windowS | |
curr := bootstrap.ValueAt(i + offset) | ||
if !math.IsNaN(curr) { | ||
// formula: ema(current) = constant * (Current Value) + (1 - constant) * ema(previous) | ||
ema = emaConstant * curr + (1 - emaConstant) * ema | ||
ema = emaConstant*curr + (1-emaConstant)*ema | ||
vals.SetValueAt(i, ema) | ||
} else { | ||
vals.SetValueAt(i, math.NaN()) | ||
|
@@ -1086,6 +1086,46 @@ func integral(ctx *common.Context, input singlePathSpec) (ts.SeriesList, error) | |
return r, nil | ||
} | ||
|
||
// integralByInterval will do the same as integral funcion, except it resets the total to 0 | ||
// at the given time in the parameter “from”. Useful for finding totals per hour/day/week. | ||
func integralByInterval(ctx *common.Context, input singlePathSpec, intervalString string) (ts.SeriesList, error) { | ||
intervalUnit, err := common.ParseInterval(intervalString) | ||
if err != nil { | ||
return ts.NewSeriesList(), err | ||
} | ||
results := make([]*ts.Series, 0, len(input.Values)) | ||
|
||
for _, series := range input.Values { | ||
var ( | ||
stepsPerInterval = intervalUnit.Milliseconds() / int64(series.MillisPerStep()) | ||
outVals = ts.NewValues(ctx, series.MillisPerStep(), series.Len()) | ||
stepCounter int64 = 0 | ||
teddywahle marked this conversation as resolved.
Show resolved
Hide resolved
|
||
currentSum float64 = 0.0 | ||
teddywahle marked this conversation as resolved.
Show resolved
Hide resolved
|
||
) | ||
|
||
for i := 0; i < series.Len(); i++ { | ||
if stepCounter == stepsPerInterval { | ||
// startNewInterval | ||
stepCounter = 0 | ||
currentSum = 0.0 | ||
} | ||
n := series.ValueAt(i) | ||
if !math.IsNaN(n) { | ||
currentSum += n | ||
} | ||
outVals.SetValueAt(i, currentSum) | ||
stepCounter += 1 | ||
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. Is it correct to increment step counter if the value is NaN? 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. Discussed offline. Answer is yes according to |
||
} | ||
|
||
newName := fmt.Sprintf("integralByInterval(%s, %s)", series.Name(), intervalString) | ||
results = append(results, ts.NewSeries(ctx, newName, series.StartTime(), outVals)) | ||
} | ||
|
||
r := ts.SeriesList(input) | ||
r.Values = results | ||
return r, nil | ||
} | ||
|
||
// This is the opposite of the integral function. This is useful for taking a | ||
// running total metric and calculating the delta between subsequent data | ||
// points. | ||
|
@@ -1798,8 +1838,6 @@ func movingMinHelper(window []float64, vals ts.MutableValues, windowPoints int, | |
} | ||
} | ||
|
||
|
||
|
||
func newMovingBinaryTransform( | ||
ctx *common.Context, | ||
input singlePathSpec, | ||
|
@@ -1830,7 +1868,7 @@ func newMovingBinaryTransform( | |
|
||
bootstrapStartTime, bootstrapEndTime := ctx.StartTime.Add(-interval), ctx.StartTime | ||
return &binaryContextShifter{ | ||
ContextShiftFunc: contextShiftingFn, | ||
ContextShiftFunc: contextShiftingFn, | ||
BinaryTransformer: func(bootstrapped, original ts.SeriesList) (ts.SeriesList, error) { | ||
bootstrapList, err := combineBootstrapWithOriginal(ctx, | ||
bootstrapStartTime, bootstrapEndTime, | ||
|
@@ -1910,7 +1948,6 @@ func movingMin(ctx *common.Context, input singlePathSpec, windowSize genericInte | |
return newMovingBinaryTransform(ctx, input, windowSize, "movingMin", movingMinHelper) | ||
} | ||
|
||
|
||
// legendValue takes one metric or a wildcard seriesList and a string in quotes. | ||
// Appends a value to the metric name in the legend. Currently one or several of: | ||
// "last", "avg", "total", "min", "max". | ||
|
@@ -2145,6 +2182,7 @@ func init() { | |
MustRegisterFunction(holtWintersForecast) | ||
MustRegisterFunction(identity) | ||
MustRegisterFunction(integral) | ||
MustRegisterFunction(integralByInterval) | ||
MustRegisterFunction(isNonNull) | ||
MustRegisterFunction(keepLastValue).WithDefaultParams(map[uint8]interface{}{ | ||
2: -1, // limit | ||
|
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
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.
Does this need to be added to
builtin_functions.go
?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.
Added