I have a problem with my influxdb flux sensors that could easily be solved:
My router logs traffic stats to influxdb. I have written a query to display the sum of downloaded data for a given range (e.g. one month). I wanted to create a sensor to display the downloaded data of the last month by setting the values of range_start
and range_end
by a template to the first and last day of last month. However the integration doesn’t support this.
My current workaround consists of setting range_start
to 0
(or any date before the first datapoint) and use a second range()
filter in the query itself. For some reason however a second range filter decreases the performance of influxdb significantly and the query that runs with one range in only 0.6 seconds takes over 10 seconds with a the second range filter leading to many warnings in the log.
As a solution I would propose to either make the range_start and range_end fields templateable or allow the user the option to write the whole query himself including the range filter by not prepending the from()
function and the range()
filter automatically.
My sensor config:
- name: internet_download_last_month
unit_of_measurement: GB
bucket: collectd
imports:
- math
group_function: last
range_start: 0
query: >
range(
start: {{ (now().replace(day=1, hour=0, minute=0, second=0, microsecond=0) - timedelta(days=1)).replace(day=1) | as_timestamp() | int() }},
stop: {{ now().replace(day=1, hour=0, minute=0, second=0, microsecond=0) | as_timestamp() | int() }}
)
|> filter(fn: (r) =>
r["_measurement"] == "interface_rx" and
r["instance"] == "eth1.32" and
r["_field"] == "value" and
r["host"] == "OpenWrt" and
r["type"] == "if_octets"
)
|> difference(nonNegative: true)
|> sum()
|> map(fn: (r) => ({
r with
_value: float(v: math.round(x: float(v: r._value) / 100000000.0))/10.0
}))