mirror of
https://github.com/pommi/python-itho-wpu.git
synced 2024-11-24 14:22:14 +01:00
Pim van den Berg
751182b70d
To check for errors: $ pre-commit run --all-files To install as pre-commit hook: $ pre-commit install
26 lines
751 B
Python
26 lines
751 B
Python
import datetime
|
|
import os
|
|
|
|
|
|
def export_to_influxdb(action, measurements):
|
|
from influxdb import InfluxDBClient
|
|
|
|
influx_client = InfluxDBClient(
|
|
host=os.getenv("INFLUXDB_HOST", "localhost"),
|
|
port=os.getenv("INFLUXDB_PORT", 8086),
|
|
username=os.getenv("INFLUXDB_USERNAME", "root"),
|
|
password=os.getenv("INFLUXDB_PASSWORD", "root"),
|
|
database=os.getenv("INFLUXDB_DATABASE"),
|
|
)
|
|
json_body = [
|
|
{
|
|
"measurement": action,
|
|
"time": datetime.datetime.utcnow().replace(microsecond=0).isoformat(),
|
|
"fields": measurements,
|
|
}
|
|
]
|
|
try:
|
|
influx_client.write_points(json_body)
|
|
except Exception as e:
|
|
print("Failed to write to influxdb: ", e)
|