-
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
Changes from 20 commits
4e901d7
f42685a
f0df4ce
bef6377
8ce6dc3
30ef946
cf9288f
50b0ae9
1ebe629
ebaafce
2a0643c
53222b9
f45d288
12a47a7
67b5378
fabdacb
58d7914
70fc626
9083619
26f1190
0f425b9
78a46a3
deb483e
9bde29f
a912b30
03ad7d5
970ed5c
b0155f4
6f85d3c
678690a
6cabbd4
54d6307
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1000,6 +1000,43 @@ 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 { | ||
stepsPerInterval := intervalUnit.Milliseconds() / int64(series.MillisPerStep()) | ||
var stepCounter int64 = 0 | ||
|
||
outvals := ts.NewValues(ctx, series.MillisPerStep(), series.Len()) | ||
var currentSum float64 | ||
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. It is more Go-ish to combine these var assignments altogether into a multi line var statement (and leave the zero values unassigned, i.e. var (
stepsPerInterval = intervalUnit.Milliseconds() / int64(series.MillisPerStep())
vals = ts.NewValues(ctx, series.MillisPerStep(), series.Len())
stepCounter int64
currentSum float64
) Also "outvals" should either be camel cased (since it's two words) or just one word (i.e. vals). 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. done! thanks for this.
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. | ||
|
@@ -1979,6 +2016,7 @@ func init() { | |
MustRegisterFunction(holtWintersForecast) | ||
MustRegisterFunction(identity) | ||
MustRegisterFunction(integral) | ||
MustRegisterFunction(integralByInterval) | ||
MustRegisterFunction(isNonNull) | ||
MustRegisterFunction(keepLastValue).WithDefaultParams(map[uint8]interface{}{ | ||
2: -1, // limit | ||
|
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