Skip to content

Instantly share code, notes, and snippets.

@jsteenb2
Created February 12, 2020 20:14
Show Gist options
  • Save jsteenb2/1229b78453fd3514b483778c9e873530 to your computer and use it in GitHub Desktop.
Save jsteenb2/1229b78453fd3514b483778c9e873530 to your computer and use it in GitHub Desktop.
reusable rate func for flux
v1Measurement = (tables=<-, measurement) =>
tables
|> filter(fn: (r) => r._measurement == measurement)
v1Fields = (tables=<-, fields=[]) =>
if length(arr: fields) == 0 then
tables
else
tables
|> filter(fn: (r) => contains(value: r._field, set: fields))
v1Select = (bucket, start=v.timeRangeStart, stop=v.timeRangeStop) =>
from(bucket: bucket)
|> range(start, stop)
inBucket = (bucket, start=v.timeRangeStart, stop=v.timeRangeStop, measurement, fields=[], predicate=(r) => true) =>
v1Select(bucket, start, stop)
|> v1Measurement(measurement)
|> filter(fn: predicate)
|> v1Fields(fields)
rateDet = (table=<-, unit=0s, nonNegative=true) =>
if string(v: unit) == "0s" then
table
|> difference(nonNegative: nonNegative, columns: ["_value"])
else
table
|> derivative(unit: unit, nonNegative: nonNegative, columns: ["_value"], timeColumn: "_time")
rate = (
bucket,
start=v.timeRangeStart,
stop=v.timeRangeStop,
measurement,
unit=0s,
nonNegative=true,
fields=[],
predicate=(r) => true
) =>
inBucket(bucket, start, stop, measurement, fields, predicate)
|>
|> rateDet(unit: unit, nonNegative: nonNegative)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment