Skip to content

Instantly share code, notes, and snippets.

@demmer
Last active August 29, 2015 14:09
Show Gist options
  • Save demmer/efc170913ef10db968c2 to your computer and use it in GitHub Desktop.
Save demmer/efc170913ef10db968c2 to your computer and use it in GitHub Desktop.
Custom reducers

There's a reducer for that...

What query language hasn't left you wanting of just a few more functions? The Juttle dataflow language comes with powerful built-in functions like percentile() but you can define your own functions or reducers that operate on every point in a stream or on batches of points. You can also import functions that other people have written.

In this example, we implement an exponentially weighted moving average calculation and import a rate of change calculation.

Your Turn:

  • Try changing the weight used in the ewma function by changing .1 to .9 on line 16.
  • On line 7, we've imported a derivative reducer. Let's plot what it does by uncommenting line 20.
  • Or instead plot it on the same graph by adding "der" to the list of columns in line 19.
export sub demo_cdn(from) {
demo cdn -every :1 second: -period :1 second:
-nhosts 5 -dos 0.7 -dos_router markov
metrics "response_ms" -from from
}
export reducer rate(current_time, current_value) {
var last_time = Date(0);
var last_value = 0;
var delta_result = 0;
function update() {
if (last_time == Date(0)) {
last_time = *current_time;
last_value = *current_value;
} else {
delta_result = (*current_value - last_value) / Duration.seconds(*current_time - last_time);
last_value = *current_value;
last_time = *current_time;
}
}
function result() {
return delta_result;
}
}
import "data.juttle" as data;
reducer ewma(fname, alpha) {
var ma = 0; // initial state
function update() {
// For every data point, update the moving average
ma = (ma * (1 - alpha)) + (*fname * alpha);
}
function result() {
return ma; // result of this batch
}
}
data.demo_cdn -from :10 minutes ago: |
filter name = "response_ms" && service = "search" && host = "sea.0" |
put ewma = ewma(value, .1) |
put rate = data.rate(time, value) |
(
@timechart -columns "value,ewma" -title "Response time" -name response_time;
// @timechart -columns "rate" -title "Response time rate of change"
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment