Skip to content

Commit

Permalink
Add min_value and max_value parameters to query_flux()
Browse files Browse the repository at this point in the history
  • Loading branch information
kizniche committed May 1, 2023
1 parent e6734e9 commit 06c76b5
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
- Add Day to unit conversion
- Add ability to disable any Input/Output/Function/Widget option (user can view but not change)
- Add ability to return Input/Output/Function object with module_function()
- Add min_value and max_value parameters to query_flux()


## 8.15.8 (2023-04-06)
Expand Down
15 changes: 10 additions & 5 deletions mycodo/utils/influx.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ def add_measurements_influxdb(unique_id, measurements, use_same_timestamp=True,

def query_flux(unit, unique_id,
value=None, measure=None, channel=None, ts_str=None,
start_str=None, end_str=None, past_sec=None, group_sec=None,
start_str=None, end_str=None, min_value=None, max_value=None, past_sec=None, group_sec=None,
limit=None):
"""Generate influxdb query string (flux edition, using influxdb_client)."""
from influxdb_client import InfluxDBClient
Expand Down Expand Up @@ -219,6 +219,11 @@ def query_flux(unit, unique_id,
else:
query += f' |> range(start: -99999d)'

if min_value:
query += f' |> filter(fn: (r) => r._value > {min_value})'
if max_value:
query += f' |> filter(fn: (r) => r._value < {max_value})'

query += f' |> filter(fn: (r) => r["_measurement"] == "{unit}")'
query += f' |> filter(fn: (r) => r["device_id"] == "{unique_id}")'

Expand Down Expand Up @@ -286,8 +291,8 @@ def query_flux(unit, unique_id,

def query_string(unit, unique_id,
value=None, measure=None, channel=None, ts_str=None,
start_str=None, end_str=None, past_sec=None, group_sec=None,
limit=None):
start_str=None, end_str=None, min_value=None, max_value=None,
past_sec=None, group_sec=None, limit=None):
"""Generate influxdb query string."""
ret_value = None
settings = db_retrieve_table_daemon(Misc, entry='first')
Expand All @@ -296,8 +301,8 @@ def query_string(unit, unique_id,
ret_value = query_flux(
unit, unique_id,
value=value, measure=measure, channel=channel, ts_str=ts_str,
start_str=start_str, end_str=end_str, past_sec=past_sec, group_sec=group_sec,
limit=limit)
start_str=start_str, end_str=end_str, min_value=min_value, max_value=max_value,
past_sec=past_sec, group_sec=group_sec, limit=limit)

return ret_value

Expand Down

0 comments on commit 06c76b5

Please sign in to comment.