Last active
January 3, 2021 13:46
-
-
Save tillsc/2d71f3e89409049833e75ed22689ad40 to your computer and use it in GitHub Desktop.
query influx data with unevenly distributed points
This file contains hidden or 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
// Replace this with your specific query/selection | |
base = from(bucket: "hassio") | |
|> range(start: v.timeRangeStart, stop: v.timeRangeStop) | |
|> filter(fn: (r) => r["_measurement"] == "kw") | |
|> filter(fn: (r) => r["entity_id"] =~ /bhz3_leistung_zus/) | |
|> filter(fn: (r) => r["_field"] == "value") | |
|> window(every: v.windowPeriod) | |
|> sort(columns: ["_time"], desc: false) | |
// This does NOT caclulate the correct mean (maybe `timeWeightedAvg` would be the right choice here but I couldn't | |
// test it for now) | |
means = base | |
|> mean() | |
|> duplicate(column: "_stop", as: "_time") | |
|> set(key: "_col", value: "mean") | |
|> drop(columns: ["_start", "_stop"]) | |
// This table contains the last datapoint from the previous bin | |
lastValueInPreviousBin = base | |
|> last() | |
|> map(fn: (r) => ({ r with _time: time(v: uint(v: r._stop) + uint(v: v.windowPeriod)) })) | |
|> set(key: "_col", value: "lastValueInPreviousBin") | |
|> drop(columns: ["_start", "_stop"]) | |
// The following `union` with `pivot` is more or less a left+right join. Since influx own `join` function is | |
// only capable for `inner` joins for now this is just a hack to do more or less the same... Maybe the new experimental | |
// `join` function will be a better choice but I dont understand it for now... | |
union(tables: [means, lastValueInPreviousBin]) | |
|> pivot(rowKey:["_time", "_measurement", "_field" "entity_id", "domain"], columnKey: ["_col"], valueColumn: "_value") | |
|> map(fn: (r) => ({ r with _value: if exists r.mean then r.mean else r.lastValueInPreviousBin })) | |
|> drop(columns: ["mean", "lastValueInPreviousBin", "_field"]) | |
|> rename(columns: {entity_id: "_field"}) | |
|> sort(columns: ["_time", "_field"], desc: false) // I Do not understand why this is needed again... But it is... |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Note: This only fixes the problem of "empty" bins filled by an averaged value and not the last value which would be the right value then.